FMT 0.9.8
Forest management tools for forest planning
Loading...
Searching...
No Matches
FMTbounds.hpp
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 FMTbounds_H_INCLUDED
9#define FMTbounds_H_INCLUDED
10#include <limits>
11#include <map>
12#include "FMTexception.hpp"
13#include "FMTutility.hpp"
14#include <vector>
15#include <boost/functional/hash.hpp>
16#include <boost/serialization/serialization.hpp>
17#include <boost/serialization/nvp.hpp>
18#include <boost/serialization/string.hpp>
19#include <algorithm>
20
21namespace Core
22{
23
24
25template<typename T>
27 {
29 template<class Archive>
30 void serialize(Archive& ar, const unsigned int version)
31 {
32 ar & BOOST_SERIALIZATION_NVP(andbound);
33 ar & BOOST_SERIALIZATION_NVP(use);
34 ar & BOOST_SERIALIZATION_NVP(section);
35 ar & BOOST_SERIALIZATION_NVP(keytype);
36 ar & BOOST_SERIALIZATION_NVP(upper);
37 ar & BOOST_SERIALIZATION_NVP(lower);
38 }
39 bool andbound;
40 bool use;
41 protected:
46 public:
47 FMTbounds() : andbound(true),use(false),section(),keytype(),upper(),lower()
48 {
49
50 }
51 FMTbounds(const FMTsection lsection, const T& lupper, const T& llower):andbound(true),use(true),section(lsection),keytype(),upper(lupper),lower(llower)
52 {
53
54 }
55 FMTbounds(const FMTsection lsection,const FMTkwor key, const T& lupper,const T& llower):andbound(true),use(true),section(lsection),keytype(key),upper(lupper),lower(llower)
56 {
57
58 }
59 virtual ~FMTbounds() = default;
61 {
62 andbound = false;
63 }
64 T getlower() const
65 {
66 return lower;
67 }
68 T getupper() const
69 {
70 return upper;
71 }
72 inline bool out(const T& value) const
73 {
74 return ((lower > value) || (upper < value));
75 }
76 inline bool in(const T& value) const
77 {
78 return (empty() || (value <= upper && value >= lower));
79 }
80 FMTbounds(const FMTbounds<T>& rhs) :
81 andbound(rhs.andbound),
82 use(rhs.use),
83 section(rhs.section),
84 keytype(rhs.keytype),
85 upper(rhs.upper),
86 lower(rhs.lower)
87 {
88
89 }
90 bool operator == (const FMTbounds<T>& rhs) const
91 {
92 return (andbound == rhs.andbound &&
93 use == rhs.use &&
94 section == rhs.section &&
95 keytype == rhs.keytype &&
96 upper == rhs.upper &&
97 lower == rhs.lower);
98 }
99
100 bool operator < (const FMTbounds<T>& rhs) const
101 {
102 //strict ordering
103 if (andbound < rhs.andbound)
104 return true;
105 if (rhs.andbound < andbound)
106 return false;
107 if (use < rhs.use)
108 return true;
109 if (rhs.use < use)
110 return false;
111 if (section < rhs.section)
112 return true;
113 if (rhs.section < section)
114 return false;
115 if (keytype < rhs.keytype)
116 return true;
117 if (rhs.keytype < keytype)
118 return false;
119 if (upper < rhs.upper)
120 return true;
121 if (rhs.upper < upper)
122 return false;
123 if (lower < rhs.lower)
124 return true;
125 if (rhs.lower < lower)
126 return false;
127 return false;
128 }
129
131 {
132 if (this!=&rhs)
133 {
134 andbound = rhs.andbound;
135 section = rhs.section;
136 use = rhs.use;
137 lower = rhs.lower;
138 upper = rhs.upper;
139 keytype = rhs.keytype;
140 }
141 return *this;
142 }
143 bool empty() const
144 {
145 return !use;
146 }
147 bool add(const FMTbounds<T>& rhs)
148 {
149 if (!this->empty())
150 {
151 if (rhs.lower!=std::numeric_limits<T>::lowest())
152 {
153 lower = rhs.lower;
154 }
155 if (rhs.upper!= std::numeric_limits<T>::max())
156 {
157 upper = rhs.upper;
158 }
159 }else{
160 *this = rhs;
161 }
162 use = true;
163 return true;
164 }
165 std::string tostring(const std::string& name) const
166 {
167 std::string line;
168 const std::string slower = std::to_string(lower);
169 std::string supper = std::to_string(upper);
170 bool beenuse = false;
172 {
173 if (name.find("_LOCK")!=std::string::npos && upper > 0)
174 {
175 line+= "_LOCKEXEMPT";
176 }else{
177 if(upper==lower)
178 {
179 line=name+" = "+supper;
180 }else{
181 if(lower!= std::numeric_limits<T>::lowest())
182 {
183 line = name+" >= "+slower;
184 beenuse = true;
185 }
186 if(upper!= std::numeric_limits<T>::max())
187 {
188 if (beenuse)
189 {
190 line+=" AND "+ name+" <= "+supper;
191 }else{
192 line=name+" <= "+supper;
193 }
194 }
195 }
196 }
198 {
199 //if (name=="_AGE")
200 //{
201 if(upper== std::numeric_limits<T>::max())
202 {
203 supper = "_MAXAGE";
204 }
205 //}
207 {
208 if (name=="_AGE")
209 {
210 line = "@AGE(";
211 }else{
212 line = "@YLD("+name+",";
213 }
214 if(upper==lower)
215 {
216 line+=slower+")";
217 }/*else if (upper == std::numeric_limits<T>::max() && name != "_AGE")
218 {
219 line+=slower+")";
220 }*/else{
221 line+=slower+".."+supper+")";
222 }
224 {
225 if (name.find("LOCK") != std::string::npos && lower >= 1)
226 {
227 line = "_INVLOCK";
228 }else if(name.find("_CP") != std::string::npos && lower == 0 && upper == 0)
229 {
230 line = "[0]";
231 }
232 }
233
234 }else if(keytype == FMTkwor::Target)
235 {
236 if(name.find("_LOCK")!= std::string::npos)
237 {
238 line+= "_LOCK " + slower;
239 }else{
240 line=name + " " + slower;
241 }
242 }
243 }
244 return line;
245 }
246 };
247
248class FMTEXPORT FMTyldbounds: public FMTbounds<double>
249 {
250 friend class FMTspec;
251 friend class boost::serialization::access;
252 template<class Archive>
253 void serialize(Archive& ar, const unsigned int version)
254 {
255 ar & boost::serialization::make_nvp("bounds",boost::serialization::base_object<FMTbounds<double>>(*this));
256 ar & BOOST_SERIALIZATION_NVP(yield);
257 }
258 std::string yield;
259 public:
260 FMTyldbounds() :FMTbounds<double>(), yield() {};
261 ~FMTyldbounds() = default;
262 FMTyldbounds(const FMTsection lsection, const std::string& lyield, const double& lupper, const double& llower);
263 FMTyldbounds(const FMTsection lsection,const FMTkwor key,const std::string& lyield, const double& lupper,const double& llower);
264 FMTyldbounds(const std::string& lyield,const FMTbounds<double>& rhs);
265 FMTyldbounds(const FMTyldbounds& rhs) : FMTbounds(rhs), yield(rhs.yield) {};
266 FMTyldbounds& operator = (const FMTyldbounds& rhs);
267 bool operator == (const FMTyldbounds& rhs) const;
268 operator std::string() const;
269 };
270
272 {
273 friend class FMTspec;
274 friend class boost::serialization::access;
275 template<class Archive>
276 void serialize(Archive& ar, const unsigned int version)
277 {
278 ar & boost::serialization::make_nvp("bounds", boost::serialization::base_object<FMTbounds<int>>(*this));
279 }
280 public:
282 ~FMTagebounds() = default;
283 FMTagebounds(FMTsection lsection,const int& lupper, const int& llower);
284 FMTagebounds(FMTsection lsection,FMTkwor key, const int& lupper, const int& llower);
287 FMTagebounds& operator = (const FMTagebounds& rhs);
288 bool operator == (const FMTagebounds& rhs) const;
289 operator std::string() const;
290 };
291
293 {
294 friend class FMTspec;
295 friend class boost::serialization::access;
296 template<class Archive>
297 void serialize(Archive& ar, const unsigned int version)
298 {
299 ar & boost::serialization::make_nvp("bounds", boost::serialization::base_object<FMTbounds<int>>(*this));
300 }
301 public:
303 ~FMTperbounds() = default;
304 FMTperbounds(const FMTsection lsection,const int& lupper,const int& llower);
307 FMTperbounds& operator = (const FMTperbounds& rhs);
308 bool operator == (const FMTperbounds& rhs) const;
309 operator std::string() const;
310 };
311
312
314 {
315 friend class FMTspec;
316 friend class boost::serialization::access;
317 template<class Archive>
318 void serialize(Archive& ar, const unsigned int version)
319 {
320 ar & boost::serialization::make_nvp("bounds", boost::serialization::base_object<FMTbounds<int>>(*this));
321 }
322 public:
324 ~FMTlockbounds() = default;
325 FMTlockbounds(const FMTsection lsection,const FMTkwor key,const int& lupper, const int& llower);
326 FMTlockbounds(const FMTsection lsection, const int& lupper, const int& llower);
328 FMTlockbounds& operator = (const FMTlockbounds& rhs);
329 bool operator == (const FMTlockbounds& rhs) const;
330 operator std::string() const;
331 };
332
333class FMTyields;
334// DocString: FMTspec
342 {
343 // DocString: FMTspec::serialize
347 friend class Core::FMTyields;
348 friend class boost::serialization::access;
349 template<class Archive>
350 void serialize(Archive& ar, const unsigned int version)
351 {
352 ar & BOOST_SERIALIZATION_NVP(per);
353 ar & BOOST_SERIALIZATION_NVP(age);
354 ar & BOOST_SERIALIZATION_NVP(lock);
355 ar & BOOST_SERIALIZATION_NVP(yieldnames);
356 ar & BOOST_SERIALIZATION_NVP(yieldbounds);
357 }
358protected:
359 // DocString: FMTperbounds::per
362 // DocString: FMTperbounds::age
365 // DocString: FMTlockbounds::lock
368 // DocString: FMTlockbounds::yieldnames
370 std::vector<std::string>yieldnames;
371 // DocString: FMTlockbounds::yieldbounds
373 std::vector<FMTyldbounds>yieldbounds;
374public:
375 // DocString: FMTspec()
380 // DocString: ~FMTspec()
384 virtual ~FMTspec()=default;
385 // DocString: FMTspec(const FMTspec&)
389 FMTspec(const FMTspec& rhs);
390 // DocString: FMTspec::operator=
394 FMTspec& operator = (const FMTspec& rhs);
395 // DocString: FMTspec::add
399 bool add(const FMTspec& rhs);
400 // DocString: FMTspec::setbounds
404 bool setbounds(const FMTperbounds& bound);
405 // DocString: FMTspec::setbounds
409 bool addbounds(const FMTagebounds& bound);
410 // DocString: FMTspec::setyldbounds
414 bool addbounds(const FMTyldbounds& bound);
415 // DocString: FMTspec::setlockbounds
419 bool addbounds(const FMTlockbounds& bound);
420 // DocString: FMTspec::allowwithoutyield
424 inline bool allowwithoutyield(const int& tperiod, const int& tage, const int& tlock) const
425 {
426 return (per.in(tperiod) &&
427 age.in(tage) &&
428 (lock.empty() || (tlock >= lock.lower)));
429 }
430 // DocString: FMTspec::getyieldbound
434 inline const FMTyldbounds& getyieldbound(const std::string& name) const
435 {
436 return yieldbounds.at(std::distance(yieldnames.begin(), std::find(yieldnames.begin(), yieldnames.end(), name)));
437 }
438 // DocString: FMTspec::allowyields
442 inline bool allowyields(const std::vector<double>& values) const
443 {
444 for (size_t location = 0; location < yieldnames.size(); ++location)
445 {
446 if (yieldbounds.at(location).out(values.at(location)))
447 {
448 return false;
449 }
450 }
451
452 return true;
453 }
454 // DocString: FMTspec::allow
458 inline bool allow(const int& tperiod, const int& tage, const int& tlock, const std::vector<double>& values) const
459 {
460 for (size_t location = 0; location < yieldnames.size(); ++location)
461 {
462 if (yieldbounds.at(location).out(values.at(location)))
463 {
464 return false;
465 }
466 }
467 return (allowwithoutyield(tperiod,tage,tlock));
468 }
469 // DocString: FMTspec::getylds
473 inline const std::vector<std::string>& getylds() const
474 {
475 return yieldnames;
476 }
477 // DocString: FMTspec::getyldbounds
481 inline const std::vector<FMTyldbounds>& getyldbounds() const
482 {
483 return yieldbounds;
484 }
485 // DocString: FMTspec::operator std::string
489 virtual operator std::string() const;
490 // DocString: FMTspec::operator==
494 bool operator == (const FMTspec& rhs) const;
495 // DocString: FMTspec::operator<
499 bool operator < (const FMTspec& rhs) const;
500 // DocString: FMTspec::hash
504 size_t hash() const;
505 // DocString: FMTspec::empty
509 bool empty() const;
510 // DocString: FMTspec::emptyage
514 bool emptyage() const;
515 // DocString: FMTspec::emptyylds
519 bool emptyylds() const;
520 // DocString: FMTspec::emptyperiod
524 bool emptyperiod() const;
525 // DocString: FMTspec::emptylock
529 bool emptylock() const;
530 // DocString: FMTspec::getageupperbound
534 int getageupperbound() const;
535 // DocString: FMTspec::getagelowerbound
539 int getagelowerbound() const;
540 // DocString: FMTspec::getperiodupperbound
545 // DocString: FMTspec::getperiodlowerbound
550 // DocString: FMTspec::getlockupperbound
554 int getlockupperbound() const;
555 // DocString: FMTspec::getlocklowerbound
559 int getlocklowerbound() const;
560 // DocString: FMTspec::issubsetof
564 bool issubsetof(const FMTspec& rhs) const;
565 };
566
567}
568
569namespace boost {
570
571 template <>
572 struct hash<Core::FMTspec>
573 {
574 std::size_t operator()(const Core::FMTspec& spec) const
575 {
576 return spec.hash();
577 }
578 };
579
580
581}
582
583
584#endif // FMTbounds_H_INCLUDED
#define FMTEXPORT
Definition: FMTutility.hpp:92
Definition: FMTbounds.hpp:272
FMTagebounds(const FMTagebounds &rhs)
FMTagebounds(const FMTbounds< int > &rhs)
FMTagebounds(FMTsection lsection, const int &lupper, const int &llower)
~FMTagebounds()=default
FMTagebounds(FMTsection lsection, FMTkwor key, const int &lupper, const int &llower)
Definition: FMTbounds.hpp:27
FMTbounds(const FMTsection lsection, const T &lupper, const T &llower)
Definition: FMTbounds.hpp:51
FMTsection section
Definition: FMTbounds.hpp:42
std::string tostring(const std::string &name) const
Definition: FMTbounds.hpp:165
bool empty() const
Definition: FMTbounds.hpp:143
FMTbounds(const FMTsection lsection, const FMTkwor key, const T &lupper, const T &llower)
Definition: FMTbounds.hpp:55
bool add(const FMTbounds< T > &rhs)
Definition: FMTbounds.hpp:147
FMTbounds< T > & operator=(const FMTbounds< T > &rhs)
Definition: FMTbounds.hpp:130
void setorbound()
Definition: FMTbounds.hpp:60
bool operator==(const FMTbounds< T > &rhs) const
Definition: FMTbounds.hpp:90
FMTbounds()
Definition: FMTbounds.hpp:47
bool in(const T &value) const
Definition: FMTbounds.hpp:76
FMTkwor keytype
Definition: FMTbounds.hpp:43
friend class boost::serialization::access
Definition: FMTbounds.hpp:28
T getupper() const
Definition: FMTbounds.hpp:68
T lower
Definition: FMTbounds.hpp:45
bool operator<(const FMTbounds< T > &rhs) const
Definition: FMTbounds.hpp:100
T upper
Definition: FMTbounds.hpp:44
bool out(const T &value) const
Definition: FMTbounds.hpp:72
T getlower() const
Definition: FMTbounds.hpp:64
virtual ~FMTbounds()=default
FMTbounds(const FMTbounds< T > &rhs)
Definition: FMTbounds.hpp:80
Definition: FMTbounds.hpp:314
FMTlockbounds(const FMTlockbounds &rhs)
FMTlockbounds(const FMTsection lsection, const int &lupper, const int &llower)
FMTlockbounds(const FMTsection lsection, const FMTkwor key, const int &lupper, const int &llower)
~FMTlockbounds()=default
Definition: FMTbounds.hpp:293
~FMTperbounds()=default
FMTperbounds(const FMTsection lsection, const int &lupper, const int &llower)
FMTperbounds(const FMTbounds< int > &rhs)
FMTperbounds(const FMTperbounds &rhs)
Definition: FMTbounds.hpp:342
int getageupperbound() const
const std::vector< FMTyldbounds > & getyldbounds() const
Definition: FMTbounds.hpp:481
bool issubsetof(const FMTspec &rhs) const
FMTlockbounds lock
Lock bounds so lower >= lock <= upper.
Definition: FMTbounds.hpp:367
int getlockupperbound() const
bool emptyage() const
int getlocklowerbound() const
bool addbounds(const FMTyldbounds &bound)
int getperiodlowerbound() const
bool addbounds(const FMTagebounds &bound)
const FMTyldbounds & getyieldbound(const std::string &name) const
Definition: FMTbounds.hpp:434
bool setbounds(const FMTperbounds &bound)
FMTspec(const FMTspec &rhs)
FMTagebounds age
Age bounds so lower >= age <= upper.
Definition: FMTbounds.hpp:364
std::vector< std::string > yieldnames
The names of each yield in the vector of yieldbounds.
Definition: FMTbounds.hpp:370
bool allowyields(const std::vector< double > &values) const
Definition: FMTbounds.hpp:442
virtual ~FMTspec()=default
bool allow(const int &tperiod, const int &tage, const int &tlock, const std::vector< double > &values) const
Definition: FMTbounds.hpp:458
bool emptylock() const
FMTperbounds per
Period bounds so lower >= period <= upper.
Definition: FMTbounds.hpp:361
int getagelowerbound() const
int getperiodupperbound() const
const std::vector< std::string > & getylds() const
Definition: FMTbounds.hpp:473
bool empty() const
bool add(const FMTspec &rhs)
bool allowwithoutyield(const int &tperiod, const int &tage, const int &tlock) const
Definition: FMTbounds.hpp:424
size_t hash() const
bool emptyylds() const
std::vector< FMTyldbounds > yieldbounds
the yields bounds vector lower>=y1<=upper,lower>=y2<=upper
Definition: FMTbounds.hpp:373
bool addbounds(const FMTlockbounds &bound)
bool emptyperiod() const
Definition: FMTyields.hpp:40
Definition: FMTbounds.hpp:249
FMTyldbounds(const std::string &lyield, const FMTbounds< double > &rhs)
FMTyldbounds(const FMTsection lsection, const std::string &lyield, const double &lupper, const double &llower)
~FMTyldbounds()=default
FMTyldbounds(const FMTsection lsection, const FMTkwor key, const std::string &lyield, const double &lupper, const double &llower)
FMTyldbounds()
Definition: FMTbounds.hpp:260
FMTyldbounds(const FMTyldbounds &rhs)
Definition: FMTbounds.hpp:265
The Core namespace provides classes for simulating stands/strata growth/harvest through time.
Definition: FMTaction.hpp:31
FMTsection
Definition: FMTutility.hpp:23
@ Outputs
Definition: FMTutility.hpp:30
@ Action
Definition: FMTutility.hpp:27
@ Transition
Definition: FMTutility.hpp:28
FMTkwor
Definition: FMTutility.hpp:39
Definition: FMTaction.hpp:364
std::size_t operator()(const Core::FMTspec &spec) const
Definition: FMTbounds.hpp:574