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
10{
12 std::shared_ptr<ILoggerXpansion> logger_;
13
14public:
15 explicit XpressManager(std::shared_ptr<ILoggerXpansion> logger);
17};
18
24{
25public:
26 XPRSprob _xprs = nullptr;
27 const std::string name_ = "XPRESS";
28
29 /*************************************************************************************************
30 ----------------------------------- Constructor/Desctructor
31 --------------------------------
32 *************************************************************************************************/
33
34public:
39 explicit SolverXpress(const SolverLogManager& log_manager);
40
41 [[nodiscard]] SolverXpress* clone() const override;
42 SolverXpress(const SolverXpress& other);
43
44 SolverXpress& operator=(const SolverXpress& other) = delete;
45
46 ~SolverXpress() override;
47 int get_number_of_instances() override;
48
49 std::string get_solver_name() const override
50 {
51 return name_;
52 }
53
54 /*************************************************************************************************
55 --------------------------------- Output and stream management
56 -----------------------------
57 *************************************************************************************************/
58
59 /*************************************************************************************************
60 ------ Destruction or creation of inner strctures and datas, closing
61 environments ----------
62 *************************************************************************************************/
63
64public:
65 void init() override;
66 void free() override;
67
68 /*************************************************************************************************
69 ------------------------------- Reading & Writing problems
70 -------------------------------
71 *************************************************************************************************/
72
73public:
74 void write_prob_mps(const std::filesystem::path& filename) override;
75 void write_prob_lp(const std::filesystem::path& filename) override;
76 void save_prob(const std::filesystem::path& filename) override;
77 void write_basis(const std::filesystem::path& filename) override;
78
79 void read_prob_mps(const std::filesystem::path& filename) override;
80 void read_prob_lp(const std::filesystem::path& filename) override;
81 void restore_prob(const std::filesystem::path& filename) override;
82 void read_basis(const std::filesystem::path& filename) override;
83 void set_basis(std::span<int> rstatus, std::span<int> cstatus) override;
84
88 XPRSprob clone_matrix_to_new_prob() const;
89
90private:
91 void read_prob(const char* prob_name, const char* flags);
92 mutable std::mutex mutex_;
93 /*************************************************************************************************
94 ----------------------- Get general informations about problem
95 ----------------------------
96 *************************************************************************************************/
97
98public:
99 int get_ncols() const override;
100 int get_nrows() const override;
101 int get_nelems() const override;
102 int get_n_integer_vars() const override;
103 void get_obj(double* obj, int first, int last) const override;
104 void set_obj_to_zero() override;
105 void set_obj(const double* obj, int first, int last) override;
106 void get_rows(int* mstart,
107 int* mclind,
108 double* dmatval,
109 int size,
110 int* nels,
111 int first,
112 int last) const override;
113 void get_row_type(char* qrtype, int first, int last) const override;
114 void get_rhs(double* rhs, int first, int last) const override;
115 void get_rhs_range(double* range, int first, int last) const override;
116 void get_cols(int* mstart,
117 int* mrwind,
118 double* dmatval,
119 int size,
120 int* nels,
121 int first,
122 int last) const override;
123 void get_col_type(char* coltype, int first, int last) const override;
124 void get_lb(double* lb, int fisrt, int last) const override;
125 void get_ub(double* ub, int fisrt, int last) const override;
126
127 int get_row_index(const std::string& name) override;
128 int get_col_index(const std::string& name) override;
129 std::vector<std::string> get_row_names(int first, int last) const override;
130 std::vector<std::string> get_row_names() override;
131 std::vector<std::string> get_col_names(int first, int last) const override;
132 std::vector<std::string> get_col_names() override;
133
134 /*************************************************************************************************
135 ------------------------------ Methods to modify problem
136 ----------------------------------
137 *************************************************************************************************/
138
139public:
140 void del_rows(int first, int last) override;
141 void del_cols(int first, int last) override;
142 void add_rows(int newrows,
143 int newnz,
144 const char* qrtype,
145 const double* rhs,
146 const double* range,
147 const int* mstart,
148 const int* mclind,
149 const double* dmatval,
150 const std::vector<std::string>& row_names) override;
151 void add_cols(int newcol,
152 int newnz,
153 const double* objx,
154 const int* mstart,
155 const int* mrwind,
156 const double* dmatval,
157 const double* bdl,
158 const double* bdu,
159 const std::vector<std::string>& col_names) override;
160 void add_name(int type, const char* cnames, int indice) override;
161 void add_names(int type, const std::vector<std::string>& cnames, int first, int end) override;
162 void chg_obj(const std::vector<int>& mindex, const std::vector<double>& obj) override;
163 void chg_obj_direction(bool minimize) override;
164 void chg_bounds(const std::vector<int>& mindex,
165 const std::vector<char>& qbtype,
166 const std::vector<double>& bnd) override;
167 void chg_col_type(const std::vector<int>& mindex, const std::vector<char>& qctype) override;
168 void chg_rhs(int id_row, double val) override;
169 void chg_coef(int id_row, int id_col, double val) override;
170 void chg_row_name(int id_row, const std::string& name) override;
171 void chg_col_name(int id_col, const std::string& name) override;
172
173 void mark_indices_to_keep_presolve(int nrows, int ncols, int* rowind, int* colind) override;
174
175 /*************************************************************************************************
176 ----------------------------- Methods to solve the problem
177 ---------------------------------
178 *************************************************************************************************/
179
180public:
181 void presolve_only() override;
182
183 int solve_lp() override;
184 int solve_mip() override;
185
186 /*************************************************************************************************
187 ------------------------- Methods to get solutions information
188 -----------------------------
189 *************************************************************************************************/
190
191public:
208 void get_basis(int* rstatus, int* cstatus) const override;
209 double get_mip_value() const override;
210 double get_lp_value() const override;
211 int get_splex_num_of_ite_last() const override;
212 void get_lp_sol(double* primals, double* duals, double* reduced_costs) const override;
213 void get_mip_sol(double* primals) override;
214
215 void get_presolve_map(int* rowmap, int* colmap) const override;
216
217 /*************************************************************************************************
218 ------------------------ Methods to set algorithm or logs levels
219 ---------------------------
220 *************************************************************************************************/
221
222public:
223 void set_output_log_level(int loglevel) final;
224 void set_algorithm(const std::string& algo) override;
225 void set_threads(int n_threads) override;
226 void set_optimality_gap(double gap) override;
227 void set_simplex_iter(int iter) override;
228
229public:
230 std::ofstream _log_stream;
231
232private:
233 std::vector<std::string> get_names(int type, int n_elements) const;
234 std::vector<std::string> get_names(int type, int first, int last) const;
235};
236
237/************************************************************************************\
238* Name: optimizermsg *
239* Purpose: Display Optimizer error messages and warnings. *
240* Arguments: const char *sMsg Message string *
241* int nLen Message length *
242* int nMsgLvl Message type *
243* Return Value: None. *
244\************************************************************************************/
245void errormsg(XPRSprob& xprs, const char* sSubName, int nLineNo, int nErrCode);
246
247/************************************************************************************\
248* Name: errormsg *
249* Purpose: Display error information about failed subroutines. *
250* Arguments: const char *sSubName Subroutine name *
251* int nLineNo Line number *
252* int nErrCode Error code *
253* Return Value: None *
254\************************************************************************************/
255void optimizermsg(XPRSprob prob, void* stream, const char* sMsg, int nLen, int nMsglvl);
This class is the entry point to load xpress in runtime.
Definition environment.h:34
Definition SolverAbstract.h:203
Definition SolverAbstract.h:20
Definition SolverXpress.h:24
void chg_coef(int id_row, int id_col, double val) override
Change a coefficient in the matrix.
Definition SolverXpress.cpp:583
void write_prob_mps(const std::filesystem::path &filename) override
writes an optimization problem in a MPS file
Definition SolverXpress.cpp:171
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 SolverXpress.cpp:601
int solve_lp() override
Solves a problem as LP.
Definition SolverXpress.cpp:624
void del_cols(int first, int last) override
Deletes col at index first and last.
Definition SolverXpress.cpp:454
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:352
int get_row_index(const std::string &name) override
Returns the index of row named "name".
Definition SolverXpress.cpp:370
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:187
std::vector< std::string > get_col_names() override
Returns the names of columns.
Definition SolverXpress.cpp:433
int get_n_integer_vars() const override
returns number of integer variables in the problem
Definition SolverXpress.cpp:280
XPRSprob clone_matrix_to_new_prob() const
Clone the matrix and all XPRESS data from this SolverXpress instance.
Definition SolverXpress.cpp:921
void init() override
Initializes a problem.
Definition SolverXpress.cpp:127
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:288
void presolve_only() override
Presolve the problem (to be implemented by derived classes)
Definition SolverXpress.cpp:614
void add_name(int type, const char *cnames, int indice) override
Adds a name to a row or a column.
Definition SolverXpress.cpp:505
void write_prob_lp(const std::filesystem::path &filename) override
writes an optimization problem in a LP file
Definition SolverXpress.cpp:178
std::string get_solver_name() const override
Returns the solver used.
Definition SolverXpress.h:49
SolverXpress()
Default constructor of a XPRESS solver.
Definition SolverXpress.cpp:81
int get_nelems() const override
returns number of non zeros elements in the matrix, excluding objective
Definition SolverXpress.cpp:271
void set_optimality_gap(double gap) override
Sets the optimality gap.
Definition SolverXpress.cpp:793
void save_prob(const std::filesystem::path &filename) override
write an optimisation problem in a file
Definition SolverXpress.cpp:808
std::vector< std::string > get_row_names() override
Returns the names of rows.
Definition SolverXpress.cpp:423
void get_basis(int *rstatus, int *cstatus) const override
Returns the current basis into the user's data arrays.
Definition SolverXpress.cpp:693
void set_algorithm(const std::string &algo) override
Sets algorithm used by solver to solve LP's.
Definition SolverXpress.cpp:762
int get_ncols() const override
returns number of columns of the problem
Definition SolverXpress.cpp:255
void restore_prob(const std::filesystem::path &filename) override
read an optimisation problem from a file
Definition SolverXpress.cpp:822
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:485
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 SolverXpress.cpp:340
void set_output_log_level(int loglevel) final
Sets log level of the solver.
Definition SolverXpress.cpp:745
void del_rows(int first, int last) override
Deletes rows between index first and last.
Definition SolverXpress.cpp:443
int get_nrows() const override
returns number of rows of the problem
Definition SolverXpress.cpp:263
void get_presolve_map(int *rowmap, int *colmap) const override
Get the presolve map (to be implemented by derived classes)
Definition SolverXpress.cpp:735
XPRSprob _xprs
Definition SolverXpress.h:26
double get_mip_value() const override
Get the optimal value of a MIP problem (available after method "solve_mip")
Definition SolverXpress.cpp:699
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:334
int get_number_of_instances() override
Returns number of instances of solver currently in memory.
Definition SolverXpress.cpp:113
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:465
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:723
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:239
void read_prob_lp(const std::filesystem::path &filename) override
reads an optimization problem contained in a MPS file
Definition SolverXpress.cpp:199
void chg_obj(const std::vector< int > &mindex, const std::vector< double > &obj) override
Change coefficients in objective function.
Definition SolverXpress.cpp:536
void chg_row_name(int id_row, const std::string &name) override
Change the name of a constraint.
Definition SolverXpress.cpp:589
int solve_mip() override
Solves a problem as MIP.
Definition SolverXpress.cpp:656
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:322
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:328
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:715
void chg_obj_direction(bool minimize) override
Change the problem's objective function sense to minimize or maximize.
Definition SolverXpress.cpp:543
void get_lb(double *lb, int fisrt, int last) const override
Returns the lower bounds for variables in a given range.
Definition SolverXpress.cpp:358
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:310
void set_obj_to_zero() override
Set the objective function coefficients to zero.
Definition SolverXpress.cpp:294
void read_prob_mps(const std::filesystem::path &filename) override
reads an optimization problem contained in a MPS file
Definition SolverXpress.cpp:193
void chg_col_type(const std::vector< int > &mindex, const std::vector< char > &qctype) override
Change type of some columns.
Definition SolverXpress.cpp:564
void get_ub(double *ub, int fisrt, int last) const override
Returns the upper bounds for variables in a given range.
Definition SolverXpress.cpp:364
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:301
void set_simplex_iter(int iter) override
Sets the maximum number of simplex iterations the solver can perform.
Definition SolverXpress.cpp:799
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:550
void chg_rhs(int id_row, double val) override
Change rhs of a row.
Definition SolverXpress.cpp:574
double get_lp_value() const override
Get the optimal value of a LP problem (available after method "solve_lp" )
Definition SolverXpress.cpp:707
void chg_col_name(int id_col, const std::string &name) override
Change the name of a variable.
Definition SolverXpress.cpp:595
void get_mip_sol(double *primals) override
Get MIP solution of a problem (available after method "solve_mip")
Definition SolverXpress.cpp:729
void set_threads(int n_threads) override
Sets the maximum number of threads used to perform optimization.
Definition SolverXpress.cpp:787
void free() override
Frees all the datas contained in the Solver environment.
Definition SolverXpress.cpp:150
int get_col_index(const std::string &name) override
Returns the index of column named "name".
Definition SolverXpress.cpp:378
Definition SolverXpress.h:10