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
18{
19 /*************************************************************************************************
20 ---------------------------------------- ATTRIBUTES
21 ---------------------------------------
22 *************************************************************************************************/
23 static int _NumberOfProblems;
27public:
28 const std::string name_ = "CBC";
29 OsiClpSolverInterface _clp_inner_solver;
30 CbcModel _cbc;
31 int _current_log_level;
32
33 /*************************************************************************************************
34 ----------------------------------- Constructor/Desctructor
35 --------------------------------
36 *************************************************************************************************/
37
38public:
42 SolverCbc();
43 explicit SolverCbc(SolverLogManager& log_manager);
44
52 explicit SolverCbc(const std::shared_ptr<const SolverAbstract> toCopy);
53
54 /*SolverCbc ctor accept only std::shared_ptr*/
55 SolverCbc(const SolverCbc& other) = delete;
56 SolverCbc& operator=(const SolverCbc& other) = delete;
57 virtual ~SolverCbc();
58 virtual int get_number_of_instances() override;
59
60 virtual std::string get_solver_name() const override
61 {
62 return name_;
63 }
64
65private:
66 void defineCbcModelFromInnerSolver();
67 void setClpSimplexColNamesFromInnerSolver(ClpSimplex* clps) const;
68 void setClpSimplexRowNamesFromInnerSolver(ClpSimplex* clps) const;
69
70 /*************************************************************************************************
71 --------------------------------- Output and stream management
72 -----------------------------
73 *************************************************************************************************/
74
75 /*************************************************************************************************
76 ------ Destruction or creation of inner strctures and datas, closing
77 environments ----------
78 *************************************************************************************************/
79
80public:
81 virtual void init() override;
82 virtual void free() override;
83
84 /*************************************************************************************************
85 ------------------------------- Reading & Writing problems
86 -------------------------------
87 *************************************************************************************************/
88
89public:
90 virtual void write_prob_mps(const std::filesystem::path& filename) override;
91 virtual void write_prob_lp(const std::filesystem::path& filename) override;
92 void save_prob(const std::filesystem::path& filename) override;
93 virtual void write_basis(const std::filesystem::path& filename) override;
94
95 virtual void read_prob_mps(const std::filesystem::path& filename) override;
96 virtual void read_prob_lp(const std::filesystem::path& filename) override;
97 void restore_prob(const std::filesystem::path& filename) override;
98 virtual void read_basis(const std::filesystem::path& filename) override;
99
100 virtual void copy_prob(const SolverAbstract::Ptr fictif_solv) override;
101
102 /*************************************************************************************************
103 ----------------------- Get general informations about problem
104 ----------------------------
105 *************************************************************************************************/
106
107public:
108 virtual int get_ncols() const override;
109 virtual int get_nrows() const override;
110 virtual int get_nelems() const override;
111 virtual int get_n_integer_vars() const override;
112 virtual void get_obj(double* obj, int first, int last) const override;
113 void set_obj_to_zero() override;
114 void set_obj(const double* obj, int first, int last) override;
115 virtual void get_rows(int* mstart,
116 int* mclind,
117 double* dmatval,
118 int size,
119 int* nels,
120 int first,
121 int last) const override;
122 virtual void get_row_type(char* qrtype, int first, int last) const override;
123 virtual void get_rhs(double* rhs, int first, int last) const override;
124 virtual void get_rhs_range(double* range, int first, int last) const override;
125 virtual void get_col_type(char* coltype, int first, int last) const override;
126 virtual void get_lb(double* lb, int fisrt, int last) const override;
127 virtual void get_ub(double* ub, int fisrt, int last) const override;
128
129 virtual int get_row_index(const std::string& name) override;
130 virtual int get_col_index(const std::string& name) override;
131 virtual std::vector<std::string> get_row_names(int first, int last) override;
132 virtual std::vector<std::string> get_row_names() override;
133 virtual std::vector<std::string> get_col_names(int first, int last) override;
134 virtual std::vector<std::string> get_col_names() override;
135
136 /*************************************************************************************************
137 ------------------------------ Methods to modify problem
138 ----------------------------------
139 *************************************************************************************************/
140
141public:
142 virtual void del_rows(int first, int last) override;
143 virtual void add_rows(int newrows,
144 int newnz,
145 const char* qrtype,
146 const double* rhs,
147 const double* range,
148 const int* mstart,
149 const int* mclind,
150 const double* dmatval,
151 const std::vector<std::string>& row_names) override;
152 virtual void add_cols(int newcol,
153 int newnz,
154 const double* objx,
155 const int* mstart,
156 const int* mrwind,
157 const double* dmatval,
158 const double* bdl,
159 const double* bdu,
160 const std::vector<std::string>& col_names) override;
161 virtual void add_name(int type, const char* cnames, int indice) override;
162 virtual void add_names(int type,
163 const std::vector<std::string>& cnames,
164 int first,
165 int end) override;
166 virtual void chg_obj(const std::vector<int>& mindex, const std::vector<double>& obj) override;
167 virtual void chg_obj_direction(const bool minimize) override;
168 virtual void chg_bounds(const std::vector<int>& mindex,
169 const std::vector<char>& qbtype,
170 const std::vector<double>& bnd) override;
171 virtual void chg_col_type(const std::vector<int>& mindex,
172 const std::vector<char>& qctype) override;
173 virtual void chg_rhs(int id_row, double val) override;
174 virtual void chg_coef(int id_row, int id_col, double val) override;
175 virtual void chg_row_name(int id_row, const std::string& name) override;
176 virtual void chg_col_name(int id_col, const std::string& name) override;
177
178 /*************************************************************************************************
179 ----------------------------- Methods to solve the problem
180 ---------------------------------
181 *************************************************************************************************/
182
183public:
184 virtual int solve_lp() override;
185 virtual int solve_mip() override;
186
187 /*************************************************************************************************
188 ------------------------- Methods to get solutions information
189 -----------------------------
190 *************************************************************************************************/
191
192public:
207 virtual void get_basis(int* rstatus, int* cstatus) const override;
208 virtual double get_mip_value() const override;
209 virtual double get_lp_value() const override;
210 virtual int get_splex_num_of_ite_last() const override;
211 virtual void get_lp_sol(double* primals, double* duals, double* reduced_costs) const override;
212 virtual void get_mip_sol(double* primals) override;
213
214 /*************************************************************************************************
215 ------------------------ Methods to set algorithm or logs levels
216 ---------------------------
217 *************************************************************************************************/
218
219public:
220 void set_output_log_level(int loglevel) final;
221 virtual void set_algorithm(const std::string& algo) override;
222 virtual void set_threads(int n_threads) override;
223 virtual void set_optimality_gap(double gap) override;
224 virtual void set_simplex_iter(int iter) override;
225};
Definition SolverAbstract.h:200
std::shared_ptr< SolverAbstract > Ptr
Definition SolverAbstract.h:215
Definition SolverCbc.h:18
virtual int get_number_of_instances() override
Returns number of instances of solver currently in memory.
Definition SolverCbc.cpp:58
virtual void add_name(int type, const char *cnames, int indice) override
Adds a name to a row or a column.
Definition SolverCbc.cpp:554
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:394
virtual int get_row_index(const std::string &name) override
Returns the index of row named "name".
Definition SolverCbc.cpp:405
virtual void read_prob_lp(const std::filesystem::path &filename) override
reads an optimization problem contained in a MPS file
Definition SolverCbc.cpp:240
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:384
virtual void init() override
Initializes a problem.
Definition SolverCbc.cpp:80
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:789
virtual int solve_lp() override
Solves a problem as LP.
Definition SolverCbc.cpp:701
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:247
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:359
virtual void write_prob_lp(const std::filesystem::path &filename) override
writes an optimization problem in a LP file
Definition SolverCbc.cpp:182
virtual void set_optimality_gap(double gap) override
Sets the optimality gap.
Definition SolverCbc.cpp:875
virtual void del_rows(int first, int last) override
Deletes rows between index first and last.
Definition SolverCbc.cpp:489
virtual void get_mip_sol(double *primals) override
Get MIP solution of a problem (available after method "solve_mip")
Definition SolverCbc.cpp:824
virtual void chg_col_name(int id_col, const std::string &name) override
Change the name of a variable.
Definition SolverCbc.cpp:692
virtual std::vector< std::string > get_col_names() override
Returns the names of columns.
Definition SolverCbc.cpp:479
virtual void copy_prob(const SolverAbstract::Ptr fictif_solv) override
copy an existing problem
Definition SolverCbc.cpp:255
void set_obj_to_zero() override
Set the objective function coefficients to zero.
Definition SolverCbc.cpp:299
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, const std::vector< std::string > &col_names) override
Adds new columns to the problem.
Definition SolverCbc.cpp:525
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:340
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:347
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:321
virtual void chg_obj_direction(const bool minimize) override
Change the problem's objective function sense to minimize or maximize.
Definition SolverCbc.cpp:576
virtual void set_simplex_iter(int iter) override
Sets the maximum number of simplex iterations the solver can perform.
Definition SolverCbc.cpp:880
SolverCbc()
Default constructor of a CBC solver.
Definition SolverCbc.cpp:23
virtual void get_basis(int *rstatus, int *cstatus) const override
Returns the current basis into the user’s data arrays.
Definition SolverCbc.cpp:774
virtual std::vector< std::string > get_row_names() override
Returns the names of rows.
Definition SolverCbc.cpp:455
virtual void get_lp_sol(double *primals, double *duals, double *reduced_costs) const override
Get LP solution of a problem (available after method "solve_lp")
Definition SolverCbc.cpp:794
void set_output_log_level(int loglevel) final
Sets log level of the solver.
Definition SolverCbc.cpp:840
void save_prob(const std::filesystem::path &filename) override
Definition SolverCbc.cpp:889
virtual int get_nrows() const override
returns number of rows of the problem
Definition SolverCbc.cpp:271
virtual void chg_obj(const std::vector< int > &mindex, const std::vector< double > &obj) override
Change coefficients in objective function.
Definition SolverCbc.cpp:567
virtual std::string get_solver_name() const override
Returns the solver used.
Definition SolverCbc.h:60
virtual void chg_rhs(int id_row, double val) override
Change rhs of a row.
Definition SolverCbc.cpp:642
virtual void write_prob_mps(const std::filesystem::path &filename) override
writes an optimization problem in a MPS file
Definition SolverCbc.cpp:95
virtual void chg_row_name(int id_row, const std::string &name) override
Change the name of a constraint.
Definition SolverCbc.cpp:687
virtual double get_lp_value() const override
Get the optimal value of a LP problem (available after method "solve_lp" )
Definition SolverCbc.cpp:784
virtual int get_n_integer_vars() const override
returns number of integer variables in the problem
Definition SolverCbc.cpp:283
virtual int get_nelems() const override
returns number of non zeros elements in the matrix, excluding objective
Definition SolverCbc.cpp:277
virtual void read_prob_mps(const std::filesystem::path &filename) override
reads an optimization problem contained in a MPS file
Definition SolverCbc.cpp:225
virtual int get_col_index(const std::string &name) override
Returns the index of column named "name".
Definition SolverCbc.cpp:420
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:289
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:333
void restore_prob(const std::filesystem::path &filename) override
Definition SolverCbc.cpp:898
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 > &row_names) override
Adds rows to the problem.
Definition SolverCbc.cpp:499
virtual int solve_mip() override
Solves a problem as MIP.
Definition SolverCbc.cpp:732
virtual void free() override
Frees all the datas contained in the Solver environment.
Definition SolverCbc.cpp:86
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:582
virtual double get_mip_value() const override
Get the optimal value of a MIP problem (available after method "solve_mip")
Definition SolverCbc.cpp:779
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:306
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:187
virtual void chg_coef(int id_row, int id_col, double val) override
Change a coefficient in the matrix.
Definition SolverCbc.cpp:678
virtual void set_algorithm(const std::string &algo) override
Sets algorithm used by solver to solve LP's.
Definition SolverCbc.cpp:865
virtual void set_threads(int n_threads) override
Sets the maximum number of threads used to perform optimization.
Definition SolverCbc.cpp:870
virtual int get_ncols() const override
returns number of columns of the problem
Definition SolverCbc.cpp:265
virtual void chg_col_type(const std::vector< int > &mindex, const std::vector< char > &qctype) override
Change type of some columns.
Definition SolverCbc.cpp:610
Definition SolverAbstract.h:17