14#include "antares-xpansion/xpansion_interfaces/LogUtils.h"
23 : log_file_path(log_file) {
31 log_file_path = other.log_file_path;
38 if (log_file_path.empty() ||
39 (log_file_ptr = fopen(log_file_path.string().c_str(),
"a+")) ==
nullptr)
41 if (log_file_path.empty() ||
42 (log_file_ptr = _fsopen(log_file_path.string().c_str(),
"a+",
43 _SH_DENYNO)) ==
nullptr)
46 std::cout <<
"Invalid log file name passed as parameter: "
47 << std::quoted(log_file_path.string()) << std::endl;
49 setvbuf(log_file_ptr,
nullptr, _IONBF, 0);
55 log_file_ptr =
nullptr;
59 FILE *log_file_ptr =
nullptr;
60 std::filesystem::path log_file_path =
"";
64 :
public LogUtils::XpansionError<std::runtime_error> {
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)",
75 :
public LogUtils::XpansionError<std::runtime_error> {
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) +
87 :
public LogUtils::XpansionError<std::runtime_error> {
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) +
99 :
public LogUtils::XpansionError<std::runtime_error> {
102 : LogUtils::XpansionError<std::runtime_error>(
103 std::string(
"Invalid bound type ") + qbtype +
" for solver.",
108 :
public LogUtils::XpansionError<std::runtime_error> {
111 : LogUtils::XpansionError<std::runtime_error>(
112 std::string(
"Invalid col type ") + qctype +
" for solver.",
117 :
public LogUtils::XpansionError<std::runtime_error> {
120 const std::string &log_location)
121 : LogUtils::XpansionError<std::runtime_error>(
122 std::string(
"Invalid option '") + option +
"' for solver.",
127 :
public LogUtils::XpansionError<std::runtime_error> {
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,
138 :
public LogUtils::XpansionError<std::runtime_error> {
141 const std::string &log_location)
142 : LogUtils::XpansionError<std::runtime_error>(
143 "Solver '" + solver_name +
"' not supported", log_location) {}
148 : std::runtime_error(message) {}
154 : std::runtime_error(message) {}
172 std::vector<std::string> SOLVER_STRING_STATUS = {
173 "OPTIMAL",
"INFEASIBLE",
"UNBOUNDED",
"INForUNBOUND",
"UNKNOWN"};
181 typedef std::shared_ptr<SolverAbstract>
Ptr;
182 std::list<std::ostream *>
230 std::filesystem::path _log_file =
"";
237 void set_fp(FILE *fp) { _fp = fp; }
247 const std::string &log_location)
const {
294 virtual void save_prob(
const std::filesystem::path &filename) = 0;
302 virtual void write_basis(
const std::filesystem::path &filename) = 0;
333 virtual void read_basis(
const std::filesystem::path &filename) = 0;
372 virtual void get_obj(
double *obj,
int first,
int last)
const = 0;
383 virtual void set_obj(
const double *obj,
int first,
int last) = 0;
400 virtual void get_rows(
int *mstart,
int *mclind,
double *dmatval,
int size,
401 int *nels,
int first,
int last)
const = 0;
411 virtual void get_row_type(
char *qrtype,
int first,
int last)
const = 0;
421 virtual void get_rhs(
double *rhs,
int first,
int last)
const = 0;
443 virtual void get_col_type(
char *coltype,
int first,
int last)
const = 0;
453 virtual void get_lb(
double *lb,
int fisrt,
int last)
const = 0;
463 virtual void get_ub(
double *ub,
int fisrt,
int last)
const = 0;
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;
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;
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;
590 virtual void chg_obj(
const std::vector<int> &mindex,
591 const std::vector<double> &obj) = 0;
611 const std::vector<char> &qbtype,
612 const std::vector<double> &bnd) = 0;
622 const std::vector<char> &qctype) = 0;
630 virtual void chg_rhs(
int id_row,
double val) = 0;
639 virtual void chg_coef(
int id_row,
int id_col,
double val) = 0;
692 virtual void get_basis(
int *rstatus,
int *cstatus)
const = 0;
727 double *reduced_costs) = 0;
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