-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessSPECIFIC.php
More file actions
270 lines (235 loc) · 13.2 KB
/
processSPECIFIC.php
File metadata and controls
270 lines (235 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
<?php
function getTime()
{
$a = explode (' ',microtime());
return(double) $a[0] + $a[1];
}
$time1 = getTime();
$time2 = 0;
$time3 = 0;
$looptimer = getTime();
/* THIS IS A COPY OF ESSENTIALS FROM CONFIG.PHP IN ORDER TO MINIMISE PAGE LOAD TIME ON THIS FILE.*/
include('libs/mongorecord/BaseMongoRecord.php');
include('models/SimulationModel.php');
include('models/AgentsModel.php');
include('models/AgentStateModel.php');
include('models/ResultModel.php');
define ("DB", "presage");
define ("SIMTREE", "simulations");
define ("LOCAL_HOST", "127.0.0.1:27017");
//define ("REMOTE_HOST", "155.198.117.244:27017");
define ("REMOTE_HOST", "46.137.119.122:27017");
session_start();
if(isset($_SESSION['database'])) {
if($_SESSION['database']=='remote') {
define ("HOST", REMOTE_HOST);
} else {
$_SESSION['database']='local';
define ("HOST", LOCAL_HOST);
}
} else {
$_SESSION['database']='local';
define ("HOST", LOCAL_HOST);
}
try {
BaseMongoRecord::$connection = new Mongo(HOST);
BaseMongoRecord::$database = 'presage';
} catch(MongoConnectionException $e) {
//var_dump($e);
}
define ("NOTICE_1", "change in kyoto state, before, after, ticknumber");
/* END OF ESSENTIALS FROM CONFIG.PHP IN ORDER TO MINIMISE PAGE LOAD TIME ON THIS FILE.*/
/*
* Some essential items:
*
* tick - each row of an agent's agentstate
* year - starts at 0
* quarter - { 0 , 1 , 2 , 3 }
*
*/
//Load SIM ID
if (isset($_GET['simid'])&&(isset($_GET['iso']))) {
//Get simulation parameters
$simulation = new SimulationModel(); // instantiate collection model
$agents = new AgentsModel(); // instantiate collection model
$sim = $simulation->findOne(array("_id" => (int)$_GET['simid']));
$simID = new MongoInt64($sim->getID()); // ensure simID is of the correct type
$simprop = $sim->getParameters();
//Process-wide definitions
define ("TICK_YEAR", $simprop['TICK_YEAR']);
//define ("TICK_LENGTH", $simprop['finishTime']);
define ("TICK_LENGTH", $sim->getCurrentTime());
define ("YEARS", floor(TICK_LENGTH/TICK_YEAR));
//define ("OUTPUTFORM", 'HTTPREQUEST');
define ("OUTPUTFORM", 'JSON');
define ("REPORT", 2);
if(REPORT>2)echo 'Number of YEARS: '.YEARS .'<br>';
//Float variable of how many ticks in a quarter
$tickquarter = ((int)TICK_YEAR)/4;
//Define the database queries by using the following loops.
for ($year = 0; $year<YEARS; $year++) {
$quarter[$year][0] = array ('offset' => ($year*TICK_YEAR)+(int)0, 'limit' => (int)floor($tickquarter));
$quarter[$year][1] = array ('offset' => ($year*TICK_YEAR)+(int)floor($tickquarter), 'limit' =>(int)floor($tickquarter));
$quarter[$year][2] = array ('offset' => ($year*TICK_YEAR)+(int)(floor($tickquarter)*2), 'limit' =>(int)floor($tickquarter));
$quarter[$year][3] = array ('offset' => ($year*TICK_YEAR)+(int)(floor($tickquarter)*3), 'limit' =>(int)(TICK_YEAR-(floor($tickquarter)*3)));
}
if(REPORT>2)var_dump($quarter); //Shows the database queries to be made against agents.
// Very first simulation query, initialise everything.
$count = $agents->find(array("simID"=>$simID));
$agentCount = $count->count(); // How many agents we have
$steps = $agentCount*4*YEARS; // Total Steps in simulation - could save a few lookups in future.
$agentSteps = YEARS*4; // How many steps each agent takes
if(REPORT>2)echo 'Number of agents: '.$agentCount .'<br>';
if(REPORT>2)echo 'Number of steps: '.$steps .'<br>';
$currentQuarter = 0; // Initialise first quarter.
if(REPORT>2)echo 'CurrentQuarter '.$currentQuarter.'<br>';
$agentslist = $agents->findOne(array("simID" => (float) $_GET['simid'], "ISO"=>$_GET['iso']),array('sort' => array('_id' => 1), 'offset' => 0, 'limit' => 1));
$progressCount = 0; // Current Step
$ensureAgentState = new AgentStateModel(); // instantiate collection model
$ensureAgentState->ensureIndex(array('aid'=>1));
var_dump($agentslist);
$info = array();
if(REPORT>2)echo "HELLO<br>";
$year = (int)floor((($progressCount)%($agentSteps)/4)); // Calculate the current year
if(REPORT>2)echo "agentsteps ".$agentSteps . "# progresscount#".$progressCount."<br>";
if(REPORT>2)echo "YEAR ".$year . "# Quarter #".$currentQuarter."<br>";
$ticksInQuarter = $quarter[$year][$currentQuarter]['limit']; // Set how many ticks are in this quarter.
$outputARY = array();
// foreach ($agentslist as $agent) {
$agent= $agentslist;
// EACH COUNTRY AS AGENT
$agentProperties = $agent->getProperties();
$iso = $agentProperties['ISO'];
$kyotostate = 'undefined';
$time2 = getTime();
$outputARY['timea'] = number_format(($time2-$time1),2);
$finishloop = false;
//CHECK FOR RECORD ALREADY INCASE ACCIDENTALLY REPEAT REQUEST:
$counter = 0;
$totalASQ = new AgentStateModel(); // instantiate collection model
$totalAS = $totalASQ->find(array("aid"=>$agent->getAid()));
$totalticks = $totalAS->count();
//Implement check here asap
$resultcheckq = new ResultModel(); // instantiate collection model
$resultcheck = $resultcheckq->findOne(array("simID" => (int)$_GET['simid'], "ISO" => $iso, 'year'=> (int)$year,'quarter'=> (int)$currentQuarter));
if (is_null($resultcheck)) {
//echo 'no record exists<bR>';
} else {
$outputARY['totalAgents'] = $steps;
$progressCount++; //Increment the step counter
if ($progressCount===$steps) {
$outputARY['success'] = 'complete';
} else {
$outputARY['success'] = 'alreadycomplete';
$outputARY['nextAgent'] = $progressCount;
$outputARY['percentage'] = (int)(($progressCount/$steps)*100);
}
header('content-type: application/json');
echo json_encode($outputARY);
die();
}
/*
* BEGIN LOOPING UNTIL FINISHED OR THE AGENT HAS COMPLETED
*/
while (($looptimer-$time1 < 20) && $counter < $totalticks){//&& (!$finishloop)) {
$year = (int)floor((($progressCount)%($agentSteps)/4));
if(REPORT>2)"YEAR ".$year . "# dave<br>"; //die();
$ticksInQuarter = $quarter[$year][$currentQuarter]['limit'];
$notices = array();
$cheated = false;
$as = new AgentStateModel(); // instantiate collection model
$agentstate = $as->find(
array("aid"=>$agent->getAid()),
array('sort' => array('_id' => 1),
'offset' => $quarter[$year][$currentQuarter]['offset'],
'limit' => (int)$quarter[$year][$currentQuarter]['limit']
)
); // Sets up query to process a quarter
if(REPORT>2)echo 'query offset '.$quarter[$year][$currentQuarter]['offset'].'<br>';
if(REPORT>2)echo 'query limit '.$quarter[$year][$currentQuarter]['limit'].'<br>';
foreach ($agentstate as $ag) {
$counter++;
if(REPORT>2)'<br>'.$ag->getTime().'GOTTIME<br>';
$countryArray = array();
$tick = $ag->getTime();
if(REPORT>2)echo 'FOUND TIME '.$tick.'<br>';
$agentTickProperties = $ag->getProperties(); //Get the tick specific agent data
// Kyoto MEMBER LEAVE OR CHANGE?
if ($agentTickProperties['is_kyoto_member'] != $kyotostate) {
if ($kyotostate=='undefined'){
//Initialise kyotostate so do nothing
} else {
// A legitimate change so make a record of this
$notices[] = array(NOTICE_1, $kyotostate, $agentTickProperties['is_kyoto_member'], $tick);
}
$kyotostate = $agentTickProperties['is_kyoto_member'];
}
// Cheating?
if ($agentTickProperties['cheated']='cheated') {
$cheated = true;
}
if(REPORT>2)echo 'Ticks in Quarter'.$ticksInQuarter.'<br>';
if(REPORT>2)echo 'Ticks in Quarter -1:'.(int)($ticksInQuarter-1).'<br>';
if(REPORT>2)echo 'Ticks'.$tick.'<br>';
if ($tick == 0) {
//FIRST DAY
} elseif ($tick % ($ticksInQuarter) == ($ticksInQuarter-1)) {
if(REPORT>2)echo 'MODRESULT:'.$tick % ($ticksInQuarter-1).'<br>';
// Last Day of the quarter
// Here is the year round up. Save and ting
if(REPORT>2)echo 'Save the damn record';
$countryArray['simID'] = (int)$_GET['simid'];//$simID;
$countryArray['tick'] = $tick;
$countryArray['quarter'] = $currentQuarter;
$countryArray['ISO'] = $iso;
$countryArray['year'] = $year;
$countryArray['GDP'] = (double)$agentTickProperties['gdp'];
$countryArray['GDPRate'] = (double)$agentTickProperties['gdp_rate'];
$countryArray['availableToSpend'] = (double)$agentTickProperties['available_to_spend'];
$countryArray['emissionsTarget'] = (double)$agentTickProperties['emission_target'];
$countryArray['carbonOffset'] = (double)$agentTickProperties['carbon_offset'];
$countryArray['carbonOutput'] = (double)$agentTickProperties['carbon_output'];
$countryArray['energyOutput'] = (double)$agentTickProperties['energy_output'];
$countryArray['arableLandArea'] = (double)$agentTickProperties['arable_land_area'];
$countryArray['carbonAbsorption'] = (double)$agentTickProperties['carbon_absorption'];
$countryArray['isKyotoMember'] = $agentTickProperties['is_kyoto_member'];
$countryArray['cheated'] = $cheated;
$countryArray['notices'] = $notices;
if(REPORT>1) echo $dave++.'Created row: '.$iso.' Y:'.$year.' Q:'.$currentQuarter.'<br>';
$notices = array();
$result = new ResultModel($countryArray); // instantiate collection model
$result->save();
} else {
// All other days of the quarter (perhaps adding and shit)
}
} //End of DAYs
//AT END O' DAY
$time3 = getTime();
//Make JSON response
$outputARY['ISO'] = $iso;
$outputARY['totalAgents'] = $steps;
$progressCount++; //Increment the step counter
if ($progressCount===$steps) {
$outputARY['success'] = 'complete';
} else {
$outputARY['timeb'] = number_format(($time3-$time2),2);
$outputARY['success'] = 'true';
$outputARY['nextAgent'] = $progressCount;
$outputARY['percentage'] = (int)(($progressCount/$steps)*100);
}
$looptimer = getTime();
if(REPORT>2)echo 'CURRENT QUARTER PRE '.$currentQuarter;
$currentQuarter = $progressCount%4;
if(REPORT>2)echo 'CURRENT QUARTER POST'.$currentQuarter;
$cheated = false;
} // end of while time loop
//}//END OF AGENT
} else {
echo 'No sim ID mate.';
}
$outputARY['info']=$info;
IF (OUTPUTFORM == 'JSON') {
if(!(REPORT>0))header('content-type: application/json');
echo json_encode($outputARY);
}
?>