Antares Xpansion
Investment simulations for Antares studies
Loading...
Searching...
No Matches
SolverClp.h
1#pragma once
2
3#include "ClpSimplex.hpp"
4#include "CoinHelperFunctions.hpp"
5#include "CoinIndexedVector.hpp"
6#include "antares-xpansion/multisolver_interface/SolverAbstract.h"
7
8enum CLP_STATUS {
9 CLP_OPTIMAL,
10 CLP_PRIMAL_INFEASIBLE,
11 CLP_DUAL_INFEASIBLE,
12 CLP_STOPPED,
13 CLP_ERRORED
14};
15
20class SolverClp : public SolverAbstract {
21 /*************************************************************************************************
22 ---------------------------------------- ATTRIBUTES
23 ---------------------------------------
24 *************************************************************************************************/
25 static int _NumberOfProblems;
29 public:
30 ClpSimplex _clp;
31 const std::string name_ = "CLP";
32
33 /*************************************************************************************************
34 ----------------------------------- Constructor/Desctructor
35 --------------------------------
36 *************************************************************************************************/
37
38 public:
42 SolverClp();
43 explicit SolverClp(SolverLogManager &log_manager);
44
52 explicit SolverClp(const std::shared_ptr<const SolverAbstract> toCopy);
53
54 /*SolverClp ctor accept only std::shared_ptr*/
55 SolverClp(const SolverClp &other) = delete;
56 SolverClp &operator=(const SolverClp &other) = delete;
57
58 ~SolverClp() override;
59 virtual int get_number_of_instances() override;
60
61 virtual std::string get_solver_name() const override { return name_; }
62
63 /*************************************************************************************************
64 --------------------------------- Output and stream management
65 -----------------------------
66 *************************************************************************************************/
67
68 /*************************************************************************************************
69 ------ Destruction or creation of inner strctures and datas, closing
70 environments ----------
71 *************************************************************************************************/
72 public:
73 virtual void init() override;
74 virtual void free() override;
75
76 /*************************************************************************************************
77 ------------------------------- Reading & Writing problems
78 -------------------------------
79 *************************************************************************************************/
80 public:
81 virtual void write_prob_mps(const std::filesystem::path &filename) override;
82 virtual void write_prob_lp(const std::filesystem::path &filename) override;
83 void save_prob(const std::filesystem::path &filename) override;
84 virtual void write_basis(const std::filesystem::path &filename) override;
85
86 virtual void read_prob_mps(const std::filesystem::path &filename) override;
87 virtual void read_prob_lp(const std::filesystem::path &filename) override;
88 void restore_prob(const std::filesystem::path &filename) override;
89 virtual void read_basis(const std::filesystem::path &filename) override;
90
91 virtual void copy_prob(const SolverAbstract::Ptr fictif_solv) override;
92
93 /*************************************************************************************************
94 ----------------------- Get general informations about problem
95 ----------------------------
96 *************************************************************************************************/
97 public:
98 virtual int get_ncols() const override;
99 virtual int get_nrows() const override;
100 virtual int get_nelems() const override;
101 virtual int get_n_integer_vars() const override;
102 virtual void get_obj(double *obj, int first, int last) const override;
103 void set_obj_to_zero() override;
104 void set_obj(const double *obj, int first, int last) override;
105 virtual void get_rows(int *mstart, int *mclind, double *dmatval, int size,
106 int *nels, int first, int last) const override;
107 virtual void get_row_type(char *qrtype, int first, int last) const override;
108 virtual void get_rhs(double *rhs, int first, int last) const override;
109 virtual void get_rhs_range(double *range, int first, int last) const override;
110 virtual void get_col_type(char *coltype, int first, int last) const override;
111 virtual void get_lb(double *lb, int fisrt, int last) const override;
112 virtual void get_ub(double *ub, int fisrt, int last) const override;
113
114 virtual int get_row_index(std::string const &name) override;
115 virtual int get_col_index(std::string const &name) override;
116 virtual std::vector<std::string> get_row_names(int first, int last) override;
117 virtual std::vector<std::string> get_row_names() override;
118 virtual std::vector<std::string> get_col_names(int first, int last) override;
119 virtual std::vector<std::string> get_col_names() override;
120
121 /*************************************************************************************************
122 ------------------------------ Methods to modify problem
123 ----------------------------------
124 *************************************************************************************************/
125 public:
126 virtual void del_rows(int first, int last) override;
127 virtual void add_rows(int newrows, int newnz, const char *qrtype,
128 const double *rhs, const double *range,
129 const int *mstart, const int *mclind,
130 const double *dmatval,
131 const std::vector<std::string> &names = {}) override;
132 virtual void add_cols(int newcol, int newnz, const double *objx,
133 const int *mstart, const int *mrwind,
134 const double *dmatval, const double *bdl,
135 const double *bdu) override;
136 virtual void add_name(int type, const char *cnames, int indice) override;
137 virtual void add_names(int type, const std::vector<std::string> &cnames,
138 int first, int end) override;
139 virtual void chg_obj(const std::vector<int> &mindex,
140 const std::vector<double> &obj) override;
141 virtual void chg_obj_direction(const bool minimize) override;
142 virtual void chg_bounds(const std::vector<int> &mindex,
143 const std::vector<char> &qbtype,
144 const std::vector<double> &bnd);
145 virtual void chg_col_type(const std::vector<int> &mindex,
146 const std::vector<char> &qctype) override;
147 virtual void chg_rhs(int id_row, double val) override;
148 virtual void chg_coef(int id_row, int id_col, double val) override;
149 virtual void chg_row_name(int id_row, std::string const &name) override;
150 virtual void chg_col_name(int id_col, std::string const &name) override;
151
152 /*************************************************************************************************
153 ----------------------------- Methods to solve the problem
154 ---------------------------------
155 *************************************************************************************************/
156 public:
157 virtual int solve_lp() override;
158 virtual int solve_mip() override;
159
160 /*************************************************************************************************
161 ------------------------- Methods to get solutions information
162 -----------------------------
163 *************************************************************************************************/
164 public:
179 virtual void get_basis(int *rstatus, int *cstatus) const override;
180 virtual double get_mip_value() const override;
181 virtual double get_lp_value() const override;
182 virtual int get_splex_num_of_ite_last() const override;
183 virtual void get_lp_sol(double *primals, double *duals,
184 double *reduced_costs) override;
185 virtual void get_mip_sol(double *primals) override;
186
187 /*************************************************************************************************
188 ------------------------ Methods to set algorithm or logs levels
189 ---------------------------
190 *************************************************************************************************/
191 public:
192 void set_output_log_level(int loglevel) final;
193 virtual void set_algorithm(std::string const &algo) override;
194 virtual void set_threads(int n_threads) override;
195 virtual void set_optimality_gap(double gap) override;
196 virtual void set_simplex_iter(int iter) override;
197};
Definition SolverAbstract.h:170
std::shared_ptr< SolverAbstract > Ptr
Definition SolverAbstract.h:181
Definition SolverClp.h:20
virtual void set_threads(int n_threads) override
Sets the maximum number of threads used to perform optimization.
Definition SolverClp.cpp:545
virtual void set_algorithm(std::string const &algo) override
Sets algorithm used by solver to solve LP's.
Definition SolverClp.cpp:537
virtual int get_row_index(std::string const &name) override
Returns the index of row named "name".
Definition SolverClp.cpp:218
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 SolverClp.cpp:157
virtual std::vector< std::string > get_col_names() override
Returns the names of columns.
Definition SolverClp.cpp:264
SolverClp()
Default constructor of a CLP solver.
Definition SolverClp.cpp:18
virtual void write_prob_lp(const std::filesystem::path &filename) override
writes an optimization problem in a LP file
Definition SolverClp.cpp:73
virtual void write_prob_mps(const std::filesystem::path &filename) override
writes an optimization problem in a MPS file
Definition SolverClp.cpp:65
virtual void write_basis(const std::filesystem::path &filename) override
Writes the current basis to a file for later input into the optimizer.
Definition SolverClp.cpp:77
virtual int solve_lp() override
Solves a problem as LP.
Definition SolverClp.cpp:430
virtual void chg_col_type(const std::vector< int > &mindex, const std::vector< char > &qctype) override
Change type of some columns.
Definition SolverClp.cpp:360
virtual void free() override
Frees all the datas contained in the Solver environment.
Definition SolverClp.cpp:57
virtual int get_n_integer_vars() const override
returns number of integer variables in the problem
Definition SolverClp.cpp:122
virtual void get_ub(double *ub, int fisrt, int last) const override
Returns the upper bounds for variables in a given range.
Definition SolverClp.cpp:210
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 SolverClp.cpp:185
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 SolverClp.cpp:165
virtual void chg_obj(const std::vector< int > &mindex, const std::vector< double > &obj) override
Change coefficients in objective function.
Definition SolverClp.cpp:328
virtual void chg_bounds(const std::vector< int > &mindex, const std::vector< char > &qbtype, const std::vector< double > &bnd)
Change bounds of some variables.
Definition SolverClp.cpp:341
virtual void read_basis(const std::filesystem::path &filename) override
Instructs the optimizer to read in a previously saved basis from a file.
Definition SolverClp.cpp:103
virtual void get_mip_sol(double *primals) override
Get MIP solution of a problem (available after method "solve_mip")
Definition SolverClp.cpp:516
virtual double get_lp_value() const override
Get the optimal value of a LP problem (available after method "solve_lp" )
Definition SolverClp.cpp:484
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 SolverClp.cpp:133
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 SolverClp.cpp:486
virtual void get_lb(double *lb, int fisrt, int last) const override
Returns the lower bounds for variables in a given range.
Definition SolverClp.cpp:202
virtual int get_ncols() const override
returns number of columns of the problem
Definition SolverClp.cpp:116
virtual void set_optimality_gap(double gap) override
Sets the optimality gap.
Definition SolverClp.cpp:547
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 SolverClp.cpp:490
virtual void chg_coef(int id_row, int id_col, double val) override
Change a coefficient in the matrix.
Definition SolverClp.cpp:411
virtual std::string get_solver_name() const override
Returns the solver used.
Definition SolverClp.h:61
virtual void init() override
Initializes a problem.
Definition SolverClp.cpp:55
virtual void add_name(int type, const char *cnames, int indice) override
Adds a name to a row or a column.
Definition SolverClp.cpp:315
virtual int get_nrows() const override
returns number of rows of the problem
Definition SolverClp.cpp:118
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 SolverClp.cpp:173
virtual void chg_obj_direction(const bool minimize) override
Change the problem's objective function sense to minimize or maximize.
Definition SolverClp.cpp:336
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 SolverClp.cpp:281
virtual std::vector< std::string > get_row_names() override
Returns the names of rows.
Definition SolverClp.cpp:252
void restore_prob(const std::filesystem::path &filename) override
Definition SolverClp.cpp:566
void set_obj_to_zero() override
Set the objective function coefficients to zero.
Definition SolverClp.cpp:141
virtual void set_simplex_iter(int iter) override
Sets the maximum number of simplex iterations the solver can perform.
Definition SolverClp.cpp:552
virtual void read_prob_mps(const std::filesystem::path &filename) override
reads an optimization problem contained in a MPS file
Definition SolverClp.cpp:90
virtual void copy_prob(const SolverAbstract::Ptr fictif_solv) override
copy an existing problem
Definition SolverClp.cpp:107
virtual double get_mip_value() const override
Get the optimal value of a MIP problem (available after method "solve_mip")
Definition SolverClp.cpp:482
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 SolverClp.cpp:302
void set_obj(const double *obj, int first, int last) override
Set the objective function coefficients for the columns in a given range.
Definition SolverClp.cpp:147
virtual void del_rows(int first, int last) override
Deletes rows between index first and last.
Definition SolverClp.cpp:273
void save_prob(const std::filesystem::path &filename) override
Definition SolverClp.cpp:558
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 SolverClp.cpp:179
void set_output_log_level(int loglevel) final
Sets log level of the solver.
Definition SolverClp.cpp:529
virtual int get_nelems() const override
returns number of non zeros elements in the matrix, excluding objective
Definition SolverClp.cpp:120
virtual void read_prob_lp(const std::filesystem::path &filename) override
reads an optimization problem contained in a MPS file
Definition SolverClp.cpp:99
virtual int solve_mip() override
Solves a problem as MIP.
Definition SolverClp.cpp:448
virtual int get_number_of_instances() override
Returns number of instances of solver currently in memory.
Definition SolverClp.cpp:44
virtual void get_basis(int *rstatus, int *cstatus) const override
Returns the current basis into the user’s data arrays.
Definition SolverClp.cpp:470
virtual int get_col_index(std::string const &name) override
Returns the index of column named "name".
Definition SolverClp.cpp:230
virtual void chg_col_name(int id_col, std::string const &name) override
Change the name of a variable.
Definition SolverClp.cpp:421
virtual void chg_rhs(int id_row, double val) override
Change rhs of a row.
Definition SolverClp.cpp:386
virtual void chg_row_name(int id_row, std::string const &name) override
Change the name of a constraint.
Definition SolverClp.cpp:416
Definition SolverAbstract.h:16