10#include <shared_mutex>
17#include "antares-xpansion/xpansion_interfaces/LogUtils.h"
30 log_file_path(log_file)
41 log_file_path = other.log_file_path;
49 if (log_file_path.empty()
50 || (log_file_ptr = fopen(log_file_path.string().c_str(),
"a+")) ==
nullptr)
52 if (log_file_path.empty()
53 || (log_file_ptr = _fsopen(log_file_path.string().c_str(),
"a+", _SH_DENYNO))
57 std::cout <<
"Invalid log file name passed as parameter: "
58 << std::quoted(log_file_path.string()) << std::endl;
62 setvbuf(log_file_ptr,
nullptr, _IONBF, 0);
71 log_file_ptr =
nullptr;
75 FILE* log_file_ptr =
nullptr;
76 std::filesystem::path log_file_path =
"";
83 LogUtils::XpansionError<std::runtime_error>(
"Failed to " + action +
": invalid status "
84 + std::to_string(status) +
" (0 expected)",
94 LogUtils::XpansionError<std::runtime_error>(
95 "Invalid row size for solver. " + std::to_string(actual_size) +
" rows available ("
96 + std::to_string(expected_size) +
" expected)",
106 LogUtils::XpansionError<std::runtime_error>(
107 "Invalid col size for solver. " + std::to_string(actual_size) +
" cols available ("
108 + std::to_string(expected_size) +
" expected)",
118 LogUtils::XpansionError<std::runtime_error>(std::string(
"Invalid bound type ") + qbtype
129 LogUtils::XpansionError<std::runtime_error>(std::string(
"Invalid col type ") + qctype
140 LogUtils::XpansionError<std::runtime_error>(std::string(
"Invalid option '") + option
151 const std::string& to_solver,
152 const std::string& log_location):
153 LogUtils::XpansionError<std::runtime_error>(
"Can't copy " + from_solver +
"solver from "
164 LogUtils::XpansionError<std::runtime_error>(
"Solver '" + solver_name +
"' not supported",
174 std::runtime_error(message)
183 std::runtime_error(message)
205 std::vector<std::string> SOLVER_STRING_STATUS = {
"OPTIMAL",
218 mutable std::list<std::ostream*>
254 std::filesystem::path _log_file =
"";
266 void set_fp(FILE* fp)
280 const std::string& action_failed,
281 const std::string& log_location)
332 virtual void save_prob(
const std::filesystem::path& filename) = 0;
340 virtual void write_basis(
const std::filesystem::path& filename) = 0;
371 virtual void read_basis(
const std::filesystem::path& filename) = 0;
373 virtual void set_basis(std::span<int> rstatus, std::span<int> cstatus) = 0;
406 virtual void get_obj(
double* obj,
int first,
int last)
const = 0;
417 virtual void set_obj(
const double* obj,
int first,
int last) = 0;
451 virtual void get_row_type(
char* qrtype,
int first,
int last)
const = 0;
461 virtual void get_rhs(
double* rhs,
int first,
int last)
const = 0;
507 virtual void get_col_type(
char* coltype,
int first,
int last)
const = 0;
517 virtual void get_lb(
double* lb,
int fisrt,
int last)
const = 0;
527 virtual void get_ub(
double* ub,
int fisrt,
int last)
const = 0;
551 virtual std::vector<std::string>
get_row_names(
int first,
int last)
const = 0;
567 virtual std::vector<std::string>
get_col_names(
int first,
int last)
const = 0;
625 const double* dmatval,
626 const std::vector<std::string>& row_names)
654 const double* dmatval,
657 const std::vector<std::string>& col_names)
668 virtual void add_name(
int type,
const char* cnames,
int indice) = 0;
670 virtual void add_names(
int type,
const std::vector<std::string>& cnames,
int first,
int end)
679 virtual void chg_obj(
const std::vector<int>& mindex,
const std::vector<double>& obj) = 0;
699 const std::vector<char>& qbtype,
700 const std::vector<double>& bnd)
710 virtual void chg_col_type(
const std::vector<int>& mindex,
const std::vector<char>& qctype) = 0;
718 virtual void chg_rhs(
int id_row,
double val) = 0;
727 virtual void chg_coef(
int id_row,
int id_col,
double val) = 0;
782 virtual void get_basis(
int* rstatus,
int* cstatus)
const = 0;
816 virtual void get_lp_sol(
double* primals,
double* duals,
double* reduced_costs)
const = 0;
Definition SolverAbstract.h:171
Definition SolverAbstract.h:115
Definition SolverAbstract.h:103
Definition SolverAbstract.h:126
Definition SolverAbstract.h:91
Definition SolverAbstract.h:148
Definition SolverAbstract.h:161
Definition SolverAbstract.h:137
Definition SolverAbstract.h:80
Definition SolverAbstract.h:180
Definition SolverAbstract.h:203
virtual int get_row_index(const std::string &name)=0
Returns the index of row named "name".
virtual void chg_col_type(const std::vector< int > &mindex, const std::vector< char > &qctype)=0
Change type of some columns.
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 int get_nrows() const =0
returns number of rows of the problem
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.
virtual int get_nelems() const =0
returns number of non zeros elements in the matrix, excluding objective
virtual void set_algorithm(const std::string &algo)=0
Sets algorithm used by solver to solve LP's.
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 std::vector< std::string > get_col_names(int first, int last) const =0
Returns the names of columns from index first to last cannot be declared as const because of some sol...
virtual ~SolverAbstract()=default
destructor of SolverAbstract class : does nothing
virtual void get_lp_sol(double *primals, double *duals, double *reduced_costs) const =0
Get LP solution of a problem (available after method "solve_lp")
virtual void init()=0
Initializes a problem.
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 int get_col_index(const std::string &name)=0
Returns the index of column named "name".
void add_stream(std::ostream &stream)
add a stream to the list of streams used by the solver instance
Definition SolverAbstract.h:261
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:217
std::list< std::ostream * > _streams
Definition SolverAbstract.h:219
virtual int get_number_of_instances()=0
Returns number of instances of solver currently in memory.
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 std::vector< std::string > get_row_names(int first, int last) const =0
Returns the names of row from index first to last cannot be declared as const because of some solver ...
virtual void restore_prob(const std::filesystem::path &filename)=0
read an optimisation problem from a file
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 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_presolve_map(int *rowmap, int *colmap) const =0
Get the presolve map (to be implemented by derived classes)
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 void mark_indices_to_keep_presolve(int nrows, int ncols, int *rowind, int *colind)=0
Mark indices to keep during presolve (to be implemented by derived classes)
virtual void get_cols(int *mstart, int *mrwind, double *dmatval, int size, int *nels, int first, int last) const =0
get coefficients of cols 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 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)=0
Adds new columns to the problem.
virtual void del_cols(int first, int last)=0
Deletes col at index first and last.
virtual void chg_col_name(int id_col, const std::string &name)=0
Change the name of a variable.
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.
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 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)=0
Adds rows to the problem.
virtual void presolve_only()=0
Presolve the problem (to be implemented by derived classes)
std::list< std::ostream * > & get_stream() const
returns the list of streams used by the solver instance
Definition SolverAbstract.cpp:132
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 chg_row_name(int id_row, const std::string &name)=0
Change the name of a constraint.
virtual void set_obj_to_zero()=0
Set the objective function coefficients to zero.
static void zero_status_check(int status, const std::string &action_failed, const std::string &log_location)
Check if a status code is different to 0, throw InvalidStatusException if it occurs.
Definition SolverAbstract.h:279
virtual void get_mip_sol(double *primals)=0
Get MIP solution of a problem (available after method "solve_mip")
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 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
Definition SolverAbstract.h:20