27 #ifndef SRC_UTIL_LSAP_SOLVER_IPP_ 28 #define SRC_UTIL_LSAP_SOLVER_IPP_ 34 cost_matrix_{cost_matrix},
35 greedy_method_{
BASIC},
36 solve_optimally_{
true},
38 row_to_col_assignments_(),
39 col_to_row_assignments_(),
45 cost_matrix_{
nullptr},
46 greedy_method_{
BASIC},
47 solve_optimally_{
true},
49 row_to_col_assignments_(),
50 col_to_row_assignments_(),
64 solve_optimally_ =
false;
65 greedy_method_ = greedy_method;
71 solve_optimally_ =
true;
78 row_to_col_assignments_.clear();
79 col_to_row_assignments_.clear();
80 row_to_col_assignments_.push_back(std::vector<std::size_t>(
num_rows()));
81 col_to_row_assignments_.push_back(std::vector<std::size_t>(
num_cols()));
82 dual_var_rows_ = std::vector<double>(
num_rows());
83 dual_var_cols_ = std::vector<double>(
num_cols());
90 if (solve_optimally_) {
91 lsape::hungarianLSAP(cost_matrix_->
data(),
num_rows(),
num_cols(), row_to_col_assignments_.at(0).data(), dual_var_rows_.data(), dual_var_cols_.data(), col_to_row_assignments_.at(0).data());
92 compute_cost_from_dual_vars_();
93 if (num_solutions > 1) {
94 std::list<std::size_t *> further_row_to_col_assignments;
95 lsape::lsapSolutions(cost_matrix_->
data(),
num_rows(),
num_cols(),
num_solutions, row_to_col_assignments_.at(0).data(), dual_var_rows_.data(), dual_var_cols_.data(),
96 further_row_to_col_assignments);
97 row_to_col_assignments_.clear();
98 col_to_row_assignments_.clear();
101 col_to_row_assignments_.push_back(construct_col_to_row_asignment_(row_to_col_assignments_.back()));
107 minimal_cost_ = lsape::greedyLSAP(cost_matrix_->
data(),
num_rows(),
num_cols(), row_to_col_assignments_.at(0).data(), col_to_row_assignments_.at(0).data(), greedy_method_);
114 return minimal_cost_;
120 return row_to_col_assignments_.at(solution_id).at(row);
126 return col_to_row_assignments_.at(solution_id).at(col);
132 return cost_matrix_->operator()(row, col) - (dual_var_rows_.at(row) + dual_var_cols_.at(col));
138 return dual_var_rows_.at(row);
144 return dual_var_cols_.at(col);
147 const std::vector<std::size_t> &
150 return row_to_col_assignments_.at(solution_id);
153 const std::vector<std::size_t> &
156 return col_to_row_assignments_.at(solution_id);
180 return row_to_col_assignments_.size();
183 std::vector<std::size_t>
187 for (std::size_t row{}; row <
num_rows(); row++) {
188 if (row_to_col_assignment.at(row) <
num_cols()) {
189 col_to_row_assignment[row_to_col_assignment.at(row)] = row;
197 compute_cost_from_assignments_() {
199 for (std::size_t row{}; row <
num_rows(); row++) {
200 minimal_cost_ += cost_matrix_->operator()(row, row_to_col_assignments_.at(0).at(row));
202 for (std::size_t col{}; col <
num_cols(); col++) {
203 if (col_to_row_assignments_.at(0).at(col) ==
num_rows()) {
204 minimal_cost_ += cost_matrix_->operator()(
num_rows(), col);
211 compute_cost_from_dual_vars_() {
213 for (
auto dual_var = dual_var_rows_.begin(); dual_var != dual_var_rows_.end(); dual_var++) {
214 minimal_cost_ += *dual_var;
216 for (
auto dual_var = dual_var_cols_.begin(); dual_var != dual_var_cols_.end(); dual_var++) {
217 minimal_cost_ += *dual_var;
std::size_t num_rows() const
Returns the number of rows of the LSAP problem instance.
GreedyMethod
Selects a greedy method.
void set_greedy_method(const GreedyMethod &greedy_method)
Makes the solver use a greedy method.
double minimal_cost() const
Returns the cost of the computed solutions.
void clear_solution()
Clears a previously computed solution.
std::size_t num_cols() const
Returns the number of columns.
void solve(int num_solutions=1)
Solves the LSAP problem instance.
const std::vector< std::size_t > & col_to_row_assignment(std::size_t solution_id=0) const
Returns the assignment from columns to rows.
void set_hungarian_algorithm()
Makes the solver use the Hungarian algorithm for optimal solving.
double get_dual_var_row(std::size_t row) const
Returns the dual variable of a row.
const DMatrix * cost_matrix() const
Returns the LSAP problem instance.
Global namespace for GEDLIB.
std::size_t get_assigned_col(std::size_t row, std::size_t solution_id=0) const
Returns the assigned column.
void set_problem(const DMatrix *cost_matrix)
Sets the LSAP problem instance.
LSAPSolver()
Constructs empty solver.
double get_dual_var_col(std::size_t col) const
Returns the dual variable of a column.
std::size_t num_cols() const
Returns the number of columns of the LSAP problem instance.
ScalarT * data()
Provides access to internal data.
See https://doi.org/10.1007/978-3-319-18224-7_1.
std::size_t num_solutions() const
Returns the number of solutions.
double get_slack(std::size_t row, std::size_t col) const
Returns the slack of a cell.
std::size_t get_assigned_row(std::size_t col, std::size_t solution_id=0) const
Returns the assigned row.
std::size_t num_rows() const
Returns the number of rows.
const std::vector< std::size_t > & row_to_col_assignment(std::size_t solution_id=0) const
Returns the assignment from rows to columns.