Antares Xpansion
Investment simulations for Antares studies
Loading...
Searching...
No Matches
SolverXpress.h
1#pragma once
2
3#include <fstream>
4#include <mutex>
5
6#include "antares-xpansion/multisolver_interface/SolverAbstract.h"
7#include "antares-xpansion/multisolver_interface/environment.h"
8
14{
15 /*************************************************************************************************
16 ---------------------------------------- ATTRIBUTES
17 ---------------------------------------
18 *************************************************************************************************/
19 static int _NumberOfProblems;
21 static std::mutex license_guard;
22
23public:
24 XPRSprob _xprs;
25 const std::string name_ = "XPRESS";
26
27 /*************************************************************************************************
28 ----------------------------------- Constructor/Desctructor
29 --------------------------------
30 *************************************************************************************************/
31
32public:
37 SolverXpress(SolverLogManager& log_manager);
38
46 explicit SolverXpress(const SolverAbstract::Ptr toCopy);
47 explicit SolverXpress(const std::shared_ptr<const SolverAbstract> toCopy);
48
49 /*SolverXpress ctor accept only std::shared_ptr*/
50 SolverXpress(const SolverXpress& other) = delete;
51 SolverXpress& operator=(const SolverXpress& other) = delete;
52
53 virtual ~SolverXpress();
54 virtual int get_number_of_instances() override;
55
56 virtual std::string get_solver_name() const override
57 {
58 return name_;
59 }
60
61 /*************************************************************************************************
62 --------------------------------- Output and stream management
63 -----------------------------
64 *************************************************************************************************/
65
66 /*************************************************************************************************
67 ------ Destruction or creation of inner strctures and datas, closing
68 environments ----------
69 *************************************************************************************************/
70
71public:
72 virtual void init() override;
73 virtual void free() override;
74
75 /*************************************************************************************************
76 ------------------------------- Reading & Writing problems
77 -------------------------------
78 *************************************************************************************************/
79
80public:
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
93private:
94 virtual void read_prob(const char* prob_name, const char* flags);
95
96 /*************************************************************************************************
97 ----------------------- Get general informations about problem
98 ----------------------------
99 *************************************************************************************************/
100
101public:
102 virtual int get_ncols() const override;
103 virtual int get_nrows() const override;
104 virtual int get_nelems() const override;
105 virtual int get_n_integer_vars() const override;
106 virtual void get_obj(double* obj, int first, int last) const override;
107 void set_obj_to_zero() override;
108 void set_obj(const double* obj, int first, int last) override;
109 virtual void get_rows(int* mstart,
110 int* mclind,
111 double* dmatval,
112 int size,
113 int* nels,
114 int first,
115 int last) const override;
116 virtual void get_row_type(char* qrtype, int first, int last) const override;
117 virtual void get_rhs(double* rhs, int first, int last) const override;
118 virtual void get_rhs_range(double* range, int first, int last) const override;
119 virtual void get_col_type(char* coltype, int first, int last) const override;
120 virtual void get_lb(double* lb, int fisrt, int last) const override;
121 virtual void get_ub(double* ub, int fisrt, int last) const override;
122
123 virtual int get_row_index(const std::string& name) override;
124 virtual int get_col_index(const std::string& name) override;
125 virtual std::vector<std::string> get_row_names(int first, int last) override;
126 virtual std::vector<std::string> get_row_names() override;
127 virtual std::vector<std::string> get_col_names(int first, int last) override;
128 virtual std::vector<std::string> get_col_names() override;
129
130 /*************************************************************************************************
131 ------------------------------ Methods to modify problem
132 ----------------------------------
133 *************************************************************************************************/
134
135public:
136 virtual void del_rows(int first, int last) override;
137 virtual void add_rows(int newrows,
138 int newnz,
139 const char* qrtype,
140 const double* rhs,
141 const double* range,
142 const int* mstart,
143 const int* mclind,
144 const double* dmatval,
145 const std::vector<std::string>& row_names) override;
146 virtual void add_cols(int newcol,
147 int newnz,
148 const double* objx,
149 const int* mstart,
150 const int* mrwind,
151 const double* dmatval,
152 const double* bdl,
153 const double* bdu,
154 const std::vector<std::string>& col_names) override;
155 virtual void add_name(int type, const char* cnames, int indice) override;
156 virtual void add_names(int type,
157 const std::vector<std::string>& cnames,
158 int first,
159 int end) override;
160 virtual void chg_obj(const std::vector<int>& mindex, const std::vector<double>& obj) override;
161 virtual void chg_obj_direction(const bool minimize) override;
162 virtual void chg_bounds(const std::vector<int>& mindex,
163 const std::vector<char>& qbtype,
164 const std::vector<double>& bnd) override;
165 virtual void chg_col_type(const std::vector<int>& mindex,
166 const std::vector<char>& qctype) override;
167 virtual void chg_rhs(int id_row, double val) override;
168 virtual void chg_coef(int id_row, int id_col, double val) override;
169 virtual void chg_row_name(int id_row, const std::string& name) override;
170 virtual void chg_col_name(int id_col, const std::string& name) override;
171
172 /*************************************************************************************************
173 ----------------------------- Methods to solve the problem
174 ---------------------------------
175 *************************************************************************************************/
176
177public:
178 virtual int solve_lp() override;
179 virtual int solve_mip() override;
180
181 /*************************************************************************************************
182 ------------------------- Methods to get solutions information
183 -----------------------------
184 *************************************************************************************************/
185
186public:
203 virtual void get_basis(int* rstatus, int* cstatus) const override;
204 virtual double get_mip_value() const override;
205 virtual double get_lp_value() const override;
206 virtual int get_splex_num_of_ite_last() const override;
207 virtual void get_lp_sol(double* primals, double* duals, double* reduced_costs) const override;
208 virtual void get_mip_sol(double* primals) override;
209
210 /*************************************************************************************************
211 ------------------------ Methods to set algorithm or logs levels
212 ---------------------------
213 *************************************************************************************************/
214
215public:
216 void set_output_log_level(int loglevel) final;
217 virtual void set_algorithm(const std::string& algo) override;
218 virtual void set_threads(int n_threads) override;
219 virtual void set_optimality_gap(double gap) override;
220 virtual void set_simplex_iter(int iter) override;
221
222public:
223 std::ofstream _log_stream;
224
225private:
226 std::vector<std::string> get_names(int type, int n_elements);
227};
228
229/************************************************************************************\
230* Name: optimizermsg *
231* Purpose: Display Optimizer error messages and warnings. *
232* Arguments: const char *sMsg Message string *
233* int nLen Message length *
234* int nMsgLvl Message type *
235* Return Value: None. *
236\************************************************************************************/
237void errormsg(XPRSprob& xprs, const char* sSubName, int nLineNo, int nErrCode);
238
239/************************************************************************************\
240* Name: errormsg *
241* Purpose: Display error information about failed subroutines. *
242* Arguments: const char *sSubName Subroutine name *
243* int nLineNo Line number *
244* int nErrCode Error code *
245* Return Value: None *
246\************************************************************************************/
247void optimizermsg(XPRSprob prob, void* stream, const char* sMsg, int nLen, int nMsglvl);
Definition SolverAbstract.h:200
std::shared_ptr< SolverAbstract > Ptr
Definition SolverAbstract.h:215
Definition SolverAbstract.h:17
Definition SolverXpress.h:14
virtual void chg_coef(int id_row, int id_col, double val) override
Change a coefficient in the matrix.
Definition SolverXpress.cpp:543
virtual void chg_obj_direction(const bool minimize) override
Change the problem's objective function sense to minimize or maximize.
Definition SolverXpress.cpp:503
virtual void write_prob_mps(const std::filesystem::path &filename) override
writes an optimization problem in a MPS file
Definition SolverXpress.cpp:160
virtual int solve_lp() override
Solves a problem as LP.
Definition SolverXpress.cpp:565
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 SolverXpress.cpp:329
virtual int get_row_index(const std::string &name) override
Returns the index of row named "name".
Definition SolverXpress.cpp:347
virtual void write_basis(const std::filesystem::path &filename) override
Writes the current basis to a file for later input into the optimizer.
Definition SolverXpress.cpp:176
virtual std::vector< std::string > get_col_names() override
Returns the names of columns.
Definition SolverXpress.cpp:417
virtual std::string get_solver_name() const override
Returns the solver used.
Definition SolverXpress.h:56
virtual int get_n_integer_vars() const override
returns number of integer variables in the problem
Definition SolverXpress.cpp:269
virtual void init() override
Initializes a problem.
Definition SolverXpress.cpp:119
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 SolverXpress.cpp:277
virtual void add_name(int type, const char *cnames, int indice) override
Adds a name to a row or a column.
Definition SolverXpress.cpp:478
virtual void write_prob_lp(const std::filesystem::path &filename) override
writes an optimization problem in a LP file
Definition SolverXpress.cpp:167
SolverXpress()
Default constructor of a XPRESS solver.
Definition SolverXpress.cpp:31
virtual int get_nelems() const override
returns number of non zeros elements in the matrix, excluding objective
Definition SolverXpress.cpp:260
virtual void set_optimality_gap(double gap) override
Sets the optimality gap.
Definition SolverXpress.cpp:728
void save_prob(const std::filesystem::path &filename) override
write an optimisation problem in a file
Definition SolverXpress.cpp:743
virtual std::vector< std::string > get_row_names() override
Returns the names of rows.
Definition SolverXpress.cpp:396
virtual void get_basis(int *rstatus, int *cstatus) const override
Returns the current basis into the user’s data arrays.
Definition SolverXpress.cpp:634
virtual void set_algorithm(const std::string &algo) override
Sets algorithm used by solver to solve LP's.
Definition SolverXpress.cpp:697
virtual int get_ncols() const override
returns number of columns of the problem
Definition SolverXpress.cpp:244
void restore_prob(const std::filesystem::path &filename) override
read an optimisation problem from a file
Definition SolverXpress.cpp:754
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 SolverXpress.cpp:458
void set_output_log_level(int loglevel) final
Sets log level of the solver.
Definition SolverXpress.cpp:680
virtual void del_rows(int first, int last) override
Deletes rows between index first and last.
Definition SolverXpress.cpp:427
virtual int get_nrows() const override
returns number of rows of the problem
Definition SolverXpress.cpp:252
XPRSprob _xprs
Definition SolverXpress.h:24
virtual double get_mip_value() const override
Get the optimal value of a MIP problem (available after method "solve_mip")
Definition SolverXpress.cpp:640
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 SolverXpress.cpp:323
virtual void copy_prob(const SolverAbstract::Ptr fictif_solv) override
copy an existing problem
Definition SolverXpress.cpp:234
virtual int get_number_of_instances() override
Returns number of instances of solver currently in memory.
Definition SolverXpress.cpp:105
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 SolverXpress.cpp:438
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 SolverXpress.cpp:664
virtual void read_basis(const std::filesystem::path &filename) override
Instructs the optimizer to read in a previously saved basis from a file.
Definition SolverXpress.cpp:228
virtual void read_prob_lp(const std::filesystem::path &filename) override
reads an optimization problem contained in a MPS file
Definition SolverXpress.cpp:188
virtual void chg_obj(const std::vector< int > &mindex, const std::vector< double > &obj) override
Change coefficients in objective function.
Definition SolverXpress.cpp:496
virtual void chg_row_name(int id_row, const std::string &name) override
Change the name of a constraint.
Definition SolverXpress.cpp:549
virtual int solve_mip() override
Solves a problem as MIP.
Definition SolverXpress.cpp:597
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 SolverXpress.cpp:311
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 SolverXpress.cpp:317
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 SolverXpress.cpp:656
virtual void get_lb(double *lb, int fisrt, int last) const override
Returns the lower bounds for variables in a given range.
Definition SolverXpress.cpp:335
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 SolverXpress.cpp:299
void set_obj_to_zero() override
Set the objective function coefficients to zero.
Definition SolverXpress.cpp:283
virtual void read_prob_mps(const std::filesystem::path &filename) override
reads an optimization problem contained in a MPS file
Definition SolverXpress.cpp:182
virtual void chg_col_type(const std::vector< int > &mindex, const std::vector< char > &qctype) override
Change type of some columns.
Definition SolverXpress.cpp:524
virtual void get_ub(double *ub, int fisrt, int last) const override
Returns the upper bounds for variables in a given range.
Definition SolverXpress.cpp:341
void set_obj(const double *obj, int first, int last) override
Set the objective function coefficients for the columns in a given range.
Definition SolverXpress.cpp:290
virtual void set_simplex_iter(int iter) override
Sets the maximum number of simplex iterations the solver can perform.
Definition SolverXpress.cpp:734
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 SolverXpress.cpp:510
virtual void chg_rhs(int id_row, double val) override
Change rhs of a row.
Definition SolverXpress.cpp:534
virtual double get_lp_value() const override
Get the optimal value of a LP problem (available after method "solve_lp" )
Definition SolverXpress.cpp:648
virtual void chg_col_name(int id_col, const std::string &name) override
Change the name of a variable.
Definition SolverXpress.cpp:555
virtual void get_mip_sol(double *primals) override
Get MIP solution of a problem (available after method "solve_mip")
Definition SolverXpress.cpp:670
virtual void set_threads(int n_threads) override
Sets the maximum number of threads used to perform optimization.
Definition SolverXpress.cpp:722
virtual void free() override
Frees all the datas contained in the Solver environment.
Definition SolverXpress.cpp:142
virtual int get_col_index(const std::string &name) override
Returns the index of column named "name".
Definition SolverXpress.cpp:355