FMT 1.2.0
Forest management tools for forest planning
Loading...
Searching...
No Matches
FMTLpModel.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 FMTlpmodel_Hm_included
10#define FMTlpmodel_Hm_included
11
12#include "FMTSrModel.h"
13#include <boost/serialization/export.hpp>
14#include <boost/serialization/split_member.hpp>
15#include <boost/serialization/unordered_map.hpp>
16#include <boost/serialization/vector.hpp>
17#include <boost/thread/recursive_mutex.hpp>
18#include <boost/thread/lock_guard.hpp>
19#include <limits>
20#include <map>
21
22namespace Heuristics
23{
24 class FMTOperatingAreaScheduler;
25 class FMTOperatingAreaClusterer;
26 class FMTOperatingAreaCluster;
27 class FMTOperatingAreaScheme;
28}
29
30namespace Models
31{
32// DocString: FMTmatrixelement
38 {
39 goalvariable=0,//period base
40 levelvariable=1,//general
42 constraint=3,//period base...
43 strictlypositive=4,//when an output have negative coef, be sure that the value of the output in the graph is strictly positive (like in woodstock)
44 nr_items=5
45 };
46
47// DocString: FMTLpModel
54 {
55
56 public:
57 // DocString: FMTLpModel::clearCache
61 void clearCache();
62 // DocString: FMTLpModel::clearConstraintLocation
68 // DocString: FMTLpModel(const FMTModel,FMTSolverInterface)
75 FMTLpModel(const FMTModel& base, FMTSolverInterface lsolvertype);
76 // DocString: FMTLpModel()
81 // DocString: FMTLpModel(const FMTModel, const Graph::FMTGraph<Graph::FMTVertexProperties,Graph::FMTEdgeProperties>, const FMTLpSolver, const std::vector<std::unordered_map<std::string,std::vector<std::vector<int>>>>)
85 /*FMTLpModel(const FMTModel& base, const Graph::FMTGraph<Graph::FMTVertexProperties, Graph::FMTEdgeProperties>& lgraph,
86 const FMTLpSolver& lsolver,const std::vector<std::unordered_map<std::string,std::vector<std::vector<int>>>>& lelements);*/
87 // DocString: FMTLpModel(const FMTLpModel)
93 // DocString: FMTLpModel::initialSolve
100 // DocString: FMTLpModel(FMTLpModel&&)
106 // DocString: FMTLpModel::operator=(FMTLpModel&& rhs)
112 FMTLpModel& operator =(FMTLpModel&& rhs);
113 // DocString: FMTLpModel::setStrictlyPositivesOutputsMatrix
118 // DocString: FMTLpModel::getOutput
127 virtual std::map<std::string, double> getOutput(const Core::FMTOutput& output,
128 int period, Core::FMToutputlevel level = Core::FMToutputlevel::standard) const;
129 // DocString: FMTLpModel::getReplanningConstraints
137 virtual std::vector<Core::FMTConstraint> getReplanningConstraints(const std::string& modeltype, const std::vector<Core::FMTConstraint>& localconstraints, const int& period) const;
138 // DocString: FMTLpModel::addScheduleToObjective
144 void addScheduleToObjective(const Core::FMTSchedule& schedule,double weight = 1000);
145 // DocString: FMTLpModel::operator==
151 bool operator == (const FMTLpModel& rhs) const;
152 // DocString: FMTLpModel::operator!=
158 bool operator != (const FMTLpModel& rhs) const;
159 // DocString: FMTLpModel::setObjective
167 // DocString: FMTLpModel::setConstraint
175 // DocString: FMTLpModel::eraseConstraint
184 // DocString: FMTLpModel::getVariabilities
193 std::map<std::string, std::vector<double>>getVariabilities(const std::vector<Core::FMTOutput>& outputs,
194 const int& periodStart,const int& periodStop);
195 // DocString: FMTLpModel::getModelFromProportions
203 FMTLpModel getModelFromProportions(const std::vector<Core::FMTMask>& globalmasks,
204 std::vector<double> tolerances) const;
205 // DocString: FMTLpModel::getAreaVariabilities
214 std::map<std::string, std::vector<double>>getAreaVariabilities(const std::vector<Core::FMTOutput>& localoutputs,
215 const std::vector<Core::FMTMask>& globalmasks,
216 std::vector<double> tolerances = std::vector<double>()) const;
217 // DocString: FMTLpModel::erasePeriod
223 Graph::FMTGraphStats erasePeriod(bool constraintsonly = false);
224 // DocString: FMTLpModel::getOperatingAreaSchedulerHeuristics
234 std::vector<Heuristics::FMTOperatingAreaScheduler>getOperatingAreaSchedulerHeuristics(const std::vector<Heuristics::FMTOperatingAreaScheme>& opareas,
235 const Core::FMTOutputNode& node,
236 size_t numberofheuristics=1,
237 bool copysolver=true);
238 // DocString: FMTLpModel::getOperatingAreaClustererHeuristics
251 std::vector<Heuristics::FMTOperatingAreaClusterer>getOperatingAreaClustererHeuristics(const std::vector<Heuristics::FMTOperatingAreaCluster>& clusters,
252 const Core::FMTOutput& statisticoutput,
253 const Core::FMTOutput& areaoutput,
254 const int& period,
255 size_t numberofheuristics=1,
256 int minimalnumberofclusters = -1,
257 int maximalnumberofclusters = -1) const;
258 // DocString: FMTLpModel::resolve
263 bool resolve();
264 // DocString: FMTLpModel::operator=
270 FMTLpModel& operator = (const FMTLpModel& rhs);
271 // DocString: ~FMTLpModel()
275 ~FMTLpModel() = default;
276 // DocString: FMTLpModel::getlpmodel
284 FMTLpModel getLocalModel(FMTModel localmodel = FMTModel(),int period = 0) const;
285 // DocString: FMTLpModel::getObjValue
290 double getObjValue() const;
291 // DocString: FMTLpModel::writeLp
296 void writeLp(const std::string& location);
297 // DocString: FMTLpModel::writeMps
302 void writeMps(const std::string& location);
303 // DocString: FMTLpModel::clone
308 virtual std::unique_ptr<FMTModel>clone() const;
309 // DocString: FMTLpModel::preSolve
315 virtual std::unique_ptr<FMTModel>preSolve(std::vector<Core::FMTActualDevelopment> optionaldevelopments = std::vector<Core::FMTActualDevelopment>()) const;
316 // DocString: FMTModel::build
323 virtual bool build(std::vector<Core::FMTSchedule> schedules=std::vector<Core::FMTSchedule>());
324 // DocString: FMTModel::solve
329 virtual bool solve();
330 // DocString: FMTLpModel::getCopy
337 virtual std::unique_ptr<FMTModel> getCopy(int period = 0) const;
338 private:
339 class ConstraintIndex
340 {
341 public:
342 ConstraintIndex(int p_constraintId, int p_period,FMTmatrixelement p_type);
343 /*ConstraintIndex(int p_constraintId, int p_period,
344 FMTmatrixelement p_type,
345 std::allocator<int>& p_allocator,size_t p_allocation=0);*/
346 //void push_back(const int& p_row);
347 bool operator < (const ConstraintIndex& p_rhs) const;
348 bool operator == (const ConstraintIndex& p_rhs) const;
349 //const std::vector<int>& getRows() const;
350 //std::vector<int>& getRowsRef();
351 size_t getHash() const;
352 private:
353 int m_constraintId;
354 int m_period;
355 FMTmatrixelement m_type;
356 //std::vector<int>m_rows;
357 };
358 // DocString: FMTLpModel::m_rowsAllocator
360 mutable std::allocator<int>m_rowsAllocator;
361 // DocString: FMTLpModel::m_indexAllocator
363 mutable std::allocator<ConstraintIndex>m_indexAllocator;
364 // DocString: FMTLpModel::m_indexes
366 std::map<ConstraintIndex,std::vector<int>>m_indexes;
367 // DocString: FMTLpModel::_getConstraintIndex
373 std::vector<Core::FMTConstraint>::const_iterator _getConstraintIndex(const Core::FMTConstraint& p_constraint) const;
374 // DocString: FMTLpModel::_getSetConstraintIndex
380 std::vector<Core::FMTConstraint>::const_iterator _getSetConstraintIndex(const Core::FMTConstraint& p_constraint);
381 // DocString: FMTLpModel::_getIndex
387 int _getIndex(const std::vector<Core::FMTConstraint>::const_iterator& p_it) const;
388 // DocString: FMTLpModel::_setGraphCache
393 void _setGraphCache(bool p_noLength = false);
394 // DocString: FMTLpModel::_setConstraintsCache
398 void _setConstraintsCache();
399 // DocString: FMTLpModel::serialize
406 friend class boost::serialization::access;
407 template<class Archive>
408 void save(Archive& ar, const unsigned int version) const
409 {
410 ar& boost::serialization::make_nvp("model", boost::serialization::base_object<FMTSrModel>(*this));
411 ar& BOOST_SERIALIZATION_NVP(m_indexes);
412 }
413 template<class Archive>
414 void load(Archive& ar, const unsigned int version)
415 {
416 ar& boost::serialization::make_nvp("model", boost::serialization::base_object<FMTSrModel>(*this));
417 ar& BOOST_SERIALIZATION_NVP(m_indexes);
418 }
419 // DocString: FMTLpModel::elements
421 //std::vector<std::unordered_map<std::string,
422 // std::vector<std::vector<int>>>>elements;
423 // DocString: FMTLpModel::getClusterer
435 const std::vector<Heuristics::FMTOperatingAreaCluster>& initialcluster,
436 const Core::FMTOutput& areaoutput,
437 const Core::FMTOutput& statisticoutput,
438 const int& period, int minimalnumberofclusters = -1, int maximalnumberofclusters = -1) const;
439 // DocString: FMTLpModel::getsetMatrixElement
450 int getsetMatrixElement(const std::vector<Core::FMTConstraint>::const_iterator& p_constraintId,
451 const FMTmatrixelement& element_type, const std::map<int, double>& indexes,
452 int period = -1,
453 double lowerbound = std::numeric_limits<double>::min(),
454 double upperbound = std::numeric_limits<double>::max());
455 // DocString: FMTLpModel::getGoals
464 bool getGoals(const std::vector<std::string>& goalsnames,
465 std::map<int, double>& index, const double& sense) const;
466 // DocString: FMTLpModel::getLevelFromLevelName
474 int getLevelFromLevelName(const std::string& variable_level, int period,
475 const std::vector<Core::FMTConstraint>::const_iterator& p_it) const;
476 // DocString: FMTLpModel::getSetLevel
484 int getSetLevel(const std::vector<Core::FMTConstraint>::const_iterator& p_it,
485 const std::string& variable_level, int period);
486 // DocString: FMTLpModel::getMatrixElement
494 const std::vector<int> getMatrixElement(const std::vector<Core::FMTConstraint>::const_iterator& it,
495 int p_period,
496 FMTmatrixelement p_element) const;
497 // DocString: FMTLpModel::getMatrixElementRef
505 std::vector<int>& getMatrixElementRef(const std::vector<Core::FMTConstraint>::const_iterator& it,
506 int p_period,
507 FMTmatrixelement p_element);
508 // DocString: FMTLpModel::locateLevels
517 void locateLevels(const std::vector<Core::FMTOutputNode>& nodes, int period,
518 std::map<int, double>& variables, const std::vector<Core::FMTConstraint>::const_iterator& p_it,
519 double multiplier = 1);
520 // DocString: FMTLpModel::locateNodes
530 std::vector<std::map<int, double>> locateNodes(const std::vector<Core::FMTOutputNode>& nodes, int period, std::map<int, double>& variables, double multiplier = 1) const;
531 // DocString: FMTLpModel::updateMatrixElements
537 void updateMatrixElements(std::vector<int>& matrixelements, const std::vector<int>& deletedelements) const;
538 // DocString: FMTLpModel::updateConstraintsMapping
544 void updateConstraintsMapping(const std::vector<int>& Dvariables, const std::vector<int>& Dconstraints);
545 // DocString: FMTLpModel::updateMatrixNGraph
551 bool updateMatrixNGraph(bool updategraph = true);
552 // DocString: FMTLpModel::getAmountOfPaths
556 //size_t getAmountOfPaths(const Core::FMTDevelopment& dev, const int& actionid) const;
557 // DocString: FMTLpModel::isMatrixElement
565 bool isMatrixElement(const std::vector<Core::FMTConstraint>::const_iterator& p_constraintId,
566 const FMTmatrixelement& element_type, int period) const;
567 // DocString: FMTLpModel::containsMatrixElements
574 bool containsMatrixElements(const std::vector<Core::FMTConstraint>::const_iterator& p_constraintId, int period) const;
575 // DocString: FMTLpModel::isSameMatrixElement
585 bool isSameMatrixElement(const int& matrixindex, const FMTmatrixelement& element_type,
586 const double& lowerb, const double& upperb, const std::map<int, double>& variables) const;
587
588 // DocString: FMTLpModel::getobjectivebounds
596 std::vector<int>setObjectiveBounds(bool dolower = true, bool doupper = true, double tolerance = FMT_DBL_TOLERANCE);
597 // DocString: FMTLpModel::updateMatrixNaming
601 void updateMatrixNaming();
602 // DocString: FMTLpModel::updateGeneralConstraintsNaming
608 void updateGeneralConstraintsNaming(std::vector<std::string>& colnames,
609 std::vector<std::string>& rownames) const;
610 // DocString: FMTLpModel::setPositiveOutputsInMatrix
618 bool setPositiveOutputsInMatrix(const std::vector<Core::FMTConstraint>::const_iterator& p_it,
619 const std::vector<std::map<int, double>>& strictlypositivesoutputs, int period);
620 // DocString: FMTLpModel::swapPtr
625 virtual void swapPtr(std::unique_ptr<FMTModel>& rhs);
626 // DocString: FMTLpModel::FMTLpModel(const FMTSrModel&)
631 FMTLpModel(const FMTSrModel& rhs);
632 // DocString: FMTLpModel::trySetSolution(const std::vector<Core::FMTSchedule>&)
638 bool trySetSolution(const std::vector<Core::FMTSchedule>& schedules);
639
640 };
641
642}
643
644BOOST_CLASS_EXPORT_KEY(Models::FMTLpModel)
645
646#endif
647#endif
#define FMT_DBL_TOLERANCE
Definition: FMTutility.h:11
#define FMTEXPORT
Definition: FMTutility.h:125
Representation of a line in the optimize section, an objective being considered a constraint.
Definition: FMTConstraint.h:52
Node of an output representing a set of developments in the graph, holding a source,...
Definition: FMTOutputNode.h:25
Class letting the user formulate a constraint in the optimize section or collect data across the grap...
Definition: FMTOutput.h:37
Spatially referenced schedule describing the area of developments for which an action can be operated...
Definition: FMTSchedule.h:29
Statistics about a FMTGraph and its FMTLpSolver: their size, elements and type of elements.
Definition: FMTGraphStats.h:28
Heuristic to do spatial clustering of operating areas across a landscape from a shapefile.
Definition: FMTOperatingAreaClusterer.h:28
Type III LP forest planning model, with the graph divided per period.
Definition: FMTLpModel.h:54
virtual std::unique_ptr< FMTModel > preSolve(std::vector< Core::FMTActualDevelopment > optionaldevelopments=std::vector< Core::FMTActualDevelopment >()) const
Return a presolved copy of the model using a vector of developments and the actual transitions,...
FMTLpModel getLocalModel(FMTModel localmodel=FMTModel(), int period=0) const
Append a child local model to the subset of this general model generated by the selected period,...
std::map< std::string, std::vector< double > > getAreaVariabilities(const std::vector< Core::FMTOutput > &localoutputs, const std::vector< Core::FMTMask > &globalmasks, std::vector< double > tolerances=std::vector< double >()) const
Return the change in outputs resulting from a variation of the area of the model for a given global m...
Graph::FMTGraphStats eraseConstraint(const Core::FMTConstraint &constraint, int period=-1)
Erase a constraint in the matrix and in the model elements.
std::vector< Heuristics::FMTOperatingAreaScheduler > getOperatingAreaSchedulerHeuristics(const std::vector< Heuristics::FMTOperatingAreaScheme > &opareas, const Core::FMTOutputNode &node, size_t numberofheuristics=1, bool copysolver=true)
Return operating area scheduler heuristics using multiple operating areas and an output node with a M...
FMTLpModel getModelFromProportions(const std::vector< Core::FMTMask > &globalmasks, std::vector< double > tolerances) const
Return a new FMTLpModel with changes in the initial area based on the tolerances of each global mask.
void clearConstraintLocation()
Clear the constraint localisation in the matrix.
void setStrictlyPositivesOutputsMatrix()
Set the model parameter STRICTLY_POSITIVE to true.
Graph::FMTGraphStats setObjective(const Core::FMTConstraint &objective)
Set the objective of the matrix for the whole planning horizon (graph length).
virtual std::vector< Core::FMTConstraint > getReplanningConstraints(const std::string &modeltype, const std::vector< Core::FMTConstraint > &localconstraints, const int &period) const
Adjust local or global constraints to the global model during replanning, using the solution of this ...
virtual bool solve()
Call initialSolve on the solver.
virtual std::map< std::string, double > getOutput(const Core::FMTOutput &output, int period, Core::FMToutputlevel level=Core::FMToutputlevel::standard) const
Get the output value for a period using the matrix solution.
void writeLp(const std::string &location)
Write the solver interface matrix to a file using the LP formulation.
bool initialSolve()
Cold start of the LP solve of a simple LP model.
FMTLpModel()
Default constructor for FMTLpModel.
FMTLpModel(const FMTModel &base, FMTSolverInterface lsolvertype)
Main constructor building a FMTLpModel from its base class and letting the user choose the solver typ...
void clearCache()
Remove the cache to release memory.
void addScheduleToObjective(const Core::FMTSchedule &schedule, double weight=1000)
Add weight to the objective function for a given schedule.
double getObjValue() const
Get the objective value of the solved matrix.
virtual std::unique_ptr< FMTModel > clone() const
Get a clone of the FMTLpModel.
std::vector< Heuristics::FMTOperatingAreaClusterer > getOperatingAreaClustererHeuristics(const std::vector< Heuristics::FMTOperatingAreaCluster > &clusters, const Core::FMTOutput &statisticoutput, const Core::FMTOutput &areaoutput, const int &period, size_t numberofheuristics=1, int minimalnumberofclusters=-1, int maximalnumberofclusters=-1) const
Return operating area clusterer heuristics for solving the operating area clustering problem using mu...
void writeMps(const std::string &location)
Write the solver interface matrix to a file using the MPS formulation.
virtual bool build(std::vector< Core::FMTSchedule > schedules=std::vector< Core::FMTSchedule >())
Build the graph and set the solution if schedules are passed.
FMTLpModel(FMTLpModel &&rhs)
Default move constructor for FMTLpModel.
Graph::FMTGraphStats erasePeriod(bool constraintsonly=false)
Delete the first period (front) of the graph and the matrix, shrinking the planning length to the ori...
Graph::FMTGraphStats setConstraint(const Core::FMTConstraint &constraint)
Set a constraint in the matrix for the whole planning horizon (graph length).
~FMTLpModel()=default
Default destructor for FMTLpModel.
FMTLpModel(const FMTLpModel &rhs)
Constructor for FMTLpModel mainly used in postSolve to pass each attribute of the class.
virtual std::unique_ptr< FMTModel > getCopy(int period=0) const
Return a copy of the FMTModel for the selected period.
std::map< std::string, std::vector< double > > getVariabilities(const std::vector< Core::FMTOutput > &outputs, const int &periodStart, const int &periodStop)
Get the variability of multiple outputs for a given tolerance for each period between periodStart and...
bool resolve()
Call solverinterface->resolve when some changes are done to the model, avoiding a full initialSolve.
Base class for the different models in FMT.
Definition: FMTModel.h:62
Base class holding the attributes and methods shared between the spatially referenced models (FMTLpMo...
Definition: FMTSrModel.h:46
FMToutputlevel
Enumerator describing the level of detail of an output (standard, total only, development).
Definition: FMTutility.h:22
@ standard
Definition: FMTutility.h:23
Namespace regrouping all tools for solving LP/MIP problems using an heuristic.
Definition: FMTAreaParser.h:47
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
FMTmatrixelement
Enum for the matrix element types present within the matrix.
Definition: FMTLpModel.h:38
@ nr_items
Definition: FMTLpModel.h:44
@ strictlypositive
Definition: FMTLpModel.h:43
@ goalvariable
Definition: FMTLpModel.h:39
@ objectivevariable
Definition: FMTLpModel.h:41
@ levelvariable
Definition: FMTLpModel.h:40
@ constraint
Definition: FMTLpModel.h:42