FMT 1.2.0
Forest management tools for forest planning
Loading...
Searching...
No Matches
RexportModel.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#ifndef REXPORTMODEL_H_INCLUDED
9#define REXPORTMODEL_H_INCLUDED
10
11#include "FMTModel.h"
12#include "FMTSrModel.h"
13#include "FMTLpModel.h"
14#include "FMTSeModel.h"
15#include "FMTSesModel.h"
16#include "FMTNssModel.h"
17#include "FMTSaModel.h"
18#include "FMTLpSolver.h"
19#include "Rdefinitions.h"
20#include "FMTSolverInterface.h"
21#include <Rcpp.h>
22#include <string>
23
27RCPP_DEFINEMAP(std::string,double);//For map<string,double>
34#ifdef FMTWITHOSI
48#endif
49
53
57
58namespace R
59{
60
62{
63 Rcpp::class_<Models::FMTModel>("FMTmodel", "@DocString(FMTModel)")
64 .derives<Core::FMTObject>("FMTobject")
65 .constructor<Models::FMTModel>("@DocString(FMTModel(Models::FMTModel))")
66 .constructor("@DocString(FMTModel())")
67 .method("getname",&Models::FMTModel::getName,
68 "@DocString(FMTModel::getname)")
69 .method("getyields",&Models::FMTModel::getYields,
70 "@DocString(FMTModel::getyields)")
71 .method("setyields", &Models::FMTModel::setYields,
72 "@DocString(FMTModel::setyields)")
73 .method("getarea",&Models::FMTModel::getArea,
74 "@DocString(FMTModel::getarea)")
75 .method("setarea", &Models::FMTModel::setArea,
76 "@DocString(FMTModel::setarea)")
77 .method("getthemes",&Models::FMTModel::getThemes,
78 "@DocString(FMTModel::getthemes)")
79 .method("setthemes", &Models::FMTModel::setThemes,
80 "@DocString(FMTModel::setthemes)")
81 .method("getactions",&Models::FMTModel::getActions,
82 "@DocString(FMTModel::getActions)")
83 .method("setactions", &Models::FMTModel::setActions,
84 "@DocString(FMTModel::setactions)")
85 .method("getoutputs", &Models::FMTModel::getOutputs,
86 "@DocString(FMTModel::getoutputs)")
87 .method("setoutputs", &Models::FMTModel::setOutputs,
88 "@DocString(FMTModel::setoutputs)")
89 .method("getconstraints", &Models::FMTModel::getConstraints,
90 "@DocString(FMTModel::getConstraints)")
91 .method("setconstraints", &Models::FMTModel::setConstraints,
92 "@DocString(FMTModel::setconstraints)")
93 .method("gettransitions",&Models::FMTModel::getTransitions,
94 "@DocString(FMTModel::gettransitions)")
95 .method("settransitions",&Models::FMTModel::setTransitions,
96 "@DocString(FMTModel::settransitions)")
97 .method("setname", &Models::FMTModel::setName,
98 "@DocString(FMTModel::setname)")
99 .method("setareaperiod", &Models::FMTModel::setAreaPeriod,
100 "@DocString(FMTModel::setareaperiod)")
101 .method("getlifespan",&Models::FMTModel::getLifespan,
102 "@DocString(FMTModel::getlifespan)")
103 .method("setlifespan", &Models::FMTModel::setLifespan,
104 "@DocString(FMTModel::setlifespan)")
105 .method("isValid",&Models::FMTModel::isValid,
106 "@DocString(FMTModel::isValid)")
107 .method("getpostsolvefilter", &Models::FMTModel::getPostSolveFilter,
108 "@DocString(FMTModel::getpostsolvefilter)")
109 .method("isoptimal", &Models::FMTModel::isOptimal,
110 "@DocString(FMTModel::isoptimal)")
111 .method("getobjectivevalue", &Models::FMTModel::getObjectiveValue,
112 "@DocString(FMTModel::getobjectivevalue)")
113 .method("GetStaticTransitionThemes", &Models::FMTModel::getStaticTransitionThemes,
114 "@DocString(FMTModel::GetStaticTransitionThemes)")
115 .method("setboolparameter",
116 static_cast<bool(Models::FMTModel::*)(const Models::FMTboolmodelparameters&, const bool&)>(&Models::FMTModel::setParameter),
117 "@DocString(Models::FMTModel::setparameter(const FMTboolmodelparameters,const bool))")
118 .method("setintparameter",
119 static_cast<bool(Models::FMTModel::*)(const Models::FMTintmodelparameters&, const int&)>(&Models::FMTModel::setParameter),
120 "@DocString(Models::FMTModel::setparameter(const FMTintmodelparameters,const int))")
121 .method("setdblparameter",
122 static_cast<bool(Models::FMTModel::*)(const Models::FMTdblmodelparameters&, const double&)>(&Models::FMTModel::setParameter),
123 "@DocString(Models::FMTModel::setparameter(const FMTdblmodelparameters,const int))")
124 .method("showparameters",&Models::FMTModel::showParameters,
125 "@DocString(FMTModel::showparameters)")
126 .method("doplanning",&Models::FMTModel::doPlanning,
127 "@DocString(FMTModel::doplanning)");
128
129 Rcpp::class_<Models::FMTSeModel>("FMTsemodel", "@DocString(FMTSeModel)")
130 .derives<Models::FMTModel>("FMTmodel")
131 .constructor("@DocString(FMTSeModel())")
132 .constructor<Models::FMTModel>("@DocString(FMTSeModel(Models::FMTModel))")
133 .method("getmapping", &Models::FMTSeModel::getMapping,
134 "@DocString(FMTSeModel::getmapping)")
135 .method("getschedule", &Models::FMTSeModel::getSchedule,
136 "@DocString(FMTSeModel::getschedule)")
137 .method("getspschedule", &Models::FMTSeModel::getSpSchedule,
138 "@DocString(FMTSeModel::getdisturbances)")
139 .method("setinitialmapping", &Models::FMTSeModel::setInitialMapping,
140 "@DocString(FMTSeModel::setinitialmapping)")
141 .method("getspatialoutput", &Models::FMTSeModel::getSpatialOutput,
142 "@DocString(FMTSeModel::getschedule)")
143 .method("getdisturbancestats", &Models::FMTSeModel::getDisturbanceStats,
144 "@DocString(FMTSeModel::getdisturbancestats)");
145
146
147 Rcpp::class_<Models::FMTSesModel>("FMTsesmodel", "@DocString(FMTSesModel)")
148 .derives<Models::FMTSeModel>("FMTsemodel")
149 .constructor("@DocString(FMTSesModel())")
150 .constructor<Models::FMTModel>("@DocString(FMTSesModel(Models::FMTModel))")
151 .method("greedyreferencebuild", &Models::FMTSesModel::greedyReferenceBuild,
152 "@DocString(FMTSesModel:::GreedyReferenceBuild)");
153
154 #ifdef FMTWITHOSI
155 Rcpp::class_<Models::FMTLpSolver>("FMTlpsolver", "@DocString(FMTLpSolver)")
156 .constructor("@DocString(FMTLpSolver())")
157 .method("isProvenOptimal", &Models::FMTLpSolver::isProvenOptimal,
158 "@DocString(FMTLpSolver::isProvenOptimal)")
159 .method("getObjValue", &Models::FMTLpSolver::getObjValue,
160 "@DocString(FMTLpSolver::getObjValue)")
161 .method("writeLP", &Models::FMTLpSolver::writeLp,
162 "@DocString(FMTLpSolver::writeLp)")
163 .method("writeMPS", &Models::FMTLpSolver::writeMps,
164 "@DocString(FMTLpSolver::writeMps)")
165 .method("getObjValue", &Models::FMTLpSolver::getObjValue,
166 "@DocString(FMTLpSolver::getObjValue)");
167
168
169 Rcpp::class_<Models::FMTSrModel>("FMTsrmodel", "@DocString(FMTSrModel)")
170 .derives<Models::FMTModel>("FMTmodel")
171 .constructor("@DocString(FMTSrModel())")
172 .method("buildperiod", &Models::FMTSrModel::buildPeriod,
173 "@DocString(FMTSrModel::buildperiod)")
174 .method("boundsolution", &Models::FMTSrModel::boundSolution,
175 "@DocString(FMTSrModel::boundsolution)")
176 .method("getsolution", &Models::FMTSrModel::getSolution,
177 "@DocString(FMTSrModel::getsolution)")
178 .method("setsolution", &Models::FMTSrModel::setSolution,
179 "@DocString(FMTSrModel::setsolution)")
180 .method("setsolutionbylp", &Models::FMTSrModel::setSolutionByLp,
181 "@DocString(FMTSrModel::setsolutionbylp)")
182 .method("getoutputsdataframe", &Models::FMTSrModel::getOutputsDataFrame,
183 "@DocString(FMTSrModel::getoutputsdataframe)")
184 .method("getoutput", &Models::FMTSrModel::getOutput,
185 "@DocString(FMTSrModel::getoutput)")
186 .method("getstats", &Models::FMTSrModel::getStats,
187 "@DocString(FMTSrModel::getstats)");
188
189 Rcpp::class_<Models::FMTNssModel>("FMTnssmodel", "@DocString(FMTNssModel)")
190 .derives<Models::FMTSrModel>("FMTsrmodel")
191 .constructor("@DocString(FMTNssModel())")
192 .constructor<Models::FMTModel, unsigned int>("@DocString(FMTNssModel(Models::FMTModel,unsigned int))")
193 .method("simulate", &Models::FMTNssModel::simulate,
194 "@DocString(FMTNssModel::simulate)");
195
196
197
198 Rcpp::class_<Models::FMTLpModel>("FMTlpmodel", "@DocString(FMTLpModel)")
199 .derives<Models::FMTSrModel>("FMTsrmodel")
200 .constructor<Models::FMTModel, Models::FMTSolverInterface>("@DocString(FMTLpModel(Models::FMTModel,Models::FMTSolverInterface))")
201 .constructor("@DocString(FMTLpModel())")
202 .constructor<Models::FMTLpModel>("@DocString(FMTLpModel(const FMTLpModel&))")
203 .method("setobjective", &Models::FMTLpModel::setObjective,
204 "@DocString(FMTLpModel::setobjective)")
205 .method("setconstraint", &Models::FMTLpModel::setConstraint,
206 "@DocString(FMTLpModel::setconstraint)")
207 .method("eraseconstraint", &Models::FMTLpModel::eraseConstraint,
208 "@DocString(FMTLpModel::eraseconstraint)")
209 .method("eraseperiod", &Models::FMTLpModel::erasePeriod,
210 "@DocString(FMTLpModel::eraseperiod)")
211 .method("resolve", &Models::FMTLpModel::resolve,
212 "@DocString(FMTLpModel::resolve)")
213 .method("initialsolve", &Models::FMTLpModel::initialSolve,
214 "@DocString(FMTLpModel::initialsolve)")
215 .method("eq", &Models::FMTLpModel::operator ==,
216 "@DocString(FMTLpModel::operator==)")
217 .method("ne", &Models::FMTLpModel::operator !=,
218 "@DocString(FMTLpModel::operator!=)")
219 .method("getoperatingareaschedulerheuristics", &Models::FMTLpModel::getOperatingAreaSchedulerHeuristics,
220 "@DocString(FMTLpModel::getoperatingareaschedulerheuristics)")
221 .method("getvariabilities", &Models::FMTLpModel::getVariabilities,
222 "@DocString(FMTLpModel::getvariabilities)")
223 .method("getareavariabilities", &Models::FMTLpModel::getAreaVariabilities,
224 "@DocString(FMTLpModel::getareavariabilities)")
225 .method("writeLP", &Models::FMTLpModel::writeLp,
226 "@DocString(FMTLpModel::writeLp)")
227 .method("setstrictlypositivesoutputsmatrix", &Models::FMTLpModel::setStrictlyPositivesOutputsMatrix,
228 "@DocString(FMTLpModel::setstrictlypositivesoutputsmatrix)")
229 .method("getObjValue", &Models::FMTLpModel::getObjValue,
230 "@DocString(FMTLpModel::getObjValue)")
231 .method("writeMPS", &Models::FMTLpModel::writeMps,
232 "@DocString(FMTLpModel::writeMps)");;
233
234
235 #endif
236
237
238 Rcpp::class_<Models::FMTSaModel>("FMTsamodel", "@DocString(FMTSaModel)")
239 .derives<Models::FMTSeModel>("FMTmodel")
240 .constructor<Models::FMTModel>("@DocString(FMTSaModel(FMTModel))")
241 .constructor("@DocString(FMTSaModel())")
242 .method("initialsolve", &Models::FMTSaModel::initialSolve,
243 "@DocString(FMTSaModel::initialsolve)");
244
245 }
246}
247#endif // PYEXPORTMODEL_H_INCLUDED
RCPP_EXPOSED_AS(Models::FMTModel)
RCPP_EXPOSED_ENUM_NODECL(Models::FMTSolverInterface)
RCPP_DEFINEVECTOR(Models::FMTModel)
RCPP_EXPOSED_WRAP(Models::FMTModel)
RCPP_DEFINEMAP(std::string, double)
Base class of multiple FMT classes holding a shared exception handler pointer and logger.
Definition: FMTObject.h:49
Type III LP forest planning model, with the graph divided per period.
Definition: FMTLpModel.h:54
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...
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).
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.
double getObjValue() const
Get the objective value of the solved matrix.
void writeMps(const std::string &location)
Write the solver interface matrix to a file using the MPS formulation.
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).
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.
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 writeMps(const std::string &location) const
Write the solver interface matrix to a file using the MPS formulation.
bool isProvenOptimal() const
Return true if the program is optimal, synchronizing the matrix with the cache first.
Base class for the different models in FMT.
Definition: FMTModel.h:62
Core::FMTMaskFilter getPostSolveFilter(const std::vector< Core::FMTTheme > &originalthemes, const Core::FMTMask &devmask) const
Get the postsolve filter used by the preSolve, to turn a presolved mask into a postsolve mask.
std::vector< Core::FMTOutput > getOutputs() const
Return a copy of the outputs of the model.
Definition: FMTModel.h:365
std::vector< Core::FMTTheme > getThemes() const
Return a copy of the themes of the model.
Definition: FMTModel.h:320
virtual bool doPlanning(const bool &solve, std::vector< Core::FMTSchedule > schedules=std::vector< Core::FMTSchedule >())
Build the model and do the initial solve or simulate, using the build and solve overridden functions ...
Core::FMTYields getYields() const
Return a copy of the yields of the model.
Definition: FMTModel.h:347
void setOutputs(const std::vector< Core::FMTOutput > &newoutputs)
Setter for the outputs of the model.
void setConstraints(const std::vector< Core::FMTConstraint > &lconstraint)
Setter for the constraints of the model, replacing the original.
std::vector< Core::FMTTransition > getTransitions() const
Return a copy of the transitions of the model.
Definition: FMTModel.h:338
virtual bool isOptimal() const
Return true if the FMTModel is optimal.
void setAreaPeriod(const int &period)
Change the initial area period to the targeted period.
void setArea(const std::vector< Core::FMTActualDevelopment > &ldevs)
Setter for the initial actual developments (area section), replacing the originals.
std::vector< Core::FMTAction > getActions() const
Return a copy of the actions of the model.
Definition: FMTModel.h:329
std::vector< size_t > getStaticTransitionThemes() const
Locate the static transition themes.
virtual bool setParameter(const FMTintmodelparameters &p_key, const int &p_value)
set int parameters to nss model.
void setYields(const Core::FMTYields &lylds)
Setter for the yields of the model, replacing the original.
Core::FMTLifespans getLifespan() const
Return a copy of the lifespan of the model.
Definition: FMTModel.h:356
virtual double getObjectiveValue() const
Return the value of the global objective.
void setLifespan(const Core::FMTLifespans &llifespan)
Setter for the lifespan of the model, replacing the original.
void setName(const std::string &newname)
Set the name of the FMTModel.
std::string getName() const
Return the name of the FMTModel.
Definition: FMTModel.h:311
virtual void showParameters(const bool &showhelp=false) const
Show the parameter values.
void setTransitions(const std::vector< Core::FMTTransition > &ltransitions)
Setter for the transitions of the model, replacing the originals.
virtual std::vector< Core::FMTActualDevelopment > getArea(int period=0, bool beforegrowanddeath=false) const
Get the area of a given period as actual developments.
void setThemes(const std::vector< Core::FMTTheme > &lthemes)
Setter for the themes of the model, replacing the originals.
bool isValid()
Validate the whole FMTModel through themes, actual developments, yields, actions, transitions,...
std::vector< Core::FMTConstraint > getConstraints() const
Return a copy of the constraints of the model.
Definition: FMTModel.h:374
void setActions(const std::vector< Core::FMTAction > &lactions)
Setter for the actions of the model, replacing the originals.
Non spatial simulation model, mainly used to simulate stochastic actions during the local replanning ...
Definition: FMTNssModel.h:29
void simulate()
Do a non spatial simulation based on the area constraints in the optimize section.
Area restricted model (ARM) using the simulated annealing meta-heuristic to solve the spatial optimiz...
Definition: FMTSaModel.h:40
bool initialSolve()
Try to solve the model from a cold start.
Abstract class for spatially explicit models, parent of FMTSesModel and FMTSaModel.
Definition: FMTSeModel.h:33
std::vector< Core::FMTSchedule > getSchedule(bool withlock=false) const
Return a copy of the operated schedules of the model, which can differ from the potential schedule.
std::string getDisturbanceStats() const
Return a string of patch statistics (area, perimeter) of the disturbances stack.
Spatial::FMTForest getMapping() const
Return a copy of the actual spatial forest state of each development.
bool setInitialMapping(const Spatial::FMTForest &forest)
Set the initial forest state, to be set before greedyReferenceBuild is called.
virtual Spatial::FMTLayer< double > getSpatialOutput(const Core::FMTOutput &output, int period) const
Get the spatial output value based on the spatial solution.
Spatial::FMTSpatialSchedule getSpSchedule() const
Return a copy of the spatially explicit solution.
Definition: FMTSeModel.h:82
Spatially explicit simulation model using a cellular automaton to simulate actions on a raster stack ...
Definition: FMTSesModel.h:25
std::map< std::string, double > greedyReferenceBuild(const Core::FMTSchedule &p_schedule, size_t p_randomIterations, int p_seed=0, double p_tolerance=FMT_DBL_TOLERANCE)
Find the best spatialization for a schedule using random draws over several iterations.
Base class holding the attributes and methods shared between the spatially referenced models (FMTLpMo...
Definition: FMTSrModel.h:46
Graph::FMTGraphStats getStats() const
Get the graph stats of the graph and matrix (number of columns, rows, edges, vertices).
bool boundSolution(int period, double tolerance=FMT_DBL_TOLERANCE)
Bound the primal variables to the primal solution present in the matrix for a given period and tolera...
Graph::FMTGraphStats buildPeriod(Core::FMTSchedule schedule=Core::FMTSchedule(), bool forcepartialbuild=false, int compressageclassoperability=1)
Main function used to build the graph and the matrix, adding a period within the graph and the matrix...
Core::FMTSchedule getSolution(int period, bool withlock=false) const final
Get the standard solution for a given period, without the natural growth solution.
bool setSolutionByLp(int period, const Core::FMTSchedule &schedule, double tolerance=FMT_DBL_TOLERANCE)
Set the solution on a partial graph using a schedule, changing the objective function,...
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.
bool setSolution(int period, const Core::FMTSchedule &schedule, double tolerance=FMT_DBL_TOLERANCE)
Set a solution for a given period to warmstart the model or to prepare to bound the model to that sol...
FMTdblmodelparameters
Enumerator of the double parameters of a FMTModel.
Definition: FMTModelParameters.h:54
FMTSolverInterface
Enumerator used to specify the solver to use when solving the FMTLpModel.
Definition: FMTsolverinterface.h:21
FMTintmodelparameters
Enumerator of the integer parameters of a FMTModel.
Definition: FMTModelParameters.h:28
FMTboolmodelparameters
Enumerator of the boolean parameters of a FMTModel.
Definition: FMTModelParameters.h:67
Definition: RexportCore.h:153
void exportModel()
Definition: RexportModel.h:61