PageRenderTime 67ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/rerun.php

https://bitbucket.org/siosonel/flora
PHP | 209 lines | 123 code | 45 blank | 41 comment | 29 complexity | e16b6689d47426310ce1a1f7ca067bbe MD5 | raw file
  1. <?php
  2. /*
  3. Message passing based simulation core engine
  4. Edgar Sioson, 10/29/2011
  5. */
  6. header("content-type: text/plain");
  7. if ($_GET["timeLimit"]) set_time_limit($_GET['timeLimit']);
  8. $time = time(); echo "\nTime: $time\n";
  9. /***********************************************************
  10. ** Parse rerun request
  11. ************************************************************/
  12. if (!$runName = $_GET['runName'] OR !$filename = $_GET['filename']) exit("Invalid runName='$runName' and/or filename='$filename'.");
  13. //the report file has gneral metadata about the run
  14. $report = json_decode(file_get_contents("results/$runName/report/$filename")); //print_r($report); exit();
  15. if (!$report) exit("Invalid report format.");
  16. if ($report->GETarray) $_GET = (array)$report->GETarray;
  17. $base = $report->versionInfo->base; //print_r($report->versionInfo);
  18. if ($changes = $report->versionInfo->changes) exit("In order to rerun $runname/$filename, the following diff must be manually applied to version $base:\n$changes");
  19. else if ($_GET['version']) echo "Will automatically revert to specified ". $_GET['version'] ." version.\n";
  20. else echo "Will automatically revert to $base version.\n";
  21. //will need to redefine configuration constants
  22. foreach($report->defConstants AS $param => $value) {
  23. if (!is_numeric($value)) $value = "\"$value\"";
  24. $config .= "define(\"$param\",$value);\n";
  25. }
  26. //the addr files have more details
  27. $addr = json_decode(file_get_contents("results/$runName/addr/$filename"));
  28. if (!$addr) exit("Invalid addr format."); //print_r($addr);
  29. if (!$report->GETarray) {
  30. $_GET['maxCycle'] = $report->maxCycle;
  31. $_GET['initBal'] = $addr->ocaup->initBal;
  32. if (!$_GET['onHgChange']) $_GET['onHgChange'] = "copy";
  33. }
  34. $runName .= "_". $filename; // .".". $time;
  35. if (!is_dir("results/reruns/")) mkdir("results/reruns");
  36. /**********************************************
  37. ** Load class files
  38. ***********************************************/
  39. eval($config);
  40. include_once "utils/Error.php";
  41. include_once "utils/Util.php";
  42. include_once "utils/msgFunc.php";
  43. include_once "utils/RandomInit_for_rerun.php";
  44. include_once "testbed/Person.php";
  45. include_once "testbed/Rsrc.php";
  46. include_once "testbed/Location.php";
  47. include_once "testbed/Waste.php";
  48. include_once "testbed/Producer.php";
  49. include_once "testbed/Consumer.php";
  50. include_once "testbed/Report.php";
  51. include_once "infras/Ocaup.php";
  52. include_once "infras/Lets.php";
  53. include_once "infras/Inventory.php";
  54. include_once "infras/BrandIndex.php";
  55. /**********************************************
  56. ** Set up version tracking
  57. ***********************************************/
  58. //catch changes using Mercurial, exit or copy -> see included Util.php
  59. $versionInfo = Util::onHgChange($runName,$time); //if no "&onHgChange=" value is provided, will just return and do nothing
  60. /**********************************************
  61. ** Prepare for simulation run
  62. ***********************************************/
  63. //set up infrastructure
  64. $Ocaup = new Ocaup("ocaup","inven");
  65. $Lets = new Lets("lets","inven");
  66. $Inven = new Inventory("inven");
  67. $Index = new BrandIndex("index", array("ocaup","lets"), array("inven"), 1, 50);
  68. //set up locations
  69. foreach($addr AS $name => $obj) {
  70. if ($obj->rsrc) {
  71. $L[ $obj->id ] = new Location($obj->id);
  72. $rsrcSet[] = $obj->rsrc;
  73. }
  74. else if ($obj->F) {
  75. $P[ $obj->id ] = new Person($obj->id);
  76. $pIds[] = $obj->id;
  77. }
  78. else if ($obj->brandLocs) {
  79. include "decision/$name.php";
  80. $d[$name] = new $name($name);
  81. $brandLocs[$name] = (array)$obj->brandLocs;
  82. $brandMembers[$name] = (array)$obj->members;
  83. $d[$name]->rcv(array(
  84. "maxCycle" => $_GET['maxCycle'], "acctant"=> $obj->acctant, "inven"=> $obj->inven, "brandIndex"=> $obj->brandIndex,
  85. "pQty" => $obj->pQty, "cQty" => $obj->cQty, "wQty" => $obj->wQty,
  86. "numP" => $name, "numLocs"=> $name
  87. ));
  88. $numP = 0;
  89. $numBrands = 0;
  90. foreach($d[$name]->members AS $brand => $personArr) {
  91. $numBrands++;
  92. foreach($personArr AS $personId) {
  93. $P[$personId]->dEng = $name;
  94. $numP++;
  95. }
  96. }
  97. $numLocs = 0;
  98. foreach($d[$name]->brandLocs AS $brand => $locInfo) {
  99. $numLocs += count($locInfo);
  100. }
  101. $dGrp[$name] = array(
  102. "acctant"=> $obj->acctant, "inven"=> $obj->inven, "brandIndex"=> $obj->brandIndex,
  103. "pQty" => $obj->pQty, "cQty" => $obj->cQty, "wQty" => $obj->wQty,
  104. "numLocs" => $numLocs, "numP" => $numP, "numBrands" => $numBrands
  105. );
  106. }
  107. }
  108. //set up Resource once and will just reset properties
  109. $Resource = new Rsrc();
  110. //set up report
  111. $Report = new Report($dGrp, $versionInfo);
  112. //Set up run parameters
  113. $maxTicks = CYCLE_PERIOD*$_GET['maxCycle'];
  114. $cycleNum = 0;
  115. $cyclicTick = -1;
  116. $linearTick = -1;
  117. if ($_GET['doMax']) {
  118. if ($_GET['doMax'] != "max") $_GET['maxCycle'] = $_GET['doMax'];
  119. }
  120. else {
  121. echo "\n\nCycle number:$cycleNum mem_usage= ". memory_get_usage() ." ";
  122. /*foreach($addr->inven->locRef AS $loc) {
  123. $r = str_split($loc[1]);
  124. for($i=0; $i<4; $i++) $rInvenDimens[$i] += $r[$i];
  125. }*/
  126. print_r($rInvenDimens); echo "\n\n";
  127. print_r($addr); print_r($Report); print_r($Addr_xyz); exit();
  128. }
  129. /**********************************************
  130. ** Run simulation
  131. ***********************************************/
  132. while ($cycleNum < $_GET['maxCycle']) {
  133. $cyclicTick++;
  134. $linearTick++;
  135. if ($cyclicTick == CYCLE_PERIOD) {
  136. $cycleNum++;
  137. $cyclicTick = 0;
  138. }
  139. //Apply changes to location qty, waste, etc.
  140. foreach($L AS $Loc) $Loc->wasteImg = $Loc->waste;
  141. foreach($L AS $Loc) $Loc->setExp($linearTick);
  142. //trigger activity
  143. shuffle($pIds); //print_r($pIds); print_r($Addr_xyz); exit();
  144. foreach($pIds as $id) {
  145. if (!REPLACE_DEAD OR !$P[$id]->death) $P[$id]->act($Resource, $linearTick, $cyclicTick); //print_r($P); print_r($Ocaup); print_r($Inven); exit();
  146. }
  147. //generate reports as needed
  148. if ($cycleNum == $reportCycle) {
  149. $Report->byDEngine($cycleNum, $P, $linearTick);
  150. $reportCycle += SUMMARY_PERIOD;
  151. }
  152. }
  153. /**********************************************
  154. ** Generate reports as needed
  155. ***********************************************/
  156. $Ocaup->genReport();
  157. $Lets->genReport();
  158. //$Report->viewObj($P, $L, $Lets, $Inven);
  159. //$Report->viewJSON($P);
  160. //$Report->viewJSON($L);
  161. print_r($Report);
  162. $Report->saveCSV($runName, $Addr_xyz);
  163. if (strpos($_GET['print'],"addr") !== false) print_r($Addr_xyz);
  164. ?>