FMT 1.2.0
Forest management tools for forest planning
Loading...
Searching...
No Matches
FMTOperatingAreaScheme.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
10#ifndef FMToperatingareascheme_Hm_included
11#define FMToperatingareascheme_Hm_included
12
13#include "FMTOperatingArea.h"
14#include <vector>
15#include <map>
16#include <memory>
17#include <boost/serialization/serialization.hpp>
18#include <boost/serialization/nvp.hpp>
19#include <boost/serialization/export.hpp>
20#include "FMTGraph.hpp"
21#include "FMTObject.h"
22
23namespace Core
24{
25 class FMTMask;
26}
27
28namespace Models
29{
30 class FMTLpSolver;
31}
32
33
34namespace Heuristics
35 {
36 // DocString: FMTOperatingAreaScheme
42 {
43 // DocString: FMTOperatingAreaScheme::serialize
50 friend class boost::serialization::access;
51 template<class Archive>
52 void serialize(Archive& ar, const unsigned int version)
53 {
54 ar & boost::serialization::make_nvp("operatingarea", boost::serialization::base_object<FMTOperatingArea>(*this));
55 ar & BOOST_SERIALIZATION_NVP(openingconstraints);
56 ar & BOOST_SERIALIZATION_NVP(openingbinaries);
57 ar & BOOST_SERIALIZATION_NVP(maximalschemesconstraint);
58 ar & BOOST_SERIALIZATION_NVP(schemesperiods);
59 ar & BOOST_SERIALIZATION_NVP(openingtime);
60 ar & BOOST_SERIALIZATION_NVP(returntime);
61 ar & BOOST_SERIALIZATION_NVP(repetition);
62 ar & BOOST_SERIALIZATION_NVP(greenup);
63 }
64 // DocString: FMTOperatingAreaScheme::openingconstraints
66 std::vector<std::vector<int>>openingconstraints;
67 // DocString: FMTOperatingAreaScheme::openingbinaries
69 std::vector<int>openingbinaries;
70 // DocString: FMTOperatingAreaScheme::maximalschemesconstraint
72 int maximalschemesconstraint;
73 // DocString: FMTOperatingAreaScheme::schemesperiods
75 std::vector<std::vector<int>>schemesperiods;
76 // DocString: FMTOperatingAreaScheme::openingtime
78 size_t openingtime;
79 // DocString: FMTOperatingAreaScheme::returntime
81 size_t returntime;
82 // DocString: FMTOperatingAreaScheme::repetition
84 size_t repetition;
85 // DocString: FMTOperatingAreaScheme::greenup
87 size_t greenup;
88 // DocString: FMTOperatingAreaScheme::startingperiod
90 size_t startingperiod;
91 // DocString: FMTOperatingAreaScheme::threshold
93 double threshold;
94 // DocString: FMTOperatingAreaScheme::maxreturntime
96 //between return time and maxreturntime for each closing
97 size_t maxreturntime;
98 // DocString: FMTOperatingAreaScheme::rejectednodescid
100 int rejectednodescid;
101 // DocString: FMTOperatingAreaScheme::getArea
109 double getPrimalArea(const double* primalsolution, const Graph::FMTGraph<Graph::FMTVertexProperties, Graph::FMTEdgeProperties>& maingraph, const std::vector<Graph::FMTGraph<Graph::FMTVertexProperties, Graph::FMTEdgeProperties>::FMTvertex_descriptor>& vertices) const;
110 // DocString: FMTOperatingAreaScheme::getBestSchemeId
116 size_t getBestSchemeId(const double* primalsolution) const;
117 // DocString: FMTOperatingAreaScheme::generateSchemes
124 std::vector<std::vector<std::vector<Graph::FMTGraph<Graph::FMTVertexProperties, Graph::FMTEdgeProperties>::FMTvertex_descriptor>>> generateSchemes(const std::vector<std::vector<Graph::FMTGraph<Graph::FMTVertexProperties, Graph::FMTEdgeProperties>::FMTvertex_descriptor>>& vertices);
125 // DocString: FMTOperatingAreaScheme::schemesToLp
136 void schemesToLp(const std::vector<std::vector<std::vector<Graph::FMTGraph<Graph::FMTVertexProperties, Graph::FMTEdgeProperties>::FMTvertex_descriptor>>>& schemes,
137 const std::vector<std::vector<Graph::FMTGraph<Graph::FMTVertexProperties, Graph::FMTEdgeProperties>::FMTvertex_descriptor>>& periodictargetednodes,
139 Models::FMTLpSolver& solver,
140 const double* primalsolution,
141 const Graph::FMTGraph<Graph::FMTVertexProperties, Graph::FMTEdgeProperties>& maingraph, const std::vector<int>& actionIDS);
142
143 // DocString: FMTOperatingAreaScheme::getIgnoredVertices
150 std::vector<Graph::FMTGraph<Graph::FMTVertexProperties, Graph::FMTEdgeProperties>::FMTvertex_descriptor> getIgnoredVertices(const std::vector<std::vector<std::vector<Graph::FMTGraph<Graph::FMTVertexProperties, Graph::FMTEdgeProperties>::FMTvertex_descriptor>>>& schemes,
151 const std::vector<std::vector<Graph::FMTGraph<Graph::FMTVertexProperties, Graph::FMTEdgeProperties>::FMTvertex_descriptor>>& targetedperiodsvertices) const;
152
153 // DocString: FMTOperatingAreaScheme::schemesToPeriods
160 std::vector<std::vector<int>> schemesToPeriods(const std::vector<std::vector<std::vector<Graph::FMTGraph<Graph::FMTVertexProperties, Graph::FMTEdgeProperties>::FMTvertex_descriptor>>>& schemes, const Graph::FMTGraph<Graph::FMTVertexProperties, Graph::FMTEdgeProperties>& maingraph) const;
161 // DocString: FMTOperatingAreaScheme::getRowsActivitySum
168 double getRowsActivitySum(const std::vector<int>& rows, const double* dualsolution) const;
169 // DocString: FMTOperatingAreaScheme::isThresholdActivityRows
177 bool isThresholdActivityRows(const std::vector<int>& rows, const double* dualsolution, double tempThreshold = -1) const;
178 // DocString: FMTOperatingAreaScheme::fillPattern
186 std::vector<double>fillPattern(const std::vector<double>& pattern, const int& startat) const;
187 // DocString: FMTOperatingAreaScheme::closeNoActivity
194 void closeNoActivity(std::vector<double>& filleduppattern, const size_t& selected, const double* dualsolution) const;
195 // DocString: FMTOperatingAreaScheme::getMaxPeriod
200 int getMaxPeriod() const;
201 public:
202 // DocString: FMTOperatingAreaScheme::setReturnTime
208 void setReturnTime(const size_t& minimalreturntime,const size_t& maximalreturntime);
209 // DocString: FMTOperatingAreaScheme::getThreshold
214 double getThreshold() const;
215 // DocString: FMTOperatingAreaScheme::getOpeningTime
220 size_t getOpeningTime() const;
221 // DocString: FMTOperatingAreaScheme::getMinimalReturnTime
226 size_t getMinimalReturnTime() const;
227 // DocString: FMTOperatingAreaScheme::getRepetition
232 size_t getRepetition() const;
233 // DocString: FMTOperatingAreaScheme::getMaximalReturnTime
238 size_t getMaximalReturnTime() const;
239 // DocString: FMTOperatingAreaScheme::empty
244 bool empty() const;
245 // DocString: FMTOperatingAreaScheme::getOpeningBinaries
250 const std::vector<int>& getOpeningBinaries() const;
251 // DocString: FMTOperatingAreaScheme::getStartingPeriod
256 size_t getStartingPeriod() const;
257 // DocString: FMTOperatingAreaScheme::getBinariesSum
263 double getBinariesSum(const double* primalsolution) const;
264 // DocString: FMTOperatingAreaScheme::getActivitySum
270 double getActivitySum(const double* dualsolution) const;
271 // DocString: FMTOperatingAreaScheme::getthresholdactivity
277 bool isThresholdActivity(const double* dualsolution) const;
278 // DocString: FMTOperatingAreaScheme::getCommonBinaries
285 std::map<int, std::vector<int>> getCommonBinaries(const FMTOperatingAreaScheme& neighbor) const;
286 // DocString: FMTOperatingAreaScheme::getPrimalSolutionIndex
292 size_t getPrimalSolutionIndex(const double* primalsolution) const;
293 // DocString: FMTOperatingAreaScheme::getDualSolutionIndex
300 bool getDualSolutionIndex(const double* upperbound, size_t& locid) const;
301 // DocString: FMTOperatingAreaScheme::havePotentialSolution
307 bool havePotentialSolution(const double* primalsolution) const;
308 // DocString: FMTOperatingAreaScheme::haveActivitySolution
314 bool haveActivitySolution(const double* dualsolution) const;
315 // DocString: FMTOperatingAreaScheme::isAllPrimalBounded
322 bool isAllPrimalBounded(const double* lowerbounds, const double* upperbounds) const;
323 // DocString: FMTOperatingAreaScheme::isAllDualBounded
329 bool isAllDualBounded(const double* upperbounds) const;
330 // DocString: FMTOperatingAreaScheme::isPrimalBounded
337 bool isPrimalBounded(const double* lowerbounds, const double* upperbounds) const;
338 // DocString: FMTOperatingAreaScheme::isDualBounded
344 bool isDualBounded(const double* upperbounds) const;
345 // DocString: FMTOperatingAreaScheme::getPotentialPrimalSchemes
354 std::vector<size_t>getPotentialPrimalSchemes(const double* p_primalsolution, const double* p_lowerbounds,
355 const double* p_upperbounds,const std::vector<FMTOperatingAreaScheme>& p_neighbors) const;
356 // DocString: FMTOperatingAreaScheme::getPotentialDualSchemes
364 std::vector<size_t>getPotentialDualSchemes(const double* p_dualsolution, const double* p_upperbound,
365 const std::vector<FMTOperatingAreaScheme>& p_neighbors) const;
366 // DocString: FMTOperatingAreaScheme::getRessourcesToDelete
372 void getRessourcesToDelete(std::vector<int>& colstodelete, std::vector<int>& rowstodelete) const; //Remove all variable and constraints related to the operating area from the solver
373 // DocString: FMTOperatingAreaScheme::pushBinaries
378 void pushBinaries(std::vector<int>& targets) const;
379 // DocString: FMTOperatingAreaScheme::unboundAllPrimalSchemes
386 size_t unboundAllPrimalSchemes(std::vector<int>& targets, std::vector<double>& bounds) const;
387 // DocString: FMTOperatingAreaScheme::unboundAllDualSchemes
394 size_t unboundAllDualSchemes(std::vector<int>& targets, std::vector<double>& bounds) const;
395 // DocString: FMTOperatingAreaScheme::boundAllPrimalSchemes
403 size_t boundAllPrimalSchemes(std::vector<int>& targets, std::vector<double>& bounds,double boundvalue = 1.0) const;
404 // DocString: FMTOperatingAreaScheme::boundAllDualSchemes
411 size_t boundAllDualSchemes(std::vector<int>& targets, std::vector<double>& bounds) const;
412 // DocString: FMTOperatingAreaScheme::boundPrimalScheme
420 bool boundPrimalScheme(std::vector<int>& targets, std::vector<double>& bounds, const size_t& schemeid) const;
421 // DocString: FMTOperatingAreaScheme::unboundDualScheme
431 bool unboundDualScheme(const double* rowactivities, std::vector<int>& targets, std::vector<double>& bounds, const size_t& schemeid, bool looseset=true) const;
432 // DocString: FMTOperatingAreaScheme::getPrimalSolution
438 std::vector<double> getPrimalSolution(const double* primalsolution) const;
439 // DocString: FMTOperatingAreaScheme::getDualSolution
447 std::vector<double> getDualSolution(const double* upperbounds, const double* dualsolution, bool& canbreakneighboring) const;
448 // DocString: FMTOperatingAreaScheme::getDualLowerBounds
455 std::vector<double> getDualLowerBounds(const double* lowerbounds,const double* upperbounds) const;
456 // DocString: FMTOperatingAreaScheme::setConstraints
470 const double* primalsolution,
471 const std::vector<int>& actionIDS);
472 // DocString: FMTOperatingAreaScheme(const FMTOperatingArea,const size_t, const size_t,const size_t,const size_t,const size_t)
485 FMTOperatingAreaScheme(const FMTOperatingArea& oparea,const size_t& lopeningtime, const size_t& lreturntime, const size_t& lmaxreturntime,
486 const size_t& lrepetition, const size_t& lgreenup,const size_t& lstartingperiod,double minimalarearatio=0.0);
487 // DocString: FMTOperatingAreaScheme()
492 // DocString: FMTOperatingAreaScheme(const FMTOperatingArea)
498 // DocString: FMTOperatingAreaScheme::operator=
504 FMTOperatingAreaScheme& operator = (const FMTOperatingAreaScheme& rhs)=default;
505 // DocString: FMTOperatingAreaScheme::fillBoundsNVariables
513 void fillBoundsNVariables(const double* lowerb, const double* upperb, std::vector<int>& constraintstargets,std::vector<double>&bounds) const;
514 // DocString: FMTOperatingAreaScheme::operator==
520 bool operator == (const FMTOperatingAreaScheme& rhs) const;
521 // DocString: FMTOperatingAreaScheme::operator!=
527 bool operator != (const FMTOperatingAreaScheme& rhs) const;
528 // DocString: ~FMTOperatingAreaScheme()
533 // DocString: FMTOperatingAreaScheme::preSolve
540 FMTOperatingAreaScheme preSolve(const Core::FMTMask& selectedmask,const std::vector<Core::FMTTheme>&presolvedthemes) const;
541 // DocString: FMTOperatingAreaScheme::getNumberOfScheme
546 size_t getNumberOfScheme() const;
547 // DocString: FMTOperatingAreaScheme::getNumberOfSimpleScheme
553 // DocString: FMTOperatingAreaScheme::getRejectedNodesCid
558 const int& getRejectedNodesCid() const;
559 // DocString: FMTOperatingAreaScheme::getschemesperiods
564 const std::vector<std::vector<int>>& getOpeningConstraints() const;
565 // DocString: FMTOperatingAreaScheme::getMaximalSchemesConstraint
570 const int& getMaximalSchemesConstraint() const;
571
572 // DocString: FMTOperatingAreaScheme::getGreenUp
577 const size_t& getGreenUp() const { return greenup; };
578
579 private:
580 // DocString: FMTOperatingAreaScheme::_maxNearThresholdActivityRows
587 double _maxNearThresholdActivityRows(const std::vector<int>& rows, const double* dualsolution) const;
588 // DocString: FMTOperatingAreaScheme::_findIndexLocation
594 size_t _findIndexLocation(int binary) const;
595 // DocString: FMTOperatingAreaScheme::_addPotentialResults
603 void _addPotentialResults(
604 size_t indexlocation,
605 std::vector<size_t>& potentialindexes,
606 std::vector<double>& potentialValues,
607 const double* dualsolution) const;
608 // DocString: FMTOperatingAreaScheme::_checkDoubleIncludes
614 static bool _checkDoubleIncludes(const std::vector<std::vector<int>>& openingconstraints);
615 };
616
617 // DocString: FMTOperatingAreaSchemeComparator
622 {
623 public:
624 // DocString: FMTOperatingAreaSchemeComparator(const Core::FMTMask)
630 // DocString: FMTOperatingAreaSchemeComparator::operator()
636 bool operator()(const FMTOperatingAreaScheme& oparea) const;
637
638 private:
639 // DocString: FMTOperatingAreaSchemeComparator::m_mask
641 Core::FMTMask m_mask;
642 };
643
644 }
645 BOOST_CLASS_EXPORT_KEY(Heuristics::FMTOperatingAreaScheme)
646#endif
647#endif
#define FMTEXPORT
Definition: FMTutility.h:125
Core class holding, as a boost dynamic bitset, which theme attributes are part of a mask.
Definition: FMTMask.h:96
Base class of multiple FMT classes holding a shared exception handler pointer and logger.
Definition: FMTObject.h:49
Definition: FMTGraphVertexToYield.h:20
Comparator to find a FMTOperatingAreaScheme with std::find_if when operating area schemes are in an s...
Definition: FMTOperatingAreaScheme.h:622
FMTOperatingAreaSchemeComparator(const Core::FMTMask &lmask)
Construct the comparator from a mask reference.
bool operator()(const FMTOperatingAreaScheme &oparea) const
Comparator for FMTOperatingAreaScheme.
Operating area scheme giving information about its opening time, green up and return time.
Definition: FMTOperatingAreaScheme.h:42
double getThreshold() const
Get the threshold.
size_t boundAllDualSchemes(std::vector< int > &targets, std::vector< double > &bounds) const
Push the constraint indexes into the targets vector and push the bound value into the bounds.
double getBinariesSum(const double *primalsolution) const
Summarize the value of all the binary variables for all the potential schemes from the primal solutio...
bool unboundDualScheme(const double *rowactivities, std::vector< int > &targets, std::vector< double > &bounds, const size_t &schemeid, bool looseset=true) const
Push the constraints of a scheme into the targets vector with -inf and the area bounds,...
bool havePotentialSolution(const double *primalsolution) const
Check if a scheme binary has a value greater than 0 in the primal solution, indicating a potential sc...
size_t unboundAllPrimalSchemes(std::vector< int > &targets, std::vector< double > &bounds) const
Push the binary indexes into the targets vector and push 0 and 1 into the bounds.
FMTOperatingAreaScheme(const FMTOperatingArea &oparea, const size_t &lopeningtime, const size_t &lreturntime, const size_t &lmaxreturntime, const size_t &lrepetition, const size_t &lgreenup, const size_t &lstartingperiod, double minimalarearatio=0.0)
Main constructor for FMTOperatingAreaScheme.
size_t unboundAllDualSchemes(std::vector< int > &targets, std::vector< double > &bounds) const
Push the constraint indexes into the targets vector, push -inf and the area into the bounds,...
bool boundPrimalScheme(std::vector< int > &targets, std::vector< double > &bounds, const size_t &schemeid) const
Push the variable index of a scheme binary into the targets vector and push 1 and 1 into the bounds.
FMTOperatingAreaScheme(const FMTOperatingAreaScheme &rhs)=default
Copy constructor for FMTOperatingAreaScheme.
size_t getStartingPeriod() const
Return the starting period at which all the schemes of the operating area start.
bool isPrimalBounded(const double *lowerbounds, const double *upperbounds) const
Return true if any binary lower or upper bound is set to 1.
FMTOperatingAreaScheme()=default
Default constructor for FMTOperatingAreaScheme.
bool empty() const
Check if the operating area is empty, which is possible if there is no potential development to opera...
~FMTOperatingAreaScheme()=default
Default destructor for FMTOperatingAreaScheme.
const int & getRejectedNodesCid() const
Return the constraint id of the rejected nodes.
size_t getMaximalReturnTime() const
Get the maximal return time.
size_t getNumberOfScheme() const
Return the number of schemes of the operating area, only usable after schemesToLp has been called.
void setConstraints(const std::vector< std::vector< Graph::FMTGraph< Graph::FMTVertexProperties, Graph::FMTEdgeProperties >::FMTvertex_descriptor > > &vertices, const std::vector< Graph::FMTGraph< Graph::FMTVertexProperties, Graph::FMTEdgeProperties >::FMTvertex_descriptor > &totalareavertices, const Graph::FMTGraph< Graph::FMTVertexProperties, Graph::FMTEdgeProperties > &graph, Models::FMTLpSolver &solver, const double *primalsolution, const std::vector< int > &actionIDS)
Set up the constraints and variables using a matrix build and a primal solution, filling all the matr...
void fillBoundsNVariables(const double *lowerb, const double *upperb, std::vector< int > &constraintstargets, std::vector< double > &bounds) const
For each scheme constraint, take the constraint bounds of the actual model.
size_t getNumberOfSimpleScheme() const
Return the number of simple schemes with a fixed return time and opening time.
const int & getMaximalSchemesConstraint() const
Return the constraint id ensuring that only one scheme is chosen.
size_t getOpeningTime() const
Get the opening time.
const size_t & getGreenUp() const
Return the green up.
Definition: FMTOperatingAreaScheme.h:577
size_t getMinimalReturnTime() const
Get the minimal return time.
double getActivitySum(const double *dualsolution) const
Summarize the value of all the constraints for all the potential schemes from the dual solution.
size_t getPrimalSolutionIndex(const double *primalsolution) const
Return the index of the scheme the operating area is bounded to, from the primal solution.
std::vector< double > getDualSolution(const double *upperbounds, const double *dualsolution, bool &canbreakneighboring) const
Return the yield solution of the dual problem by setting the selected scheme solution into a vector.
void getRessourcesToDelete(std::vector< int > &colstodelete, std::vector< int > &rowstodelete) const
Push all the variables and constraints of the operating area into the columns and rows to delete vect...
std::vector< size_t > getPotentialPrimalSchemes(const double *p_primalsolution, const double *p_lowerbounds, const double *p_upperbounds, const std::vector< FMTOperatingAreaScheme > &p_neighbors) const
Return the potential scheme indexes from the primal solution, its bounds and the neighboring operatin...
bool isAllDualBounded(const double *upperbounds) const
Return false if any constraint upper bound is different from 0.
std::vector< double > getDualLowerBounds(const double *lowerbounds, const double *upperbounds) const
Return the lower bounds of the solution for bounding the minimal harvested area.
std::vector< double > getPrimalSolution(const double *primalsolution) const
Return the yield solution of the primal problem by summing up all the binary variables into a single ...
bool isDualBounded(const double *upperbounds) const
Return true if any constraint upper bound is set to 0.
FMTOperatingAreaScheme preSolve(const Core::FMTMask &selectedmask, const std::vector< Core::FMTTheme > &presolvedthemes) const
Return a presolved FMTOperatingAreaScheme using a mask and a subset of the original themes.
bool isAllPrimalBounded(const double *lowerbounds, const double *upperbounds) const
Return false if any binary lower or upper bound is not set to 1.
bool haveActivitySolution(const double *dualsolution) const
Check if a set of scheme constraints has an activity value greater than 0 in the dual solution,...
bool isThresholdActivity(const double *dualsolution) const
Return true if every constraint activity is above the threshold.
bool getDualSolutionIndex(const double *upperbound, size_t &locid) const
Return the index of the scheme used by the operating area from the rows upper bounds.
void pushBinaries(std::vector< int > &targets) const
Push all the binaries into a targets vector.
std::map< int, std::vector< int > > getCommonBinaries(const FMTOperatingAreaScheme &neighbor) const
Return a map with the binary indexes of this operating area as keys and the binary indexes of the nei...
size_t getRepetition() const
Get the repetition of the pattern.
const std::vector< int > & getOpeningBinaries() const
Return the variable indexes of the binaries of all the potential schemes of the operating area.
void setReturnTime(const size_t &minimalreturntime, const size_t &maximalreturntime)
Set the minimal and maximal return time.
std::vector< size_t > getPotentialDualSchemes(const double *p_dualsolution, const double *p_upperbound, const std::vector< FMTOperatingAreaScheme > &p_neighbors) const
Return the potential scheme indexes from the dual solution, the rows upper bounds and the neighboring...
const std::vector< std::vector< int > > & getOpeningConstraints() const
Return the opening constraints of the operating area.
size_t boundAllPrimalSchemes(std::vector< int > &targets, std::vector< double > &bounds, double boundvalue=1.0) const
Push the variable indexes into the targets vector and push the bound value into the bounds.
Operating area giving information about its mask, neighbors and neighbors perimeter.
Definition: FMTOperatingArea.h:29
Abstract class to ease the usage of the OsiSolverInterface in FMT for linear programming models.
Definition: FMTLpSolver.h:49
The Core namespace provides classes for simulating stands/strata growth/harvest through time.
Definition: FMTAction.h:34
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