PageRenderTime 53ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/main.php

https://bitbucket.org/siosonel/flora
PHP | 168 lines | 91 code | 41 blank | 36 comment | 21 complexity | 84694ede5e79417cb147479dd7712932 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. if (!$_GET['maxCycle']) $_GET['maxCycle'] = 1;
  10. /***********************************************************
  11. ** Parse run request and set config file path as requested
  12. ************************************************************/
  13. if (!$runName = $_GET['runName']) $runName = "__";
  14. if ($configFile = $_GET['config']) {
  15. include "setConfig.php";
  16. }
  17. else if ($_GET['runName']) {
  18. $configFile = "results/$runName/config.php";
  19. if (!file_exists($configFile)) {
  20. if (!is_dir("results/$runName")) mkdir("results/$runName");
  21. file_put_contents($configFile, file_get_contents("config.php"));
  22. }
  23. }
  24. else include "config.php";
  25. /**********************************************
  26. ** Load class files
  27. ***********************************************/
  28. include_once "utils/Error.php";
  29. include_once "utils/Util.php";
  30. include_once "utils/msgFunc.php";
  31. include_once "utils/RandomInit.php";
  32. include_once "testbed/Person.php";
  33. include_once "testbed/Rsrc.php";
  34. include_once "testbed/Location.php";
  35. include_once "testbed/Waste.php";
  36. include_once "testbed/Producer.php";
  37. include_once "testbed/Consumer.php";
  38. include_once "testbed/Report.php";
  39. include_once "infras/Ocaup.php";
  40. include_once "infras/Lets.php";
  41. include_once "infras/Inventory.php";
  42. include_once "infras/BrandIndex.php";
  43. /**********************************************
  44. ** Set up version tracking
  45. ***********************************************/
  46. //catch changes using Mercurial, exit or copy -> see included Util.php
  47. $versionInfo = Util::onHgChange($runName,$time); //if no "&onHgChange=" value is provided, will just return and do nothing
  48. /**********************************************
  49. ** Prepare for simulation run
  50. ***********************************************/
  51. //set up infrastructure
  52. $systemAlias = explode(",",$_GET['infras']);
  53. foreach($systemAlias AS $system) {
  54. list($className, $addrAlias) = explode(".", $system);
  55. include_once "infras/$className.php";
  56. $Infras[$addrAlias] = new $className($addrAlias, $_GET[$addrAlias]) ;
  57. }
  58. //set up locations
  59. for($i=0; $i < $_GET['locN']; $i++) $L[] = new Location($i); //print_r($L); exit();
  60. //setup agents
  61. for($i=0; $i < $_GET['popN']; $i++) {
  62. $P[] = new Person($i);
  63. $pIds[] = $i; //will be used for shuffling ID for random scheduling order
  64. }
  65. //set up Resource once and will just reset properties
  66. $Resource = new Rsrc();
  67. if ($_GET['rsrcN']) {
  68. for($i=0; $i<$_GET['rsrcN']; $i++) $rsrcSet[] = RandomInit::genRsrcId();
  69. if ($_GET['rId_unique']) {
  70. $rsrcSet = array_values(array_unique($rsrcSet));
  71. if ($_GET['rId_unique'] > count($rsrcSet)) exit("Not enough unique resource from the random ID generator."); //print_r($rsrcSet);
  72. }
  73. }
  74. //set up decision engines; dEngs is created in prepRun.php
  75. $dEngsAlias = explode(",",$_GET['dEngs']);
  76. foreach($dEngsAlias AS $dE) {
  77. list($className,$addrAlias) = explode(".", $dE);
  78. include_once "decision/$className.php";
  79. $DEngs[$addrAlias] = new $className($addrAlias, $_GET[$addrAlias] );
  80. msg2way($addrAlias, array(
  81. "maxCycle" => $_GET['maxCycle'], "IMMED_LOC" => IMMED_LOC
  82. ));
  83. for($p=0; $p < $DEngs[$addrAlias]->numP; $p++) {
  84. $id = $DEngs[$addrAlias]->begP + $p;
  85. $P[$id]->dEng = $addrAlias;
  86. }
  87. }
  88. //set up report
  89. $Report = new Report($DEngs, $Infras, $versionInfo);
  90. //Set up run parameters
  91. $maxTicks = CYCLE_PERIOD*$_GET['maxCycle'];
  92. $cycleNum = 0;
  93. $cyclicTick = -1;
  94. $linearTick = -1; //print_r($Report); print_r($Addr_xyz); exit();
  95. /**********************************************
  96. ** Run simulation
  97. ***********************************************/
  98. while ($cycleNum < $_GET['maxCycle']) {
  99. $cyclicTick++;
  100. $linearTick++;
  101. if ($cyclicTick == CYCLE_PERIOD) {
  102. $cycleNum++;
  103. $cyclicTick = 0;
  104. }
  105. foreach($Infras as $alias => $Obj) $Obj->syncTick($linearTick); //decided to call directly instead of msg2way
  106. //Apply changes to location qty, waste, etc.
  107. foreach($L AS $Loc) $Loc->wasteImg = $Loc->waste;
  108. foreach($L AS $Loc) $Loc->setExp($linearTick);
  109. //trigger activity
  110. shuffle($pIds); //print_r($pIds); //print_r($Addr_xyz); exit();
  111. foreach($pIds as $id) {
  112. if (!REPLACE_DEAD OR !$P[$id]->death) $P[$id]->act($Resource, $linearTick, $cyclicTick); //print_r($P); print_r($Ocaup); print_r($Inven); exit();
  113. }
  114. //generate reports as needed
  115. $Report->logData($linearTick, $cycleNum, $P, $L);
  116. }
  117. /**********************************************
  118. ** Generate reports as needed
  119. ***********************************************/
  120. foreach($Infras AS $sys) $sys->genReport();
  121. //$Report->viewObj($P, $L, $Lets, $Inven);
  122. //$Report->viewJSON($P);
  123. //$Report->viewJSON($L);
  124. echo "\n\n"; print_r($Report);
  125. $Report->saveCSV($time, $runName, $Addr_xyz);
  126. if (strpos($_GET['print'],"addr") !== false) print_r($Addr_xyz);
  127. ?>