GEDLIB  1.0
lsape_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 
27 #ifndef LSAPE_SOLVER_HPP
28 #define LSAPE_SOLVER_HPP 1
29 
30 #include "../env/common_types.hpp"
31 #include "../env/matrix.hpp"
32 
33 namespace ged {
34 
38 class LSAPESolver {
39 
40 public:
41 
49  enum Model {
50  ECBP=0,
51  FLWC=1,
52  EBP=2,
53  FLCC=3,
54  FBP=4,
55  FBP0=5,
56  SFBP=6
57  };
58 
66  enum GreedyMethod {
67  BASIC=0,
68  REFINED=1,
69  LOSS=2,
72  };
73 
79 
83  LSAPESolver();
84 
89  void set_problem(const DMatrix * cost_matrix);
90 
94  void clear_solution();
95 
100  void set_model(const Model & model);
101 
106  void set_greedy_method(const GreedyMethod & greedy_method);
107 
112  void solve(int num_solutions = 1);
113 
118  double minimal_cost() const;
119 
126  std::size_t get_assigned_col(std::size_t row, std::size_t solution_id = 0) const;
127 
134  std::size_t get_assigned_row(std::size_t col, std::size_t solution_id = 0) const;
135 
142  double get_slack(std::size_t row, std::size_t col) const;
143 
149  double get_dual_var_row(std::size_t row) const;
150 
156  double get_dual_var_col(std::size_t col) const;
157 
162  const DMatrix * cost_matrix() const;
163 
169  const std::vector<std::size_t> & row_to_col_assignment(std::size_t solution_id = 0) const;
170 
176  const std::vector<std::size_t> & col_to_row_assignment(std::size_t solution_id = 0) const;
177 
182  std::size_t num_rows() const;
183 
188  std::size_t num_cols() const;
189 
194  std::size_t total_num_rows() const;
195 
200  std::size_t total_num_cols() const;
201 
206  std::size_t num_solutions() const;
207 
208 
209 private:
210 
211  const DMatrix * cost_matrix_;
212 
213  Model model_;
214 
215  GreedyMethod greedy_method_;
216 
217  bool solve_optimally_;
218 
219  double minimal_cost_;
220 
221  std::vector<std::vector<std::size_t>> row_to_col_assignments_;
222 
223  std::vector<std::vector<std::size_t>> col_to_row_assignments_;
224 
225  std::vector<double> dual_var_rows_;
226 
227  std::vector<double> dual_var_cols_;
228 
229  // Helper functions.
230  std::vector<std::size_t> construct_col_to_row_asignment_(const std::vector<std::size_t> row_to_col_assignment) const;
231 
232  void compute_cost_from_assignments_();
233 
234  void compute_cost_from_dual_vars_();
235 
236 };
237 
238 }
239 
240 #include "lsape_solver.ipp"
241 
242 #endif
void set_model(const Model &model)
Makes the solver use a specific model for optimal solving.
Reduction to compact LSAP instance for quasimetric costs.
double get_dual_var_col(std::size_t col) const
Returns the dual variable of a column.
Reduction to compact LSAP instance for quasimetric costs.
std::size_t total_num_rows() const
Returns the total number of rows of the LSAPE problem instance.
This class solves LSAPE instances by calling the library lsape available at https://bougleux.users.greyc.fr/lsape/.
Reduction to compact LSAP instance without cost constraints.
std::size_t num_cols() const
Returns the number of real columns of the LSAPE problem instance, i.e, the number of columns of the i...
LSAPESolver()
Constructs empty solver.
GreedyMethod
Selects a greedy method.
void solve(int num_solutions=1)
Solves the LSAPE problem instance.
Reduction to extended LSAP instance without cost constraints.
double get_dual_var_row(std::size_t row) const
Returns the dual variable of a row.
double get_slack(std::size_t row, std::size_t col) const
Returns the slack of a cell.
void clear_solution()
Clears a previously computed solution.
ged::LSAPESolver class definition.
const DMatrix * cost_matrix() const
Returns the LSAPE problem instance.
Adaption of Hungarian Algorithm to LSAPE.
const std::vector< std::size_t > & col_to_row_assignment(std::size_t solution_id=0) const
Returns the assignment from columns to rows.
See https://doi.org/10.1007/978-3-319-18224-7_1.
double minimal_cost() const
Returns the cost of the computed solutions.
std::size_t get_assigned_row(std::size_t col, std::size_t solution_id=0) const
Returns the assigned row.
std::size_t total_num_cols() const
Returns the total number of columns of the LSAPE problem instance.
const std::vector< std::size_t > & row_to_col_assignment(std::size_t solution_id=0) const
Returns the assignment from rows to columns.
std::size_t num_rows() const
Returns the number of real rows of the LSAPE problem instance, i.e, the number of rows of the interna...
Reduction to compact LSAP instance for quasimetric costs.
std::size_t get_assigned_col(std::size_t row, std::size_t solution_id=0) const
Returns the assigned column.
Global namespace for GEDLIB.
See https://doi.org/10.1007/978-3-319-18224-7_1.
See https://doi.org/10.1007/978-3-319-18224-7_1.
void set_problem(const DMatrix *cost_matrix)
Sets the LSAPE problem instance.
Reduction to compact LSAP instance for quasimetric costs.
std::size_t num_solutions() const
Returns the number of solutions.
Model
Selects a model for solving LSAPE with the Hungarian algorithm.
See https://doi.org/10.1007/978-3-319-18224-7_1.
void set_greedy_method(const GreedyMethod &greedy_method)
Makes the solver use a greedy method.
See https://doi.org/10.1007/978-3-319-18224-7_1.