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