Antares Xpansion
Investment simulations for Antares studies
Loading...
Searching...
No Matches
SolverCbc.h
1#pragma once
2
3#include <cstdio>
4
5#include "CbcModel.hpp"
6#include "Cbc_C_Interface.h"
7#include "CoinHelperFunctions.hpp"
8#include "CoinMpsIO.hpp"
9#include "OsiClpSolverInterface.hpp"
10#include "antares-xpansion/multisolver_interface/SolverAbstract.h"
11
17class SolverCbc : public SolverAbstract {
18 /*************************************************************************************************
19 ---------------------------------------- ATTRIBUTES
20 ---------------------------------------
21 *************************************************************************************************/
22 static int _NumberOfProblems;
26 public:
27 const std::string name_ = "CBC";
28 OsiClpSolverInterface _clp_inner_solver;
29 CbcModel _cbc;
30 int _current_log_level;
31
32 /*************************************************************************************************
33 ----------------------------------- Constructor/Desctructor
34 --------------------------------
35 *************************************************************************************************/
36
37 public:
41 SolverCbc();
42 explicit SolverCbc(SolverLogManager&log_manager);
43
51 explicit SolverCbc(const std::shared_ptr<const SolverAbstract> toCopy);
52
53 /*SolverCbc ctor accept only std::shared_ptr*/
54 SolverCbc(const SolverCbc &other) = delete;
55 SolverCbc &operator=(const SolverCbc &other) = delete;
56 virtual ~SolverCbc();
57 virtual int get_number_of_instances() override;
58
59 virtual std::string get_solver_name() const override { return name_; }
60
61 private:
62 void defineCbcModelFromInnerSolver();
63 void setClpSimplexColNamesFromInnerSolver(ClpSimplex *clps) const;
64 void setClpSimplexRowNamesFromInnerSolver(ClpSimplex *clps) const;
65
66 /*************************************************************************************************
67 --------------------------------- Output and stream management
68 -----------------------------
69 *************************************************************************************************/
70
71 /*************************************************************************************************
72 ------ Destruction or creation of inner strctures and datas, closing
73 environments ----------
74 *************************************************************************************************/
75 public:
76 virtual void init() override;
77 virtual void free() override;
78
79 /*************************************************************************************************
80 ------------------------------- Reading & Writing problems
81 -------------------------------
82 *************************************************************************************************/
83 public:
84 virtual void write_prob_mps(const std::filesystem::path &filename) override;
85 virtual void write_prob_lp(const std::filesystem::path &filename) override;
86 void save_prob(const std::filesystem::path &filename) override;
87 virtual void write_basis(const std::filesystem::path &filename) override;
88
89 virtual void read_prob_mps(const std::filesystem::path &filename) override;
90 virtual void read_prob_lp(const std::filesystem::path &filename) override;
91 void restore_prob(const std::filesystem::path &filename) override;
92 virtual void read_basis(const std::filesystem::path &filename) override;
93
94 virtual void copy_prob(const SolverAbstract::Ptr fictif_solv) override;
95
96 /*************************************************************************************************
97 ----------------------- Get general informations about problem
98 ----------------------------
99 *************************************************************************************************/
100 public:
101 virtual int get_ncols() const override;
102 virtual int get_nrows() const override;
103 virtual int get_nelems() const override;
104 virtual int get_n_integer_vars() const override;
105 virtual void get_obj(double *obj, int first, int last) const override;
106 void set_obj_to_zero() override;
107 void set_obj(const double *obj, int first, int last) override;
108 virtual void get_rows(int *mstart, int *mclind, double *dmatval, int size,
109 int *nels, int first, int last) const override;
110 virtual void get_row_type(char *qrtype, int first, int last) const override;
111 virtual void get_rhs(double *rhs, int first, int last) const override;
112 virtual void get_rhs_range(double *range, int first, int last) const override;
113 virtual void get_col_type(char *coltype, int first, int last) const override;
114 virtual void get_lb(double *lb, int fisrt, int last) const override;
115 virtual void get_ub(double *ub, int fisrt, int last) const override;
116
117 virtual int get_row_index(std::string const &name) override;
118 virtual int get_col_index(std::string const &name) override;
119 virtual std::vector<std::string> get_row_names(int first, int last) override;
120 virtual std::vector<std::string> get_row_names() override;
121 virtual std::vector<std::string> get_col_names(int first, int last) override;
122 virtual std::vector<std::string> get_col_names() override;
123
124 /*************************************************************************************************
125 ------------------------------ Methods to modify problem
126 ----------------------------------
127 *************************************************************************************************/
128 public:
129 virtual void del_rows(int first, int last) override;
130 virtual void add_rows(int newrows, int newnz, const char *qrtype,
131 const double *rhs, const double *range,
132 const int *mstart, const int *mclind,
133 const double *dmatval,
134 const std::vector<std::string> &names = {}) override;
135 virtual void add_cols(int newcol, int newnz, const double *objx,
136 const int *mstart, const int *mrwind,
137 const double *dmatval, const double *bdl,
138 const double *bdu) override;
139 virtual void add_name(int type, const char *cnames, int indice) override;
140 virtual void add_names(int type, const std::vector<std::string> &cnames,
141 int first, int end) override;
142 virtual void chg_obj(const std::vector<int> &mindex,
143 const std::vector<double> &obj) override;
144 virtual void chg_obj_direction(const bool minimize) override;
145 virtual void chg_bounds(const std::vector<int> &mindex,
146 const std::vector<char> &qbtype,
147 const std::vector<double> &bnd) override;
148 virtual void chg_col_type(const std::vector<int> &mindex,
149 const std::vector<char> &qctype) override;
150 virtual void chg_rhs(int id_row, double val) override;
151 virtual void chg_coef(int id_row, int id_col, double val) override;
152 virtual void chg_row_name(int id_row, std::string const &name) override;
153 virtual void chg_col_name(int id_col, std::string const &name) override;
154
155 /*************************************************************************************************
156 ----------------------------- Methods to solve the problem
157 ---------------------------------
158 *************************************************************************************************/
159 public:
160 virtual int solve_lp() override;
161 virtual int solve_mip() override;
162
163 /*************************************************************************************************
164 ------------------------- Methods to get solutions information
165 -----------------------------
166 *************************************************************************************************/
167 public:
182 virtual void get_basis(int *rstatus, int *cstatus) const override;
183 virtual double get_mip_value() const override;
184 virtual double get_lp_value() const override;
185 virtual int get_splex_num_of_ite_last() const override;
186 virtual void get_lp_sol(double *primals, double *duals,
187 double *reduced_costs) override;
188 virtual void get_mip_sol(double *primals) override;
189
190 /*************************************************************************************************
191 ------------------------ Methods to set algorithm or logs levels
192 ---------------------------
193 *************************************************************************************************/
194 public:
195 void set_output_log_level(int loglevel) final;
196 virtual void set_algorithm(std::string const &algo) override;
197 virtual void set_threads(int n_threads) override;
198 virtual void set_optimality_gap(double gap) override;
199 virtual void set_simplex_iter(int iter) override;
200};
Definition SolverAbstract.h:170
std::shared_ptr< SolverAbstract > Ptr
Definition SolverAbstract.h:181
Definition SolverCbc.h:17
virtual int get_number_of_instances() override
Returns number of instances of solver currently in memory.
Definition SolverCbc.cpp:48
virtual void add_name(int type, const char *cnames, int indice) override
Adds a name to a row or a column.
Definition SolverCbc.cpp:443
virtual void get_ub(double *ub, int fisrt, int last) const override
Returns the upper bounds for variables in a given range.
Definition SolverCbc.cpp:324
virtual void chg_col_name(int id_col, std::string const &name) override
Change the name of a variable.
Definition SolverCbc.cpp:551
virtual void read_prob_lp(const std::filesystem::path &filename) override
reads an optimization problem contained in a MPS file
Definition SolverCbc.cpp:201
virtual void get_lb(double *lb, int fisrt, int last) const override
Returns the lower bounds for variables in a given range.
Definition SolverCbc.cpp:316
virtual void init() override
Initializes a problem.
Definition SolverCbc.cpp:66
virtual int get_splex_num_of_ite_last() const override
Get the number of simplex iterations done in the last resolution of the problem.
Definition SolverCbc.cpp:624
virtual int solve_lp() override
Solves a problem as LP.
Definition SolverCbc.cpp:559
virtual void read_basis(const std::filesystem::path &filename) override
Instructs the optimizer to read in a previously saved basis from a file.
Definition SolverCbc.cpp:207
virtual void get_col_type(char *coltype, int first, int last) const override
Returns the column types for the columns in a given range.
Definition SolverCbc.cpp:299
virtual void write_prob_lp(const std::filesystem::path &filename) override
writes an optimization problem in a LP file
Definition SolverCbc.cpp:150
virtual void set_optimality_gap(double gap) override
Sets the optimality gap.
Definition SolverCbc.cpp:692
virtual void del_rows(int first, int last) override
Deletes rows between index first and last.
Definition SolverCbc.cpp:400
virtual void get_mip_sol(double *primals) override
Get MIP solution of a problem (available after method "solve_mip")
Definition SolverCbc.cpp:652
virtual std::vector< std::string > get_col_names() override
Returns the names of columns.
Definition SolverCbc.cpp:391
virtual void copy_prob(const SolverAbstract::Ptr fictif_solv) override
copy an existing problem
Definition SolverCbc.cpp:215
virtual void add_cols(int newcol, int newnz, const double *objx, const int *mstart, const int *mrwind, const double *dmatval, const double *bdl, const double *bdu) override
Adds new columns to the problem.
Definition SolverCbc.cpp:429
void set_obj_to_zero() override
Set the objective function coefficients to zero.
Definition SolverCbc.cpp:252
virtual int get_col_index(std::string const &name) override
Returns the index of column named "name".
Definition SolverCbc.cpp:345
virtual void get_rhs(double *rhs, int first, int last) const override
Returns the right-hand sides of the rows in a given range.
Definition SolverCbc.cpp:282
virtual void get_rhs_range(double *range, int first, int last) const override
Returns the right hand side range values for the rows in a given range.
Definition SolverCbc.cpp:288
virtual void get_rows(int *mstart, int *mclind, double *dmatval, int size, int *nels, int first, int last) const override
get coefficients of rows from index first to last
Definition SolverCbc.cpp:268
virtual void chg_obj_direction(const bool minimize) override
Change the problem's objective function sense to minimize or maximize.
Definition SolverCbc.cpp:465
virtual void set_simplex_iter(int iter) override
Sets the maximum number of simplex iterations the solver can perform.
Definition SolverCbc.cpp:697
SolverCbc()
Default constructor of a CBC solver.
Definition SolverCbc.cpp:19
virtual void get_basis(int *rstatus, int *cstatus) const override
Returns the current basis into the user’s data arrays.
Definition SolverCbc.cpp:616
virtual std::vector< std::string > get_row_names() override
Returns the names of rows.
Definition SolverCbc.cpp:372
void set_output_log_level(int loglevel) final
Sets log level of the solver.
Definition SolverCbc.cpp:665
void save_prob(const std::filesystem::path &filename) override
Definition SolverCbc.cpp:706
virtual int get_nrows() const override
returns number of rows of the problem
Definition SolverCbc.cpp:229
virtual void chg_obj(const std::vector< int > &mindex, const std::vector< double > &obj) override
Change coefficients in objective function.
Definition SolverCbc.cpp:457
virtual std::string get_solver_name() const override
Returns the solver used.
Definition SolverCbc.h:59
virtual void chg_rhs(int id_row, double val) override
Change rhs of a row.
Definition SolverCbc.cpp:513
virtual void write_prob_mps(const std::filesystem::path &filename) override
writes an optimization problem in a MPS file
Definition SolverCbc.cpp:79
virtual void chg_row_name(int id_row, std::string const &name) override
Change the name of a constraint.
Definition SolverCbc.cpp:547
virtual double get_lp_value() const override
Get the optimal value of a LP problem (available after method "solve_lp" )
Definition SolverCbc.cpp:622
virtual void set_algorithm(std::string const &algo) override
Sets algorithm used by solver to solve LP's.
Definition SolverCbc.cpp:686
virtual int get_n_integer_vars() const override
returns number of integer variables in the problem
Definition SolverCbc.cpp:239
virtual void get_lp_sol(double *primals, double *duals, double *reduced_costs) override
Get LP solution of a problem (available after method "solve_lp")
Definition SolverCbc.cpp:628
virtual int get_nelems() const override
returns number of non zeros elements in the matrix, excluding objective
Definition SolverCbc.cpp:234
virtual void read_prob_mps(const std::filesystem::path &filename) override
reads an optimization problem contained in a MPS file
Definition SolverCbc.cpp:188
virtual void get_obj(double *obj, int first, int last) const override
returns the objective function coefficients for the columns in a given range
Definition SolverCbc.cpp:244
virtual void get_row_type(char *qrtype, int first, int last) const override
Returns the row types for the rows in a given range.
Definition SolverCbc.cpp:275
void restore_prob(const std::filesystem::path &filename) override
Definition SolverCbc.cpp:714
virtual int get_row_index(std::string const &name) override
Returns the index of row named "name".
Definition SolverCbc.cpp:333
virtual int solve_mip() override
Solves a problem as MIP.
Definition SolverCbc.cpp:582
virtual void free() override
Frees all the datas contained in the Solver environment.
Definition SolverCbc.cpp:71
virtual void chg_bounds(const std::vector< int > &mindex, const std::vector< char > &qbtype, const std::vector< double > &bnd) override
Change bounds of some variables.
Definition SolverCbc.cpp:470
virtual double get_mip_value() const override
Get the optimal value of a MIP problem (available after method "solve_mip")
Definition SolverCbc.cpp:620
void set_obj(const double *obj, int first, int last) override
Set the objective function coefficients for the columns in a given range.
Definition SolverCbc.cpp:258
virtual void write_basis(const std::filesystem::path &filename) override
Writes the current basis to a file for later input into the optimizer.
Definition SolverCbc.cpp:154
virtual void chg_coef(int id_row, int id_col, double val) override
Change a coefficient in the matrix.
Definition SolverCbc.cpp:539
virtual void set_threads(int n_threads) override
Sets the maximum number of threads used to perform optimization.
Definition SolverCbc.cpp:690
virtual void add_rows(int newrows, int newnz, const char *qrtype, const double *rhs, const double *range, const int *mstart, const int *mclind, const double *dmatval, const std::vector< std::string > &names={}) override
Adds rows to the problem.
Definition SolverCbc.cpp:408
virtual int get_ncols() const override
returns number of columns of the problem
Definition SolverCbc.cpp:224
virtual void chg_col_type(const std::vector< int > &mindex, const std::vector< char > &qctype) override
Change type of some columns.
Definition SolverCbc.cpp:489
Definition SolverAbstract.h:16