25 #include "IpIpoptApplication.hpp" 26 #include "IpSolveStatistics.hpp" 95 for(uint i=0;i<
regIds2.size();i++){
145 double q1 = 1/ ( 1+pow(dl/da,1/psi)*(pl/pWo) );
148 q1*pow(da,(psi-1)/psi) + p1*pow(dl,(psi-1)/psi)
152 double pc = (da/dc)*pWo
154 double pw = (dl*pl+da*pWo)/(dl+da);
179 double t1 = 1/ ( 1+(pow(sl/sa,1/eta))*(pl/pWo) );
182 t1*pow(sa,(eta-1)/eta) + r1*pow(sl,(eta-1)/eta)
186 double pc = (sa/sc)*pWo+(sl/sc)*pl;
187 double pw = (sl*pl+sa*pWo)/(sl+sa);
199 for(uint r1=0;r1<
l2r.size();r1++){
200 for(uint r2=0;r2<
l2r[r1].size();r2++){
202 for(uint r2To=0;r2To<
l2r[r1].size();r2To++){
220 int previousYear = thisYear-1;
222 for(uint i=0;i<
regIds2.size();i++){
235 double k = (1+g1)*k_1;
236 double aa = (sigma/(sigma+1))*pc_1*pow(dc_1,-1/sigma);
237 double gg = dc_1*pow(pc_1,-sigma);
254 double bb = (sigma/(sigma+1))*pc_1*pow(sc_1,-1/sigma)*pow(in_1/in,gamma/sigma);
255 double ff = sc_1*pow(pc_1,-sigma)*pow(in/in_1,gamma);
269 SmartPtr<IpoptApplication> application =
new IpoptApplication();
276 application->Options()->SetStringValue(
"linear_solver", linearSolver);
279 application->Options()->SetNumericValue(
"obj_scaling_factor", -1);
280 application->Options()->SetNumericValue(
"max_cpu_time", 1800);
281 application->Options()->SetStringValue(
"check_derivatives_for_naninf",
"yes");
284 ApplicationReturnStatus status;
285 status = application->Initialize();
286 if (status != Solve_Succeeded) {
287 printf(
"\n\n*** Error during initialization!\n");
288 msgOut(
MSG_INFO,
"Error during initialization! Do you have the solver compiled for the specified linear solver?");
293 msgOut(
MSG_INFO,
"Running optimisation problem for this year (it may take a few minutes for large models)..");
294 status = application->OptimizeTNLP(
MTHREAD->
OPT);
305 if (status == Solve_Succeeded) {
306 Index iter_count = application->Statistics()->IterationCount();
307 Number final_obj = application->Statistics()->FinalObjective();
308 printf(
"\n*** The problem solved in %d iterations!\n", iter_count);
309 printf(
"\n*** The final value of the objective function is %e.\n", final_obj);
310 msgOut(
MSG_INFO,
"The problem solved successfully in "+
i2s(iter_count)+
" iterations.");
311 int icount = iter_count;
312 double obj = final_obj;
316 cout <<
"***ERROR: MODEL DIDN'T SOLVE FOR THIS YEAR" << endl;
326 for(uint r2= 0; r2<
regIds2.size();r2++){
347 double pw = (dl*pl+da*pworld)/dt;
356 double pw = (sl*pl+sa*pworld)/st;
368 int previousYear = thisYear-1;
370 for(uint i=0;i<
regIds2.size();i++){
375 vector < vector < vector <double> > > hV_byPrd_regional;
376 for(uint j=0;j<
fTypes.size();j++){
378 vector < vector <double> > hV_byPrd_ft;
393 double pastRegArea =
gfd(
"regArea",regId,ft,
"",thisYear-tp_u0);
396 double vReg = pastRegArea*vHa/1000000;
397 sfd(vReg,
"vReg",regId,ft,
"");
400 for (uint u=0; u<
dClasses.size(); u++){
403 double pastYearVol = u?
gfd(
"vol",regId,ft,dc,previousYear):0.;
405 vector <double> hV_byPrd_dc;
416 double hV_byPrd_dc_prd = hr_pr*pastYearVol;
418 hV_byPrd_dc.push_back( hV_byPrd_dc_prd);
421 double hV = hr*pastYearVol;
442 double tp =
gfd(
"tp",regId,ft,dc);
443 double mort = u?
gfd(
"mortCoef",regId,ft,dc):0.;
444 double vReg =
gfd(
"vReg",regId,ft,
"");
445 double beta = u?
gfd(
"betaCoef",regId,ft,dc):0.;
447 double vHa =
gfd(
"vHa",regId,ft,dc);
448 double finalHarvestFlag =
gfd(
"finalHarvestFlag",regId,ft,dc);
453 vol = (1-1/tp-mort)*pastYearVol+vReg;
454 newMortVol = mort*pastYearVol;
457 double inc = (u==
dClasses.size()-1)?0:1./tp;
459 double pastYearVol_1 =
gfd(
"vol",regId,ft,
dClasses[u-1],previousYear);
460 vol = (1-inc-mort-hr)*pastYearVol+(1/tp_1)*beta*pastYearVol_1;
461 newMortVol = mort*pastYearVol;
464 double freeArea_byU = u?finalHarvestFlag*1000000*hV/vHa:0;
473 sfd(freeArea_byU,
"harvestedArea",regId,ft,dc,
DATA_NOW,
true);
474 hV_byPrd_ft.push_back(hV_byPrd_dc);
476 hV_byPrd_regional.push_back(hV_byPrd_ft);
478 hV_byPrd.push_back(hV_byPrd_regional);
499 for(uint i=0;i<
regIds2.size();i++){
500 vector < vector <vector <vector <double> > > > expReturnsDebug_region;
504 vector <double> cachedExpectedReturnsByFt;
508 for(uint j=0;j<
fTypes.size();j++){
510 vector <vector <vector <double> > > expReturnsDebug_ft;
528 double expReturns = 0;
531 for (uint u=0; u<
dClasses.size(); u++){
533 double finalHarvestFlag =
gfd(
"finalHarvestFlag",regId,ft,dc);
534 double hV =
gfd(
"hV",regId,ft,dc);
535 hV_byFT += finalHarvestFlag * hV;
538 if(hV_byFT==0. || !weightedAverageExpectedReturns){
539 for (uint u=0; u<
dClasses.size(); u++){
540 vector <vector <double> > expReturnsDebug_dc;
542 double vHa =
gfd(
"vHa_exp",regId,ft,dc);
543 double finalHarvestFlag =
gfd(
"finalHarvestFlag",regId,ft,dc);
544 double cumTp_u =
gfd(
"cumTp_exp",regId,ft,dc);
546 vector <double> expReturnsDebug_pp;
548 double raw_amount = finalHarvestFlag*pw*vHa*
app(
priProducts[pp],ft,dc);
550 if (anualised_amount>expReturns) {
551 expReturns=anualised_amount;
557 expReturnsDebug_pp.push_back(0.0);
558 expReturnsDebug_pp.push_back(hV_byFT);
559 expReturnsDebug_pp.push_back(finalHarvestFlag);
560 expReturnsDebug_pp.push_back(0.0);
561 expReturnsDebug_pp.push_back(pw);
562 expReturnsDebug_pp.push_back(cumTp_u);
563 expReturnsDebug_pp.push_back(vHa);
564 expReturnsDebug_pp.push_back(anualised_amount);
565 expReturnsDebug_pp.push_back(0);
567 expReturnsDebug_dc.push_back(expReturnsDebug_pp);
569 expReturnsDebug_ft.push_back(expReturnsDebug_dc);
572 for (uint u=0; u<
dClasses.size(); u++){
573 vector <vector <double> > expReturnsDebug_dc;
575 double vHa =
gfd(
"vHa_exp",regId,ft,dc);
576 double finalHarvestFlag =
gfd(
"finalHarvestFlag",regId,ft,dc);
577 double cumTp_u =
gfd(
"cumTp_exp",regId,ft,dc);
580 vector <double> expReturnsDebug_pp;
585 double hVol_byUPp =
hV_byPrd.at(i).at(j).at(u).at(pp);
588 double raw_amount = finalHarvestFlag*pw*vHa* hVol_byUPp/hV_byFT;
596 expReturns += anualised_amount;
599 expReturnsDebug_pp.push_back(hVol_byUPp);
600 expReturnsDebug_pp.push_back(hV_byFT);
601 expReturnsDebug_pp.push_back(finalHarvestFlag);
602 expReturnsDebug_pp.push_back(finalHarvestFlag*hVol_byUPp/hV_byFT);
603 expReturnsDebug_pp.push_back(pw);
604 expReturnsDebug_pp.push_back(cumTp_u);
605 expReturnsDebug_pp.push_back(vHa);
607 expReturnsDebug_pp.push_back(1);
609 expReturnsDebug_dc.push_back(expReturnsDebug_pp);
612 expReturnsDebug_ft.push_back(expReturnsDebug_dc);
616 double debug = expReturns;
617 sfd(expReturns,
"expReturns",regId, ft,
"",
DATA_NOW,
true);
618 cachedExpectedReturnsByFt.push_back(expReturns);
619 expReturnsDebug_region.push_back(expReturnsDebug_ft);
641 double maxExpReturns = *( max_element( cachedExpectedReturnsByFt.begin(), cachedExpectedReturnsByFt.end() ) );
642 bool foundMaxExpReturns =
false;
643 for(uint j=0;j<
fTypes.size();j++){
645 double harvestedAreaForThisFT =
gfd(
"harvestedArea",regId,ft,
DIAM_ALL);
648 double harvestedArea = harvestedAreaForThisFT;
649 sfd(harvestedArea,
"regArea",regId,ft,
"",
DATA_NOW,
true);
653 if (!foundMaxExpReturns && cachedExpectedReturnsByFt[j] == maxExpReturns){
655 regArea += totalHarvestedArea*
mr;
656 foundMaxExpReturns =
true;
659 regArea += harvestedAreaForThisFT*(1-
mr)*freq;
672 msgOut(
MSG_INFO,
"Starting computing inventary available for this year..");
676 for(uint i=0;i<
regIds2.size();i++){
680 for(uint ft=0;ft<
fTypes.size();ft++){
681 for(uint dc=0;dc<
dClasses.size();dc++){
715 if((expType<0 || expType>1) &&
expType != -1){
737 for(uint r2= 0; r2<
regIds2.size();r2++){
739 for(uint j=0;j<
fTypes.size();j++){
757 vector <double> cumTp_temp;
758 vector <double> vHa_temp;
759 vector <double> cumAlive_temp;
760 vector <double> cumTp_exp_temp;
761 vector <double> vHa_exp_temp;
762 vector <double> cumAlive_exp_temp;
765 for (uint u=0; u<
dClasses.size(); u++){
767 double cumTp_u, cumTp_u_exp, cumTp_u_noExp, cumTp_u_fullExp;
768 double vHa_u, vHa_u_exp, vHa_u_noExp, vHa_u_fullExp, beta, beta_exp, beta_noExp, beta_fullExp, mort, mort_exp, mort_noExp, mort_fullExp;
770 double cumAlive_u, cumAlive_exp_u;
777 cumTp_temp.push_back(cumTp_u);
778 cumTp_exp_temp.push_back(cumTp_u);
779 vHa_temp.push_back(vHa_u);
780 vHa_exp_temp.push_back(vHa_u);
781 cumAlive_temp.push_back(cumAlive_u);
782 cumAlive_exp_temp.push_back(cumAlive_u);
784 sfd(cumTp_u,
"cumTp_exp",regId,ft,dc,
DATA_NOW,
true);
787 sfd(cumAlive_u,
"cumAlive",regId,ft,dc,
DATA_NOW,
true);
788 sfd(cumAlive_u,
"cumAlive_exp",regId,ft,dc,
DATA_NOW,
true);
792 cumTp_u = cumTp_temp[u-1] +
gfd(
"tp",regId,ft,
dClasses[u-1],thisYear);
794 vHa_u =
gfd(
"entryVolHa",regId,ft,
"",thisYear);
797 mort = 1-pow(1-
gfd(
"mortCoef",regId,ft,
dClasses[u-1],thisYear),
gfd(
"tp",regId,ft,
dClasses[u-1],thisYear));
798 beta =
gfd(
"betaCoef",regId,ft,dc, thisYear);
799 vHa_u = vHa_temp[u-1]*beta*(1-mort);
801 cumAlive_u = max(0.,cumAlive_temp[u-1]*(1-mort));
802 cumAlive_temp.push_back(cumAlive_u);
803 cumTp_temp.push_back(cumTp_u);
804 vHa_temp.push_back(vHa_u);
807 sfd(cumAlive_u,
"cumAlive",regId,ft,dc,
DATA_NOW,
true);
812 cumTp_exp_temp.push_back(cumTp_u_exp);
819 vHa_u_exp = vHa_exp_temp[u-1]*beta_exp*(1-mort_exp);
822 cumTp_u_noExp = cumTp_exp_temp[u-1]+
gfd(
"tp",regId,ft,
dClasses[u-1]);
823 cumTp_u_fullExp = cumTp_exp_temp[u-1]+
gfd(
"tp",regId,ft,
dClasses[u-1],thisYear+ceil(cumTp_exp_temp[u-1]));
825 cumTp_exp_temp.push_back(cumTp_u_exp);
827 vHa_u_noExp =
gfd(
"entryVolHa",regId,ft,
"",
DATA_NOW);
828 vHa_u_fullExp =
gfd(
"entryVolHa",regId,ft,
"",thisYear+ceil(cumTp_u));
832 mort_noExp = 1-pow(1-
gfd(
"mortCoef",regId,ft,
dClasses[u-1],
DATA_NOW),cumTp_exp_temp[u]-cumTp_exp_temp[u-1]);
833 mort_fullExp = 1-pow(1-
gfd(
"mortCoef",regId,ft,
dClasses[u-1],thisYear+ceil(cumTp_temp[u-1])),cumTp_exp_temp[u]-cumTp_exp_temp[u-1]);
834 beta_noExp =
gfd(
"betaCoef",regId,ft,dc,
DATA_NOW);
835 beta_fullExp =
gfd(
"betaCoef",regId,ft,dc, thisYear+ceil(cumTp_u));
838 vHa_u_exp = vHa_exp_temp[u-1]*beta_exp*(1-mort_exp);
841 vHa_exp_temp.push_back(vHa_u_exp);
842 cumAlive_exp_u = max(0.,cumAlive_exp_temp[u-1]*(1-mort_exp));
843 cumAlive_exp_temp.push_back(cumAlive_exp_u);
844 sfd(cumTp_u_exp,
"cumTp_exp",regId,ft,dc,
DATA_NOW,
true);
845 sfd(vHa_u_exp,
"vHa_exp",regId,ft,dc,
DATA_NOW,
true);
846 sfd(cumAlive_exp_u,
"cumAlive_exp",regId,ft,dc,
DATA_NOW,
true);
899 map<int,double> forestArea;
900 pair<int,double > forestAreaPair;
904 int nFTypes = fTypes.size();
905 int nL2Regions = l2Regions.size();
906 for(uint i=0;i<nL2Regions;i++){
907 int regId = l2Regions[i];
910 for(uint j=0;j<nFTypes;j++){
911 string ft = fTypes[j];
912 double oldRegioForArea;
914 oldRegioForArea = reg->
getValue(
"forArea_"+ft)/10000;
916 oldRegioForArea =
gfd(
"forArea",regId,ft,
DIAM_ALL,thisYear-1);
922 double harvestedArea =
gfd(
"harvestedArea",regId,ft,
DIAM_ALL);
924 double newRegioForArea = oldRegioForArea + regArea - harvestedArea;
925 sfd(newRegioForArea,
"forArea",regId,ft,
"",
DATA_NOW,
true);
926 for(uint z=0;z<rpx.size();z++){
927 double oldValue = rpx[z]->getDoubleValue(
"forArea_"+ft,
true);
928 double ratio = newRegioForArea/oldRegioForArea;
929 double newValue = oldValue*ratio;
930 rpx[z]->changeValue(
"forArea_"+ft, newValue);
Print an ERROR message, but don't stop the model.
The required data is for the current year.
void computeCumulativeData()
computes cumTp, vHa, cumTp_exp, vHa_exp,
int getIntSetting(const string &name_h, int position=0, int reg=WORLD) const
vector< string > priProducts
void sfd(const double &value_h, const string &type_h, const int ®Id_h, const string &forType_h, const string &freeDim_h, const int &year=DATA_NOW, const bool &allowCreate=false) const
bool getBoolSetting(const string &name_h, int position=0, int reg=WORLD) const
Do not actually output any message.
string i2s(const int &int_h) const
integer to string conversion
ThreadManager * MTHREAD
Pointer to the Thread manager.
double getValue(string layerName, int op=OP_SUM)
return the values of its own pixels for the specified layer. Possible operations: OP_SUM or OP_AVG ...
double calculateAnnualisedEquivalent(const double &amount_h, const int &years_h, const double &ir) const
Calculate the annual equivalent flow.
vector< string > dClasses
ModelData * MD
the model data object
Output verbosity level print everything.
void runManagementModule()
computes regArea and expectedReturns
void spd(const double &value_h, const string &type_h, const int ®Id_h, const string &prodId_h, const int &year=DATA_NOW, const bool &allowCreate=false, const string &freeDim_h="") const
void printOptLog(bool optimal, int &nIterations, double &obj)
Scheduler * SCD
the scheduler object (simulation-loops scheduler)
void msgOut(const int &msgCode_h, const string &msg_h, const bool &refreshGUI_h=true) const
Overloaded function to print the output log.
Gis * GIS
GIS information and methods.
Thread manager. Responsable to manage the main thread and "speak" with the GUI.
vector< string > getStringVectorSetting(const string &name_h, int reg=WORLD) const
vector< vector< int > > l2r
double gfd(const string &type_h, const int ®Id_h, const string &forType_h, const string &freeDim_h, const int &year=DATA_NOW) const
string getStringSetting(const string &name_h, int position=0, int reg=WORLD) const
vector< vector< vector< vector< double > > > > hV_byPrd
bool app(const string &prod_h, const string &forType_h, const string &dClass_h) const
Print an error message and stop the model.
double gpd(const string &type_h, const int ®Id_h, const string &prodId_h, const int &year=DATA_NOW, const string &freeDim_h="") const
Ipopt::SmartPtr< Ipopt::TNLP > OPT
Market optimisation.
#define FT_ALL
All forest types.
vector< Pixel * > getAllPlotsByRegion(ModelRegion ®ion_h, bool shuffle=false)
Return the vector of all plots by a specific region (main region or subregion), optionally shuffled;...
vector< string > getForTypeIds(bool all=false)
By default it doesn't return forTypes used only as input.
void initMarketModule()
computes st and pw for second year and several needed-only-at-t0-vars for the market module ...
vector< string > pDClasses
void runBiologicalModule()
computes hV, hArea and new vol at end of year
void setErrorLevel(int errorLevel_h)
vector< int > getRegionIds(int level_h, bool excludeResidual=true)
void print(bool earlyPrint=true)
Print output. If earlyPrinting it doesn't print some stuff for which we don't yet have values...
double getDoubleSetting(const string &name_h, int position=0, int reg=WORLD) const
vector< string > secProducts
vector< vector< vector< vector< vector< double > > > > > expReturnsDebug
l2_region, for type, d.c., pr prod, variable name
vector< string > allProducts
void updateMapAreas()
computes forArea_{ft}
ModelRegion * getRegion(int regId_h)
void runMarketModule()
computes st (supply total) and pw (weighted price). Optimisation inside.
#define DIAM_ALL
All diameter classes.
ModelCore(ThreadManager *MTHREAD_h)
void cacheSettings()
just cache exogenous settings from ModelData
void computeInventary()
in=f(vol_t-1)