Antares Xpansion
Investment simulations for Antares studies
Loading...
Searching...
No Matches
SolverAbstract.h
1#pragma once
2
3#include <cstdio>
4#include <filesystem>
5#include <iomanip>
6#include <iostream>
7#include <list>
8#include <memory>
9#include <sstream>
10#include <stdexcept>
11#include <string>
12#include <vector>
13
14#include "antares-xpansion/xpansion_interfaces/LogUtils.h"
15
17 public:
18 explicit SolverLogManager() = default;
19 explicit SolverLogManager(const SolverLogManager &other)
20 : SolverLogManager(other.log_file_path) {}
21
22 explicit SolverLogManager(const std::filesystem::path &log_file)
23 : log_file_path(log_file) {
24 init();
25 }
26
27 SolverLogManager &operator=(const SolverLogManager &other) {
28 if (this == &other) {
29 return *this;
30 }
31 log_file_path = other.log_file_path;
32 init();
33 return *this;
34 }
35
36 void init() {
37#ifdef __linux__
38 if (log_file_path.empty() ||
39 (log_file_ptr = fopen(log_file_path.string().c_str(), "a+")) == nullptr)
40#elif _WIN32
41 if (log_file_path.empty() ||
42 (log_file_ptr = _fsopen(log_file_path.string().c_str(), "a+",
43 _SH_DENYNO)) == nullptr)
44#endif
45 {
46 std::cout << "Invalid log file name passed as parameter: "
47 << std::quoted(log_file_path.string()) << std::endl;
48 } else {
49 setvbuf(log_file_ptr, nullptr, _IONBF, 0);
50 }
51 }
53 if (log_file_ptr) {
54 fclose(log_file_ptr);
55 log_file_ptr = nullptr;
56 }
57 }
58
59 FILE *log_file_ptr = nullptr;
60 std::filesystem::path log_file_path = "";
61};
62
64 : public LogUtils::XpansionError<std::runtime_error> {
65 public:
66 InvalidStatusException(int status, const std::string &action,
67 const std::string &log_message)
68 : LogUtils::XpansionError<std::runtime_error>(
69 "Failed to " + action + ": invalid status " +
70 std::to_string(status) + " (0 expected)",
71 log_message) {}
72};
73
75 : public LogUtils::XpansionError<std::runtime_error> {
76 public:
77 InvalidRowSizeException(int expected_size, int actual_size,
78 const std::string &log_location)
79 : LogUtils::XpansionError<std::runtime_error>(
80 "Invalid row size for solver. " + std::to_string(actual_size) +
81 " rows available (" + std::to_string(expected_size) +
82 " expected)",
83 log_location) {}
84};
85
87 : public LogUtils::XpansionError<std::runtime_error> {
88 public:
89 InvalidColSizeException(int expected_size, int actual_size,
90 const std::string &log_location)
91 : LogUtils::XpansionError<std::runtime_error>(
92 "Invalid col size for solver. " + std::to_string(actual_size) +
93 " cols available (" + std::to_string(expected_size) +
94 " expected)",
95 log_location) {}
96};
97
99 : public LogUtils::XpansionError<std::runtime_error> {
100 public:
101 InvalidBoundTypeException(char qbtype, const std::string &log_location)
102 : LogUtils::XpansionError<std::runtime_error>(
103 std::string("Invalid bound type ") + qbtype + " for solver.",
104 log_location) {}
105};
106
108 : public LogUtils::XpansionError<std::runtime_error> {
109 public:
110 InvalidColTypeException(char qctype, const std::string &log_location)
111 : LogUtils::XpansionError<std::runtime_error>(
112 std::string("Invalid col type ") + qctype + " for solver.",
113 log_location) {}
114};
115
117 : public LogUtils::XpansionError<std::runtime_error> {
118 public:
119 InvalidSolverOptionException(const std::string &option,
120 const std::string &log_location)
121 : LogUtils::XpansionError<std::runtime_error>(
122 std::string("Invalid option '") + option + "' for solver.",
123 log_location) {}
124};
125
127 : public LogUtils::XpansionError<std::runtime_error> {
128 public:
129 InvalidSolverForCopyException(const std::string &from_solver,
130 const std::string &to_solver,
131 const std::string &log_location)
132 : LogUtils::XpansionError<std::runtime_error>(
133 "Can't copy " + from_solver + "solver from " + to_solver,
134 log_location) {}
135};
136
138 : public LogUtils::XpansionError<std::runtime_error> {
139 public:
140 InvalidSolverNameException(const std::string &solver_name,
141 const std::string &log_location)
142 : LogUtils::XpansionError<std::runtime_error>(
143 "Solver '" + solver_name + "' not supported", log_location) {}
144};
145class GenericSolverException : public std::runtime_error {
146 public:
147 GenericSolverException(const std::string &message)
148 : std::runtime_error(message) {}
149};
150
151class NotImplementedFeatureSolverException : public std::runtime_error {
152 public:
153 NotImplementedFeatureSolverException(const std::string &message)
154 : std::runtime_error(message) {}
155};
156
157// Definition of optimality codes
158enum SOLVER_STATUS {
159 OPTIMAL,
160 INFEASIBLE,
161 UNBOUNDED,
162 INForUNBOUND,
163 UNKNOWN,
164};
165
171 public:
172 std::vector<std::string> SOLVER_STRING_STATUS = {
173 "OPTIMAL", "INFEASIBLE", "UNBOUNDED", "INForUNBOUND", "UNKNOWN"};
174
175 /*************************************************************************************************
176 ---------------------------------------- ATTRIBUTES
177 ---------------------------------------
178 *************************************************************************************************/
179 public:
180 std::string _name;
181 typedef std::shared_ptr<SolverAbstract> Ptr;
182 std::list<std::ostream *>
185 /*************************************************************************************************
186 ----------------------------------- Constructor/Desctructor
187 --------------------------------
188 *************************************************************************************************/
189 public:
194
203 SolverAbstract(const std::string &name, const SolverAbstract::Ptr toCopy){};
204
208 virtual ~SolverAbstract(){};
209
213 virtual int get_number_of_instances() = 0;
214
218 virtual std::string get_solver_name() const = 0;
219
220 /*************************************************************************************************
221 ----------------------------------- Output stream management
222 ------------------------------
223 *************************************************************************************************/
224 public:
228 std::list<std::ostream *> &get_stream() { return _streams; };
229 FILE *_fp = nullptr;
230 std::filesystem::path _log_file = "";
236 void add_stream(std::ostream &stream) { get_stream().push_back(&stream); };
237 void set_fp(FILE *fp) { _fp = fp; }
246 void zero_status_check(int status, const std::string &action_failed,
247 const std::string &log_location) const {
248 if (status != 0) {
249 throw InvalidStatusException(status, action_failed, log_location);
250 }
251 };
252
253 /*************************************************************************************************
254 ------ Destruction or creation of inner strctures and datas, closing
255 environments ----------
256 *************************************************************************************************/
257 public:
261 virtual void init() = 0;
262
266 virtual void free() = 0;
267
268 /*************************************************************************************************
269 ------------------------------- Reading & Writing problems
270 -------------------------------
271 *************************************************************************************************/
272 public:
278 virtual void write_prob_mps(const std::filesystem::path &filename) = 0;
279
285 virtual void write_prob_lp(const std::filesystem::path &filename) = 0;
286
294 virtual void save_prob(const std::filesystem::path &filename) = 0;
295
302 virtual void write_basis(const std::filesystem::path &filename) = 0;
303
309 virtual void read_prob_mps(const std::filesystem::path &filename) = 0;
310
316 virtual void read_prob_lp(const std::filesystem::path &filename) = 0;
317
325 virtual void restore_prob(const std::filesystem::path &filename) = 0;
326
333 virtual void read_basis(const std::filesystem::path &filename) = 0;
334
340 virtual void copy_prob(Ptr fictif_solv) = 0;
341
342 /*************************************************************************************************
343 ----------------------- Get general informations about problem
344 ----------------------------
345 *************************************************************************************************/
346 public:
350 virtual int get_ncols() const = 0;
351
355 virtual int get_nrows() const = 0;
356
361 virtual int get_nelems() const = 0;
362
366 virtual int get_n_integer_vars() const = 0;
367
372 virtual void get_obj(double *obj, int first, int last) const = 0;
373
377 virtual void set_obj_to_zero() = 0;
378
383 virtual void set_obj(const double *obj, int first, int last) = 0;
384
400 virtual void get_rows(int *mstart, int *mclind, double *dmatval, int size,
401 int *nels, int first, int last) const = 0;
402
411 virtual void get_row_type(char *qrtype, int first, int last) const = 0;
412
421 virtual void get_rhs(double *rhs, int first, int last) const = 0;
422
432 virtual void get_rhs_range(double *range, int first, int last) const = 0;
433
443 virtual void get_col_type(char *coltype, int first, int last) const = 0;
444
453 virtual void get_lb(double *lb, int fisrt, int last) const = 0;
454
463 virtual void get_ub(double *ub, int fisrt, int last) const = 0;
464
470 virtual int get_row_index(std::string const &name) = 0;
471
477 virtual int get_col_index(std::string const &name) = 0;
478
487 virtual std::vector<std::string> get_row_names(int first, int last) = 0;
488
493 virtual std::vector<std::string> get_row_names() = 0;
494
503 virtual std::vector<std::string> get_col_names(int first, int last) = 0;
504
509 virtual std::vector<std::string> get_col_names() = 0;
510
511 /*************************************************************************************************
512 ------------------------------ Methods to modify problem
513 ----------------------------------
514 *************************************************************************************************/
515 public:
522 virtual void del_rows(int first, int last) = 0;
523
543 virtual void add_rows(int newrows, int newnz, const char *qrtype,
544 const double *rhs, const double *range,
545 const int *mstart, const int *mclind,
546 const double *dmatval,
547 const std::vector<std::string> &names = {}) = 0;
548
567 virtual void add_cols(int newcol, int newnz, const double *objx,
568 const int *mstart, const int *mrwind,
569 const double *dmatval, const double *bdl,
570 const double *bdu) = 0;
571
580 virtual void add_name(int type, const char *cnames, int indice) = 0;
581 virtual void add_names(int type, const std::vector<std::string> &cnames,
582 int first, int end) = 0;
583
590 virtual void chg_obj(const std::vector<int> &mindex,
591 const std::vector<double> &obj) = 0;
592
599 virtual void chg_obj_direction(const bool minimize) = 0;
600
610 virtual void chg_bounds(const std::vector<int> &mindex,
611 const std::vector<char> &qbtype,
612 const std::vector<double> &bnd) = 0;
613
621 virtual void chg_col_type(const std::vector<int> &mindex,
622 const std::vector<char> &qctype) = 0;
623
630 virtual void chg_rhs(int id_row, double val) = 0;
631
639 virtual void chg_coef(int id_row, int id_col, double val) = 0;
640
647 virtual void chg_row_name(int id_row, std::string const &name) = 0;
648
655 virtual void chg_col_name(int id_col, std::string const &name) = 0;
656
657 /*************************************************************************************************
658 ----------------------------- Methods to solve the problem
659 ---------------------------------
660 *************************************************************************************************/
661 public:
667 virtual int solve_lp() = 0;
668
674 virtual int solve_mip() = 0;
675
676 /*************************************************************************************************
677 ------------------------- Methods to get solutions information
678 -----------------------------
679 *************************************************************************************************/
680 public:
692 virtual void get_basis(int *rstatus, int *cstatus) const = 0;
693
700 virtual double get_mip_value() const = 0;
701
708 virtual double get_lp_value() const = 0;
709
717 virtual int get_splex_num_of_ite_last() const = 0;
718
726 virtual void get_lp_sol(double *primals, double *duals,
727 double *reduced_costs) = 0;
728
734 virtual void get_mip_sol(double *primals) = 0;
735
736 /*************************************************************************************************
737 ------------------------ Methods to set algorithm or logs levels
738 ---------------------------
739 *************************************************************************************************/
740 public:
746 virtual void set_output_log_level(int loglevel) = 0;
747
753 virtual void set_algorithm(std::string const &algo) = 0;
754
760 virtual void set_threads(int n_threads) = 0;
761
767 virtual void set_optimality_gap(double gap) = 0;
768
774 virtual void set_simplex_iter(int iter) = 0;
775};
Definition SolverAbstract.h:145
Definition SolverAbstract.h:99
Definition SolverAbstract.h:87
Definition SolverAbstract.h:108
Definition SolverAbstract.h:75
Definition SolverAbstract.h:127
Definition SolverAbstract.h:138
Definition SolverAbstract.h:117
Definition SolverAbstract.h:64
Definition SolverAbstract.h:151
Definition SolverAbstract.h:170
std::list< std::ostream * > & get_stream()
returns the list of streams used by the solver instance
Definition SolverAbstract.h:228
virtual void chg_col_type(const std::vector< int > &mindex, const std::vector< char > &qctype)=0
Change type of some columns.
virtual int get_row_index(std::string const &name)=0
Returns the index of row named "name".
virtual void read_prob_lp(const std::filesystem::path &filename)=0
reads an optimization problem contained in a MPS file
virtual void get_ub(double *ub, int fisrt, int last) const =0
Returns the upper bounds for variables in a given range.
virtual std::vector< std::string > get_col_names(int first, int last)=0
Returns the names of columns from index first to last cannot be declared as const because of some sol...
virtual int get_nrows() const =0
returns number of rows of the problem
virtual int get_col_index(std::string const &name)=0
Returns the index of column named "name".
virtual void get_rhs_range(double *range, int first, int last) const =0
Returns the right hand side range values for the rows in a given range.
std::list< std::ostream * > _streams
Definition SolverAbstract.h:183
virtual int get_nelems() const =0
returns number of non zeros elements in the matrix, excluding objective
virtual int solve_lp()=0
Solves a problem as LP.
virtual void free()=0
Frees all the datas contained in the Solver environment.
virtual void set_output_log_level(int loglevel)=0
Sets log level of the solver.
virtual int get_n_integer_vars() const =0
returns number of integer variables in the problem
virtual void del_rows(int first, int last)=0
Deletes rows between index first and last.
virtual void init()=0
Initializes a problem.
SolverAbstract(const std::string &name, const SolverAbstract::Ptr toCopy)
Copy constructor, copy the problem "toCopy" in memory and name it "name" if possible.
Definition SolverAbstract.h:203
virtual void chg_col_name(int id_col, std::string const &name)=0
Change the name of a variable.
virtual std::vector< std::string > get_row_names(int first, int last)=0
Returns the names of row from index first to last cannot be declared as const because of some solver ...
virtual void get_obj(double *obj, int first, int last) const =0
returns the objective function coefficients for the columns in a given range
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)=0
Adds new columns to the problem.
void add_stream(std::ostream &stream)
add a stream to the list of streams used by the solver instance
Definition SolverAbstract.h:236
virtual void chg_obj_direction(const bool minimize)=0
Change the problem's objective function sense to minimize or maximize.
virtual double get_lp_value() const =0
Get the optimal value of a LP problem (available after method "solve_lp" )
std::string _name
Definition SolverAbstract.h:180
virtual int get_number_of_instances()=0
Returns number of instances of solver currently in memory.
virtual void set_algorithm(std::string const &algo)=0
Sets algorithm used by solver to solve LP's.
virtual std::vector< std::string > get_col_names()=0
Returns the names of columns.
virtual void write_prob_mps(const std::filesystem::path &filename)=0
writes an optimization problem in a MPS file
virtual void chg_bounds(const std::vector< int > &mindex, const std::vector< char > &qbtype, const std::vector< double > &bnd)=0
Change bounds of some variables.
virtual void write_basis(const std::filesystem::path &filename)=0
Writes the current basis to a file for later input into the optimizer.
virtual void set_obj(const double *obj, int first, int last)=0
Set the objective function coefficients for the columns in a given range.
virtual void restore_prob(const std::filesystem::path &filename)=0
read an optimisation problem from a file
SolverAbstract()
constructor of SolverAbstract class : does nothing
Definition SolverAbstract.h:193
virtual ~SolverAbstract()
destructor of SolverAbstract class : does nothing
Definition SolverAbstract.h:208
virtual void set_optimality_gap(double gap)=0
Sets the optimality gap.
virtual void chg_obj(const std::vector< int > &mindex, const std::vector< double > &obj)=0
Change coefficients in objective function.
virtual void copy_prob(Ptr fictif_solv)=0
copy an existing problem
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={})=0
Adds rows to the problem.
virtual void get_lb(double *lb, int fisrt, int last) const =0
Returns the lower bounds for variables in a given range.
virtual std::vector< std::string > get_row_names()=0
Returns the names of rows.
virtual void get_rows(int *mstart, int *mclind, double *dmatval, int size, int *nels, int first, int last) const =0
get coefficients of rows from index first to last
virtual int get_ncols() const =0
returns number of columns of the problem
virtual void set_simplex_iter(int iter)=0
Sets the maximum number of simplex iterations the solver can perform.
virtual double get_mip_value() const =0
Get the optimal value of a MIP problem (available after method "solve_mip")
virtual void get_basis(int *rstatus, int *cstatus) const =0
Returns the current basis into the user’s data arrays.
virtual void get_col_type(char *coltype, int first, int last) const =0
Returns the column types for the columns in a given range.
virtual void set_threads(int n_threads)=0
Sets the maximum number of threads used to perform optimization.
std::shared_ptr< SolverAbstract > Ptr
Definition SolverAbstract.h:181
virtual void read_prob_mps(const std::filesystem::path &filename)=0
reads an optimization problem contained in a MPS file
virtual int solve_mip()=0
Solves a problem as MIP.
virtual void read_basis(const std::filesystem::path &filename)=0
Instructs the optimizer to read in a previously saved basis from a file.
virtual void get_rhs(double *rhs, int first, int last) const =0
Returns the right-hand sides of the rows in a given range.
virtual void chg_rhs(int id_row, double val)=0
Change rhs of a row.
virtual void save_prob(const std::filesystem::path &filename)=0
write an optimisation problem in a file
virtual void chg_coef(int id_row, int id_col, double val)=0
Change a coefficient in the matrix.
virtual void set_obj_to_zero()=0
Set the objective function coefficients to zero.
virtual void get_mip_sol(double *primals)=0
Get MIP solution of a problem (available after method "solve_mip")
virtual void chg_row_name(int id_row, std::string const &name)=0
Change the name of a constraint.
virtual std::string get_solver_name() const =0
Returns the solver used.
virtual int get_splex_num_of_ite_last() const =0
Get the number of simplex iterations done in the last resolution of the problem.
virtual void get_row_type(char *qrtype, int first, int last) const =0
Returns the row types for the rows in a given range.
virtual void get_lp_sol(double *primals, double *duals, double *reduced_costs)=0
Get LP solution of a problem (available after method "solve_lp")
virtual void add_name(int type, const char *cnames, int indice)=0
Adds a name to a row or a column.
virtual void write_prob_lp(const std::filesystem::path &filename)=0
writes an optimization problem in a LP file
void zero_status_check(int status, const std::string &action_failed, const std::string &log_location) const
Check if a status code is different to 0, throw InvalidStatusException if it occurs.
Definition SolverAbstract.h:246
Definition SolverAbstract.h:16