33#ifndef BASECLASSBASECLASS_H
34#define BASECLASSBASECLASS_H
135#define M_PI 3.1415926535897932384626433832795
139#define M_LN2 0.69314718055994530941723212145818
143#define M_LN10 2.3025850929940456840179914546844
148#define PROD_ALL "PROD_ALL"
151#define PROD_PRI "PROD_PRI"
154#define PROD_SEC "PROD_SEC"
157#define DIAM_ALL "DIAM_ALL"
160#define DIAM_PROD "DIAM_PROD"
163#define DIAM_FIRST "DIAM_FIRST_CLASS"
166#define FT_ALL "FT_ALL"
171#define LBOUND_MIN -20000000000000000000.0
174#define UBOUND_MAX 20000000000000000000.0
182 iskey(
int i_h,
string s_h);
198 iiskey(
int i_h,
int i2_h,
string s_h);
216 iisskey(
int i_h,
int i2_h,
string s_h,
string s2_h);
219 string print()
const;
245 void msgOut(
const int& msgCode_h,
const string& msg_h,
const bool& refreshGUI_h=
true)
const;
246 void msgOut(
const int& msgCode_h,
const int& msg_h,
const bool& refreshGUI_h=
true)
const;
247 void msgOut(
const int& msgCode_h,
const double& msg_h,
const bool& refreshGUI_h=
true)
const;
249 int s2i (
const string& string_h)
const;
250 double s2d (
const string& string_h)
const;
251 double s2d (
const string& string_h,
const bool& replaceComma)
const;
252 bool s2b (
const string& string_h)
const;
257 string i2s (
const int& int_h)
const;
258 string d2s (
const double& double_h)
const;
259 string b2s (
const bool& bool_h)
const;
261 vector<int>
s2i (
const vector<string>& string_h)
const;
262 vector<double>
s2d (
const vector<string>& string_h,
const bool& replaceComma =
false)
const;
263 vector<bool>
s2b (
const vector<string>& string_h)
const;
264 vector<string>
i2s (
const vector<int>& int_h)
const;
265 vector<string>
d2s (
const vector<double>& double_h)
const;
266 vector<string>
b2s (
const vector<bool>& bool_h)
const;
268 int getType(
const string &type_h)
const;
272 template<
typename T>
string toString(
const T& x)
const;
273 template<
typename T> T
stringTo(
const std::string& s)
const;
276 int vSum(
const vector<int>& vector_h)
const {
return accumulate(vector_h.begin(),vector_h.end(),0);};
277 double vSum(
const vector<double>& vector_h)
const {
return accumulate(vector_h.begin(),vector_h.end(),0.);};
278 int vSum(
const vector< vector <int> >& vector_h)
const;
279 double vSum(
const vector< vector <double> >& vector_h)
const;
282 void tokenize(
const string& str, vector<string>& tokens,
const string& delimiter =
" ")
const;
283 void untokenize(
string &str, vector<string>& tokens,
const string& delimiter =
" ")
const;
286 template <
typename K,
typename V> V
findMap(
const map <K, V> &mymap,
const K& key,
const int& error_level=
MSG_CRITICAL_ERROR,
const V ¬FoundValue=numeric_limits<V>::min())
const{
287 typename map<K, V>::const_iterator p;
289 if(p != mymap.end()) {
293 msgOut(error_level,
"Error in finding a value in a map (no value found)");
294 return notFoundValue;
300 typename map<K, V>::iterator p;
302 if(p != mymap.end()) {
307 msgOut(error_level,
"Error in finding a value in a map (no value found)");
313 typename map<K, V>::iterator p;
315 if(p != mymap.end()) {
316 p->second = p->second + value;
320 msgOut(error_level,
"Error in finding a value in a map (no value found)");
325 template <
typename K,
typename V>
void incrOrAddMapValue(map <K, V> &mymap,
const K& key,
const V& value){
326 typename map<K, V>::iterator p;
328 if(p != mymap.end()) {
330 p->second = p->second + value;
335 pair<K,V> myPair(key,value);
336 mymap.insert(myPair);
341 template <
typename K,
typename V>
void resetMapValues(map <K, V> &mymap,
const V& value){
342 typename map<K, V>::iterator p;
343 for(p=mymap.begin(); p!=mymap.end(); p++) {
349 template <
typename K,
typename V> map <K, V>
vectorToMap(
const vector <K>& keys,
const V& value=0.0){
351 for(
unsigned int i=0; i<keys.size();i++){
352 pair<K,V> apair(keys[i],value);
353 returnMap.insert(apair);
359 template <
typename T> vector <T>
positionsToContent(
const vector <T> & vector_h,
const vector<int> &positions){
361 for(uint i=0; i<positions.size(); i++){
362 toReturn.push_back(vector_h.at(positions[i]));
368 template <
typename V>
void debugMap(
const map <iisskey, V> &mymap){
369 iisskey mykey(NULL,NULL,
"",
"");
372 template <
typename K,
typename V>
void debugMap(
const map <K, V> &mymap,
const K& key){
373 cout<<
"Debugging a map" << endl;
374 for (
auto const &themap: mymap) {
375 if(themap.first.filter(key)){
376 cout << themap.first.print() <<
'\t' << themap.second << endl;
383 template <
typename K>
int getMaxPos (
const vector <K> & v){
384 return (minmax_element(v.begin(), v.end()).second - v.begin());
387 template <
typename K>
int getMinPos (
const vector <K> & v){
388 return (minmax_element(v.begin(), v.end()).first - v.begin());
391 template <
typename K> K
getMax(
const vector <K> & v){
392 return *minmax_element(v.begin(), v.end()).second;
395 template <
typename K> K
getMin (
const vector <K> & v){
396 return *minmax_element(v.begin(), v.end()).first;
399 template <
typename K>
double getAvg (
const vector <K> & v){
400 return v.size()==0 ? 0.0 :
vSum(v)/ ( (double) v.size() );
408 template <
typename K>
double getSd (
const vector <K> & v,
bool sample=
true){
409 if (v.size()==0)
return 0.0;
410 int sampleCorrection = sample==
true?1:0;
411 double sum = std::accumulate(std::begin(v), std::end(v), 0.0);
412 double m = sum / v.size();
414 std::for_each (std::begin(v), std::end(v), [&](
const double d) {
415 accum += (d - m) * (d - m);
417 double stdev = sqrt(accum / ( (
double) (v.size()-sampleCorrection)));
422 for(
unsigned int i=0; i<v.size(); i++){
423 if(v[i]== element)
return i;
425 msgOut(msgCode_h,
"Element not found in vector in getPos()");
429 template <
typename K>
bool inVector (
const K & element,
const vector <K> & v){
430 for(
unsigned int i=0; i<v.size(); i++){
431 if(v[i]== element)
return true;
440 double normSample (
const double& avg,
const double& stdev,
const double& minval=NULL,
const double& maxval=NULL)
const;
443 template <
typename K> K
normSample (normal_distribution<K>& d, std::mt19937& gen,
const K& minval=NULL,
const K& maxval=NULL)
const {
444 if(minval != NULL && maxval != NULL){
445 if (maxval <= minval){
451 if( (minval == NULL || c >= minval) && (maxval == NULL || c <= maxval) ){
472 void msgOut2(
const int& msgCode_h,
const string& msg_h,
const bool& refreshGUI_h)
const;
dataRequest
A generic enum to deal with data requests.
@ OP_SUM
Perform a SUM operation.
@ OP_AVG
Perform an AVERAGE operation.
@ DATA_ERROR
There is an error in retrieving the data.
@ WORLD
Request something that is not region-specific.
@ DATA_NOW
The required data is for the current year.
@ DATA_INIT
Setting a data request for the init period.
@ RETNA
Request the (scenario specific) NO VALUE to be returned.
emissionType
Emission types.
@ EM_ENSUB
Energy substitution.
@ EM_MATSUB
Material substitution.
@ EM_FOROP
Flow from forest operations.
dataType
Type of data requested.
@ TYPE_DOUBLE
The required data is a double.
@ TYPE_BOOL
The required data is a bool.
@ TYPE_INT
The required data is an integer.
@ TYPE_STRING
The required data is a string.
outputVerbosity
Verbosity level of the output.
@ OUTVL_DETAILED
Output verbosity level print (also) detailed output.
@ OUTVL_MAPS
Output verbosity level print (also) the maps in ascii grid format.
@ OUTVL_BINMAPS
Output verbosity level print (also) binary (png) maps.
@ OUTVL_NONE
Output verbosity level none.
@ OUTVL_ALL
Output verbosity level print everything.
@ OUTVL_AGGREGATED
Output verbosity level print aggregated output (e.g. optimisation log)
carbonStocks
Carbon stocks.
@ STOCK_EXTRA
Extra biomass (soils, branches..)
@ STOCK_PRODUCTS
Biomass in forest products (sawns, pannels..)
@ STOCK_INV
Invetoried biomass (live and death tree logs)
messageType
Type of message to be printed.
@ MSG_CRITICAL_ERROR
Print an error message and stop the model.
@ MSG_ERROR
Print an ERROR message, but don't stop the model.
@ MSG_DEBUG
Print a debug message, normally filtered out.
@ MSG_WARNING
Print a WARNING message.
@ MSG_INFO
Print an INFO message.
@ MSG_NO_MSG
Do not actually output any message.
domains
Domain associated to a variable or a constrain in the optimisation of the market module.
@ DOM_PRI_PR
Primary products // domain of variables and constrains: primary, secondary, all products or all produ...
@ DOM_R2_SEC_PR
Secondary products over r2 couple regions (in-country commercial flows)
@ DOM_SEC_PR
Secondary products.
@ DOM_SCALAR
Scalar variable (not used)
@ DOM_R2_PRI_PR
Primary products over r2 couple regions (in-country commercial flows)
@ DOM_R2_ALL_PR
All products over r2 couple regions (in-country commercial flows)
@ DOM_PRI_PR_ALLCOMBS
All possible combinations of primary products (2^ number of primary products)
@ DOM_ALL_PR
All products (primary+secondary)
Base class for the regmas application.
ThreadManager * MTHREAD
Pointer to the Thread manager.
string b2s(const bool &bool_h) const
bool to string conversion
int s2i(const string &string_h) const
string to integer conversion
void untokenize(string &str, vector< string > &tokens, const string &delimiter=" ") const
int vSum(const vector< int > &vector_h) const
vector< T > positionsToContent(const vector< T > &vector_h, const vector< int > &positions)
Return a vector of content from a vector and a vector of positions (int)
void tokenize(const string &str, vector< string > &tokens, const string &delimiter=" ") const
Tokenize a string using a delimiter (default is space)
void incrOrAddMapValue(map< K, V > &mymap, const K &key, const V &value)
Increments a value stored in a map of the specified value, given the key.
int getMaxPos(const vector< K > &v)
Returns the position of the maximum element in the vector (the last one in case of multiple equivalen...
void incrMapValue(map< K, V > &mymap, const K &key, const V &value, const int &error_level=MSG_CRITICAL_ERROR)
Increments a value stored in a map of the specified value, given the key.
string d2s(const double &double_h) const
double to string conversion
K getMax(const vector< K > &v)
Returns the value of the maximum element in the vector (the last one in case of multiple equivalent m...
void debugMap(const map< K, V > &mymap, const K &key)
K getMin(const vector< K > &v)
Returns the value of the minimum element in the vector (the first one in case of multiple equivalent ...
map< K, V > vectorToMap(const vector< K > &keys, const V &value=0.0)
Returns a map built using the given vector and the given (scalar) value as keys/values pairs.
double getAvg(const vector< K > &v)
Returns the average of the elements in the vector.
V findMap(const map< K, V > &mymap, const K &key, const int &error_level=MSG_CRITICAL_ERROR, const V ¬FoundValue=numeric_limits< V >::min()) const
Lookup a map for a value. Return the value starting from the key.
K normSample(normal_distribution< K > &d, std::mt19937 &gen, const K &minval=NULL, const K &maxval=NULL) const
Sample from a normal distribution with bounds. Faster (half time) as the normal_distribution is made ...
T stringTo(const std::string &s) const
void debugMap(const map< iisskey, V > &mymap)
Debug a map.
void resetMapValues(map< K, V > &mymap, const V &value)
Reset all values stored in a map to the specified one.
void msgOut(const int &msgCode_h, const string &msg_h, const bool &refreshGUI_h=true) const
Overloaded function to print the output log.
bool inVector(const K &element, const vector< K > &v)
double normSample(const double &avg, const double &stdev, const double &minval=NULL, const double &maxval=NULL) const
Sample from a normal distribution with bounds. Slower (double time, but still you see the diff only a...
void refreshGUI() const
Ping to periodically return the control to the GUI.
bool s2b(const string &string_h) const
string to bool conversion
int getType(const string &type_h) const
Return a type according to enum TYPE_* from a string (eg: "string" -> TYPE_STRING (2))
double vSum(const vector< double > &vector_h) const
string i2s(const int &int_h) const
integer to string conversion
int getPos(const K &element, const vector< K > &v, const int &msgCode_h=MSG_CRITICAL_ERROR)
void changeMapValue(map< K, V > &mymap, const K &key, const V &value, const int &error_level=MSG_CRITICAL_ERROR)
Change the value stored in a map given the key and the new value.
double s2d(const string &string_h) const
string to double conversion
int getMinPos(const vector< K > &v)
Returns the position of the minimum element in the vector (the first one in case of multiple equivale...
double getSd(const vector< K > &v, bool sample=true)
void msgOut2(const int &msgCode_h, const string &msg_h, const bool &refreshGUI_h) const
Do the job of the overloaded functions.
string toString(const T &x) const
Thread manager. Responsable to manage the main thread and "speak" with the GUI.
Class to provide a simple integer-integer-string key in std maps.
iiskey()
iiskey class (note the double ii) ///
bool operator>(const iiskey &op2) const
bool operator>=(const iiskey &op2) const
bool operator<(const iiskey &op2) const
bool operator==(const iiskey &op2) const
bool operator!=(const iiskey &op2) const
bool operator<=(const iiskey &op2) const
Class to provide a simple integer-integer-string-string key in std maps.
iisskey()
iisskey class (note the double ii and double ss) ///
bool filter(const iisskey &key_h) const
bool operator>=(const iisskey &op2) const
bool operator==(const iisskey &op2) const
bool operator<(const iisskey &op2) const
bool operator>(const iisskey &op2) const
bool operator<=(const iisskey &op2) const
bool operator!=(const iisskey &op2) const
Class to provide a simple integer-string key to be used in std maps.
bool operator==(const iskey &op2) const
bool operator<(const iskey &op2) const
bool operator!=(const iskey &op2) const
bool operator>(const iskey &op2) const
bool operator>=(const iskey &op2) const
bool operator<=(const iskey &op2) const