00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <algorithm>
00021 #include <cmath>
00022
00023 #include "Init.h"
00024 #include "Agent_farmer.h"
00025 #include "Pixel.h"
00026 #include "ThreadManager.h"
00027 #include "Scheduler.h"
00028 #include "Manager_base.h"
00029 #include "Manager_farmers.h"
00030
00031 Agent_farmer::Agent_farmer(ThreadManager* MTHREAD_h, Manager_base* manager_h): Agent_space(MTHREAD_h, manager_h){
00032
00033 }
00034
00035 Agent_farmer::Agent_farmer(ThreadManager* MTHREAD_h, int uniqueID_h, Manager_base* manager_h): Agent_space(MTHREAD_h, uniqueID_h, manager_h){
00036
00037
00038
00039
00040 vector<RegActivities*> actMoulds = MTHREAD->RD->getRegActivities();
00041 int nPolicyPremiums = MTHREAD->RD->getResourceSize(RESTYPE_POLICYPREMIUM);
00042 vector <double> tempVector1(nPolicyPremiums, 0);
00043 vector <int> tempVector2(nPolicyPremiums, 0);
00044 for(uint i=0;i<actMoulds.size();i++){
00045 decRights.push_back(tempVector1);
00046 decYears.push_back(tempVector2);
00047 decHa.push_back(tempVector1);
00048 }
00049 grossCoupledPremiums = 0;
00050 grossDecoupledPayment = 0;
00051 netTotalPremium = 0;
00052
00053 }
00054
00055 Agent_farmer::~Agent_farmer(){
00056
00057 }
00058
00059 void
00060 Agent_farmer::initMIP(){
00061 OPT.update();
00062 OPT.solve();
00063 }
00064
00065 void
00066 Agent_farmer::test(){
00067 OPT.solve();
00068 }
00069
00070 double
00071 Agent_farmer::getAgrDistCost(const Pixel* px1){
00072 return getDistance(px1)* MTHREAD->RD->getDoubleSetting("agrDistCost");
00073 }
00074
00075 bool
00076 Agent_farmer::filterActivity(matrixActivities* ACT){
00077 if (ACT->plotHost){
00078 if (MTHREAD->GIS->layerExist("dtm")){
00079
00080 double altitude = ACT->plotHost->getDoubleValue("dtm");
00081 double toSubtract = fabs(ACT->objValue) * (MTHREAD->RD->getDoubleSetting("altitudeGMReduction")* altitude/100);
00082 ACT->objValue -= toSubtract;
00083 }
00084
00085 ACT->objValue -= getAgrDistCost(ACT->plotHost);
00086 }
00087 return true;
00088
00089 }
00090
00098 double
00099 Agent_farmer::getShadowPrice(const Pixel* px_h, bool initSolve){
00100
00101
00102
00103 double z1=0, z2=0;
00104
00105 if(initSolve) OPT.solve();
00106
00107 z1 = OPT.getZ();
00108 OPT.saveToCache();
00109 OPT.addPixel(px_h);
00110 OPT.solve();
00111 z2 = OPT.getZ();
00112 OPT.removeLastAddedPixel();
00113 OPT.restoreFromCache();
00114 return max(((double) 0), z2-z1);
00115 }
00116
00123 double
00124 Agent_farmer::offerRentalPrice(const Pixel* px_h, bool initSolve){
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140 double shPrice, toOffer;
00141 double fixedRentalCosts, variableRentalCosts;
00142
00143 fixedRentalCosts = MTHREAD->RD->getDoubleSetting("fixedRentalCosts");
00144 variableRentalCosts = MTHREAD->RD->getDoubleSetting("variableRentalCosts");
00145
00146 shPrice = getShadowPrice(px_h, initSolve);
00147 toOffer = (shPrice - fixedRentalCosts)*(1-variableRentalCosts);
00148
00149 if (toOffer >= 0 ) {
00150 return toOffer;
00151 } else {
00152 return ((double)0);
00153 }
00154
00155 }
00156
00157 double
00158 Agent_farmer::getUAA(int ptype){
00159 int nPlots=0;
00160 if (ptype == PLOTS_OWNED || ptype== PLOTS_ALL){
00161 nPlots += ownedPlots.size();
00162 }
00163 if (ptype==PLOTS_RENTED || ptype== PLOTS_ALL){
00164 nPlots += rentedPlots.size();
00165 }
00166 return nPlots*MTHREAD->GIS->getHaByPixel();
00167 }
00168
00169
00170 void
00171 Agent_farmer::collectProductionEffects(){
00172
00173
00174
00175 debugLandUse = 0;
00176 vector <string> policyPremiumNames = MTHREAD->RD->getResourceNames(RESTYPE_POLICYPREMIUM);
00177 int nPolicyPremiums = policyPremiumNames.size();
00178 double plotHostID;
00179
00180
00181 vector<RegActivities*> actMoulds = MTHREAD->RD->getRegActivities();
00182 vector< vector<double> > currentCoupledPremiums;
00183 vector< vector<double> > currentCoupledHa;
00184 for(uint i=0;i<actMoulds.size();i++){
00185 vector<double> currentCoupledPremiumsTemp(nPolicyPremiums,0);
00186 vector<double> currentCoupledHaTemp(nPolicyPremiums,0);
00187 currentCoupledPremiums.push_back(currentCoupledPremiumsTemp);
00188 currentCoupledHa.push_back(currentCoupledHaTemp);
00189 }
00190
00191
00192 vector <matrixActivities*> activities = OPT.getActs();
00193 for(uint i=0; i<activities.size(); i++){
00194 if(activities[i]->isInvest){
00195 for(uint q=0; q< ((uint)activities[i]->q); q++){
00196 ModelObject newObject = MTHREAD->RD->getObjectByName(activities[i]->name);
00197 liquidity -= newObject.getInitialCost();
00198 if(activities[i]->isSpatial()){
00199
00200 plotHostID = activities[i]->plotHost->getID();
00201 MTHREAD->GIS->getPixel(plotHostID)->acquireObject(newObject);
00202 }
00203 else {
00204 this->acquireObject(newObject);
00205 }
00206 }
00207 }
00208
00209
00210
00211 if(activities[i]->isSpatial() && activities[i]->q >0 ){
00212 RegActivities* actMould=0;
00213 plotHostID = activities[i]->plotHost->getID();
00214 if (activities[i]->actMould) { actMould = activities[i]->actMould;}
00215 else if ( activities[i]->objMould) {
00216 actMould=MTHREAD->RD->getRegActivityByName(activities[i]->objMould->getRequiredFor());
00217 }
00218 else {msgOut(MSG_CRITICAL_ERROR, "BOOM! All spatial activities should have an object or activity mould!");}
00222 if( activities[i]->q > 0.6){
00223 MTHREAD->GIS->getPixel(plotHostID)->changeValue("farmActivities",actMould->getMapCode());
00224 }
00225 else {
00226 if(activities[i]->plotHost->getDoubleValue("farmActivities")==MTHREAD->GIS->getNoValue()){
00227 MTHREAD->GIS->getPixel(plotHostID)->changeValue("farmActivities",900);
00228 }
00229 }
00230 debugLandUse += activities[i]->q;
00231 }
00232
00233
00234 if(activities[i]->q >0 ){
00235 for(uint j=0;j<actMoulds.size();j++){
00236 bool pass = false;
00237 if (activities[i]->actMould && (activities[i]->name == actMoulds[j]->getName())) {
00238 pass = true;
00239 } else if (activities[i]->objMould && (activities[i]->objMould->getRequiredFor() == actMoulds[j]->getName())) {
00240 pass = true;
00241 }
00242
00243 if(pass ){
00244 for(uint y=0;y<nPolicyPremiums;y++){
00245 currentCoupledPremiums[j][y] += -activities[i]->q * actMoulds[j]->getMatrixCoefficientByName(policyPremiumNames[y]);
00246 currentCoupledHa[j][y] += activities[i]->q;
00247 }
00248 break;
00249 }
00250 }
00251 }
00252 }
00253
00254
00255 for(uint i=0;i<actMoulds.size();i++){
00256 for(uint y=0;y<nPolicyPremiums;y++){
00257 if (actMoulds[i]->getDecouplingOption(y) == -1) {
00258 double oldDecRights = decRights[i][y];
00259 double oldDecHa = decHa[i][y];
00260 int oldCountedYears = decYears[i][y];
00261
00262
00263 decRights[i][y]= (oldDecRights*((double)oldCountedYears) + currentCoupledPremiums[i][y]) / ((double) oldCountedYears +1);
00264 decHa[i][y]= (oldDecHa*((double)oldCountedYears) + currentCoupledHa[i][y]) / ((double) oldCountedYears +1);
00265 decYears[i][y]++;
00266 }
00267 }
00268 }
00269
00270
00271
00272 grossCoupledPremiums=0;
00273 for(uint i=0;i<actMoulds.size();i++){
00274 for(uint y=0;y<nPolicyPremiums;y++){
00275 if(currentCoupledPremiums[i][y]>0){
00276 grossCoupledPremiums += currentCoupledPremiums[i][y];
00277 }
00278 }
00279 }
00280 netProductionProfit = OPT.getZ()-grossCoupledPremiums;
00281 grossDecoupledPayment = getDecoupledPayments();
00282
00283 netTotalPremium = applyModulation(grossCoupledPremiums + grossDecoupledPayment);
00284
00285
00286
00287
00288
00289
00290
00291
00292 liquidity += (netProductionProfit+netTotalPremium);
00293
00294 }
00295
00296 double
00297 Agent_farmer::applyModulation(double grossPremium){
00298
00299 double threshold1 = MTHREAD->RD->getDoubleSetting("threshold1",DATA_NOW);
00300 double threshold2 = MTHREAD->RD->getDoubleSetting("threshold2",DATA_NOW);
00301 double threshold3 = MTHREAD->RD->getDoubleSetting("threshold3",DATA_NOW);
00302 double threshold4 = MTHREAD->RD->getDoubleSetting("threshold4",DATA_NOW);
00303 double threshold5 = MTHREAD->RD->getDoubleSetting("threshold5",DATA_NOW);
00304
00305 double modulationShare1 = MTHREAD->RD->getDoubleSetting("modulationShare1",DATA_NOW);
00306 double modulationShare2 = MTHREAD->RD->getDoubleSetting("modulationShare2",DATA_NOW);
00307 double modulationShare3 = MTHREAD->RD->getDoubleSetting("modulationShare3",DATA_NOW);
00308 double modulationShare4 = MTHREAD->RD->getDoubleSetting("modulationShare4",DATA_NOW);
00309 double modulationShare5 = MTHREAD->RD->getDoubleSetting("modulationShare5",DATA_NOW);
00310 double modulationShare6 = MTHREAD->RD->getDoubleSetting("modulationShare6",DATA_NOW);
00311
00312
00313 bool firstSpecial = MTHREAD->RD->getBoolSetting("firstModLevelIsATreasholdOnly",DATA_NOW);
00314
00315 double byeByeMoney=0;
00316
00317
00318 if (firstSpecial && grossPremium >= threshold1) modulationShare1 = 0;
00319
00320 if(grossPremium > threshold5){
00321 byeByeMoney = modulationShare6 * (grossPremium-threshold5)
00322 + modulationShare5 * (threshold5 -threshold4)
00323 + modulationShare4 * (threshold4 -threshold3)
00324 + modulationShare3 * (threshold3 -threshold2)
00325 + modulationShare2 * (threshold2 -threshold1)
00326 + modulationShare1 * threshold1;
00327
00328 } else if (grossPremium > threshold4){
00329 byeByeMoney = modulationShare5 * (grossPremium -threshold4)
00330 + modulationShare4 * (threshold4 -threshold3)
00331 + modulationShare3 * (threshold3 -threshold2)
00332 + modulationShare2 * (threshold2 -threshold1)
00333 + modulationShare1 * threshold1;
00334
00335 } else if (grossPremium > threshold3){
00336 byeByeMoney = modulationShare4 * (grossPremium -threshold3)
00337 + modulationShare3 * (threshold3 -threshold2)
00338 + modulationShare2 * (threshold2 -threshold1)
00339 + modulationShare1 * threshold1;
00340
00341 } else if (grossPremium > threshold2){
00342 byeByeMoney = modulationShare3 * (grossPremium -threshold2)
00343 + modulationShare2 * (threshold2 -threshold1)
00344 + modulationShare1 * threshold1;
00345
00346 } else if (grossPremium > threshold1){
00347 byeByeMoney = modulationShare2 * (grossPremium -threshold1)
00348 + modulationShare1 * threshold1;
00349
00350 } else {
00351 byeByeMoney = modulationShare1 * grossPremium;
00352 }
00353
00354 return grossPremium-byeByeMoney;
00355
00356 }
00357
00358 void
00359 Agent_farmer::update(){
00360
00361 vector<ModelObject>::iterator p;
00362 for ( p = myObjects.begin() ; p != myObjects.end();){
00363 if(p->getLastYear() <= MTHREAD->SCD->getYear()){
00364 myObjects.erase(p);
00365 }
00366 else {
00367 ++p;
00368 }
00369 }
00370
00371
00372 vector<Pixel*>:: iterator p2;
00373 for ( p2 = rentedPlots.begin(); p2 != rentedPlots.end();){
00374 if( (*p2)->getLastRentingYear() <= MTHREAD->SCD->getYear()){
00375 (*p2)->newYear();
00376 rentedPlots.erase(p2);
00377 }
00378 else {
00379 ++p2;
00380 }
00381 }
00382 OPT.update();
00383 }
00384
00385 int
00386 Agent_farmer::leaveActivity(){
00387
00388 double minEqC = MTHREAD->RD->getDoubleSetting("minEC");
00389 bool forceFarmingForCashPremium = MTHREAD->RD->getBoolSetting("forceFarmingForCashPremium", DATA_NOW);
00390
00391 if(getCapital(true)<=0){
00392 msgOut(MSG_DEBUG, "Farm "+i2s(getAgentUniqueID())+" left the model for shortage of owned capital.. bye, bye..");
00393 return AGEXIT_NOCAPITAL;
00394 } else if (liquidity < - ( getCapital(true) * (1-minEqC)/minEqC) ) {
00395 msgOut(MSG_DEBUG, "Farm "+i2s(getAgentUniqueID())+" left the model for shortage of liquidity.. bye, bye..");
00396 return AGEXIT_DEBS;
00397 } else {
00398 double realProfit = netProductionProfit + netTotalPremium - sunkCostsPaid;
00399 liquidityShPrice = max(((double)0), liquidity)* OPT.getActivityObjValue("sellLiquidity");
00400 labourShPrice = getResource("labour") * OPT.getActivityObjValue("offFarmLabour");
00401 landShPrice = 0 ;
00402 vector <double> rentalPrices = MTHREAD->RD->getDoubleVectorSetting("agrRentalPrices");
00403 vector <int> agrLandTypes = MTHREAD->RD->getIntVectorSetting("agrLandTypes");
00404
00405
00406 for (uint i=0;i<ownedPlots.size();i++){
00407
00408 int type= ((int)ownedPlots[i]->getDoubleValue("landUse"));
00409 for (uint j=0;j<agrLandTypes.size();j++){
00410 if (type == agrLandTypes[j]){
00411 double plotShadowPrice = rentalPrices.at(j)*ownedPlots[i]->getShRentalPriceCoeff();
00412 landShPrice += plotShadowPrice;
00413 break;
00414 }
00415 }
00416 }
00417
00418 double premiumLeaving =0;
00419 if (!forceFarmingForCashPremium){
00420 premiumLeaving += applyModulation(grossDecoupledPayment);
00421 }
00422
00423 if (realProfit < (MTHREAD->RD->getDoubleSetting("oppCostsCoeff") * (liquidityShPrice+labourShPrice+landShPrice) ) + premiumLeaving ) {
00424 msgOut(MSG_DEBUG, "Farm "+i2s(getAgentUniqueID())+" left the model for better shadow prices of owned factors.. bye, bye..");
00425 return AGEXIT_SHPRICES;
00426 }
00427 }
00428 return AGEXIT_FALSE;
00429
00430 }
00431
00432 double
00433 Agent_farmer::getDecoupledPayments(){
00434
00435 double decPayments = initialDecPayment;
00436 vector<RegActivities*> actMoulds = MTHREAD->RD->getRegActivities();
00437 vector <string> policyPremiums = MTHREAD->RD->getResourceNames(RESTYPE_POLICYPREMIUM);
00438 int nPolicyPremiums = policyPremiums.size();
00439
00440 int debugYear = MTHREAD->SCD->getYear();
00441 int farmerID = getAgentUniqueID();
00442
00443 if(!MTHREAD->RD->getBoolSetting("regionalisation", DATA_NOW)){
00444 for(uint i=0;i<actMoulds.size();i++){
00445 for(uint y=0; y<nPolicyPremiums;y++){
00446 if (actMoulds[i]->getDecouplingOption(y) >= 0) {
00447 decPayments += actMoulds[i]->getDecouplingOption(y)*decRights.at(i).at(y);
00448 }
00449 }
00450 }
00451 } else {
00452 double totHa = 0;
00453 for(uint i=0;i<actMoulds.size();i++){
00454 totHa += dVSum(decHa[i]);
00455 }
00456 decPayments += ((Manager_farmers*)manager) -> getAverageRightsByAct() * totHa;
00457 }
00458
00459 return decPayments;
00460 }
00461