/main.php
PHP | 168 lines | 91 code | 41 blank | 36 comment | 21 complexity | 84694ede5e79417cb147479dd7712932 MD5 | raw file
- <?php
- /*
- Message passing based simulation core engine
- Edgar Sioson, 10/29/2011
- */
-
- header("content-type: text/plain");
- if ($_GET["timeLimit"]) set_time_limit($_GET['timeLimit']);
- $time = time(); echo "\nTime: $time\n";
-
- if (!$_GET['maxCycle']) $_GET['maxCycle'] = 1;
-
- /***********************************************************
- ** Parse run request and set config file path as requested
- ************************************************************/
- if (!$runName = $_GET['runName']) $runName = "__";
-
- if ($configFile = $_GET['config']) {
- include "setConfig.php";
- }
- else if ($_GET['runName']) {
- $configFile = "results/$runName/config.php";
-
- if (!file_exists($configFile)) {
- if (!is_dir("results/$runName")) mkdir("results/$runName");
- file_put_contents($configFile, file_get_contents("config.php"));
- }
- }
- else include "config.php";
-
-
- /**********************************************
- ** Load class files
- ***********************************************/
-
- include_once "utils/Error.php";
- include_once "utils/Util.php";
- include_once "utils/msgFunc.php";
- include_once "utils/RandomInit.php";
-
- include_once "testbed/Person.php";
- include_once "testbed/Rsrc.php";
- include_once "testbed/Location.php";
- include_once "testbed/Waste.php";
- include_once "testbed/Producer.php";
- include_once "testbed/Consumer.php";
- include_once "testbed/Report.php";
-
- include_once "infras/Ocaup.php";
- include_once "infras/Lets.php";
- include_once "infras/Inventory.php";
- include_once "infras/BrandIndex.php";
-
-
- /**********************************************
- ** Set up version tracking
- ***********************************************/
-
- //catch changes using Mercurial, exit or copy -> see included Util.php
- $versionInfo = Util::onHgChange($runName,$time); //if no "&onHgChange=" value is provided, will just return and do nothing
-
-
-
- /**********************************************
- ** Prepare for simulation run
- ***********************************************/
-
- //set up infrastructure
- $systemAlias = explode(",",$_GET['infras']);
-
- foreach($systemAlias AS $system) {
- list($className, $addrAlias) = explode(".", $system);
- include_once "infras/$className.php";
-
- $Infras[$addrAlias] = new $className($addrAlias, $_GET[$addrAlias]) ;
- }
-
-
- //set up locations
- for($i=0; $i < $_GET['locN']; $i++) $L[] = new Location($i); //print_r($L); exit();
-
- //setup agents
- for($i=0; $i < $_GET['popN']; $i++) {
- $P[] = new Person($i);
- $pIds[] = $i; //will be used for shuffling ID for random scheduling order
- }
-
- //set up Resource once and will just reset properties
- $Resource = new Rsrc();
- if ($_GET['rsrcN']) {
- for($i=0; $i<$_GET['rsrcN']; $i++) $rsrcSet[] = RandomInit::genRsrcId();
- if ($_GET['rId_unique']) {
- $rsrcSet = array_values(array_unique($rsrcSet));
- if ($_GET['rId_unique'] > count($rsrcSet)) exit("Not enough unique resource from the random ID generator."); //print_r($rsrcSet);
- }
- }
-
- //set up decision engines; dEngs is created in prepRun.php
- $dEngsAlias = explode(",",$_GET['dEngs']);
-
- foreach($dEngsAlias AS $dE) {
- list($className,$addrAlias) = explode(".", $dE);
- include_once "decision/$className.php";
-
- $DEngs[$addrAlias] = new $className($addrAlias, $_GET[$addrAlias] );
- msg2way($addrAlias, array(
- "maxCycle" => $_GET['maxCycle'], "IMMED_LOC" => IMMED_LOC
- ));
-
- for($p=0; $p < $DEngs[$addrAlias]->numP; $p++) {
- $id = $DEngs[$addrAlias]->begP + $p;
- $P[$id]->dEng = $addrAlias;
- }
- }
-
- //set up report
- $Report = new Report($DEngs, $Infras, $versionInfo);
-
- //Set up run parameters
- $maxTicks = CYCLE_PERIOD*$_GET['maxCycle'];
- $cycleNum = 0;
- $cyclicTick = -1;
- $linearTick = -1; //print_r($Report); print_r($Addr_xyz); exit();
-
-
-
- /**********************************************
- ** Run simulation
- ***********************************************/
-
- while ($cycleNum < $_GET['maxCycle']) {
- $cyclicTick++;
- $linearTick++;
-
- if ($cyclicTick == CYCLE_PERIOD) {
- $cycleNum++;
- $cyclicTick = 0;
- }
-
- foreach($Infras as $alias => $Obj) $Obj->syncTick($linearTick); //decided to call directly instead of msg2way
-
- //Apply changes to location qty, waste, etc.
- foreach($L AS $Loc) $Loc->wasteImg = $Loc->waste;
- foreach($L AS $Loc) $Loc->setExp($linearTick);
-
- //trigger activity
- shuffle($pIds); //print_r($pIds); //print_r($Addr_xyz); exit();
- foreach($pIds as $id) {
- if (!REPLACE_DEAD OR !$P[$id]->death) $P[$id]->act($Resource, $linearTick, $cyclicTick); //print_r($P); print_r($Ocaup); print_r($Inven); exit();
- }
-
- //generate reports as needed
- $Report->logData($linearTick, $cycleNum, $P, $L);
- }
-
- /**********************************************
- ** Generate reports as needed
- ***********************************************/
- foreach($Infras AS $sys) $sys->genReport();
-
- //$Report->viewObj($P, $L, $Lets, $Inven);
- //$Report->viewJSON($P);
- //$Report->viewJSON($L);
- echo "\n\n"; print_r($Report);
- $Report->saveCSV($time, $runName, $Addr_xyz);
- if (strpos($_GET['print'],"addr") !== false) print_r($Addr_xyz);
-
- ?>