00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "Output.h"
00021 #include "Agent_base.h"
00022 #include "Agent_space.h"
00023 #include "SuperAgentManager.h"
00024 #include "ThreadManager.h"
00025 #include "Scheduler.h"
00026 #include "Manager_base.h"
00027
00028
00029 Output::Output(ThreadManager* MTHREAD_h){
00030 MTHREAD=MTHREAD_h;
00031 }
00032
00033 Output::~Output(){
00034 }
00035
00036
00037
00038 void
00039 Output::collectData(){
00040 if(MTHREAD->RD->getIntSetting("outputLevel")<=OUTVL_NONE) return;
00041 vector <Agent_base*> agents = MTHREAD-> SAM ->getAgentsByType(false);
00042 vector<int> agrSoilTypes = MTHREAD-> RD ->getIntVectorSetting("agrLandTypes");
00043 vector<Pixel*> agrPixels = MTHREAD-> GIS ->getAllPlotsByValue("landUse", agrSoilTypes);
00044 double noValue = MTHREAD-> GIS ->getNoValue();
00045 int idleCode = MTHREAD-> RD ->getActGisCodeByName("idleLand");
00046
00047
00048 for (uint i=0;i<agents.size();i++){
00049 AgentSurvey SURVEY;
00050 agents.at(i)->answerStats(SURVEY);
00051 surveys.push_back(SURVEY);
00052 }
00053
00054
00055 abandonedAgrLandByType.clear();
00056 abandonedAgrLandByType.resize(agrSoilTypes.size(),0);
00057 unpossessedAgrLandByType.clear();
00058 unpossessedAgrLandByType.resize(agrSoilTypes.size(),0);
00059 idleAgrLandByType.clear();
00060 idleAgrLandByType.resize(agrSoilTypes.size(),0);
00061 MTHREAD->GIS->resetLayer("agrUsage");
00062
00063 for(uint i=0;i<agrPixels.size();i++){
00064 int pxActValue = ((int)agrPixels[i]->getDoubleValue("farmActivities"));
00065 int pxLandType = ((int)agrPixels[i]->getDoubleValue("landUse"));
00066 for(uint j=0;j<agrSoilTypes.size();j++){
00067 if(pxLandType == agrSoilTypes[j]){
00068 if(pxActValue == noValue){
00069 abandonedAgrLandByType.at(j)++;
00070 }
00071 if(pxActValue == idleCode){
00072 idleAgrLandByType.at(j)++;
00073 }
00074 if(!agrPixels[i]->getTenant()) {
00075 if (!agrPixels[i]->getOwner() || agrPixels[i]->getOwner()->getAgentUniqueID() <0){
00076 unpossessedAgrLandByType.at(j)++;
00077 }
00078 }
00079
00080 break;
00081 }
00082 }
00083
00084 if (pxActValue==noValue){
00085 agrPixels[i]->changeValue("agrUsage",0);
00086 } else if (pxActValue==idleCode) {
00087 agrPixels[i]->changeValue("agrUsage",1);
00088 } else {
00089 agrPixels[i]->changeValue("agrUsage",2);
00090 }
00091 }
00092 MTHREAD->GIS->updateImage("agrUsage");
00093 }
00094
00095 void
00096 Output::initOutput(){
00097 initOutputAggregatedData();
00098 initOutputMaps();
00099 initOutputDetailedData();
00100 initOutputLeavingAgents();
00101 }
00102
00103 void
00104 Output::initOutputAggregatedData(){
00105 if(MTHREAD->RD->getIntSetting("outputLevel")<OUTVL_AGGREGATED) return;
00106
00107
00108 char d = getOutputFieldDelimiter();
00109 vector <string> agentCategories = MTHREAD->SAM->getAgentTypeNames();
00110 vector <Agent_base*> agents = MTHREAD->SAM->getAgentsByType(false);
00111 vector<int> landTypes;
00112 if(!MTHREAD->RD->getBoolSetting("compactOutput")){
00113 landTypes = MTHREAD->RD->getIntVectorSetting("landTypes");
00114 } else {
00115 landTypes = MTHREAD->RD->getIntVectorSetting("agrLandTypes");
00116 }
00117 vector<string> objNames = MTHREAD->RD->getObjectNames();
00118 vector<string> actNames = MTHREAD->RD->getActivityNames();
00119 vector<int> agrSoilTypes = MTHREAD->RD->getIntVectorSetting("agrLandTypes");
00120 vector<string> agrSoilLabels = MTHREAD->RD->getStringVectorSetting("agrLandTypesShortLabels");
00121
00122 vector< vector<int> > mouldsByCategory;
00123 vector <Manager_base*> listOfManagers = MTHREAD->SAM->getManagers();
00124 for(uint i=0;i<listOfManagers.size();i++){
00125 vector<int> localMoulds = listOfManagers[i]->getMouldIDs();
00126 mouldsByCategory.push_back(localMoulds);
00127 }
00128
00129 string outFileName;
00130
00131
00132 if(MTHREAD->RD->getBoolSetting("outputAgentAggregatedHumanReadable")) {
00133 outFileName = MTHREAD->RD->getBaseDirectory()
00134 + MTHREAD->RD->getOutputDirectory()
00135 + "results/aggregatedOutput_"
00136 + MTHREAD->getScenarioName()
00137 + MTHREAD->RD->getStringSetting("outputFileExtension");
00138 ofstream out(outFileName.c_str(), ios::out);
00139 if (!out){ msgOut(MSG_CRITICAL_ERROR,"Error in opening the file "+outFileName+" for reading.");}
00140 out << "YEAR" << d << "\n";
00141 out << "AGENT COUNT" << d << "\n";
00142 for (uint i=0;i<agentCategories.size();i++){
00143 out << " - " << agentCategories.at(i) <<" TOTAL"<< d << "\n";
00144 for (uint j=0;j<mouldsByCategory.at(i).size();j++){
00145 out << " -- mould " << mouldsByCategory.at(i).at(j) << d << "\n";
00146 }
00147 }
00148 out << "avg. capital" << d << "\n"
00149 << "avg. landCapital" << d << "\n"
00150 << "avg. objectsCapital" << d << "\n"
00151 << "avg. ownLabour" << d << "\n"
00152 << "avg. companyLabour" << d << "\n"
00153 << "avg. liquidity" << d << "\n"
00154 << "avg. netProductionProfit" << d << "\n"
00155 << "avg. coupledPremiums" << d << "\n"
00156 << "avg. decoupledPayment" << d << "\n"
00157 << "avg. lastSunkCostsPaid" << d << "\n"
00158 << "avg. companyProfit" << d << "\n"
00159 << "avg. companyProfitWSupport"<< d << "\n"
00160 << "avg. incomes" << d << "\n";
00161
00162 out << "RENTAL PRICE BY (AGR) TYPE" << d << "\n";
00163 for (uint i=0;i<agrSoilTypes.size();i++){
00164 out << " - " << agrSoilLabels.at(i) << d << "\n";
00165 }
00166 out << "Precised abandoned land" <<d << "\n";
00167
00168
00169 out << "ENTIRELY ABAND.ED PLOTS BY (AGR) TYPE" << d << "\n";
00170 for (uint i=0;i<agrSoilTypes.size();i++){
00171 out << " - " << agrSoilLabels.at(i) << d << "\n";
00172 }
00173 out << "IDLE PLOTS BY (AGR) TYPE" << d << "\n";
00174 for (uint i=0;i<agrSoilTypes.size();i++){
00175 out << " - " << agrSoilLabels.at(i) << d << "\n";
00176 }
00177 out << "TOTAL_PLOTS" << d << "\n";
00178 out << "OWNED PLOTS BY TYPE" << d << "\n";
00179 for(uint i=0;i<landTypes.size();i++){
00180 out << " - " << landTypes[i] << d << "\n";
00181 }
00182 out << "RENTED PLOTS BY TYPE" << d << "\n";
00183 for(uint i=0;i<landTypes.size();i++){
00184 out << " - " << landTypes[i] << d << "\n";
00185 }
00186 out << "AVAILABLE OBJECT BY TYPE" << d << "\n";
00187 for(uint i=0;i<objNames.size();i++){
00188 out << " - " << objNames[i] << d << "\n";
00189 }
00190 out << "NEWLY AV. OBJECT BY TYPE" << d << "\n";
00191 for(uint i=0;i<objNames.size();i++){
00192 out << " - " << objNames[i] << d << "\n";
00193 }
00194 out << "ACTIVITIES BY TYPE" << d << "\n";
00195 for(uint i=0;i<actNames.size();i++){
00196 out << " - " << actNames[i] << d << "\n";
00197 }
00198 out.close();
00199 } else {
00200 outFileName = MTHREAD->RD->getBaseDirectory()
00201 + MTHREAD->RD->getOutputDirectory()
00202 + "results/aggregatedOutput"
00203
00204 + MTHREAD->RD->getStringSetting("outputFileExtension");
00205 ifstream in(outFileName.c_str(), ios::in);
00206 if(in.is_open()) {
00207 in.close();
00208 cleanScenario(outFileName, MTHREAD->getScenarioName(), d);
00209 return;
00210 } else {
00211 in.close();
00212 }
00213 ofstream out(outFileName.c_str(), ios::out);
00214 if (!out){ msgOut(MSG_CRITICAL_ERROR,"Error in opening the file "+outFileName+" for reading.");}
00215 out << "SCENARIO" << d ;
00216 out << "YEAR" << d ;
00217 out << "AGENT_COUNT" << d ;
00218 for (uint i=0;i<agentCategories.size();i++){
00219 out << agentCategories.at(i) <<"_total"<< d;
00220 for (uint j=0;j<mouldsByCategory.at(i).size();j++){
00221 out << agentCategories.at(i) << "_mould_" << mouldsByCategory.at(i).at(j) << d;
00222 }
00223 }
00224 out << "avg_capital" << d
00225 << "avg_landCapital" << d
00226 << "avg_objectsCapital" << d
00227 << "avg. ownLabour" << d
00228 << "avg. companyLabour" << d
00229 << "avg_liquidity" << d
00230 << "avg_netProductionProfit" << d
00231 << "avg_coupledPremiums" << d
00232 << "avg_decoupledPayment" << d
00233 << "avg_lastSunkCostsPaid" << d
00234 << "avg. companyProfit" << d
00235 << "avg. companyProfitWSupport" << d
00236 << "avg. incomes" << d;
00237
00238
00239
00240 out << "RENTAL_PRICE_BY_AGR_TYPE" << d;
00241 for (uint i=0;i<agrSoilTypes.size();i++){
00242 out << "rPrice_" << agrSoilLabels.at(i) << d;
00243 }
00244 out << "Precise abandoned land" <<d << "\n";
00245
00246
00247 out << "ABANDONED_PLOTS_BY_AGR_TYPE" << d;
00248 for (uint i=0;i<agrSoilTypes.size();i++){
00249 out << "abPlots_" << agrSoilLabels.at(i) << d;
00250 }
00251 out << "IDLE_PLOTS_BY_AGR_TYPE" << d;
00252 for (uint i=0;i<agrSoilTypes.size();i++){
00253 out << "idlePlots_" << agrSoilLabels.at(i) << d;
00254 }
00255 out << "TOTAL_PLOTS" << d;
00256 out << "OWNED_PLOTS_BY_TYPE" << d;
00257 for(uint i=0;i<landTypes.size();i++){
00258 out << "oPlots_" << landTypes[i] << d;
00259 }
00260 out << "RENTED_PLOTS_BY_TYPE" << d;
00261 for(uint i=0;i<landTypes.size();i++){
00262 out << "rPlots_" << landTypes[i] << d;
00263 }
00264 out << "AVAILABLE_OBJECT_BY_TYPE" << d;
00265 for(uint i=0;i<objNames.size();i++){
00266 out << "avObjs_" << objNames[i] << d;
00267 }
00268 out << "NEWLY_AV_OBJECT_BY_TYPE" << d;
00269 for(uint i=0;i<objNames.size();i++){
00270 out << "newObjs_" << objNames[i] << d;
00271 }
00272 out << "ACTIVITIES_BY_TYPE" << d;
00273 for(uint i=0;i<actNames.size();i++){
00274 out << "acts_" << actNames[i] << d;
00275 }
00276 out << "\n";
00277 out.close();
00278 }
00279
00280
00281
00282
00283
00284 }
00289 void
00290 Output::initOutputMaps(){
00291 if(MTHREAD->RD->getIntSetting("outputLevel")<OUTVL_MAPS) return;
00292 string mapBaseDirectory = MTHREAD->RD->getBaseDirectory()+MTHREAD->RD->getOutputDirectory()+"maps/";
00293 string filenameToSaveScenarioName = mapBaseDirectory+"scenarioName";
00294 string filenameListIntLayers = mapBaseDirectory+"integerListLayers";
00295 string filenameListFloatLayers = mapBaseDirectory+"floatListLayers";
00296
00297
00298 ofstream outSN(filenameToSaveScenarioName.c_str(), ios::out);
00299 if (!outSN){ msgOut(MSG_ERROR,"Error in opening the file "+filenameToSaveScenarioName+".");}
00300 outSN << MTHREAD->getScenarioName() << "\n";
00301 outSN.close();
00302
00303 ofstream outi(filenameListIntLayers.c_str(), ios::out);
00304 outi.close();
00305 ofstream outf(filenameListFloatLayers.c_str(), ios::out);
00306 outf.close();
00307
00308
00309 MTHREAD->GIS->addLayer("agrUsage","Overall agricultural usage",true, true);
00310 MTHREAD->GIS->addLegendItem("agrUsage", 0, "Abandoned plots", 255, 0, 0, ((double)0), ((double)0));
00311 MTHREAD->GIS->addLegendItem("agrUsage", 1, "Idle plots", 120, 120, 0, ((double)0), ((double)0));
00312 MTHREAD->GIS->addLegendItem("agrUsage", 2, "Normal agr. usage", 0, 255, 0, ((double)0), ((double)0));
00313
00314 }
00315 void
00316 Output::initOutputDetailedData(){
00317 if(MTHREAD->RD->getIntSetting("outputLevel")<OUTVL_DETAILED) return;
00318
00319 vector <Agent_base*> agents = MTHREAD->SAM->getAgentsByType(false);
00320 char d = getOutputFieldDelimiter();
00321 vector<int> landTypes;
00322 if(!MTHREAD->RD->getBoolSetting("compactOutput")){
00323 landTypes = MTHREAD->RD->getIntVectorSetting("landTypes");
00324 } else {
00325 landTypes = MTHREAD->RD->getIntVectorSetting("agrLandTypes");
00326 }
00327 vector<string> objNames = MTHREAD->RD->getObjectNames();
00328 vector<string> actNames = MTHREAD->RD->getActivityNames();
00329
00330 if(MTHREAD->RD->getBoolSetting("outputAgentDetailsHumanReadable")){
00331 for (uint i=0;i<agents.size();i++){
00332 string agName= i2s(agents[i]->getAgentUniqueID());
00333 string outFileName = MTHREAD->RD->getBaseDirectory()
00334 + MTHREAD->RD->getOutputDirectory()
00335 + "results/ag_"
00336 + agName
00337 + "_"
00338 + MTHREAD->getScenarioName()
00339 + MTHREAD->RD->getStringSetting("outputFileExtension");
00340 ofstream out(outFileName.c_str(), ios::out);
00341 if (!out){ msgOut(MSG_CRITICAL_ERROR,"Error in opening the file "+outFileName+".");}
00342 out << "RECORDID" << d << "\n"
00343 << "YEAR" << d << "\n"
00344 << "agentType" << d << "\n"
00345 << "agentID" << d << "\n"
00346 << "agentMouldID" << d << "\n"
00347 << "spatial" << d << "\n"
00348 << "hostPlotID" << d << "\n"
00349 << "hostPlotX" << d << "\n"
00350 << "hostPlotY" << d << "\n"
00351 << "capital" << d << "\n"
00352 << "landCapital" << d << "\n"
00353 << "objectsCapital" << d << "\n"
00354 << "ownLabour" << d << "\n"
00355 << "companyLabour" << d << "\n"
00356 << "liquidity" << d << "\n"
00357 << "netProductionProfit" << d << "\n"
00358 << "coupledPremiums" << d << "\n"
00359 << "decoupledPayment" << d << "\n"
00360 << "lastSunkCostsPaid" << d << "\n"
00361 << "companyProfit" << d << "\n"
00362 << "companyProfitWSupport"<< d << "\n"
00363 << "incomes" << d << "\n";
00364 out << "TOTAL PLOTS" << d << "\n";
00365 out << "OWNED PLOTS BY TYPE" << d << "\n";
00366 for(uint i=0;i<landTypes.size();i++){
00367 out << " - " << landTypes[i] << d << "\n";
00368 }
00369 out << "RENTED PLOTS BY TYPE" << d << "\n";
00370 for(uint i=0;i<landTypes.size();i++){
00371 out << " - " << landTypes[i] << d << "\n";
00372 }
00373 out << "OWNED OBJECT BY TYPE" << d << "\n";
00374 for(uint i=0;i<objNames.size();i++){
00375 out << " - " << objNames[i] << d << "\n";
00376 }
00377 out << "AVAILABLE OBJECT BY TYPE" << d << "\n";
00378 for(uint i=0;i<objNames.size();i++){
00379 out << " - " << objNames[i] << d << "\n";
00380 }
00381 out << "NEWLY AV. OBJECT BY TYPE" << d << "\n";
00382 for(uint i=0;i<objNames.size();i++){
00383 out << " - " << objNames[i] << d << "\n";
00384 }
00385 out << "ACTIVITIES BY TYPE" << d << "\n";
00386 for(uint i=0;i<actNames.size();i++){
00387 out << " - " << actNames[i] << d << "\n";
00388 }
00389 out << "UNUSED O+R LAND" << d << "\n";
00390 out.close();
00391 }
00392 } else {
00393 string outFileName = MTHREAD->RD->getBaseDirectory()
00394 + MTHREAD->RD->getOutputDirectory()
00395 + "results/agentsDetails"
00396
00397 + MTHREAD->RD->getStringSetting("outputFileExtension");
00398 ifstream in(outFileName.c_str(), ios::in);
00399 if(in.is_open()) {
00400 in.close();
00401 cleanScenario(outFileName, MTHREAD->getScenarioName(), d);
00402 return;
00403 } else {
00404 in.close();
00405 }
00406 ofstream out(outFileName.c_str(), ios::out);
00407 if (!out){ msgOut(MSG_CRITICAL_ERROR,"Error in opening the file "+outFileName+".");}
00408 out << "SCENARIO" << d ;
00409 out << "RECORDID" << d ;
00410 out << "YEAR" << d
00411 << "agentType" << d
00412 << "agentID" << d
00413 << "agentMouldID" << d
00414 << "spatial" << d
00415 << "hostPlotID" << d
00416 << "hostPlotX" << d
00417 << "hostPlotY" << d
00418 << "capital" << d
00419 << "landCapital" << d
00420 << "objectsCapital" << d
00421 << "ownLabour" << d
00422 << "companyLabour" << d
00423 << "liquidity" << d
00424 << "netProductionProfit" << d
00425 << "coupledPremiums" << d
00426 << "decoupledPayment" << d
00427 << "lastSunkCostsPaid" << d
00428 << "companyProfit" << d
00429 << "companyProfitWSupport"<< d
00430 << "incomes" << d;
00431
00432 out << "TPLOTS" << d ;
00433 out << "OPLOTS" << d ;
00434 for(uint i=0;i<landTypes.size();i++){
00435 out << "oPlots_" << landTypes[i] << d ;
00436 }
00437 out << "RPLOTS" << d ;
00438 for(uint i=0;i<landTypes.size();i++){
00439 out << "rPlots_" << landTypes[i] << d ;
00440 }
00441 out << "OOBJS" << d ;
00442 for(uint i=0;i<objNames.size();i++){
00443 out << "oObjs_" << objNames[i] << d ;
00444 }
00445 out << "AVOBJS" << d ;
00446 for(uint i=0;i<objNames.size();i++){
00447 out << "avObjs_" << objNames[i] << d ;
00448 }
00449 out << "NEWOBJS" << d ;
00450 for(uint i=0;i<objNames.size();i++){
00451 out << "newObjs_" << objNames[i] << d ;
00452 }
00453 out << "ACTS" << d ;
00454 for(uint i=0;i<actNames.size();i++){
00455 out << "acts_" << actNames[i] << d ;
00456 }
00457 out << "UNUSED O+R LAND" << d ;
00458 out << "\n" ;
00459 out.close();
00460 }
00461 }
00462
00463 void
00464 Output::initOutputLeavingAgents(){
00465 if(MTHREAD->RD->getIntSetting("outputLevel")<OUTVL_DETAILED) return;
00466
00467 string outFileName = MTHREAD->RD->getBaseDirectory()
00468 + MTHREAD->RD->getOutputDirectory()
00469 + "results/leavingAgents"
00470
00471
00472 + MTHREAD->RD->getStringSetting("outputFileExtension");
00473
00474 char d = getOutputFieldDelimiter();
00475
00476 ifstream in(outFileName.c_str(), ios::in);
00477 if(in.is_open()) {
00478 in.close();
00479 cleanScenario(outFileName, MTHREAD->getScenarioName(), d);
00480 return;
00481 } else {
00482 in.close();
00483 }
00484
00485
00486
00487 ofstream out(outFileName.c_str(), ios::out);
00488 if (!out){ msgOut(MSG_CRITICAL_ERROR,"Error in opening the file "+outFileName+".");}
00489
00490 out << "SCENARIO" << d ;
00491 out << "YEAR" << d << "ID" << d << "TYPE" << d
00492 << "MOULD" << d << "MOTIVATION" << d << "TotPLOTS" << d << "OwPLOTS" << d << "RePLOTS" << d << "CAP" << d
00493 << "LIQ" << d << "MAXDEB" << d << "PRODPROFIT" << d << "COUPPREMIUMS" << d << "DECPAYMENT" << d
00494 << "SUNKCOSTS" << d << "LIQSHPR" << d << "LABSHPR" << d
00495 << "LANDSHPR" << d << "REALPROFIT" << d << "TOTSHPR" << d << "\n";
00496
00497 out.close();
00498 }
00499
00500 void
00501 Output::print(){
00502
00503 printAggregatedData();
00504 printMaps();
00505 printDetailedData();
00506 printDebugMatrices();
00507
00508
00509 surveys.clear();
00510 }
00511
00512 void
00513 Output::printAggregatedData(){
00514 if(MTHREAD->RD->getIntSetting("outputLevel")<OUTVL_AGGREGATED) return;
00515
00516 string inOutFileName;
00517
00518 char d = getOutputFieldDelimiter();
00519 vector <int> agentCounts = MTHREAD->SAM->getAgentCountByType();
00520 vector <Agent_base*> agents = MTHREAD->SAM->getAgentsByType(false);
00521 vector<int> landTypes = MTHREAD->RD->getIntVectorSetting("landTypes");
00522 vector<string> objNames = MTHREAD->RD->getObjectNames();
00523 vector<string> actNames = MTHREAD->RD->getActivityNames();
00524 vector<int> agrSoilTypes = MTHREAD->RD->getIntVectorSetting("agrLandTypes");
00525 vector<double> agrRPrices = MTHREAD->RD->getDoubleVectorSetting("agrRentalPrices");
00526 vector <Manager_base*> listOfManagers = MTHREAD->SAM->getManagers();
00527
00528
00529 string out;
00530 vector<string> data;
00531 int agSize = agents.size();
00532
00533 double SUMcapital=0;
00534 double SUMlandCapital=0;
00535 double SUMobjectsCapital=0;
00536 double SUMownLabour=0;
00537 double SUMcompanyLabour=0;
00538 double SUMliquidity=0;
00539 double SUMnetProductionProfit=0;
00540 double SUMcoupledPremiums=0;
00541 double SUMdecoupledPayment=0;
00542 double SUMlastSunkCostsPaid=0;
00543 double SUMcompanyProfit=0;
00544 double SUMcompanyProfitWSupport=0;
00545 double SUMincomes=0;
00546 double SUMsurveyUnusedLand=0;
00547
00548 vector <int> SUMownedPlotsByAllTypes (landTypes.size(), 0);
00549 vector <int> SUMrentedPlotsByAllTypes (landTypes.size(), 0);
00550 vector <int> SUMavailableObjectsByType (objNames.size(), 0);
00551 vector <int> SUMnewlyAvailableObjectsByType (objNames.size(), 0);
00552 vector <double> SUMactivitiesByType (actNames.size(), 0);
00553 vector < vector <int> > SUMagentsByTypeAndMould;
00554
00555 for(uint i=0;i<listOfManagers.size();i++){
00556 int nMoulds = listOfManagers[i]->getMouldLocalIDCounter();
00557 vector <int> moulds (nMoulds,0);
00558 SUMagentsByTypeAndMould.push_back(moulds);
00559 }
00560
00561
00562
00563 for(uint i=0;i<surveys.size();i++){
00564 SUMcapital += surveys[i].capital;
00565 SUMlandCapital += surveys[i].landCapital;
00566 SUMobjectsCapital += surveys[i].objectsCapital;
00567 SUMownLabour += surveys[i].labour;
00568 SUMcompanyLabour += surveys[i].labour + getSurveyActivityByName(surveys[i], "hiredLabour") - getSurveyActivityByName(surveys[i], "offFarmLabour");
00569 SUMliquidity += surveys[i].liquidity;
00570 SUMnetProductionProfit += surveys[i].netProductionProfit;
00571 SUMcoupledPremiums += surveys[i].coupledPremiums;
00572 SUMdecoupledPayment += surveys[i].decoupledPayment;
00573 SUMlastSunkCostsPaid += surveys[i].lastSunkCostsPaid;
00574 SUMcompanyProfit += surveys[i].netProductionProfit
00575 - getSurveyActivityByName(surveys[i], "offFarmLabour")* MTHREAD->RD->getDoubleSetting("labourShadowPrice", DATA_NOW)
00576 - surveys[i].lastSunkCostsPaid;
00577 SUMcompanyProfitWSupport += surveys[i].netProductionProfit
00578 - getSurveyActivityByName(surveys[i], "offFarmLabour")* MTHREAD->RD->getDoubleSetting("labourShadowPrice", DATA_NOW)
00579 - surveys[i].lastSunkCostsPaid
00580 + surveys[i].coupledPremiums
00581 + surveys[i].decoupledPayment;
00582 SUMincomes += surveys[i].netProductionProfit
00583 + surveys[i].coupledPremiums
00584 + surveys[i].decoupledPayment
00585 - surveys[i].lastSunkCostsPaid;
00586
00587
00588 for(uint j=0;j<landTypes.size();j++){
00589 SUMownedPlotsByAllTypes.at(j) += surveys[i].ownedPlotsByAllTypes.at(j);
00590 SUMrentedPlotsByAllTypes.at(j) += surveys[i].rentedPlotsByAllTypes.at(j);
00591 }
00592 for(uint j=0;j<objNames.size();j++){
00593 SUMavailableObjectsByType.at(j) += surveys[i].availableObjectsByType.at(j);
00594 SUMnewlyAvailableObjectsByType.at(j) += surveys[i].newlyAvailableObjectsByType.at(j);
00595 }
00596 for(uint j=0;j<actNames.size();j++){
00597 SUMactivitiesByType.at(j) += surveys[i].activitiesByType.at(j);
00598 }
00599 for(uint j=0;j<listOfManagers.size();j++){
00600 int managerID =listOfManagers[j]->getManagerID();
00601 if(surveys[i].managerID == managerID){
00602 SUMagentsByTypeAndMould.at(j).at(surveys[i].agentMouldID)++;
00603 break;
00604 }
00605 }
00606 SUMsurveyUnusedLand += getSurveyUnusedLand(surveys[i]);
00607 }
00608
00609 data.push_back(i2s(MTHREAD->SCD->getYear()));
00610 data.push_back("");
00611 for (uint i=0;i<agentCounts.size();i++){
00612 data.push_back(i2s(agentCounts.at(i)));
00613 for(uint j=0;j<SUMagentsByTypeAndMould.at(i).size();j++){
00614 data.push_back(i2s(SUMagentsByTypeAndMould.at(i).at(j)));
00615 }
00616 }
00617 data.push_back(d2s(SUMcapital/agSize));
00618 data.push_back(d2s(SUMlandCapital/agSize));
00619 data.push_back(d2s(SUMobjectsCapital/agSize));
00620 data.push_back(d2s(SUMownLabour/agSize));
00621 data.push_back(d2s(SUMcompanyLabour/agSize));
00622 data.push_back(d2s(SUMliquidity/agSize));
00623 data.push_back(d2s(SUMnetProductionProfit/agSize));
00624 data.push_back(d2s(SUMcoupledPremiums/agSize));
00625 data.push_back(d2s(SUMdecoupledPayment/agSize));
00626 data.push_back(d2s(SUMlastSunkCostsPaid/agSize));
00627 data.push_back(d2s(SUMcompanyProfit/agSize));
00628 data.push_back(d2s(SUMcompanyProfitWSupport/agSize));
00629 data.push_back(d2s(SUMincomes/agSize));
00630
00631 data.push_back("");
00632 for (uint i=0;i<agrSoilTypes.size();i++){
00633 data.push_back(d2s(agrRPrices.at(i)));
00634 }
00635
00636 data.push_back(d2s(SUMsurveyUnusedLand + ((double) iVSum(unpossessedAgrLandByType)) * MTHREAD->GIS->getHaByPixel() ));
00637
00638 data.push_back(i2s(iVSum(abandonedAgrLandByType)));
00639 for (uint i=0;i<agrSoilTypes.size();i++){
00640 data.push_back(i2s(abandonedAgrLandByType.at(i)));
00641 }
00642 data.push_back(i2s(iVSum(idleAgrLandByType)));
00643 for (uint i=0;i<agrSoilTypes.size();i++){
00644 data.push_back(i2s(idleAgrLandByType.at(i)));
00645 }
00646 data.push_back(i2s(iVSum(SUMownedPlotsByAllTypes)+iVSum(SUMrentedPlotsByAllTypes)));
00647 data.push_back(i2s(iVSum(SUMownedPlotsByAllTypes)));
00648 for (uint i=0;i<landTypes.size();i++){
00649 if(MTHREAD->GIS->isAgrCode(landTypes[i]))
00650 data.push_back(i2s(SUMownedPlotsByAllTypes.at(i)));
00651 }
00652 data.push_back(i2s(iVSum(SUMrentedPlotsByAllTypes)));
00653 for (uint i=0;i<landTypes.size();i++){
00654 if(MTHREAD->GIS->isAgrCode(landTypes[i]))
00655 data.push_back(i2s(SUMrentedPlotsByAllTypes.at(i)));
00656 }
00657 data.push_back(i2s(iVSum(SUMavailableObjectsByType)));;
00658 for (uint i=0;i<objNames.size();i++){
00659 data.push_back(i2s(SUMavailableObjectsByType.at(i)));
00660 }
00661 data.push_back(i2s(iVSum(SUMnewlyAvailableObjectsByType)));
00662 for (uint i=0;i<objNames.size();i++){
00663 data.push_back(i2s(SUMnewlyAvailableObjectsByType.at(i)));
00664 }
00665 data.push_back("");
00666 for (uint i=0;i<actNames.size();i++){
00667 data.push_back(d2s(SUMactivitiesByType.at(i)));
00668 }
00669
00670 if (MTHREAD->RD->getBoolSetting("outputAgentAggregatedHumanReadable")){
00671 inOutFileName = MTHREAD->RD->getBaseDirectory()
00672 + MTHREAD->RD->getOutputDirectory()
00673 + "results/aggregatedOutput_"
00674 + MTHREAD->getScenarioName()
00675 + MTHREAD->RD->getStringSetting("outputFileExtension");
00676 ifstream inFile (inOutFileName.c_str(), ios::in);
00677 if (!inFile){ msgOut(MSG_CRITICAL_ERROR,"Error in opening the file "+inOutFileName+" for reading.");}
00678 for(uint i=0;i<data.size();i++) {
00679 string tempString;
00680 getline (inFile,tempString);
00681 out.append(tempString);
00682 out.append(data[i]+d);
00683 out.append("\n");
00684 }
00685 inFile.close();
00686
00687 ofstream outFile (inOutFileName.c_str(), ios::out);
00688 if (!outFile){ msgOut(MSG_CRITICAL_ERROR,"Error in opening the file "+inOutFileName+" for writing.");}
00689 outFile << out ;
00690 outFile.close();
00691 } else {
00692
00693 inOutFileName = MTHREAD->RD->getBaseDirectory()
00694 + MTHREAD->RD->getOutputDirectory()
00695 + "results/aggregatedOutput"
00696
00697 + MTHREAD->RD->getStringSetting("outputFileExtension");
00698
00699 ofstream outFile (inOutFileName.c_str(), ios::app);
00700 if (!outFile){ msgOut(MSG_CRITICAL_ERROR,"Error in opening the file "+inOutFileName+" for writing.");}
00701 outFile << MTHREAD->getScenarioName() << d ;
00702 for (uint i=0;i<data.size();i++){
00703 outFile << data[i] << d ;
00704 }
00705 outFile << "\n" ;
00706 outFile.close();
00707 }
00708 }
00709
00710 void
00711 Output::printMaps(){
00712 if(MTHREAD->RD->getIntSetting("outputLevel")<OUTVL_MAPS) return;
00713 MTHREAD->GIS->printLayers();
00714 if(MTHREAD->RD->getIntSetting("outputLevel")<OUTVL_BINMAPS) return;
00715 MTHREAD->GIS->printBinMaps();
00716
00717 }
00718
00719 void
00720 Output::printDetailedData(){
00721 if(MTHREAD->RD->getIntSetting("outputLevel")<OUTVL_DETAILED) return;
00722
00723 char d = getOutputFieldDelimiter();
00724 vector<int> landTypes = MTHREAD->RD->getIntVectorSetting("landTypes");
00725
00726 vector<string> objNames = MTHREAD->RD->getObjectNames();
00727 vector<string> actNames = MTHREAD->RD->getActivityNames();
00728
00729 for(uint i=0;i<surveys.size();i++){
00730 string out;
00731 string agName= i2s(surveys[i].agentID);
00732 string surveyID;
00733 surveyID = i2s(MTHREAD->RD->getScenarioIndex())+i2s(surveys[i].year)+i2s(surveys[i].agentID);
00734
00735 double companyLabour = surveys[i].labour + getSurveyActivityByName(surveys[i], "hiredLabour") - getSurveyActivityByName(surveys[i], "offFarmLabour");
00736 double companyProfit =
00737 surveys[i].netProductionProfit
00738 - getSurveyActivityByName(surveys[i], "offFarmLabour")* MTHREAD->RD->getDoubleSetting("labourShadowPrice", DATA_NOW)
00739 - surveys[i].lastSunkCostsPaid;
00740 double companyProfitWithSupport =
00741 companyProfit
00742 + surveys[i].coupledPremiums
00743 + surveys[i].decoupledPayment;
00744 double incomes =
00745 surveys[i].netProductionProfit
00746 + surveys[i].coupledPremiums
00747 + surveys[i].decoupledPayment
00748 - surveys[i].lastSunkCostsPaid;
00749
00750 vector <string> sData;
00751 sData.push_back(surveyID);
00752 sData.push_back(i2s(surveys[i].year));
00753 sData.push_back(surveys[i].agentType);
00754 sData.push_back(i2s(surveys[i].agentID));
00755 sData.push_back(i2s(surveys[i].agentMouldID));
00756 sData.push_back(b2s(surveys[i].spatial));
00757 sData.push_back(d2s(surveys[i].hostPlotID));
00758 sData.push_back(i2s(surveys[i].hostPlotX));
00759 sData.push_back(i2s(surveys[i].hostPlotY));
00760 sData.push_back(d2s(surveys[i].capital));
00761 sData.push_back(d2s(surveys[i].landCapital));
00762 sData.push_back(d2s(surveys[i].objectsCapital));
00763 sData.push_back(d2s(surveys[i].labour));
00764 sData.push_back(d2s(companyLabour));
00765 sData.push_back(d2s(surveys[i].liquidity));
00766 sData.push_back(d2s(surveys[i].netProductionProfit));
00767 sData.push_back(d2s(surveys[i].coupledPremiums));
00768 sData.push_back(d2s(surveys[i].decoupledPayment));
00769 sData.push_back(d2s(surveys[i].lastSunkCostsPaid));
00770
00771 sData.push_back(d2s(companyProfit));
00772 sData.push_back(d2s(companyProfitWithSupport));
00773 sData.push_back(d2s(incomes));
00774
00775 sData.push_back(i2s(iVSum(surveys[i].ownedPlotsByAllTypes)+iVSum(surveys[i].rentedPlotsByAllTypes) ));
00776 sData.push_back(i2s(iVSum(surveys[i].ownedPlotsByAllTypes)));
00777 for(uint j=0;j<landTypes.size();j++){
00778 if(MTHREAD->GIS->isAgrCode(landTypes[j]))
00779 sData.push_back(i2s(surveys[i].ownedPlotsByAllTypes.at(j)));
00780 }
00781
00782 sData.push_back(i2s(iVSum(surveys[i].rentedPlotsByAllTypes)));
00783 for(uint j=0;j<landTypes.size();j++){
00784 if(MTHREAD->GIS->isAgrCode(landTypes[j]))
00785 sData.push_back(i2s(surveys[i].rentedPlotsByAllTypes.at(j)));
00786 }
00787 sData.push_back(i2s(iVSum(surveys[i].ownedObjectsByType)));
00788 for(uint j=0;j<objNames.size();j++){
00789 sData.push_back(i2s(surveys[i].ownedObjectsByType.at(j)));
00790 }
00791 sData.push_back(i2s(iVSum(surveys[i].availableObjectsByType)));
00792 for(uint j=0;j<objNames.size();j++){
00793 sData.push_back(i2s(surveys[i].availableObjectsByType.at(j)));
00794 }
00795 sData.push_back(i2s(iVSum(surveys[i].newlyAvailableObjectsByType)));
00796 for(uint j=0;j<objNames.size();j++){
00797 sData.push_back(i2s(surveys[i].newlyAvailableObjectsByType.at(j)));
00798 }
00799 sData.push_back("");
00800 for(uint j=0;j<actNames.size();j++){
00801 sData.push_back(d2s(surveys[i].activitiesByType.at(j)));
00802 }
00803 sData.push_back(d2s(getSurveyUnusedLand(surveys[i])));
00804
00805 if(MTHREAD->RD->getBoolSetting("outputAgentDetailsHumanReadable")){
00806
00807 string inOutFileName = MTHREAD->RD->getBaseDirectory()
00808 + MTHREAD->RD->getOutputDirectory()
00809 + "results/ag_"
00810 + agName
00811 + "_"
00812 + MTHREAD->getScenarioName()
00813 + MTHREAD->RD->getStringSetting("outputFileExtension");
00814
00815 ifstream inFile (inOutFileName.c_str(), ios::in);
00816 if (!inFile){ msgOut(MSG_CRITICAL_ERROR,"Error in opening the file "+inOutFileName+" for reading.");}
00817 for(uint i=0;i<sData.size();i++) {
00818 string tempString;
00819 getline (inFile,tempString);
00820 out.append(tempString);
00821 out.append(sData[i]+d);
00822 out.append("\n");
00823 }
00824 inFile.close();
00825
00826 ofstream outFile (inOutFileName.c_str(), ios::out);
00827 if (!outFile){ msgOut(MSG_CRITICAL_ERROR,"Error in opening the file "+inOutFileName+" for writing.");}
00828 outFile << out ;
00829 outFile.close();
00830 } else {
00831 string outFileName = MTHREAD->RD->getBaseDirectory()
00832 + MTHREAD->RD->getOutputDirectory()
00833 + "results/agentsDetails"
00834
00835 + MTHREAD->RD->getStringSetting("outputFileExtension");
00836 ofstream outFile (outFileName.c_str(), ios::app);
00837 if (!outFile){ msgOut(MSG_CRITICAL_ERROR,"Error in opening the file "+outFileName+".");}
00838 outFile << MTHREAD->getScenarioName() << d ;
00839 for (uint i=0;i<sData.size();i++){
00840 outFile << sData[i] << d ;
00841 }
00842 outFile << "\n" ;
00843 outFile.close();
00844 }
00845 }
00846 }
00847
00848 void
00849 Output::printDebugMatrices(){
00850 if(MTHREAD->RD->getIntSetting("outputLevel")<OUTVL_ALL) return;
00851 vector <Agent_base*> agents = MTHREAD->SAM->getAgentsByType(false);
00852 for (uint i=0;i<agents.size();i++){
00853 agents.at(i)->debugOptProblem();
00854 }
00855 }
00856
00857 void
00858 Output::printLeavingAgent(Agent_base* agent, int cause){
00859 if(MTHREAD->RD->getIntSetting("outputLevel")<OUTVL_DETAILED) return;
00860
00861 string outFileName = MTHREAD->RD->getBaseDirectory()
00862 + MTHREAD->RD->getOutputDirectory()
00863 + "results/leavingAgents"
00864
00865
00866 + MTHREAD->RD->getStringSetting("outputFileExtension");
00867 char d = getOutputFieldDelimiter();
00868 string causeStr;
00869 double minEqC = MTHREAD->RD->getDoubleSetting("minEC");
00870
00871 double netProductionProfit = agent->getNetProductionProfit();
00872 double grossCoupledPremiums = agent->getGrossCoupledPremiums();
00873 double grossDecoupledPayment = agent->getGrossDecoupledPayment();
00874 double netTotalPremium = agent->getNetTotalPremium();
00875
00876 double netCoupledPremiums = grossCoupledPremiums * netTotalPremium / (grossCoupledPremiums+grossDecoupledPayment);
00877 double netDecoupledPayment = grossDecoupledPayment * netTotalPremium / (grossCoupledPremiums+grossDecoupledPayment);
00878
00879 double sunkCostsPaid = agent->getSunkCostsPaid();
00880 double liquidityShPrice = agent->getLiquidityShPrice();
00881 double labourShPrice = agent->getLabourShPrice();
00882 double landShPrice = agent->getLandShPrice();
00883
00884 switch (cause) {
00885 case AGEXIT_FALSE:
00886 msgOut(MSG_CRITICAL_ERROR, "Why we are printing as leaving the model the record of an agent that is NOT leaving? Something wrong in the code!");
00887 break;
00888 case AGEXIT_NOCAPITAL:
00889 causeStr = "NOCAPITAL";
00890 break;
00891 case AGEXIT_DEBS:
00892 causeStr = "DEBS";
00893 break;
00894 case AGEXIT_SHPRICES:
00895 causeStr = "SHPRICES";
00896 break;
00897 default:
00898 msgOut(MSG_ERROR, "You have specified an unknow cause of agent leaving the model.");
00899 causeStr = "UNKNOW";
00900 }
00901 ofstream out(outFileName.c_str(), ios::app);
00902 if (!out){ msgOut(MSG_CRITICAL_ERROR,"Error in opening the file "+outFileName+".");}
00903 out << MTHREAD->getScenarioName() << d ;
00904 out << MTHREAD->SCD->getYear() << d
00905 << agent->getAgentUniqueID() << d
00906 << agent->getCategory() << d
00907 << agent->getMouldLocalID() << d
00908 << causeStr << d
00909 << agent->getNPlots() << d
00910 << agent->getNPlots(PLOTS_OWNED) << d
00911 << agent->getNPlots(PLOTS_RENTED) << d
00912 << agent->getCapital() << d
00913 << agent->getLiquidity() << d
00914 << ( agent->getCapital() * (1-minEqC)/minEqC) << d
00915 << netProductionProfit << d
00916 << netCoupledPremiums << d
00917 << netDecoupledPayment << d
00918 << sunkCostsPaid << d
00919 << liquidityShPrice << d
00920 << labourShPrice << d
00921 << landShPrice << d
00922 << netProductionProfit + netCoupledPremiums + netDecoupledPayment - sunkCostsPaid << d
00923 << liquidityShPrice + labourShPrice + landShPrice << d << "\n";
00924
00925 out.close();
00926 }
00927
00928 char
00929 Output::getOutputFieldDelimiter(){
00930 int delimiterID = MTHREAD->RD->getIntSetting("outputFieldDelimiter");
00931 switch (delimiterID) {
00932 case 1:
00933 return ',';
00934 break;
00935 case 2:
00936 return ';';
00937 break;
00938 case 3:
00939 return ':';
00940 break;
00941 case 4:
00942 return '\t';
00943 break;
00944 case 5:
00945 return ' ';
00946 break;
00947 default:
00948 msgOut(MSG_ERROR, "You have specified an unknow output file field delimiter. Using \";\".");
00949 return ',';
00950 }
00951 }
00952
00960 void
00961 Output::cleanScenario(string fileName, string scenarioName, char d){
00962 string dStr(&d,1);
00963 vector <string> rows;
00964 string tempRow;
00965 ifstream inFile (fileName.c_str(), ios::in);
00966 if (!inFile){
00967 msgOut(MSG_ERROR,"Error in opening the file "+fileName+" for reading.");
00968 return;
00969 }
00970 while( getline (inFile,tempRow) ){
00971 vector<string> tokens;
00972 tokenize(tempRow,tokens,dStr);
00973 if(tokens[0] != scenarioName)
00974 rows.push_back( tempRow );
00975 }
00976 inFile.close();
00977 ofstream out(fileName.c_str(), ios::out);
00978 for(uint i=0;i<rows.size();i++){
00979 out << rows[i];
00980 out << "\n";
00981 }
00982
00983
00984
00985
00986
00987
00988
00989
00990
00991
00992
00993
00994
00995
00996
00997
00998
00999
01000
01001
01002
01003
01004
01005
01006
01007
01008
01009
01010
01011
01012 }
01013
01014 double
01015 Output::getSurveyActivityByName(const AgentSurvey& survey, const string& actName){
01016
01017 vector <string> actNames = MTHREAD->RD->getActivityNames();
01018 for (uint i=0;i<actNames.size();i++){
01019 if (actName == actNames[i]){
01020 return survey.activitiesByType.at(i);
01021 }
01022 }
01023 msgOut(MSG_CRITICAL_ERROR, "in function getSurveyActivityByName() activity named "+actName+" not found.");
01024 return 0;
01025 }
01026
01027 double
01028 Output::getSurveyUnusedLand(const AgentSurvey& survey){
01029 double usedLand = 0;
01030 vector <RegActivities*> activities = MTHREAD->RD->getRegActivities();
01031 for (uint i=0;i<activities.size();i++){
01032 if (activities[i]->getSpatiallyExplicit()){
01033 usedLand += survey.activitiesByType.at(i);
01034 }
01035 }
01036 return ( (double) iVSum(survey.ownedPlotsByAllTypes)+iVSum(survey.rentedPlotsByAllTypes)) - usedLand;
01037
01038 }