FMT 1.2.0
Forest management tools for forest planning
Loading...
Searching...
No Matches
FMTLpSolver.h
Go to the documentation of this file.
1/*
2Copyright (c) 2019 Gouvernement du Qu�bec
3
4SPDX-License-Identifier: LiLiQ-R-1.1
5License-Filename: LICENSES/EN/LiLiQ-R11unicode.txt
6*/
7
8#ifdef FMTWITHOSI
9#ifndef FMTsolve_Hm_included
10#define FMTsolve_Hm_included
11#include "FMTSolverInterface.h"
12#include "FMTMatrixBuild.h"
13#include "FMTExceptionHandler.h"
14#include "FMTObject.h"
15#include <memory>
16#include <unordered_map>
17#include <string_view>
18#include <boost/serialization/split_member.hpp>
19#include <boost/serialization/export.hpp>
20#include <boost/archive/binary_oarchive.hpp>
21#include <boost/archive/binary_iarchive.hpp>
23
24#if __has_include("OsiGlpkSolverInterface.hpp")
25 #ifndef FMTWITHGLPK
26 #define FMTWITHGLPK
27 #endif
28#endif
29
30#if __has_include("OsiClpSolverInterface.hpp")
31 #ifndef FMTWITHCLP
32 #define FMTWITHCLP
33 #endif
34#endif
35
36
37class OsiSolverInterface;
38class CoinPackedMatrix;
39
40namespace Models
41{
42
43// DocString: FMTLpSolver
49 {
50 public:
51 // DocString: FMTLpSolver::swap
56 void swap(FMTLpSolver& rhs);
57 // DocString: FMTLpSolver::passInLogger
62 void passInLogger(const std::unique_ptr<Logging::FMTLogger>& logger) override;
63 // DocString: FMTLpSolver::unmarkHotStart
68 // DocString: FMTLpSolver::stockResolve
74 // DocString: FMTLpSolver()
79 // DocString: ~FMTLpSolver()
83 virtual ~FMTLpSolver() = default;
84 // DocString: FMTLpSolver(const FMTLpSolver&)
90 // DocString: FMTLpSolver(FMTLpSolver&&)
95 FMTLpSolver(FMTLpSolver&& rhs)=default;
96 // DocString: FMTLpSolver::operator=
102 FMTLpSolver& operator =(const FMTLpSolver& rhs);
103 // DocString: FMTLpSolver::operator=
109 FMTLpSolver& operator =(FMTLpSolver&& rhs) =default;
110 // DocString: FMTLpSolver(FMTSolverInterface,const std::string,const std::string)
119 const std::string& p_ColdStartParameters,
120 const std::string& p_WarmStartParameters,
121 const std::string& p_problemName);
122 // DocString: FMTLpSolver::operator==
128 bool operator == (const FMTLpSolver& rhs) const;
129 // DocString: FMTLpSolver::operator!=
135 bool operator != (const FMTLpSolver& rhs) const;
136 // DocString: FMTLpSolver::resolve
141 virtual bool resolve();
142 // DocString: FMTLpSolver::initialSolve
148 virtual bool initialSolve();
149 // DocString: FMTLpSolver::setNumberOfThreads
155 void setNumberOfThreads(const size_t& nthread);
156 // DocString: FMTLpSolver::setMipGapTolerance
162 void setMipGapTolerance(const double& gap);
163 // DocString: FMTLpSolver::setOptimizerMaxTime
169 void setOptimizerMaxTime(const double& time);
170 // DocString: FMTLpSolver::setFeasiblePumpImprovSol
175 // DocString: FMTLpSolver::getObjValue
180 double getObjValue() const;
181 // DocString: FMTLpSolver::getIterationCount
186 int getIterationCount() const;
187 // DocString: FMTLpSolver::passInSolver
192 void passInSolver(const FMTLpSolver& solver);
193 // DocString: FMTLpSolver::passInMessageHandler
199 // DocString: FMTLpSolver::setSolverType
204 void setSolverType(FMTSolverInterface& lsolvertype) const;
205 // DocString: FMTLpSolver::gotLicense
210 bool gotLicense() const;
211 // DocString: FMTLpSolver::getSolverName
216 std::string getSolverName() const;
217 // DocString: FMTLpSolver::getNumCols
222 int getNumCols() const;
223 // DocString: FMTLpSolver::getNumRows
228 int getNumRows() const;
229 // DocString: FMTLpSolver::getObjCoefficients
234 const double* getObjCoefficients() const;
235 // DocString: FMTLpSolver::getColLower
240 const double* getColLower() const;
241 // DocString: FMTLpSolver::getColUpper
246 const double* getColUpper() const;
247 // DocString: FMTLpSolver::getColSolution
252 const double* getColSolution() const;
253 // DocString: FMTLpSolver::getRowPrice
258 const double* getRowPrice() const;
259 // DocString: FMTLpSolver::getRowActivity
264 const double* getRowActivity() const;
265 // DocString: FMTLpSolver::getRowUpper
270 const double* getRowUpper() const;
271 // DocString: FMTLpSolver::getRowLower
276 const double* getRowLower() const;
277 // DocString: FMTLpSolver::getRow
287 int getRow(int whichRow, double &rowLower, double &rowUpper,
288 std::vector<int>& indices, std::vector<double>&elements) const;
289 // DocString: FMTLpSolver::getCol
300 int getCol(int whichCol, double &colLower, double &colUpper, double &objectiveValue,
301 std::vector<int>& indices, std::vector<double>&elements) const;
302 // DocString: FMTLpSolver::getObjSense
307 double getObjSense() const;
308 // DocString: FMTLpSolver::setObjective
313 void setObjective(const double* objectivevalues);
314 // DocString: FMTLpSolver::setObjSense
319 void setObjSense(const double& newsense);
320 // DocString: FMTLpSolver::addRow
330 void addRow(int numberInRow, const int * columns, const double * elements,
331 double rowLower = -std::numeric_limits<double>::max(), double rowUpper = std::numeric_limits<double>::max());
332 // DocString: FMTLpSolver::addCol
343 void addCol(int numberInColumn, const int * rows, const double * elements, double columnLower = 0.0,
344 double columnUpper = std::numeric_limits<double>::max(), double objectiveValue = 0.0);
345 // DocString: FMTLpSolver::addRows
355 void addRows(const int numrows, const int* rowStarts, const int* columns,
356 const double* elements, const double* rowlb, const double* rowub);
357 // DocString: FMTLpSolver::addCols
368 void addCols(const int numcols, const int* columnStarts, const int* rows,
369 const double* elements, const double* collb, const double* colub, const double* obj);
370 // DocString: FMTLpSolver::isProvenOptimal
375 bool isProvenOptimal() const;
376 // DocString: FMTLpSolver::deleteRow
381 void deleteRow(const int& rowindex);
382 // DocString: FMTLpSolver::deleteCol
387 void deleteCol(const int& colindex);
388 // DocString: FMTLpSolver::deleteRows
394 void deleteRows(int numberofrows,const int* rowindexes);
395 // DocString: FMTLpSolver::deleteCols
401 void deleteCols(int numberofcols, const int* colindexes);
402 // DocString: FMTLpSolver::setColSolution
407 void setColSolution(const double* newsolution);
408 // DocString: FMTLpSolver::setRowPrice
413 void setRowPrice(const double* rowprice);
414 // DocString: FMTLpSolver::setColSetBounds
421 void setColSetBounds(const int* indexFirst, const int* indexLast, const double* boundlist);
422 // DocString: FMTLpSolver::setRowSetBoundss
429 void setRowSetBounds(const int* indexFirst, const int* indexLast, const double* boundlist);
430 // DocString: FMTLpSolver::setInteger
435 void setInteger(const int& colindex);
436 // DocString: FMTLpSolver::setInteger
442 void setInteger(const int* indices,int len);
443 // DocString: FMTLpSolver::updateMatrixNaming
447 //void updateMatrixNaming(const std::unordered_map<int, std::string>& colsnames,
448 // const std::unordered_map<int, std::string>& rownames);
449 // DocString: FMTLpSolver::setColName
455 void setColName(const std::string& name, const int& columnid) const;
456 // DocString: FMTLpSolver::setRowName
462 void setRowName(const std::string& name, const int& rowid) const;
463 // DocString: FMTLpSolver::writeLp
468 void writeLp(const std::string& location) const;
469 // DocString: FMTLpSolver::writeMps
474 void writeMps(const std::string& location) const;
475 // DocString: FMTLpSolver::branchAndBound
480 // DocString: FMTLpSolver::enableMatrixCaching
485 // DocString: FMTLpSolver::disableMatrixCaching
490 // DocString: FMTLpSolver::getSolverType()
496 {
497 return solvertype;
498 }
499 // DocString: FMTLpSolver::synchronize
504 // DocString: FMTLpSolver::sortDeletedCache
509 // DocString: FMTLpSolver::getCacheDeletedConstraints
514 inline const std::vector<int>& getCacheDeletedConstraints() const
515 {
516 return matrixcache.getDeletedConstraints();
517 }
518 // DocString: FMTLpSolver::getcachedeletedvariable
523 inline const std::vector<int>& getCacheDeletedVariables() const
524 {
525 return matrixcache.getDeletedVariables();
526 }
527 // DocString: FMTLpSolver::lowerNUpperToStr
534 std::string lowerNUpperToStr(const double& lower, const double& upper) const;
535 // DocString: FMTLpSolver::getCacheElements
540 std::string getCacheElements() const;
541 // DocString: FMTLpSolver::updateRowsAndColsNames
546 void updateRowsAndColsNames(bool shortformat = true);
547 // DocString: FMTLpSolver::getMskErrorDesc
553 std::string getMskErrorDesc(int error) const;
554 // DocString: FMTLpSolver::SupportsMultiThreading
560 // DocString: FMTLpSolver::getAvailableSolverInterface
565 static std::vector<Models::FMTSolverInterface> getAvailableSolverInterface();
566 // DocString: FMTLpSolver::toString
572 static std::string_view toString(Models::FMTSolverInterface p_solver);
573 protected:
574 // DocString: FMTLpSolver::matrixcache
577 // DocString: FMTLpSolver::solvertype
580 // DocString: FMTLpSolver::canUpdateSource
585 bool canUpdateSource() const;
586 // DocString: FMTLpSolver::buildSolverInterface
592 std::shared_ptr<OsiSolverInterface> buildSolverInterface(const FMTSolverInterface& lsolvertype) const;
593 // DocString: FMTLpSolver::copySolverInterface
600 std::shared_ptr<OsiSolverInterface> copySolverInterface(const std::shared_ptr<OsiSolverInterface>& solver_ptr, const FMTSolverInterface& lsolvertype) const;
601 // DocString: FMTLpSolver::clearRowCache
606 // DocString: FMTLpSolver::getMatrixByRow
611 const CoinPackedMatrix* getMatrixByRow() const;
612 // DocString: FMTLpSolver::getMatrixByCol
617 const CoinPackedMatrix* getMatrixByCol() const;
618 private:
619 // DocString: FMTLpSolver::strtoParams
625 static std::vector<std::pair<std::string, std::string>>strtoParams(const std::string& p_params);
626 friend class boost::serialization::access;
627 // DocString: FMTLpSolver::save
634 template<class Archive>
635 void save(Archive& ar, const unsigned int version) const
636 {
637 try {
638 ar& boost::serialization::make_nvp("FMTobject", boost::serialization::base_object<Core::FMTObject>(*this));
639 ar& BOOST_SERIALIZATION_NVP(usecache);
640 matrixcache.synchronize(solverinterface);
641 const FMTSerializableMatrix matrix(solverinterface);
642 ar& BOOST_SERIALIZATION_NVP(solvertype);
643 ar& BOOST_SERIALIZATION_NVP(matrix);
644 }
645 catch (...)
646 {
647 _exhandler->raiseFromCatch("", "FMTLpSolver::save", __LINE__, __FILE__);
648 }
649 }
650 // DocString: FMTLpSolver::load
657 template<class Archive>
658 void load(Archive& ar, const unsigned int version)
659 {
660 try {
661 ar& boost::serialization::make_nvp("FMTobject", boost::serialization::base_object<FMTObject>(*this));
662 ar& BOOST_SERIALIZATION_NVP(usecache);
663 matrixcache.synchronize(solverinterface);
664 FMTSerializableMatrix matrix;
665 ar& BOOST_SERIALIZATION_NVP(solvertype);
666 ar& BOOST_SERIALIZATION_NVP(matrix);
667 solverinterface = this->buildSolverInterface(solvertype);
668 matrix.setMatrix(solverinterface);
669 }
670 catch (...)
671 {
672 _exhandler->raiseFromCatch("", "FMTLpSolver::load", __LINE__, __FILE__);
673 }
674
675 }
676 BOOST_SERIALIZATION_SPLIT_MEMBER()
677 // DocString: FMTLpSolver::solverinterface
679 std::shared_ptr<OsiSolverInterface>solverinterface;
680 // DocString: FMTLpSolver::usecache
682 bool usecache;
683 // DocString: FMTLpSolver::>m_ColdStartParameters
685 std::vector<std::pair<std::string, std::string>>m_ColdStartParameters;
686 // DocString: FMTLpSolver::>m_WarmStartParameters
688 std::vector<std::pair<std::string, std::string>>m_WarmStartParameters;
689 // DocString: FMTLpSolver::_mskOptimizeWithParameters
694 int _mskOptimizeWithParameters();
695 // DocString: FMTLpSolver::_mskOptimizeWithDefaultParameters
700 int _mskOptimizeWithDefaultParameters();
701 // DocString: FMTLpSolver::_setGlpkLogLevel
706 void _setGlpkLogLevel(Logging::FMTLogger& p_logger);
707 // DocString: FMTLpSolver::_setClpOptions
711 void _setClpOptions();
712 };
713}
714BOOST_CLASS_EXPORT_KEY(Models::FMTLpSolver)
715#endif
716#endif
#define FMTEXPORT
Definition: FMTutility.h:125
Base class of multiple FMT classes holding a shared exception handler pointer and logger.
Definition: FMTObject.h:49
Base class handling the level of information printed by FMT.
Definition: FMTLogger.h:37
Abstract class to ease the usage of the OsiSolverInterface in FMT for linear programming models.
Definition: FMTLpSolver.h:49
void writeLp(const std::string &location) const
Write the solver interface matrix to a file using the LP formulation.
double getObjValue() const
Get the objective value of the solved matrix.
void enableMatrixCaching()
Enable the rows and columns caching when adding or deleting a variable to the solver interface,...
void disableMatrixCaching()
Synchronize the matrix cache with the solver interface and turn off the matrix caching.
void clearRowCache()
Clear the row caching of the OsiSolverInterface if Mosek is used.
void setColSetBounds(const int *indexFirst, const int *indexLast, const double *boundlist)
Synchronize the matrix and set the lower and upper bounds of a set of columns.
const double * getRowActivity() const
Return the row activity of the solver interface, without synchronizing with the cache.
void setColSolution(const double *newsolution)
Set a solution for all columns of the solver interface, synchronizing the matrix first.
void swap(FMTLpSolver &rhs)
Swap this FMTLpSolver with another one.
void setInteger(const int &colindex)
Synchronize the matrix and set a given column as integer.
void updateRowsAndColsNames(bool shortformat=true)
Update the rows and columns names.
void deleteRows(int numberofrows, const int *rowindexes)
Delete multiple rows; if the cache is used the matrix is synchronized before deleting the rows.
virtual bool resolve()
Call solverinterface->resolve when some changes are done to the model, avoiding a full initialSolve.
const std::vector< int > & getCacheDeletedConstraints() const
Return the row ids that are in the cache and marked as deleted.
Definition: FMTLpSolver.h:514
bool stockResolve()
Call the plain OsiSolverInterface resolve, synchronizing the matrix first.
FMTSolverInterface solvertype
Solver type used maybe usefull for initialSolve or resolve to know what solver we are using to speed-...
Definition: FMTLpSolver.h:579
void synchronize()
Update the OsiSolverInterface using the cached matrix information (new or deleted rows and columns) w...
int getNumCols() const
Return the number of columns by looking in the solver interface and in the cache.
std::string getSolverName() const
Return the name of the solver used.
static std::string_view toString(Models::FMTSolverInterface p_solver)
return the string value of the solver
void writeMps(const std::string &location) const
Write the solver interface matrix to a file using the MPS formulation.
std::string getCacheElements() const
Return the constraints and variables sitting in the matrix cache to be added to the solver interface,...
const double * getColLower() const
Return the column lower bounds of the matrix, synchronizing the solver interface with the cache first...
void addCol(int numberInColumn, const int *rows, const double *elements, double columnLower=0.0, double columnUpper=std::numeric_limits< double >::max(), double objectiveValue=0.0)
Add a column with its rows, elements, bounds and objective value.
bool isProvenOptimal() const
Return true if the program is optimal, synchronizing the matrix with the cache first.
double getObjSense() const
Return the objective sense of the solver interface, synchronizing with the cache first.
FMTLpSolver(const FMTLpSolver &rhs)
Default copy constructor for FMTLpSolver.
int getRow(int whichRow, double &rowLower, double &rowUpper, std::vector< int > &indices, std::vector< double > &elements) const
Fill the bounds, indices and elements of a given row, trying to synchronize the matrix as little as p...
const double * getColUpper() const
Return the column upper bounds of the matrix, synchronizing the solver interface with the cache first...
void setColName(const std::string &name, const int &columnid) const
Update the constraints and variables names.
void addRow(int numberInRow, const int *columns, const double *elements, double rowLower=-std::numeric_limits< double >::max(), double rowUpper=std::numeric_limits< double >::max())
Add a row with its columns, elements and bounds.
virtual ~FMTLpSolver()=default
Default virtual destructor for FMTLpSolver.
void passInSolver(const FMTLpSolver &solver)
Share the solver interface pointer of another solver.
void setRowName(const std::string &name, const int &rowid) const
Set a row name.
static std::vector< Models::FMTSolverInterface > getAvailableSolverInterface()
Return the available solver interfaces.
const double * getObjCoefficients() const
Return the objective coefficients of the matrix, synchronizing the solver interface with the cache fi...
void passInMessageHandler(Logging::FMTLogger &logger)
Pass the FMTLogger to the solver interface.
void deleteRow(const int &rowindex)
Delete a given row; if the cache is used it is only deleted from the cache.
FMTMatrixBuild matrixcache
The matrix cache follow the constraints or variables that need to be added or removed to the problem.
Definition: FMTLpSolver.h:576
virtual bool initialSolve()
Cold start of the LP solve of a simple LP model.
void addRows(const int numrows, const int *rowStarts, const int *columns, const double *elements, const double *rowlb, const double *rowub)
Add multiple rows directly to the matrix, synchronizing the matrix with the cache first.
std::string lowerNUpperToStr(const double &lower, const double &upper) const
Convert double bounds to string bounds (-inf, inf).
void mipParameters()
Set the mixed-integer optimizer parameters, including the way the feasibility pump heuristic is emplo...
void setObjective(const double *objectivevalues)
Set the objective values of the columns of the matrix.
void setOptimizerMaxTime(const double &time)
Set the maximum amount of time the optimizer is allowed to spend on the optimization.
FMTSolverInterface getSolverType() const
Return the solver type of the solver.
Definition: FMTLpSolver.h:495
void unmarkHotStart()
Clear all cached data in the OsiSolverInterface.
void setRowSetBounds(const int *indexFirst, const int *indexLast, const double *boundlist)
Synchronize the matrix and set the lower and upper bounds of a set of rows.
FMTLpSolver(FMTLpSolver &&rhs)=default
Default move constructor for FMTLpSolver.
int getNumRows() const
Return the number of rows by looking in the solver interface and in the cache.
void addCols(const int numcols, const int *columnStarts, const int *rows, const double *elements, const double *collb, const double *colub, const double *obj)
Add multiple columns directly to the matrix, synchronizing the matrix with the cache first.
bool canUpdateSource() const
Return true if the solver interface source can be updated.
void deleteCols(int numberofcols, const int *colindexes)
Delete multiple columns; if the cache is used the matrix is synchronized before deleting the columns.
void branchAndBound()
Synchronize the matrix cache and call the branch and bound MIP enumeration solver.
std::shared_ptr< OsiSolverInterface > buildSolverInterface(const FMTSolverInterface &lsolvertype) const
Build a shared pointer to a solver interface, passing the message handler to the pointer.
int getCol(int whichCol, double &colLower, double &colUpper, double &objectiveValue, std::vector< int > &indices, std::vector< double > &elements) const
Fill the bounds, objective, indices and elements of a given column, trying to synchronize the matrix ...
bool gotLicense() const
Return true if the license of the solver is available.
bool SupportsMultiThreading() const
Return true if the solver support multithreading.
const CoinPackedMatrix * getMatrixByRow() const
Return a CoinPackedMatrix by row of the problem, synchronizing the matrix first.
void setRowPrice(const double *rowprice)
Set a solution for all rows of the solver interface, synchronizing the matrix first.
const double * getRowPrice() const
Return the row price (dual solution) of the solver interface, without synchronizing with the cache.
void deleteCol(const int &colindex)
Delete a given column; if the cache is used it is only deleted from the cache.
FMTLpSolver(FMTSolverInterface lsolvertype, const std::string &p_ColdStartParameters, const std::string &p_WarmStartParameters, const std::string &p_problemName)
Main constructor for FMTLpSolver with a solver type, cold and warm start parameters and a problem nam...
std::string getMskErrorDesc(int error) const
Return the description of an error code from Mosek.
void setSolverType(FMTSolverInterface &lsolvertype) const
Setter of the solver type of the serializable matrix.
void setObjSense(const double &newsense)
Set the objective sense of the solver interface.
const double * getColSolution() const
Return the column solution of the solver interface, without synchronizing with the cache.
void setInteger(const int *indices, int len)
Synchronize the matrix and set multiple columns as integer variables.
const std::vector< int > & getCacheDeletedVariables() const
Return the column ids that are in the cache and marked as deleted.
Definition: FMTLpSolver.h:523
void setMipGapTolerance(const double &gap)
Set the tolerance between the relaxed optimal and the integer optimal.
const CoinPackedMatrix * getMatrixByCol() const
Return a CoinPackedMatrix by column of the problem, synchronizing the matrix first.
FMTLpSolver()
Default constructor for FMTLpSolver.
const double * getRowLower() const
Return the row lower bounds of the solver interface, synchronizing with the cache first.
void sortDeletedCache()
Sort and remove duplicates in the rows and columns to delete in the matrix cache.
void setNumberOfThreads(const size_t &nthread)
Set the maximum number of threads to be used by the solver.
std::shared_ptr< OsiSolverInterface > copySolverInterface(const std::shared_ptr< OsiSolverInterface > &solver_ptr, const FMTSolverInterface &lsolvertype) const
Copy a shared pointer to a solver interface into another shared pointer, passing the message handler ...
const double * getRowUpper() const
Return the row upper bounds of the solver interface, synchronizing with the cache first.
void passInLogger(const std::unique_ptr< Logging::FMTLogger > &logger) override
Pass in the logger, overridden for the OsiSolverInterface.
int getIterationCount() const
Get the number of iterations done by the solver.
Helper caching rows and columns to add to a solver matrix in batch for efficiency.
Definition: FMTMatrixBuild.h:32
void synchronize(std::shared_ptr< OsiSolverInterface > solver)
Synchronize the cached columns and rows with the solver matrix.
Helper for the serialization of an OSI solver interface matrix.
Definition: FMTSerializableMatrix.h:29
Namespace for the log management, provides different log handlers.
Definition: FMTDebugLogger.h:14
Namespace containing all enumerator and classes used to construct or manipulate the different type of...
Definition: FMTAreaParser.h:54
FMTSolverInterface
Enumerator used to specify the solver to use when solving the FMTLpModel.
Definition: FMTsolverinterface.h:21
void load(Ar &ar, dynamic_bitset< Block, Alloc > &bs, unsigned)
Definition: FMTMask.h:53
STL namespace.