GEDLIB  1.0
lsap_solver.hpp
Go to the documentation of this file.
1 /***************************************************************************
2 * *
3 * Copyright (C) 2018 by David B. Blumenthal *
4 * *
5 * This file is part of GEDLIB. *
6 * *
7 * GEDLIB is free software: you can redistribute it and/or modify it *
8 * under the terms of the GNU Lesser General Public License as published *
9 * by the Free Software Foundation, either version 3 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * GEDLIB is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU Lesser General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU Lesser General Public *
18 * License along with GEDLIB. If not, see <http://www.gnu.org/licenses/>. *
19 * *
20 ***************************************************************************/
21 
22 
28 #ifndef SRC_UTIL_LSAP_SOLVER_HPP_
29 #define SRC_UTIL_LSAP_SOLVER_HPP_
30 
31 #include "../env/common_types.hpp"
32 #include "../env/matrix.hpp"
33 
34 namespace ged {
35 
39 class LSAPSolver {
40 
41 public:
42 
50  enum GreedyMethod {
51  BASIC=0,
52  REFINED=1,
53  LOSS=2,
56  };
57 
63 
67  LSAPSolver();
68 
73  void solve(int num_solutions = 1);
74 
79  void set_problem(const DMatrix * cost_matrix);
80 
85  void set_greedy_method(const GreedyMethod & greedy_method);
86 
91 
95  void clear_solution();
96 
101  double minimal_cost() const;
102 
109  std::size_t get_assigned_col(std::size_t row, std::size_t solution_id = 0) const;
110 
117  std::size_t get_assigned_row(std::size_t col, std::size_t solution_id = 0) const;
118 
125  double get_slack(std::size_t row, std::size_t col) const;
126 
132  double get_dual_var_row(std::size_t row) const;
133 
139  double get_dual_var_col(std::size_t col) const;
140 
145  const DMatrix * cost_matrix() const;
146 
151  std::size_t num_rows() const;
152 
157  std::size_t num_cols() const;
158 
164  const std::vector<std::size_t> & row_to_col_assignment(std::size_t solution_id = 0) const;
165 
171  const std::vector<std::size_t> & col_to_row_assignment(std::size_t solution_id = 0) const;
172 
177  std::size_t num_solutions() const;
178 
179 private:
180 
181  const DMatrix * cost_matrix_;
182 
183  GreedyMethod greedy_method_;
184 
185  bool solve_optimally_;
186 
187  double minimal_cost_;
188 
189  std::vector<std::vector<std::size_t>> row_to_col_assignments_;
190 
191  std::vector<std::vector<std::size_t>> col_to_row_assignments_;
192 
193  std::vector<double> dual_var_rows_;
194 
195  std::vector<double> dual_var_cols_;
196 
197  // Helper functions.
198 
199  std::vector<std::size_t> construct_col_to_row_asignment_(const std::vector<std::size_t> row_to_col_assignment) const;
200 
201  void compute_cost_from_assignments_();
202 
203  void compute_cost_from_dual_vars_();
204 
205 };
206 
207 }
208 
209 #include "lsap_solver.ipp"
210 
211 #endif /* SRC_UTIL_LSAP_SOLVER_HPP_ */
std::size_t num_rows() const
Returns the number of rows of the LSAP problem instance.
See https://doi.org/10.1007/978-3-319-18224-7_1.
Definition: lsap_solver.hpp:52
GreedyMethod
Selects a greedy method.
Definition: lsap_solver.hpp:50
void set_greedy_method(const GreedyMethod &greedy_method)
Makes the solver use a greedy method.
Definition: lsap_solver.ipp:63
ged::LSAPSolver class definition.
See https://doi.org/10.1007/978-3-319-18224-7_1.
Definition: lsap_solver.hpp:53
double minimal_cost() const
Returns the cost of the computed solutions.
void clear_solution()
Clears a previously computed solution.
Definition: lsap_solver.ipp:76
See https://doi.org/10.1007/978-3-319-18224-7_1.
Definition: lsap_solver.hpp:55
void solve(int num_solutions=1)
Solves the LSAP problem instance.
Definition: lsap_solver.ipp:88
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.
Definition: lsap_solver.ipp:70
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.
This class solves LSAP instances by calling the library lsape available at https://bougleux.users.greyc.fr/lsape/.
Definition: lsap_solver.hpp:39
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.
Definition: lsap_solver.ipp:56
See https://doi.org/10.1007/978-3-319-18224-7_1.
Definition: lsap_solver.hpp:54
LSAPSolver()
Constructs empty solver.
Definition: lsap_solver.ipp:44
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.
See https://doi.org/10.1007/978-3-319-18224-7_1.
Definition: lsap_solver.hpp:51
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.
const std::vector< std::size_t > & row_to_col_assignment(std::size_t solution_id=0) const
Returns the assignment from rows to columns.