FFSM++  1.1.0
French Forest Sector Model ++
Sandbox.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2015 by Laboratoire d'Economie Forestière *
3  * http://ffsm-project.org *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 3 of the License, or *
8  * (at your option) any later version, given the compliance with the *
9  * exceptions listed in the file COPYING that is distribued together *
10  * with this file. *
11  * *
12  * This program is distributed in the hope that it will be useful, *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15  * GNU General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU General Public License *
18  * along with this program; if not, write to the *
19  * Free Software Foundation, Inc., *
20  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21  ***************************************************************************/
22 #include <algorithm>
23 
24 #include <cmath>
25 #include <algorithm>
26 #include <map>
27 #include <tr1/array>
28 
29 #include <Eigen/Dense>
30 
31 #include "Sandbox.h"
32 #include "ThreadManager.h"
33 #include "ModelData.h"
34 #include "Gis.h"
35 #include "ModelRegion.h"
36 #include "Carbon.h"
37 
38 
39 //Testing random distribution, using some new in C++ random generator seeds..
40 #include <iostream>
41 #include <iomanip>
42 #include <string>
43 #include <map>
44 #include <random>
45 #include <cmath>
46 #include <float.h>
47 #include <limits>
48 #include <cstddef>
49 
50 
51 // Testing zip library...
52 //#include "zip.h"
53 //#include "unzip.h"
54 //#include <QFile>
55 //#include <QFileInfo>
56 //#include <QString>
57 //#include <QStringList>
58 //#include <QList>
59 //#include <iostream>
60 //#include <iomanip>
61 
62 
63 //Testing FlopC++ (requires modified src.pro qmake file)
64 //#include "flopc.hpp"
65 //using namespace flopc;
66 //#include <OsiClpSolverInterface.hpp>
67 //#include <OsiCbcSolverInterface.hpp>
68 
69 #include "IpIpoptApplication.hpp"
70 #include "IpSolveStatistics.hpp"
71 
73 #include "Adolc_debugtest.h"
74 
75 
76 typedef map<string, string> TStrStrMap;
77 typedef pair<string, string> TStrStrPair;
78 
79 using namespace std;
80 
82  MTHREAD=MTHREAD_h;
83 }
84 
86 
87 }
88 
89 
91 
92 }
93 
94 // ---------------------------------------------
95 struct GccTest
96 {
97 
98  GccTest(string name_h){
99  nameMember = name_h;
100  };
101 
102  string nameMember;
103 
104  operator string ()
105  {
106 
107  cout << "the first function\n";
108  cout << nameMember << endl;
109  return "42";
110  }
111 
112  operator int ()
113  {
114  cout << "its \"underload\"\n";
115  return 42;
116  }
117 
118  operator vector<int> ()
119  {
120  cout << "within vector <int>" << endl;
121  vector <int> toReturn;
122  toReturn.push_back(3);
123  toReturn.push_back(4);
124  toReturn.push_back(5);
125  return toReturn;
126  }
127 
128 };
129 
130 // --------------------------------------
131 void
133 
134  /*
135  // Checking integer division
136 
137  int a = 19;
138  int b = 5;
139  int c = a/b;
140  int d = a%b;
141  cout << a << " - " << b << " - " << c << " - " << d << endl;
142  exit(0);
143  */
144 
145  /*
146  // Testing initializating a matrix with fixed values in C++11
147  //vector< vector<double> > deltaAreas(fTypes.size()+1, vector<double>(fTypes.size()+1,5));
148  vector< vector<double> > deltaAreas(3, vector<double>(5,4));
149  for (uint i=0;i<3;i++){
150  for (uint j=0;j<5;j++){
151  cout << deltaAreas.at(i).at(j) << "\t";
152  }
153  cout << endl;
154  }
155  exit(1);
156  */
157 
158  /*
159  // Testing map of generic vector of strings
160  //map<vector<string>,double> testMap;
161  map<tr1::array<string, 3>,double> testMap;
162 
163  //vector<string> k1 = {"aa","aa","aa"};
164  tr1::array<string, 3> k1 = {"aa","aa","aa"};
165  double v1 = 1;
166  double v1b = 3;
167  //vector<string> k2 = {"aa","bb","aa"};
168  tr1::array<string, 3> k2 = {"aa","bb","aa"};
169  double v2 = 2;
170  incrOrAddMapValue(testMap, k1, v1);
171  incrOrAddMapValue(testMap, k2, v2);
172  incrOrAddMapValue(testMap, k1, v1b);
173  double debug = findMap(testMap, k1);
174  cout << debug << endl;
175  double debug2 = findMap(testMap, k2);
176  cout << debug2 << endl;
177  exit(1);
178  */
179 
180 
181  /*
182  // Testing debugging a map
183  iisskey k1(2007,11021,"broadL_HighF","15");
184  iisskey k2(2007,11021,"broadL_HighF","30");
185  iisskey k3(2007,11021,"con_HighF","15");
186  iisskey k4(2007,11022,"broadL_HighF","15");
187  iisskey k5(2008,11021,"broadL_HighF","15");
188 
189  // Testing the new changeMapValue(), incrMapValue(), resetMapValues(), incrOrAddMapValue(map, key, value) and vectorToMap() funcions
190  map<iisskey,double> testMap;
191  pair<iisskey,double> pair1(k1,1.1);
192  pair<iisskey,double> pair2(k2,1.2);
193  pair<iisskey,double> pair3(k3,1.3);
194  pair<iisskey,double> pair4(k4,1.4);
195  pair<iisskey,double> pair5(k5,1.5);
196  testMap.insert(pair1);
197  testMap.insert(pair2);
198  testMap.insert(pair3);
199  testMap.insert(pair4);
200  testMap.insert(pair5);
201  debugMap(testMap,iisskey(NULL,NULL,"",""));
202  debugMap(testMap,iisskey(2007,NULL,"con_HighF",""));
203  exit(0);
204  */
205 
206 
207 
208 
209  /*
210  // Testing standard deviation algorithm, as from http://stackoverflow.com/questions/7616511/calculate-mean-and-standard-deviation-from-a-vector-of-samples-in-c-using-boos
211  vector<double> v;
212  v.push_back(3.0);
213  v.push_back(2.0);
214  v.push_back(5.0);
215  v.push_back(4.0);
216  double sum = std::accumulate(std::begin(v), std::end(v), 0.0);
217  double m = sum / v.size();
218  double accum = 0.0;
219  std::for_each (std::begin(v), std::end(v), [&](const double d) {
220  accum += (d - m) * (d - m);
221  });
222  double stdev = sqrt(accum / (v.size()-1));
223  cout << stdev << endl;
224  double sd2 = getSd(v);
225  double sd3 = getSd(v,false);
226  cout << sd2 << endl;
227  cout << sd3 << endl;
228  exit(0);
229  */
230 
231  /*
232  // Testing tokenize, untokenize functions
233  vector<string> istrings;
234  istrings.push_back("Questo");
235  istrings.push_back("cielo");
236  istrings.push_back("è");
237  istrings.push_back("sempre");
238  istrings.push_back("più");
239  istrings.push_back("blu.");
240  string delimiter = " . ";
241 
242  string fullstring="";
243  vector<string> ostrings;
244  untokenize(fullstring, istrings, delimiter);
245  cout << fullstring << endl;
246 
247  fullstring += delimiter;
248  cout << fullstring << endl;
249 
250  tokenize(fullstring, ostrings, delimiter);
251  for (uint i=0;i<ostrings.size();i++){
252  cout << ostrings[i] << endl;
253  }
254  exit(0);
255  */
256 
257 
258  /*
259  // Testing FlopC++
260  // For a single file compile as:
261  // -- two passages:
262  // g++ -O3 -I /usr/include/coin -DFLOPCPP_BUILD `PKG_CONFIG_PATH=/usr/lib64/pkgconfig:/usr/lib/pkgconfig:/usr/share/pkgconfig: pkg-config --libs flopcpp osi-cbc osi-clp` transport.cpp -c -o transport.o
263  // g++ -o transport2 transport.o -Wl,-rpath,'$ORIGIN' -L . -DFLOPCPP_BUILD `PKG_CONFIG_PATH=/usr/lib64/pkgconfig:/usr/lib/pkgconfig:/usr/share/pkgconfig: pkg-config --libs flopcpp osi-cbc osi-clp`
264  // -- single passage:
265  // g++ -O3 -I /usr/include/coin transport.cpp -DFLOPCPP_BUILD `PKG_CONFIG_PATH=/usr/lib64/pkgconfig:/usr/lib/pkgconfig:/usr/share/pkgconfig: pkg-config --libs flopcpp osi-cbc osi-clp` -o transport3
266 
267  MP_model::getDefaultModel().setSolver(new OsiClpSolverInterface);
268  //MP_model::getDefaultModel().setSolver(new OsiCbcSolverInterface);
269  enum {seattle, sandiego, numS};
270  enum {newyork, chicago, topeka,numD};
271 
272  MP_set S(numS); // Sources
273  MP_set D(numD); // Destinations
274  MP_subset<2> Link(S,D); // Transportation links (sparse subset of S*D)
275 
276  Link.insert(seattle,newyork);
277  Link.insert(seattle,chicago);
278  Link.insert(sandiego,chicago);
279  Link.insert(sandiego,topeka);
280 
281  MP_data SUPPLY(S);
282  MP_data DEMAND(D);
283 
284  SUPPLY(seattle)=350; SUPPLY(sandiego)=600;
285  DEMAND(newyork)=325; DEMAND(chicago)=300; DEMAND(topeka)=275;
286 
287  MP_data COST(Link);
288 
289  COST(Link(seattle,newyork)) = 2.5;
290  COST(Link(seattle,chicago)) = 1.7;
291  COST(Link(sandiego,chicago))= 1.8;
292  COST(Link(sandiego,topeka)) = 1.4;
293 
294  COST(Link) = 90 * COST(Link) / 1000.0;
295 
296  MP_variable x(Link);
297  x.display("...");
298 
299  MP_constraint supply(S);
300  MP_constraint demand(D);
301 
302  supply.display("...");
303 
304  supply(S) = sum( Link(S,D), x(Link) ) <= SUPPLY(S);
305  demand(D) = sum( Link(S,D), x(Link) ) >= DEMAND(D);
306 
307  cout<<"Here"<<endl;
308 
309  minimize( sum(Link, COST(Link)*x(Link)) );
310  assert(MP_model::getDefaultModel()->getNumRows()==5);
311  assert(MP_model::getDefaultModel()->getNumCols()==4);
312  assert(MP_model::getDefaultModel()->getNumElements()==8);
313  assert(MP_model::getDefaultModel()->getObjValue()>=156.14 && MP_model::getDefaultModel()->getObjValue()<=156.16);
314 
315  x.display("Optimal solution:");
316  supply.display("Supply dual solution");
317  cout<<"Test transport passed."<<endl;
318  */
319 
320 
321 
322  /*
323  // Testing limits for 0
324  double test = DBL_MIN;
325  cout << test << endl;
326  test = numeric_limits<double>::min();
327  cout << test << endl;
328  exit(0);
329  */
330 
331 
332  /*
333  // Testing getMaxPos()
334  vector<double> test {7,2,6,4,7,2,5,7,2};
335  double maxpos = getMaxPos(test);
336  double maxvalue = getMax(test);
337  double minpos = getMinPos(test);
338  double minvalue = getMin(test);
339  //double maxpos = testB();
340  cout << "maxpos: " << maxpos << endl;
341  cout << "maxvalue: " << maxvalue << endl;
342  cout << "minpos: " << minpos << endl;
343  cout << "minvalue: " << minvalue << endl;
344  exit(0);
345  */
346 
347 
348  /*
349  //This was in ModelData::debug():
350  // ********** START DEBUG CODE....... ************
351  double ddebuga=0; //20080209
352  uint idebuga=0;
353  double ddebugb=0; //20080209
354  uint idebugb=0;
355  double ddebugc=0; //20080209
356  uint idebugc=0;
357  double debugmin = 0;
358  double debugmax = 1000;
359  for (uint q=0;q<10000;q++){
360  ddebuga += debugmin + ( (double)rand() / ((double)(RAND_MAX)+(double)(1)) )*(debugmax-debugmin+1);
361  ddebugb += debugmin + ( (double)rand() / ((double)(RAND_MAX)+(double)(1)) )*(debugmax-debugmin+1);
362  ddebugc += debugmin + ( (double)rand() / ((double)(RAND_MAX)+(double)(1)) )*(debugmax-debugmin+1);
363  }
364  idebuga = ddebuga;
365  idebugb = ddebugb;
366  idebugc = ddebugc;
367  cout << "idebuga: "<<idebuga<<endl;
368  cout << "idebugb: "<<idebugb<<endl;
369  cout << "idebugc: "<<idebugc<<endl;
370  throw 2;
371  // ******** .....END DEBUG CODE *******************
372  */
373 
374  /*
375  // Testing the new iskey class
376  iskey op1(2100,"test");
377  iskey op2(2100,"test");
378  iskey op3(2101,"test");
379  iskey op4(2101,"tgst");
380  iskey op5(2101,"tb");
381  iskey op6(2101,"testa");
382  if(op1 == op2){
383  cout << "op1 and op2 are equal" << endl;
384  }
385  if(op1 == op3){
386  cout << "op1 and op3 are equal" << endl;
387  }
388  if(op6 > op3) cout << "test3 passed" << endl;
389  if(op5 < op3) cout << "test4 passed" << endl;
390  if(op6 >= op3) cout << "test5 passed" << endl;
391  if(op6 != op3) cout << "test6 passed" << endl;
392  if(op4 <= op3) cout << "test7 passed that it shoudn't" << endl;
393  exit(0);
394  */
395 
396  /*
397  // Testing the new changeMapValue(), incrMapValue(), resetMapValues(), incrOrAddMapValue(map, key, value) and vectorToMap() funcions
398  map<int,double> testMap;
399  for (uint i=0;i<5;i++){
400  pair<int,double> mypair(i,i*2.5);
401  testMap.insert(mypair);
402  }
403  double result = findMap(testMap,3,MSG_NO_MSG);
404  double result2 = findMap(testMap,1,MSG_ERROR);
405  double result3 = findMap(testMap,7,MSG_DEBUG);
406  cout << findMap(testMap,3,MSG_NO_MSG)<< endl;
407  changeMapValue(testMap,3,200.0,MSG_ERROR);
408  cout << findMap(testMap,3,MSG_NO_MSG)<< endl;
409  incrMapValue(testMap,3,5.0,MSG_ERROR);
410  cout << findMap(testMap,3,MSG_NO_MSG)<< endl;
411  incrOrAddMapValue(testMap, 3, 200.0);
412  cout << findMap(testMap,3,MSG_NO_MSG)<< endl;
413  incrOrAddMapValue(testMap, 10, 100.0);
414  cout << findMap(testMap,10,MSG_NO_MSG)<< endl;
415  cout << "done" << endl;
416 
417  vector<string> mykeys;
418  mykeys.push_back("andrea");
419  mykeys.push_back("antonello");
420  mykeys.push_back("paolo");
421  map<string,double> mymap = vectorToMap(mykeys,15.0);
422  string searchkey;
423  searchkey = "andrea";
424  cout << findMap(mymap,searchkey,MSG_DEBUG)<< endl;
425  resetMapValues(mymap,32.0);
426  cout << findMap(mymap,searchkey,MSG_DEBUG)<< endl;
427  exit(0);
428  */
429 
430 
431 
432  /*
433  // -------------------------------------------------------------------
434  // Sampling from uniform distribution with local random seed
435  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
436 
437  //this code sample from a uniform distribution. In this case also the seed is reinitialisated, but it it valid only locally: the rest of the program run with the same seed
438 
439  std::random_device rd;
440  std::mt19937 gen(rd());
441  std::uniform_int_distribution<> dis(1, 6);
442 
443  for (int n=0; n<10; ++n)
444  std::cout << dis(gen) << ' ';
445  std::cout << '\n';
446  exit(0);
447  */
448 
449 
450 
451  /*
452  // -------------------------------------------------------------------
453  // Testing how to get all elements in a map by substrings
454  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
455  map <string,double> values;
456  pair <string,double> val1("AAAAAA",1);
457  pair <string,double> val2("AAABBB",2);
458  pair <string,double> val3("BBBAAA",3);
459  pair <string,double> val4("BBBBBB",4);
460  pair <string,double> val5("CCCAAA",5);
461  pair <string,double> val6("C",6);
462  pair <string,double> val7("BBB",7);
463 
464  values.insert(val1);
465  values.insert(val2);
466  values.insert(val3);
467  values.insert(val4);
468  values.insert(val5);
469  values.insert(val6);
470  values.insert(val7);
471 
472  cout << "Printing whole map" << endl;
473  for (std::map<string,double>::iterator it=values.begin(); it!=values.end(); ++it)
474  std::cout << it->first << " => " << it->second << '\n';
475 
476  string search_for = "BBB";
477 
478  cout << "Using lower bound " << endl;
479  for (std::map<string,double>::iterator it=values.lower_bound(search_for); it!=values.end(); ++it)
480  std::cout << it->first << " => " << it->second << '\n';
481  cout << "Using upper bound " << endl;
482  for (std::map<string,double>::iterator it=values.upper_bound(search_for); it!=values.end(); ++it)
483  std::cout << it->first << " => " << it->second << '\n';
484 
485  cout << "Printing only substrings " << endl;
486  for (std::map<string,double>::iterator it=values.lower_bound(search_for); it!=values.end(); ++it){
487  string key = it->first;
488  if (key.compare(0, search_for.size(), search_for) == 0){
489  std::cout << it->first << " => " << it->second << '\n';
490  }
491  }
492 
493 
494  exit(0);
495  */
496 
497  /*
498  // testing findMap
499  map<int,double> testMap;
500  for (uint i=0;i<5;i++){
501  pair<int,double> mypair(i,i*2.5);
502  testMap.insert(mypair);
503  }
504  double result = findMap(testMap,3,MSG_NO_MSG);
505  double result2 = findMap(testMap,1,MSG_ERROR);
506  double result3 = findMap(testMap,7,MSG_DEBUG);
507  cout << "Done" << endl;
508  map<int, vector <double> > testMap2;
509  for (uint i=0;i<5;i++){
510  vector <double> myvector;
511  for(uint j=0;j<10;j++) {
512  myvector.push_back(i*100+j);
513  }
514  pair<int,vector <double> > mypair2(i,myvector);
515  testMap2.insert(mypair2);
516  }
517  vector <double> resultb = findMap(testMap2,3,MSG_NO_MSG);
518  vector <double> resultb2 = findMap(testMap2,1,MSG_ERROR);
519  vector <double> resultb3 = findMap(testMap2,7);
520  cout << "Done2" << endl;
521  exit(1);
522  */
523 
524 
525 
526  /*
527  // Testing vSum
528  vector <int> ivector(5,5);
529  vector <double> dvector(5,1.5);
530  vector < vector <int> > ivector2;
531  vector <vector <double > > dvector2;
532 
533 
534  for(uint i=0;i<5;i++){
535  ivector2.push_back(ivector);
536  dvector2.push_back(dvector);
537  }
538 
539  int iSum = vSum(ivector);
540  int iSum2 = vSum(ivector2[2]);
541  double dSum = vSum(dvector);
542  double dSum2 = vSum(dvector2[1]);
543  int iSum3 = vSum(ivector2);
544  double dSum3 = vSum(dvector2);
545 
546  cout << "hi there" << endl;
547  */
548 
549  /*
550  // Testing Eigen
551  using Eigen::MatrixXd;
552  MatrixXd m(2,2);
553  m(0,0) = 4;
554  m(1,0) = 2.5;
555  m(0,1) = -1;
556  m(1,1) = m(1,0) + m(0,1);
557  std::cout << m << std::endl;
558  exit(0);
559  */
560 
561  /*
562  // Test on two different type of partial matching over map values
563  testPartMatching2();
564  testPartMatching();
565  */
566 
567  /*
568  // -------------------------------------------------------------------
569  // Testing how to erase elements from a vector according to conditions
570  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
571 
572  vector<string> myvector;
573  myvector.push_back("a");
574  myvector.push_back("b");
575  myvector.push_back("c");
576  myvector.push_back("d");
577  myvector.push_back("e");
578 
579  for (uint i=0; i<myvector.size();i++){
580  cout << "i:" << i << " myvector[i]: " << myvector[i] << endl;
581  if(myvector[i]== "c" || myvector[i]=="d"){
582  cout << " -- TBR: " << "i:" << i << " myvector[i]: " << myvector[i] << endl;
583  myvector.erase (myvector.begin()+i);
584  i--;
585  }
586  }
587 
588  cout << "Myvector now contains:" << endl;
589  for (int i=0; i<myvector.size(); i++) {
590  cout << "i: " << i << " myvector[i]: " << myvector[i] << endl;
591  }
592  exit (0);
593  */
594 
595 
596 }
597 
598 void
600 
601  /*
602  // Testing adfditional Mortality..
603  Pixel* px = MTHREAD->GIS->getPixel(8967);
604  double myAddMortality = px->getSTData("additionalMortality", "", 2013, "", 0.0);
605  cout << "Additional mortality: " << myAddMortality <<endl;
606  exit(0);
607  */
608 
609 
610  /*
611  // checking that the overriding works also for secondary dimension
612  // yep, it works!! ;-)
613  //ct 11003 beechRoundW 11001 69.71 100
614  double ct_11003_11002_beech = MTHREAD->MD->getProdData("ct",11003,"beechRoundW",2013,"11002");
615  double ct_11003_11001_beech = MTHREAD->MD->getProdData("ct",11003,"beechRoundW",2013,"11001");
616  double ct_11003_11002_hardW = MTHREAD->MD->getProdData("ct",11003,"hardWRoundW",2013,"11002");
617  double ct_11003_11001_hardW = MTHREAD->MD->getProdData("ct",11003,"hardWRoundW",2013,"11001");
618  exit(1);
619  */
620 
621  /*
622  // getting x,y of the pixel with sawmill in the TIGA project
623  Pixel* px = MTHREAD->GIS->getPixel(8967);
624  int x = px->getX();
625  int y = px->getY();
626  cout << "(X,Y) of pixel 8967: " << x << "\t" << y << endl;
627  px = MTHREAD->GIS->getPixel(0);
628  x = px->getX();
629  y = px->getY();
630  cout << "(X,Y) of pixel 0: " << x << "\t" << y <<endl;
631  exit(1);
632  */
633 
634  /*
635  // Testing overriding of just a product/region if it works
636  // Perfect.. the override works even just setting one specific ft/region value !! :-)
637  double ad = MTHREAD->MD->getProdData("additionalDemand",11001,"beechSawnW",2020); // this should be 0.004
638  double adt = MTHREAD->MD->getProdData("additionalDemand",11001,"beechSawnW",2022);
639  double adr = MTHREAD->MD->getProdData("additionalDemand",11002,"beechSawnW",2020);
640  double adp = MTHREAD->MD->getProdData("additionalDemand",11001,"hardWSawnW",2020);
641  cout << "additional demand test:" << endl;
642  cout << ad << "\t" << adt << "\t" << adr << "\t" << adp << endl;
643 
644  // Testing (again, for the TIGA project) the availalibily coefficient of Hetre
645  Pixel* px = MTHREAD->GIS->getPixel(32,30);
646  double ac = px->getSTData("avalCoef","Fut_Hetre",2013);
647  double act1 = px->getSTData("avalCoef","Fut_Hetre",2010);
648  double act2 = px->getSTData("avalCoef","Fut_Hetre",2014);
649  double act3 = px->getSTData("avalCoef","Fut_Hetre",2020);
650  double acft = px->getSTData("avalCoef","Fut_Feu",2013);
651  cout << "availability coefficient test:" << endl;
652  cout << ac << "\t" << act1 << "\t" << act2 << "\t" << act3 << "\t" << acft << endl;
653  exit(1);
654  */
655 
656  /*
657  // Testing picking up a national level variable defined only at national level. This may raise a segfault ?
658  //double debug = MTHREAD->MD->getProdData("carbonPrice",11000,"",2007);
659  cout << debug << endl;
660  exit(1);
661  */
662 
663  /*
664  // Testing getTableFromFile() function
665  LLData debug = MTHREAD->MD->getTableFromFile("debug", "scenarios/debug.csv");
666  exit(1);
667  */
668 
669 
670  /*
671  // Testing regional overriding in the new getBaseData() function
672  double mr11061 = MTHREAD->MD->getDoubleSetting("mr",DATA_NOW,11061);
673  double mr11042 = MTHREAD->MD->getDoubleSetting("mr",DATA_NOW,11042);
674  double mr = MTHREAD->MD->getDoubleSetting("mr",DATA_NOW);
675  // this should give critical error:
676  double mr5 = MTHREAD->MD->getDoubleSetting("mr",DATA_NOW,5);
677  exit(1);
678  */
679 
680  /*
681  // Testing the new getSTData() function
682  cout << "Starting test" << endl;
683  Pixel* px = MTHREAD->GIS->getPixel(32,30);
684  cout << "got pixel" << endl;
685  //double avalCoef = px->getMultiplier("avalCoefTest", "con_highF", 2007);
686  double avalCoef = px->getSTData("avalCoef","broadL_highF",2008);
687  cout << avalCoef << endl;
688  exit(1);
689  */
690 
691 
692  /*
693  // Testing shareMortalityUsableTimber call for a specific forest type and at the opposite with a null forest type
694  // to see if gfd() expands as expected
695 
696  double test1 = MTHREAD->MD->getForData("shareMortalityUsableTimber",11041,"","",2016);
697  cout << "test1: " << test1 << endl;
698  exit(1);
699  */
700 
701  /*
702  //testing supply_fromForestShare
703  double test = MTHREAD->MD->getProdData("supply_fromForestShare",11042,"hardWRoundW",2006);
704  cout << test << endl;
705  exit(1);
706  */
707 
708  /*
709  // Testing getSiblings() ok
710  ModelRegion* reg1 = MTHREAD->MD->getRegion(11042);
711  vector <ModelRegion*> siblings = reg1->getSiblings();
712  for(uint i=0;i<siblings.size();i++){
713  cout << siblings[i]->getRegId() << endl;
714  }
715  exit(1);
716  */
717 
718  /*
719  // Testing if after not found gpd(.) or gfd(.) we always have a negative tempBool.. yes
720  bool debug = true;
721  MTHREAD->MD->setTempBool(true);
722  double test1 = MTHREAD->MD->getForData("vol",11042,"broadL_highF","15");
723  debug = MTHREAD->MD->getTempBool();
724  double test2 = MTHREAD->MD->getForData("fgdfgdfhdfhdfhuk",11042,"broadL_highF","");
725  debug = MTHREAD->MD->getTempBool();
726  debug = true;
727  exit(1);
728  */
729 
730  /*
731  // Testing if asking a value from freeDim when this is not efined what returns
732  // no, if the freeDim is either null or 11000 I can't ask for e.g. 11042, I didn't developed expansion in the freeDim
733  double debug = MTHREAD->MD->getProdData("pol_ctSub",11042,"fuelW",DATA_NOW);
734  cout << debug << endl;
735  exit(1);
736  */
737 
738  /*
739  // Getting forest area by each forest type
740  vector<int> regIds2 = MTHREAD->MD->getRegionIds(2);
741  for(uint r=0;r<regIds2.size();r++){
742  int rId = regIds2[r];
743  ModelRegion* reg = MTHREAD->MD->getRegion(regIds2[r]);
744  vector <string> fTypes= MTHREAD->MD->getForTypeIds();
745  for(uint f=0;f<fTypes.size();f++){
746  string ft = fTypes[f];
747  forType* FT = MTHREAD->MD->getForType(ft);
748  double totalArea = 0.0;
749  vector <Pixel*> rpx = MTHREAD->GIS->getAllPlotsByRegion(regIds2[r]);
750  for (uint p=0;p<rpx.size();p++){
751  Pixel* px = rpx[p];
752  totalArea += px->getDoubleValue (FT->forLayer, true);
753  }
754  cout << rId << "\t" << ft << "\t" << totalArea << endl;
755  }
756  }
757  exit(1);
758  */
759 
760  /*
761  // Testing the new getForTypeParents()function
762  vector<string> parents = MTHREAD->MD->getForTypeParents();
763  for(uint i=0;i<parents.size();i++){
764  vector <string> childIds = MTHREAD->MD->getForTypeChilds(parents[i]);
765  vector <int> childPos = MTHREAD->MD->getForTypeChilds_pos(parents[i]);
766  double debug = 0.0;
767  }
768  */
769 
770  /*
771  // Testing the reg->getArea() functions
772  // Actually this need to be run further later, as pixels doesn't yet have area information
773  vector <string> dClasses = MTHREAD->MD->getStringVectorSetting("dClasses");
774  vector <string> fTypes= MTHREAD->MD->getForTypeIds();
775  ModelRegion* REG = MTHREAD->MD->getRegion(11041);
776  cout << "Total ft area: "<< REG->getArea()<< endl;
777 
778  for(uint j=0;j<fTypes.size();j++){
779  cout << fTypes[j] << "\t" << REG->getArea(fTypes[j]) << "\t" << REG->getArea(j) << endl;
780  }
781  for(uint j=0;j<fTypes.size();j++){
782  cout << fTypes[j] << "\t" << REG->getArea(fTypes[j]) << "\t";
783  for(uint u=0;u<dClasses.size();u++){
784  cout << REG->getArea(j,u) << " ";
785  }
786  cout << endl;
787  }
788  */
789 
790  /*
791  // Testing getForData() function with no forest id specified
792  double vartest= MTHREAD->MD->getForData("forestChangeAreaIncrementsRel",11061,"","",2009);
793  cout << vartest << endl;
794  exit(0);
795  */
796 
797 
798  /*
799  // Testing the decay model - ok, passed
800  double initialValue = 100;
801  double halfLife = 2;
802  double years = 0;
803  double remStock = MTHREAD->CBAL->getRemainingStock(initialValue, halfLife, years); ///< Apply a single exponential decay model to retrieve the remining stock given the initial stock, the half life and the time passed from stock formation.
804  cout << "Remaining stock: " << remStock << endl;
805  years = 1;
806  remStock = MTHREAD->CBAL->getRemainingStock(initialValue, halfLife, years);
807  cout << "Remaining stock: " << remStock << endl;
808  years = 5;
809  remStock = MTHREAD->CBAL->getRemainingStock(initialValue, halfLife, years);
810  cout << "Remaining stock: " << remStock << endl;
811  years =10;
812  remStock = MTHREAD->CBAL->getRemainingStock(initialValue, halfLife, years);
813  cout << "Remaining stock: " << remStock << endl;
814  years = 200;
815  remStock = MTHREAD->CBAL->getRemainingStock(initialValue, halfLife, years);
816  cout << "Remaining stock: " << remStock << endl;
817  */
818 
819  /*
820  // Testing normSample
821  // template <typename K> K normSample (const K& avg, const K& stdev, const K& minval=NULL, const K& maxval=NULL)
822  // template <typename K> K normSample (const normal_distribution<K>& d, const std::mt19937& gen, const K& minval=NULL, const K& maxval=NULL)
823  double avg = 0.8;
824  double stdev = 0.2;
825  double minval = 0.0;
826  double maxval = 1.0;
827  double result;
828 
829  cout << "Starting first method.." << endl;
830  normal_distribution<double> d(avg,stdev);
831  std::mt19937 gen = *MTHREAD->gen;
832  for (uint i=0;i<1000;i++){
833  result = normSample(d, gen, minval, maxval);
834  cout << "Result1: " << result << endl;
835  }
836  cout << "Finished first method and starting second one.." << endl;
837  for (uint i=0;i<1000;i++){
838  result = normSample(avg, stdev, minval, maxval);
839  cout << "Result2: " << result << endl;
840  }
841  cout << "Finished second method."<< endl;
842 
843  exit(0);
844  */
845 
846 
847  //double disttest = MTHREAD->MD->getProdData("dist",11042,"",DATA_NOW,i2s(11061));
848  //cout << disttest << endl;
849  //exit(0);
850 
851 
852  /*double test = MTHREAD->CBAL->getStock(11061, STOCK_INV);
853  //STOCK_INV -> from inventary source and death trees
854  //STOCK_EXTRA -> from inventary source and death trees
855  //STOCK_PRODUCTS -> from products
856  cout << "DONE" << endl;
857  exit(0);
858  */
859 
860  /*
861  // Testing if forestData can uses other arbitrary elements in the diameter field in order to generalise it
862  double test = MTHREAD->MD->getForData("covar",11082,"con_highF","con_highF");
863  MTHREAD->MD->setForData(0.1,"covar",11082,"con_highF","con_highF");
864  MTHREAD->MD->setForData(0.1,"covar",11061,"con_highF","con_highF",DATA_NOW,true);
865  test = MTHREAD->MD->getForData("covar",11082,"con_highF","con_highF");
866  test = MTHREAD->MD->getForData("covar",11061,"con_highF","con_highF");
867  test = MTHREAD->MD->getForData("covar",11082,"con_highF","");
868  cout << test << endl;
869  exit(0);
870  */
871 
872  /*
873  // Testing getProdData for the freeDimension
874  MTHREAD->MD->setProdData(0.4,"rt",11041,"hardWSawnW",DATA_NOW,true,"11061");
875  MTHREAD->MD->setProdData(0.3,"rt",11041,"hardWSawnW",DATA_NOW,true,"11030");
876  MTHREAD->MD->setProdData(0.2,"rt",11041,"hardWSawnX",DATA_NOW,true,"11030");
877  double debug = MTHREAD->MD->getProdData("rt",11041,"hardWSawnW",DATA_NOW,"11061");
878  double debug2 = MTHREAD->MD->getProdData("rt",11041,"hardWSawnW",DATA_NOW);
879  cout << debug << " " << debug2 << endl;
880  exit(0);
881  */
882 
883  /*
884  // Testing api to call generic forest type data, parent/child
885  cout << "Hello world " << endl;
886  cout << MTHREAD->MD->getForData("freq_norm",11041,"broadL","",2040) << endl;
887  MTHREAD->MD->setForData(100,"freq_norm",11041,"broadL","",2040);
888  cout << MTHREAD->MD->getForData("freq_norm",11041,"broadL","",2040) << endl;
889  cout << MTHREAD->MD->getForTypeParentId("broadL_highF")<< endl;
890  cout << MTHREAD->MD->getForTypeParentId("con_highF")<< endl;
891  exit(0);
892  */
893 
894  /*
895  // Testing for each region how far is the average of the multipliers from 1
896  vector<int> regIds = MTHREAD->MD->getRegionIds(2);
897  vector <string> ftypes = MTHREAD->MD->getForTypeIds();
898 
899  cout << "*** Checking how far is the tpMultiplier far from 1 in each region:" << endl;
900  for (int i=0;i< regIds.size();i++){
901  ModelRegion* region = MTHREAD->MD->getRegion(regIds[i]);
902  vector <Pixel*> regpixels = MTHREAD->GIS->getAllPlotsByRegion(regIds[i]);
903  if(regpixels.size()==0) continue;
904  cout << "*** " << region->getRegLName() << ": "<< endl;
905  for(int ft = 0;ft<ftypes.size();ft++){
906  double tot = 0;
907  double avg = 0;
908  for(int j=0;j<regpixels.size();j++){
909  tot += regpixels[j]->getSpModifier(ftypes[ft]);
910  }
911  avg = tot/regpixels.size();
912  cout << ftypes[ft] << ": " << avg << endl;
913  }
914  }
915  exit(0);
916  */
917 
918  /*
919  // Testing the number of plots in the model
920  vector <ModelRegion*> regions = MTHREAD->MD->getAllRegions();
921  int total = 0;
922  cout << "*** Pixels by region:" << endl;
923  for (int i=0;i< regions.size();i++){
924  vector <Pixel*> regpixels = MTHREAD->GIS->getAllPlotsByRegion(*regions[i]);
925  cout << regions[i]->getRegLName() << ": " << regpixels.size() << endl;
926  total += regpixels.size() ;
927  }
928  cout << "** Total: " << total << endl;
929  exit(0);
930  */
931 
932  /*
933  // Testing the new random distributions. Requires the pointer MTHREAD->gen to be initialised,
934  // so this test can't run in basic test.
935  std::normal_distribution<double> d(100000,3); // default any how to double
936  for(int n=0; n<20; ++n) {
937  double x = d(*MTHREAD->gen);
938  int i = round(d(*MTHREAD->gen));
939  cout << i << ';' << 1 << endl;
940  }
941  exit (0);
942  */
943 
944  /*
945  // Testing I have correctly the info about world price !!!
946  // yes, it seems ok here !!!
947  int firstYear = MTHREAD->MD->getIntSetting("initialYear");
948  int initialOptYear= MTHREAD->MD->getIntSetting("initialOptYear");
949  int simulationYears = MTHREAD->MD->getIntSetting("simulationYears");
950  int WL2 = MTHREAD->MD->getIntSetting("worldCodeLev2");
951  vector <string> priProducts = MTHREAD->MD->getStringVectorSetting("priProducts");
952  vector <string> secProducts = MTHREAD->MD->getStringVectorSetting("secProducts");
953  vector <string> allProducts = priProducts;
954  allProducts.insert( allProducts.end(), secProducts.begin(), secProducts.end() );
955 
956  for(uint i=0;i<allProducts.size();i++){
957  for(int y=firstYear; y<initialOptYear+simulationYears; y++){
958  double pw = MTHREAD->MD->getProdData("pl",WL2,allProducts[i],y);
959  cout << allProducts[i] << " " << y << " " << pw << endl;
960  }
961  }
962  exit (0);
963  */
964 
965  /*
966  // testing Pixel::getMultiplier (const string& multiplierName, const string& forName, int year)
967  Pixel* px = MTHREAD->GIS->getPixel(0);
968  double debug1 = px->getMultiplier("tp_multiplier","broadL_highF",2012);
969  double debug2 = px->getMultiplier("tp_multiplier","broadL_highF",2008);
970  double debug3 = px->getMultiplier("tp_multiplier","broadL_highF",2009);
971  double debug4 = px->getMultiplier("tp_multiplier","broadL_highF",2010);
972  double debug5 = px->getMultiplier("mortCoeff_multiplier","broadL_highF",2012);
973  double debug6 = px->getMultiplier("mortCoeff_multiplier","con_copp",2012);
974  double debug7 = px->getMultiplier("blaaaa","broadL_highF",2012);
975 
976  double debug10 = debug1;
977 */
978 
979  /*
980  // testing reading a directory
981  string dir = MTHREAD->MD->getBaseDirectory()+MTHREAD->MD->getStringSetting("spatialDataSubfolder");
982  vector<string> files = vector<string>();
983 
984  MTHREAD->MD->getFilenamesByDir (dir,files, ".grd");
985 
986  for (unsigned int i = 0;i < files.size();i++) {
987  cout << files[i] << endl;
988  }
989  */
990 
991  /*
992  // testing ModelData:: ModelData::calculateAnnualisedEquivalent(double amount_h, int years_h)
993  cout << "Value:" << MTHREAD->MD->calculateAnnualisedEquivalent(500.,4) << endl;
994  cout << "Value:" << MTHREAD->MD->calculateAnnualisedEquivalent(500.,30) << endl;
995  cout << "Value:" << MTHREAD->MD->calculateAnnualisedEquivalent(107.035040105,10) << endl;
996  cout << "Value:" << MTHREAD->MD->calculateAnnualisedEquivalent(8.91507,1) << endl;
997  cout << "Done" << endl;
998  exit(0);
999  */
1000 
1001  /*
1002  // testing snprintf
1003  vector <int> myintegers;
1004  vector <double> mydoubles;
1005  char szResult[24];
1006 
1007  myintegers.push_back(1);
1008  myintegers.push_back(202);
1009  myintegers.push_back(3003);
1010  myintegers.push_back(400004);
1011  myintegers.push_back(50000005);
1012  myintegers.push_back(6000000006);
1013  mydoubles.push_back(1.1234567890);
1014  mydoubles.push_back(12345678.9);
1015  mydoubles.push_back(12345678.90123456);
1016  mydoubles.push_back(6000000006.123456789012);
1017  for(uint i=0;i<myintegers.size();i++){
1018  snprintf ( szResult, sizeof(szResult), "%d", myintegers[i] ); // "safe" version
1019  cout << "int/string: " << myintegers[i] << " / " << szResult << endl;
1020  }
1021  for(uint i=0;i<mydoubles.size();i++){
1022  snprintf ( szResult, sizeof(szResult), "%f", mydoubles[i] ); // "safe" version
1023  cout << "double/string: " << mydoubles[i] << " / " << szResult << endl;
1024  }
1025  exit(0);
1026  */
1027 
1028  /*
1029  // testing stod() ..
1030  // this is giving different results if gui or console mode !!
1031  vector<string> numbers;
1032  numbers.push_back("123.1234567890");
1033  numbers.push_back("123.1234");
1034  numbers.push_back("123,1234567890");
1035  numbers.push_back("123,1234");
1036  double outd;
1037  for(uint i=0;i<numbers.size();i++){
1038  try {
1039  outd = stod(numbers[i]);
1040  cout << "Conversion passed: " << numbers[i] << " - " << outd << endl;
1041  } catch (...) {
1042  cout << "Conversion DID NOT passed: " << numbers[i] << " - " <<endl;
1043  }
1044  }
1045  exit(0);
1046  */
1047 
1048 /*
1049 // ------------------------------------
1050 // Testing makeKeyProdData() and unpackKeyProdData()
1051  string parName = "za";
1052  int regId = 20000;
1053  string prod = "wood";
1054  string freeDim = "";
1055  string key = MTHREAD->MD->makeKeyProdData(parName,i2s(regId),prod,freeDim);
1056  cout << "key: " << key << endl;
1057  MTHREAD->MD->unpackKeyProdData(key,parName,regId,prod,freeDim);
1058  cout << "parName: " << parName << endl;
1059  cout << "regId: " << regId << endl;
1060  cout << "prod: " << prod << endl;
1061  cout << "freeDim: " << freeDim << endl;
1062  exit(0);
1063 */
1064 
1065 /*
1066 // ---------------------------------------------
1067 // checking the functions dataMapCheckExist() and dataMapGetValue() works well
1068 typedef map<string, vector <double> > DataMap;
1069 typedef pair<string, vector <double> > DataPair;
1070 
1071 vector <double> abaa (5, 1.);
1072 vector <double> abcc (5,10);
1073 vector <double> anbb (5,100);
1074 vector <double> andd (5,5);
1075 vector <double> anff (5,3);
1076 vector <double> ag (5,2);
1077 vector <double> agii (5,7);
1078 
1079 
1080 
1081 DataMap dM;
1082 dM.insert(DataPair("abaa", abaa));
1083 dM.insert(DataPair("abcc", abcc));
1084 dM.insert(DataPair("anbb", anbb));
1085 dM.insert(DataPair("andd", andd));
1086 dM.insert(DataPair("anff", anff));
1087 dM.insert(DataPair("ag", ag));
1088 dM.insert(DataPair("agii", agii));
1089 
1090 vector<string> tests;
1091 tests.push_back("ab");
1092 tests.push_back("anbb");
1093 tests.push_back("ane");
1094 tests.push_back("an");
1095 tests.push_back("ac");
1096 tests.push_back("ag");
1097 tests.push_back("agii");
1098 tests.push_back("al");
1099 
1100 
1101 bool found;
1102 double value;
1103 
1104 for(uint i=0;i<tests.size();i++){
1105  found = MTHREAD->MD->dataMapCheckExist(dM, tests[i]);
1106  value = MTHREAD->MD->dataMapGetValue(dM, tests[i],2010);
1107  cout << tests[i] << ": " << b2s(found) << " value: "<< value << endl;
1108 }
1109 
1110 exit(0);
1111 */
1112 
1113 
1114  /*
1115  // testing how to search on a vector using the find algorithm
1116 
1117  vector<string> names;
1118  names.push_back("pippo");
1119  names.push_back("topolino");
1120  names.push_back("minni");
1121  names.push_back("paperino");
1122 
1123  string toSearch1 = "minni";
1124  string toSearch2 = "zio paperone";
1125 
1126  if( find(names.begin(), names.end(), toSearch1)!= names.end() ){
1127  cout << "minni trovata" << endl;
1128  }
1129  if( find(names.begin(), names.end(), toSearch2)!= names.end() ){
1130  cout << "zio paperone trovato" << endl;
1131  }
1132  cout << "test on find ended." << endl;
1133  exit(0);
1134  */
1135 
1136 // ------------------------------------------------------------------
1137 
1138 
1139  /*
1140  int a;
1141  a = getSetting<int>("myIntData", TYPE_INT);
1142 
1143  string b;
1144  b = getSetting<string>("myStringData", TYPE_STRING);
1145 
1146  bool c;
1147  c = getSetting<bool>("myBoolData", TYPE_BOOL);
1148 
1149 
1150  cout << "A is: " << a << endl;
1151 
1152  cout << "B is: " << b << endl;
1153 
1154  cout << "C is: " << c << endl;
1155 
1156  //vector<string> getVectorSetting <string> ("test", TYPE_STRING);
1157  //template <class T> vector <T> getVectorSetting(string name_h, int type);
1158 
1159  //vector <string> myStrings = getVectorSetting <vector<string> > ("test", TYPE_STRING);
1160 
1161  string s = GccTest("test");
1162  int i = GccTest("test");
1163  vector <int> iVector = GccTest("test");
1164 
1165  for (int i=0; i< iVector.size(); i++){
1166  cout << "iVector: " << iVector.at(i) << endl;
1167  }
1168  */
1169 
1170  // ------------------------------------------------------------------
1171 
1172  /* // I learned: how to access elements - both objects and pointers - of a vector using pointers
1173  // testing how to operate with iterators over a pointer element in an array:
1174 
1175  cout << "Starting iterator test..." << endl;
1176 
1177  TestStructure a,b,c,d;
1178  a.i=0; b.i=1; c.i=2; d.i=3;
1179  TestStructure* ap;
1180  TestStructure* bp;
1181  TestStructure* cp;
1182  TestStructure* dp;
1183 
1184  ap = &a;
1185  bp = &b;
1186  cp = &c;
1187  dp = &d;
1188 
1189  vector <TestStructure> objects;
1190  vector <TestStructure*> pointers;
1191 
1192  objects.push_back(a);
1193  objects.push_back(b);
1194  objects.push_back(c);
1195  objects.push_back(d);
1196 
1197  pointers.push_back(ap);
1198  pointers.push_back(bp);
1199  pointers.push_back(cp);
1200  pointers.push_back(dp);
1201 
1202  vector<TestStructure>::iterator pi;
1203  vector<TestStructure*>::iterator pp;
1204 
1205  //ok it works
1206  //for ( pi = objects.begin() ; pi != objects.end();){
1207  // if(pi->i==2){
1208  // objects.erase(pi);
1209  // }
1210  // else {
1211  // ++pi;
1212  // }
1213  //}
1214 
1215  //for (int j=0;j<objects.size();j++){
1216  // cout << j << " object is: " << objects[j].i << endl;
1217  //}
1218 
1219 
1220  // works as well ;-))
1221  for ( pp = pointers.begin() ; pp != pointers.end();){
1222  if( (*pp)->i==2){
1223  //delete (*pp);
1224  pointers.erase(pp);
1225  }
1226  else {
1227  ++pp;
1228  }
1229  }
1230 
1231  for (int j=0;j<pointers.size();j++){
1232  cout << j << " pointers is: " << pointers[j]->i << endl;
1233  }
1234 
1235  // c is not destructed if we don't explicitelly call delete over the pointer...
1236  cout << c.i << endl; // this go in seg-frag if we call delete (*pp)..
1237  */
1238 
1239  // ------------------------------------------------------------------
1240  /* test on how to remove from a map.. deletable
1241  map <int, string> test;
1242  test.insert(pair<int, string>(2, "pippo"));
1243  test.insert(pair<int, string>(1, "pluto"));
1244  test.insert(pair<int, string>(5, "minni"));
1245  test.insert(pair<int, string>(3, "topolino"));
1246 
1247 
1248  map <int, string>::iterator p;
1249  p=test.find(3);
1250  if(p != test.end()){
1251  cout << p->second <<endl;
1252  test.erase(p);
1253  }
1254  else {
1255  cout << "not found " << endl;
1256  }
1257 
1258  map <int, string>::iterator p2;
1259  p2=test.find(3);
1260  if(p2 != test.end()){
1261  cout << p2->second <<endl;
1262  test.erase(p2);
1263  }
1264  else {
1265  cout << "not found " << endl;
1266  }
1267  */
1268 
1269  /*vector<int> test;
1270  for (int i=0;i<5;i++) test.push_back(i);
1271  cout << "test.." << endl;
1272  for (uint i=0;i<test.size();i++){
1273  cout << "Test "<<i<<": "<<test.at(i) << endl;
1274  }
1275  //test.erase(2);
1276 
1277  vector<int>::iterator p;
1278  for ( p = test.begin() ; p != test.end();){
1279  if(*p == 1 || *p == 2 || *p==4){
1280  test.erase(p);
1281  }
1282  else {
1283  ++p;
1284  }
1285  }
1286 
1287 
1288  for (uint i=0;i<test.size();i++){
1289  cout << "Test "<<i<<": "<<test.at(i) << endl;
1290  }
1291 
1292 // test.erase(remove_if(test.begin(), test.end(), FindMatchingString(&fs))
1293 
1294 // for (int i=0;i<test.size();i++) cout << "TEST: "<<i<< " " << test.at(i) << endl;
1295  */
1296 
1297  /*
1298  // On this test I am showing how to "move" one pointer from a vector of pointers to an other one. The real case is used to move Agent_farmer* pointers from the managedAgents vector to the removedVector.
1299 
1300  double* myDouble1 = new double(1);
1301  double* myDouble2 = new double(2);
1302  double* myDouble3 = new double(3);
1303 
1304  vector <double*> origin;
1305  vector <double*> destination;
1306 
1307  origin.push_back(myDouble1);
1308  origin.push_back(myDouble2);
1309  origin.push_back(myDouble3);
1310 
1311  cout << "MyDouble2: "<< *myDouble2 << endl;
1312  vector<double*>::iterator doublePIterator;
1313 
1314  for (int i=0;i<origin.size();i++){
1315  cout << i << " origin is: " << *origin[i] << endl;
1316  }
1317 
1318  for ( doublePIterator = origin.begin() ; doublePIterator !=origin.end();){
1319  if(*doublePIterator == myDouble2){
1320  destination.push_back(myDouble2);
1321  origin.erase(doublePIterator);
1322  }
1323  else {
1324  ++doublePIterator;
1325  }
1326  }
1327 
1328  for (int i=0;i<origin.size();i++){
1329  cout << i << " origin is now: " << *origin[i] << endl;
1330  }
1331 
1332  for (int i=0;i<destination.size();i++){
1333  cout << i << " destination is: " << *destination[i] << endl;
1334  } */
1335 
1336  // ------------------------------------------------------------------
1337  /*
1338  // Test on how to return a vector of pointers from a member vector of data
1339  TestStructure a,b,c,d;
1340  a.i=0; b.i=1; c.i=2; d.i=3;
1341  testVector.push_back(a);
1342  testVector.push_back(b);
1343  testVector.push_back(c);
1344  testVector.push_back(d);
1345 
1346  vector<TestStructure*> myVector=getTestStructure();
1347 
1348  for(uint i=0;i<myVector.size();i++){
1349  msgOut(MSG_DEBUG, i2s(myVector[i]->i));
1350  }
1351  */
1352 
1353  /*
1354  // Deleting an object and inserting a new one on a vector of objects.. it doesn't works.. problems with the last element..
1355  vector<BasicData>::iterator p;
1356  for(p=programSettingsVector.begin();p!=programSettingsVector.end();p++){
1357  if(p->name == SETT.name){
1358  programSettingsVector.erase(p);
1359  programSettingsVector.insert(p,1,SETT);
1360  cout << SETT.name <<endl;
1361  break;
1362  }
1363  }
1364  */
1365 
1366  /*double test = -987654321.987654321;
1367  double result;
1368  result = fabs(test);
1369  cout << "Test: "<< result << endl;*/
1370 
1371 
1372  /*
1373  // Testing the zip library:
1374 
1375  cout <<"Hello world Zip!" << endl;
1376 
1377  QString file = "data/testInput.ods";
1378  QString out = "data/tempInput";
1379  QString pwd = "";
1380  if (!QFile::exists(file))
1381  {
1382  cout << "File does not exist." << endl << endl;
1383  //return false;
1384  }
1385 
1386  UnZip::ErrorCode ec;
1387  UnZip uz;
1388 
1389  if (!pwd.isEmpty())
1390  uz.setPassword(pwd);
1391 
1392  ec = uz.openArchive(file);
1393  if (ec != UnZip::Ok)
1394  {
1395  //cout << "Failed to open archive: " << uz.formatError(ec).toAscii().data() << endl << endl; // Qt4
1396  cout << "Failed to open archive: " << uz.formatError(ec).toLatin1().data() << endl << endl; // Qt5
1397  //return false;
1398  }
1399 
1400  ec = uz.extractAll(out);
1401  if (ec != UnZip::Ok)
1402  {
1403  //cout << "Extraction failed: " << uz.formatError(ec).toAscii().data() << endl << endl; // Qt4
1404  cout << "Extraction failed: " << uz.formatError(ec).toLatin1().data() << endl << endl; // Qt5
1405  uz.closeArchive();
1406  //return false;
1407  }
1408  */
1409 
1410  /*
1411  // How to : delete an element from an array from its position
1412  cout << "How to : delete an element from an array from its position" << endl;
1413 
1414  vector <string> headers;
1415  vector < vector <string> > records;
1416  vector <string> firstrecord;
1417  vector <string> secondrecord;
1418  records.push_back(firstrecord);
1419  records.push_back(secondrecord);
1420 
1421  headers.push_back("a");
1422  headers.push_back("b");
1423  headers.push_back("");
1424  headers.push_back("d");
1425  headers.push_back("e");
1426  headers.push_back("");
1427 
1428  records[0].push_back("0");
1429  records[0].push_back("1");
1430  records[0].push_back("2");
1431  records[0].push_back("3");
1432  records[0].push_back("4");
1433  records[0].push_back("5");
1434  records[1].push_back("00");
1435  records[1].push_back("11");
1436  records[1].push_back("22");
1437  records[1].push_back("33");
1438  records[1].push_back("44");
1439  records[1].push_back("55");
1440 
1441  for (int i=headers.size()-1;i>=0;i--){
1442  if(headers[i] == ""){
1443  headers.erase(headers.begin()+i);
1444  for (int j=0;j<records.size();j++){
1445  records[j].erase(records[j].begin()+i);
1446  }
1447  }
1448  }
1449  for(uint i=0;i<headers.size();i++){
1450  cout << headers.at(i) << " - " << records[0].at(i) << " - " << records[1].at(i) << endl;
1451  }
1452  cout << "done!" << endl;
1453  */
1454 
1455  //testThreads();
1456  /*vector<double> numbers;
1457  double cumNumbers = 0.00;
1458  numbers.push_back(0.40);
1459  numbers.push_back(0.10);
1460  numbers.push_back(0.20);
1461  numbers.push_back(0.08);
1462  numbers.push_back(0.22);
1463 
1464  for (uint i=0;i<numbers.size();i++){
1465  cumNumbers += numbers[i];
1466  }
1467 
1468  if (cumNumbers <= 0.99999999 || cumNumbers >= 1.00000001) {
1469  cout <<"Bastardo!"<<endl;
1470  } else {
1471  cout <<"qui funzia!"<<endl;
1472  }*/
1473 
1474 }
1475 
1476 template <class T> vector <T> getVectorSetting(string name_h, int type) {
1477 
1478  vector <string> myStringDatas;
1479  myStringDatas.push_back("aaaaa");
1480  myStringDatas.push_back("bbbbb");
1481  myStringDatas.push_back("ccccc");
1482  vector <T> xVector;
1483 
1484  for (int i=0;i<myStringDatas.size();i++){
1485  istringstream iss(myStringDatas[i]);
1486  T x;
1487  iss >> x;
1488  xVector.push_back(x);
1489  }
1490 
1491  return xVector;
1492 }
1493 
1494 
1495 
1496 
1497 template <class T> T
1498 Sandbox::getSetting(string name_h, int type){
1499 
1500  string myIntData;
1501  myIntData = "34";
1502  string myStringData;
1503  myStringData = "abcdefg";
1504 
1505  string myBoolData;
1506  myBoolData = "false";
1507 
1508  if(type==TYPE_INT){
1509  istringstream iss(myIntData);
1510  T x;
1511  iss >> x;
1512  return x;
1513  }
1514 
1515  if(type==TYPE_STRING){
1516  istringstream iss(myStringData);
1517  T x;
1518  iss >> x;
1519  return x;
1520  }
1521 
1522  if(type==TYPE_BOOL){
1523  string tempBoolString;
1524  if (myBoolData == "1" || myBoolData == "true" || myBoolData == "True" || myBoolData == "TRUE" || myBoolData == "vero" || myBoolData == "Vero"|| myBoolData == "VERO"){
1525  tempBoolString = "1";
1526  }
1527  else if (myBoolData == "0" || myBoolData == "false" || myBoolData == "False" || myBoolData == "FALSE" || myBoolData == "falso" || myBoolData == "falso"|| myBoolData == "FALSO"){
1528  tempBoolString = "0";
1529  }
1530  else {
1531  msgOut(MSG_CRITICAL_ERROR, "Impossible conversion of "+myBoolData+" to bool!. Aborted.");
1532  }
1533  istringstream iss(tempBoolString);
1534  T x;
1535  iss >> x;
1536  return x;
1537  }
1538 
1539 
1540 }
1541 
1542 template<typename T> T
1543 Sandbox::test2(const std::string& s) {
1544  std::istringstream iss(s);
1545  T x;
1546  iss >> x;
1547  return x;
1548 }
1549 
1550 
1551 vector <TestStructure*>
1553  vector <TestStructure*> toReturn;
1554  for (uint i=0;i<testVector.size();i++){
1555  //TestStructure* tempTest = new TestStructure;
1556  toReturn.push_back(&testVector[i]);
1557  }
1558  return toReturn;
1559 
1560 }
1561 
1562 
1563 
1564 void
1566 
1567  /*
1568  PSEUDOCODE
1569  - attivo i vari thread
1570  - per ogni closestAgent itero fra i vari thread e se "è libero" gli assegno il closestAgent
1571  - quando ho finito i closestAgent aspetto che tutti i threads abbiano finito il lavoro
1572  - chiudo i threads
1573  - vado avanti
1574  */
1575  int nAgents= 50;
1576  vector<TestStructure*> myAgents;
1577  vector<double> myResults (nAgents, (double) 0);
1578  //int nThreads = MTHREAD->MD->getIntSetting("nThreads");
1579  int nThreads= 5;
1580 
1581  for (int i=0; i < nAgents; i++){
1582  TestStructure* myAgent = new TestStructure;
1583  myAgent->i = i;
1584  myAgent->random = (0+( (double)rand() / ((double)(RAND_MAX)+(double)(1)) )*(10-0+1))/ (double)100;
1585  myAgents.push_back(myAgent);
1586  }
1587 
1588  vector <testThread*> myThreads ;
1589 
1590  for (int i=0; i < nThreads; i++){
1591  testThread* myThread = new testThread;
1592  myThreads.push_back(myThread);
1593  }
1594 
1595  for (uint i=0;i<myAgents.size();i++){
1596  bool assigned = false;
1597  while(!assigned) {
1598  for (uint j=0;j<myThreads.size();j++){
1599  if (!myThreads[j]->isRunning()){
1600  cout << "Assigning agent " << i << " to thread " << j << endl;
1601  myThreads[j]->assignJob(myAgents[i]);
1602  myThreads[j]->start();
1603  assigned = true;
1604  break;
1605  }
1606  else {
1607  cout << "Thread " << j << " is busy" << endl;
1608  }
1609  }
1610  }
1611  }
1612  /*
1613  volatile bool somethingStopping = true;
1614  while (somethingStopping){
1615  somethingStopping = false;
1616  for (uint i=0;i<myThreads.size();i++){
1617  if(myThreads[i]->isRunning()){
1618  somethingStopping = true;
1619  //cout << "somethingStopping is true" << endl;
1620  }
1621  }
1622  }
1623 
1624  if (somethingStopping) {
1625  cout << "somethingStopping is true" << endl;
1626  }
1627  else {
1628  cout << "somethingStopping is false" << endl;
1629  }
1630  cout << "pinco pallo sono nel mezzo dei threads..."<<endl;
1631  */
1632  for (int i=0; i < nThreads; i++){
1633  myThreads[i]->wait();
1634  }
1635 
1636 
1637  for (int i=0; i < nThreads; i++){
1638  delete myThreads[i];
1639  }
1640 
1641  for (uint i=0;i<myAgents.size();i++){
1642  //cout <<myAgents[i]->cachedOffer<<endl;
1643 
1644  double random = (0+( (double)rand() / ((double)(RAND_MAX)+(double)(1)) )*(10-0+1))/ (double)100;
1645 
1646  // important !
1647  // for random integer see also std::uniform_int_distribution :
1648  // http://stackoverflow.com/questions/7780918/stduniform-int-distributionint-range-in-g-and-msvc
1649  // in regmas:
1650  // int randomRed = int (50+( (double)rand() / ((double)(RAND_MAX)+(double)(1)) )*(255-50+1)); // randomRed is [50,255] Don't use "randomNumber % range" !!
1651 
1652  //cout <<random<<endl;
1653  }
1654 
1655  //thread1.stop();
1656  cout << "FINITO"<<endl;
1657 
1658 
1659 }
1660 
1662 
1663 }
1664 
1665 void
1667 
1668  cout << agent->i << endl;
1669 
1670  double randChange = (0+( (double)rand() / ((double)(RAND_MAX)+(double)(1)) )*(10-0+1))/ (double)100; //rand() must be not thread safe !!!!
1671 
1672  int justn = 10000;
1673  vector <double> takeTimeVector (justn, 0);
1674  for (int i =0; i< justn;i++){
1675  takeTimeVector.at(i)=i*2;
1676  }
1677  agent->cachedOffer = agent->random;
1678 }
1679 
1680 void
1682  agent = agent_h;
1683  agent->cachedOffer = 0;
1684 }
1685 
1686 void
1688 
1689 
1690  using namespace Ipopt;
1691 
1692  // Create a new instance of your nlp
1693  // (use a SmartPtr, not raw)
1694  SmartPtr<TNLP> mynlp = new Ipopt_nlp_problem_debugtest();
1695 
1696  // Create a new instance of IpoptApplication
1697  // (use a SmartPtr, not raw)
1698  // We are using the factory, since this allows us to compile this
1699  // example with an Ipopt Windows DLL
1700  SmartPtr<IpoptApplication> app = IpoptApplicationFactory();
1701 
1702  // Change some options
1703  // Note: The following choices are only examples, they might not be
1704  // suitable for your optimization problem.
1705  app->Options()->SetNumericValue("tol", 1e-7);
1706  app->Options()->SetStringValue("mu_strategy", "adaptive");
1707  app->Options()->SetStringValue("output_file", "ipopt.out");
1708  //app->Options()->SetStringValue("hessian_approximation", "limited-memory");
1709  //app->Options()->SetStringValue("derivative_test", "second-order");
1710  //app->Options()->SetStringValue("derivative_test_print_all", "yes");
1711 
1712 
1713  // The following overwrites the default name (ipopt.opt) of the
1714  // options file
1715  // app->Options()->SetStringValue("option_file_name", "hs071.opt");
1716 
1717  // Intialize the IpoptApplication and process the options
1718  ApplicationReturnStatus status;
1719  status = app->Initialize();
1720  if (status != Solve_Succeeded) {
1721  std::cout << std::endl << std::endl << "*** Error during initialization!" << std::endl;
1722  //return (int) status; // here the abort
1723  }
1724 
1725  // Ask Ipopt to solve the problem
1726  status = app->OptimizeTNLP(mynlp);
1727 
1728  if (status == Solve_Succeeded) {
1729  std::cout << std::endl << std::endl << "*** The problem solved!" << std::endl;
1730  }
1731  else {
1732  std::cout << std::endl << std::endl << "*** The problem FAILED!" << std::endl;
1733  }
1734 
1735 }
1736 
1737 int
1739 
1740  using namespace Ipopt;
1741  // Create an instance of your nlp...
1742  SmartPtr<TNLP> myadolc_nlp = new MyADOLC_NLP();
1743  //SmartPtr<TNLP> myadolc_nlp = new MyADOLC_sparseNLP();
1744 
1745  // Create an instance of the IpoptApplication
1746  SmartPtr<IpoptApplication> app = new IpoptApplication();
1747 
1748  // Initialize the IpoptApplication and process the options
1749  ApplicationReturnStatus status;
1750  status = app->Initialize();
1751  if (status != Solve_Succeeded) {
1752  printf("\n\n*** Error during initialization!\n");
1753  return (int) status;
1754  }
1755 
1756  status = app->OptimizeTNLP(myadolc_nlp);
1757 
1758  if (status == Solve_Succeeded) {
1759  // Retrieve some statistics about the solve
1760  Index iter_count = app->Statistics()->IterationCount();
1761  printf("\n\n*** The problem solved in %d iterations!\n", iter_count);
1762 
1763  Number final_obj = app->Statistics()->FinalObjective();
1764  printf("\n\n*** The final value of the objective function is %e.\n", final_obj);
1765  }
1766 
1767  return (int) status;
1768 }
1769 
1770 // ------------------------------------------------------------------
1771 // How to partial matching the key of a map
1772 
1773 /*TStrStrMap::iterator
1774 Sandbox::FindPrefix(const TStrStrMap& map, const string& search_for) {
1775  TStrStrMap::iterator i = map.lower_bound(search_for);
1776  if (i != map.end()) {
1777  const string& key = i->first;
1778  if (key.compare(0, search_for.size(), search_for) == 0) // Really a prefix?
1779  return i;
1780  }
1781  return map.end();
1782 }
1783 */
1784 
1785 /*
1786 void
1787 Sandbox::testSearchMap(const TStrStrMap& map, const string& search_for) {
1788  cout << search_for;
1789  TStrStrMap::iterator i = FindPrefix(map, search_for);
1790  if (i != map.end())
1791  cout << '\t' << i->first << ", " << i->second;
1792  cout << endl;
1793 
1794 }
1795 */
1796 
1797 void
1798 Sandbox::testSearchMap(const TStrStrMap& map, const string& search_for) {
1799  TStrStrMap::const_iterator i = map.lower_bound(search_for);
1800  for(;i != map.end();i++){
1801  const string& key = i->first;
1802  if (key.compare(0, search_for.size(), search_for) == 0) {// Really a prefix?
1803  cout << i->first << ", " << i->second << endl;
1804  } else {
1805  break;
1806  }
1807  }
1808 
1809 }
1810 
1811 
1812 void
1814 
1815  TStrStrMap tMap;
1816 
1817  tMap.insert(TStrStrPair("John", "AA"));
1818  tMap.insert(TStrStrPair("Mary", "BBB"));
1819  tMap.insert(TStrStrPair("Mother", "A"));
1820  tMap.insert(TStrStrPair("Moliere", "D"));
1821  tMap.insert(TStrStrPair("Marlon", "C"));
1822 
1823  testSearchMap(tMap, "Marl");
1824  testSearchMap(tMap, "Mo");
1825  testSearchMap(tMap, "ther");
1826  testSearchMap(tMap, "Mad");
1827  testSearchMap(tMap, "Mom");
1828  testSearchMap(tMap, "Perr");
1829  testSearchMap(tMap, "Jo");
1830 
1831  exit(0);
1832  return;
1833 }
1834 
1835 void
1836 Sandbox::testSearchMap2(const TStrStrMap& map_h, const string& search_for) {
1837  TStrStrMap::const_iterator i = map_h.upper_bound(search_for);
1838  if(i!= map_h.begin()) i--;
1839  const string& key = i->first;
1840  string search_base = search_for.substr(0,search_for.size()-4);
1841  if (key.compare(0, search_base.size(), search_base) == 0){
1842  cout << "MATCH: " << search_for <<", "<< i->first << ", " << i->second << endl;
1843  } else {
1844  cout << "NOTM: " << search_for <<", "<< i->first << endl;
1845  }
1846 
1847 }
1848 
1849 void
1851 
1852  TStrStrMap tMap;
1853 
1854 
1855  tMap.insert(TStrStrPair("mortCoeff_multiplier#broadL_highF##2005", "2005"));
1856  tMap.insert(TStrStrPair("regLev_1", "-9999"));
1857  tMap.insert(TStrStrPair("regLev_2", "-9999"));
1858  tMap.insert(TStrStrPair("tp_multiplier#broadL_copp##2005", "-9999"));
1859  tMap.insert(TStrStrPair("tp_multiplier#broadL_highF##2005", "50"));
1860  tMap.insert(TStrStrPair("tp_multiplier#broadL_highF##2010", "2010"));
1861  tMap.insert(TStrStrPair("tp_multiplier#broadL_mixedF##2005", "-9999"));
1862  tMap.insert(TStrStrPair("tp_multiplier#con_copp##2005", "-9999"));
1863  tMap.insert(TStrStrPair("tp_multiplier#con_highF##2005", "-9999"));
1864  tMap.insert(TStrStrPair("tp_multiplier#con_mixedF##2005", "aa"));
1865 
1866  TStrStrMap::const_iterator i;
1867 
1868  for(i=tMap.begin();i!=tMap.end();i++){
1869  cout << i->first << ", " << i->second << endl;
1870  }
1871  cout << endl;
1872 
1873  testSearchMap2(tMap, "mortCoeff_multiplier#broadL_highF##2006");
1874  testSearchMap2(tMap, "tp_multiplier#broadL_highF##2008");
1875  testSearchMap2(tMap, "aaaaaa");
1876  testSearchMap2(tMap, "zzzzzz");
1877 
1878  exit(0);
1879  return;
1880 }
1881 
1882 
void testPartMatching()
How to partial matching the key of a map.
Definition: Sandbox.cpp:1813
int testAdolc()
Definition: Sandbox.cpp:1738
void testPartMatching2()
How to partial matching the key of a map.
Definition: Sandbox.cpp:1850
void testIpopt()
Definition: Sandbox.cpp:1687
vector< TestStructure * > getTestStructure()
Definition: Sandbox.cpp:1552
The required data is a string.
Definition: BaseClass.h:68
The required data is a bool.
Definition: BaseClass.h:69
pair< string, string > TStrStrPair
Definition: Sandbox.cpp:77
STL namespace.
Thread manager. Responsable to manage the main thread and "speak" with the GUI.
Definition: ThreadManager.h:65
void run()
Definition: Sandbox.cpp:1666
void testSearchMap2(const map< string, string > &map_h, const string &search_for)
Definition: Sandbox.cpp:1836
Sandbox()
Definition: Sandbox.cpp:85
Print an error message and stop the model.
Definition: BaseClass.h:62
void fullTest()
Tests that require a full sandbox object including MTHREAD. Normally empty.
Definition: Sandbox.cpp:599
~Sandbox()
Definition: Sandbox.cpp:90
double random
Definition: Sandbox.h:73
string nameMember
Definition: Sandbox.cpp:100
map< string, string > TStrStrMap
Definition: Sandbox.cpp:76
GccTest(string name_h)
Definition: Sandbox.cpp:98
T test2(const std::string &s)
Definition: Sandbox.cpp:1543
vector< T > getVectorSetting(string name_h, int type)
Definition: Sandbox.cpp:1476
The required data is an integer.
Definition: BaseClass.h:66
T getSetting(string name_h, int type)
Definition: Sandbox.cpp:1498
double cachedOffer
Definition: Sandbox.h:72
void testSearchMap(const map< string, string > &map, const string &search_for)
Definition: Sandbox.cpp:1798
void assignJob(TestStructure *agent_h)
Definition: Sandbox.cpp:1681
void testThreads()
Definition: Sandbox.cpp:1565
void basicTest()
Simple tests that doesn&#39;t require anything else (are encapsulated) and so they can be run at the begi...
Definition: Sandbox.cpp:132