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 "CoinMpsIO.hpp"
7#include "OsiClpSolverInterface.hpp"
8#include "antares-xpansion/multisolver_interface/SolverAbstract.h"
9
16{
17public:
18 const std::string name_ = "CBC";
19 OsiClpSolverInterface _clp_inner_solver;
20 CbcModel _cbc;
21 int _current_log_level;
22
23 /*************************************************************************************************
24 ----------------------------------- Constructor/Desctructor
25 --------------------------------
26 *************************************************************************************************/
27
28public:
32 SolverCbc();
33 explicit SolverCbc(const SolverLogManager& log_manager);
34
35 [[nodiscard]] SolverCbc* clone() const override;
36 SolverCbc(const SolverCbc& other);
37 SolverCbc& operator=(const SolverCbc& other) = delete;
38 ~SolverCbc();
39 int get_number_of_instances() override;
40
41 std::string get_solver_name() const override
42 {
43 return name_;
44 }
45
46private:
47 void defineCbcModelFromInnerSolver();
48 void setClpSimplexColNamesFromInnerSolver(ClpSimplex* clps) const;
49 void setClpSimplexRowNamesFromInnerSolver(ClpSimplex* clps) const;
50
51 /*************************************************************************************************
52 --------------------------------- Output and stream management
53 -----------------------------
54 *************************************************************************************************/
55
56 /*************************************************************************************************
57 ------ Destruction or creation of inner strctures and datas, closing
58 environments ----------
59 *************************************************************************************************/
60
61public:
62 void init() override;
63 void free() override;
64
65 /*************************************************************************************************
66 ------------------------------- Reading & Writing problems
67 -------------------------------
68 *************************************************************************************************/
69
70public:
71 void write_prob_mps(const std::filesystem::path& filename) override;
72 void write_prob_lp(const std::filesystem::path& filename) override;
73 void save_prob(const std::filesystem::path& filename) override;
74 void write_basis(const std::filesystem::path& filename) override;
75
76 void read_prob_mps(const std::filesystem::path& filename) override;
77 void read_prob_lp(const std::filesystem::path& filename) override;
78 void restore_prob(const std::filesystem::path& filename) override;
79 void read_basis(const std::filesystem::path& filename) override;
80 void set_basis(std::span<int> rstatus, std::span<int> cstatus) override;
81
82 /*************************************************************************************************
83 ----------------------- Get general informations about problem
84 ----------------------------
85 *************************************************************************************************/
86
87public:
88 int get_ncols() const override;
89 int get_nrows() const override;
90 int get_nelems() const override;
91 int get_n_integer_vars() const override;
92 void get_obj(double* obj, int first, int last) const override;
93 void set_obj_to_zero() override;
94 void set_obj(const double* obj, int first, int last) override;
95 void get_rows(int* mstart,
96 int* mclind,
97 double* dmatval,
98 int size,
99 int* nels,
100 int first,
101 int last) const override;
102 void get_row_type(char* qrtype, int first, int last) const override;
103 void get_rhs(double* rhs, int first, int last) const override;
104 void get_rhs_range(double* range, int first, int last) const override;
105 void get_cols(int* mstart,
106 int* mrwind,
107 double* dmatval,
108 int size,
109 int* nels,
110 int first,
111 int last) const override;
112 void get_col_type(char* coltype, int first, int last) const override;
113 void get_lb(double* lb, int fisrt, int last) const override;
114 void get_ub(double* ub, int fisrt, int last) const override;
115
116 int get_row_index(const std::string& name) override;
117 int get_col_index(const std::string& name) override;
118 std::vector<std::string> get_row_names(int first, int last) const override;
119 std::vector<std::string> get_row_names() override;
120 std::vector<std::string> get_col_names(int first, int last) const override;
121 std::vector<std::string> get_col_names() override;
122
123 /*************************************************************************************************
124 ------------------------------ Methods to modify problem
125 ----------------------------------
126 *************************************************************************************************/
127
128public:
129 void del_rows(int first, int last) override;
130 void del_cols(int first, int last) override;
131 void add_rows(int newrows,
132 int newnz,
133 const char* qrtype,
134 const double* rhs,
135 const double* range,
136 const int* mstart,
137 const int* mclind,
138 const double* dmatval,
139 const std::vector<std::string>& row_names) override;
140 void add_cols(int newcol,
141 int newnz,
142 const double* objx,
143 const int* mstart,
144 const int* mrwind,
145 const double* dmatval,
146 const double* bdl,
147 const double* bdu,
148 const std::vector<std::string>& col_names) override;
149 void add_name(int type, const char* cnames, int indice) override;
150 void add_names(int type, const std::vector<std::string>& cnames, int first, int end) override;
151 void chg_obj(const std::vector<int>& mindex, const std::vector<double>& obj) override;
152 void chg_obj_direction(bool minimize) override;
153 void chg_bounds(const std::vector<int>& mindex,
154 const std::vector<char>& qbtype,
155 const std::vector<double>& bnd) override;
156 void chg_col_type(const std::vector<int>& mindex, const std::vector<char>& qctype) override;
157 void chg_rhs(int id_row, double val) override;
158 void chg_coef(int id_row, int id_col, double val) override;
159 void chg_row_name(int id_row, const std::string& name) override;
160 void chg_col_name(int id_col, const std::string& name) override;
161
162 /*************************************************************************************************
163 ----------------------------- Methods to solve the problem
164 ---------------------------------
165 *************************************************************************************************/
166
167public:
168 int solve_lp() override;
169 int solve_mip() override;
170 void presolve_only() override;
171
172 /*************************************************************************************************
173 ------------------------- Methods to get solutions information
174 -----------------------------
175 *************************************************************************************************/
176
177public:
192 void get_basis(int* rstatus, int* cstatus) const override;
193 double get_mip_value() const override;
194 double get_lp_value() const override;
195 int get_splex_num_of_ite_last() const override;
196 void get_lp_sol(double* primals, double* duals, double* reduced_costs) const override;
197 void get_mip_sol(double* primals) override;
198 void get_presolve_map(int* rowmap, int* colmap) const override;
199
200 /*************************************************************************************************
201 ------------------------ Methods to set algorithm or logs levels
202 ---------------------------
203 *************************************************************************************************/
204
205public:
206 void set_output_log_level(int loglevel) final;
207 void set_algorithm(const std::string& algo) override;
208 void set_threads(int n_threads) override;
209 void set_optimality_gap(double gap) override;
210 void set_simplex_iter(int iter) override;
211 void mark_indices_to_keep_presolve(int nrows, int ncols, int* rowind, int* colind) override;
212};
Definition SolverAbstract.h:203
Definition SolverCbc.h:16
int get_number_of_instances() override
Returns number of instances of solver currently in memory.
Definition SolverCbc.cpp:64
void add_name(int type, const char *cnames, int indice) override
Adds a name to a row or a column.
Definition SolverCbc.cpp:582
void get_ub(double *ub, int fisrt, int last) const override
Returns the upper bounds for variables in a given range.
Definition SolverCbc.cpp:410
int get_row_index(const std::string &name) override
Returns the index of row named "name".
Definition SolverCbc.cpp:421
void del_cols(int first, int last) override
Deletes col at index first and last.
Definition SolverCbc.cpp:517
void read_prob_lp(const std::filesystem::path &filename) override
reads an optimization problem contained in a MPS file
Definition SolverCbc.cpp:245
void presolve_only() override
Presolve the problem (to be implemented by derived classes)
Definition SolverCbc.cpp:947
void get_lb(double *lb, int fisrt, int last) const override
Returns the lower bounds for variables in a given range.
Definition SolverCbc.cpp:400
void init() override
Initializes a problem.
Definition SolverCbc.cpp:86
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:827
int solve_lp() override
Solves a problem as LP.
Definition SolverCbc.cpp:739
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:252
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:375
void write_prob_lp(const std::filesystem::path &filename) override
writes an optimization problem in a LP file
Definition SolverCbc.cpp:187
void set_optimality_gap(double gap) override
Sets the optimality gap.
Definition SolverCbc.cpp:913
void del_rows(int first, int last) override
Deletes rows between index first and last.
Definition SolverCbc.cpp:507
void get_mip_sol(double *primals) override
Get MIP solution of a problem (available after method "solve_mip")
Definition SolverCbc.cpp:862
void chg_col_name(int id_col, const std::string &name) override
Change the name of a variable.
Definition SolverCbc.cpp:730
std::vector< std::string > get_col_names() override
Returns the names of columns.
Definition SolverCbc.cpp:497
void get_cols(int *mstart, int *mrwind, double *dmatval, int size, int *nels, int first, int last) const override
get coefficients of cols from index first to last
Definition SolverCbc.cpp:363
void set_obj_to_zero() override
Set the objective function coefficients to zero.
Definition SolverCbc.cpp:303
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:553
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:344
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:351
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:325
void set_simplex_iter(int iter) override
Sets the maximum number of simplex iterations the solver can perform.
Definition SolverCbc.cpp:918
SolverCbc()
Default constructor of a CBC solver.
Definition SolverCbc.cpp:38
void get_basis(int *rstatus, int *cstatus) const override
Returns the current basis into the user's data arrays.
Definition SolverCbc.cpp:812
std::vector< std::string > get_row_names() override
Returns the names of rows.
Definition SolverCbc.cpp:472
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:832
void set_output_log_level(int loglevel) final
Sets log level of the solver.
Definition SolverCbc.cpp:878
void save_prob(const std::filesystem::path &filename) override
Definition SolverCbc.cpp:927
int get_nrows() const override
returns number of rows of the problem
Definition SolverCbc.cpp:275
std::string get_solver_name() const override
Returns the solver used.
Definition SolverCbc.h:41
void chg_obj(const std::vector< int > &mindex, const std::vector< double > &obj) override
Change coefficients in objective function.
Definition SolverCbc.cpp:595
void mark_indices_to_keep_presolve(int nrows, int ncols, int *rowind, int *colind) override
Mark indices to keep during presolve (to be implemented by derived classes)
Definition SolverCbc.cpp:941
void chg_rhs(int id_row, double val) override
Change rhs of a row.
Definition SolverCbc.cpp:670
void write_prob_mps(const std::filesystem::path &filename) override
writes an optimization problem in a MPS file
Definition SolverCbc.cpp:101
void chg_row_name(int id_row, const std::string &name) override
Change the name of a constraint.
Definition SolverCbc.cpp:725
double get_lp_value() const override
Get the optimal value of a LP problem (available after method "solve_lp" )
Definition SolverCbc.cpp:822
int get_n_integer_vars() const override
returns number of integer variables in the problem
Definition SolverCbc.cpp:287
int get_nelems() const override
returns number of non zeros elements in the matrix, excluding objective
Definition SolverCbc.cpp:281
void read_prob_mps(const std::filesystem::path &filename) override
reads an optimization problem contained in a MPS file
Definition SolverCbc.cpp:230
int get_col_index(const std::string &name) override
Returns the index of column named "name".
Definition SolverCbc.cpp:436
void get_presolve_map(int *rowmap, int *colmap) const override
Get the presolve map (to be implemented by derived classes)
Definition SolverCbc.cpp:952
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:293
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:337
void restore_prob(const std::filesystem::path &filename) override
Definition SolverCbc.cpp:936
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:527
int solve_mip() override
Solves a problem as MIP.
Definition SolverCbc.cpp:770
void free() override
Frees all the datas contained in the Solver environment.
Definition SolverCbc.cpp:92
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:610
double get_mip_value() const override
Get the optimal value of a MIP problem (available after method "solve_mip")
Definition SolverCbc.cpp:817
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:310
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:192
void chg_coef(int id_row, int id_col, double val) override
Change a coefficient in the matrix.
Definition SolverCbc.cpp:716
void chg_obj_direction(bool minimize) override
Change the problem's objective function sense to minimize or maximize.
Definition SolverCbc.cpp:604
void set_algorithm(const std::string &algo) override
Sets algorithm used by solver to solve LP's.
Definition SolverCbc.cpp:903
void set_threads(int n_threads) override
Sets the maximum number of threads used to perform optimization.
Definition SolverCbc.cpp:908
int get_ncols() const override
returns number of columns of the problem
Definition SolverCbc.cpp:269
void chg_col_type(const std::vector< int > &mindex, const std::vector< char > &qctype) override
Change type of some columns.
Definition SolverCbc.cpp:638
Definition SolverAbstract.h:20