// Copyright 1999 Regents of The University of Michigan import umich.coabs.simulator.*; import java.awt.*; class Simulate { /** This is the main program users of the simulator write to create and initialize entities and decision makers in the environment and to start up the control loop of the simulator. This program in particular is used to automatically generate specified numbers of interconnected locations, evacuees, safety locations to which evacuees are evacuated, and transports for carrying the evacuees. While these numbers are hardcoded in this program, the SimulatorStartFrame class lets a user specify the values at runtime in another window. @author Brad Clement, bradc@umich.edu */ public static void main(String[] args) { int numLocations = 60, numTransports = 30, numEvacuees = 40; // these are the hardcoded init parameters int numSafetyLocations = 15; double meanRouteTripTime = 10.0; // transport trip times across routes vary a little; this is the average double meanRouteDestroyTime = 3.0; // mean time for a transport to destroy a route // the simulator's clock decrements 1.0 on each cycle int dim = (int) java.lang.Math.sqrt(numLocations) + 1; // the width and height of the grid of locations Location[][] locations = new Location[dim][dim]; // the array of locations LinkedList evacuees = new LinkedList(); // list of evacuees generated LinkedList safeLocations = new LinkedList(); // list of safety locations evacuees are avacuated to Location l1, l2; // temporary variables Route r1; Transport t1; Evacuee e1; DecisionMakerForEvacuees dmfe; // this is the agent that controls all of the evacuees TransportDecisionMaker tdm; // every transport has its own decision maker int i, j, k, n, r; // we always need a bunch of one character integer variables boolean madeSafe; // we have found a location that can be made into a safety location // array below used for picking random colors for generated entities Color[] colorArray = new Color[] {Color.red, Color.pink, Color.orange, Color.yellow, Color.green, Color.magenta, Color.cyan, Color.blue}; String id; // identification string for entities Color c; // temp variable // used for graphical placement of entities int x = 50, y = 50; // offset from corner of window int dx = 80, dy = 80; // spacing between centers of locations int tx, ty; // actual center of new location // Instantiate a control object, which drives the simulation Control ctrl = new Control(false); // Create grid of locations k = 0; for (i = 0; i < dim; i++) { ty = y + dy * i; for (j = 0; j < dim; j++) { id = "l" + i + "-" + j; tx = x + dx * j; if (k < numLocations) // The control acts as a factory for creating entities locations[i][j] = ctrl.CreateLocation(id, null, null, 1000, 1000, tx, ty, Color.white); k++; } } // Pick random locations and make them into safety locations for (n = 0; n < numSafetyLocations; n++) { madeSafe = false; while (!madeSafe) { // pick a random location k = (int) (numLocations * java.lang.Math.random()); i = k / dim; j = k % dim; l1 = locations[i][j]; // check to see that the location was not already made into a safety location if (safeLocations.Find(l1) == null) { madeSafe = true; l1.setColor(Color.green); safeLocations.Add(l1); } } } // Walk through locations in array and create routes to their adjacent locations k = 0; for (i = 0; i < dim; i++) { for (j = 0; j < dim; j++) { if (k < numLocations) { l1 = locations[i][j]; // create route from l1 to the location to its right if (j < dim - 1) { // assign the routes id id = "r" + i + "-" + j + "h"; l2 = locations[i][j + 1]; // create the two lane route if the locations exist if ((l1 != null) && (l2 != null)) ctrl.CreateRoute(id, null, null, l1, l2, meanRouteTripTime, meanRouteDestroyTime, Route.TWOWAY, true, 0.0, Color.black); } // create route from l1 to the location below it if (i < dim - 1) { l2 = locations[i + 1][j]; id = "r" + i + "-" + j + "v"; if ((l1 != null) && (l2 != null)) ctrl.CreateRoute(id, null, null, l1, l2, meanRouteTripTime, meanRouteDestroyTime, Route.TWOWAY, true, 0.0, Color.black); } // randomly create a route from l1 to the location diagonally down and to the right of l1 if (java.lang.Math.random() < 0.20) if ((j < dim - 1) && (i < dim - 1)) { l2 = locations[i + 1][j + 1]; id = "r" + i + "-" + j + "dr"; if ((l1 != null) && (l2 != null)) ctrl.CreateRoute(id, null, null, l1, l2, meanRouteTripTime, meanRouteDestroyTime, Route.TWOWAY, true, 0.0, Color.black); } // randomly create a route from l1 to the location diagonally down and to the left of l1 if (java.lang.Math.random() > 0.80) if ((j > 0) && (i < dim - 1)) { l2 = locations[i + 1][j - 1]; id = "r" + i + "-" + j + "dl"; if ((l1 != null) && (l2 != null)) ctrl.CreateRoute(id, null, null, l1, l2, meanRouteTripTime, meanRouteDestroyTime, Route.TWOWAY, true, 0.0, Color.black); } } k++; } } // Create transports with random color and location along with their private decision maker (or mind) for (n = 0; n < numTransports; n++) { // pick the random location k = (int) (numLocations * java.lang.Math.random()); i = k / dim; j = k % dim; l1 = locations[i][j]; // create and assign the id of the transport's decision maker id = "dmt" + n; tdm = new TransportDecisionMaker(id, null, ctrl); // assign the id of the transport id = "t" + n; // get a random color for the transport r = (int) (colorArray.length * java.lang.Math.random()); c = colorArray[r]; // create and put the transport in its random location t1 = ctrl.CreateTransport(id, tdm, null, l1, null, 100, 100, true, 0.0, l1.getX(), l1.getY(), c); // tie the decision maker (mind) to its transport (body), and make the control object aware of its presence tdm.Possess(t1); ctrl.connect(tdm); } // Create evacuees with random color and location for (n = 0; n < numEvacuees; n++) { // pick the random location k = (int) (numLocations * java.lang.Math.random()); i = k / dim; j = k % dim; l1 = locations[i][j]; // assign the evacuee's id id = "e" + n; // pick a random color for the evacuee r = (int) (colorArray.length * java.lang.Math.random()); c = colorArray[r]; // create the evacuee and add it to the "evacuees" list evacuees.Add(ctrl.CreateEvacuee(id, null, null, 1, safeLocations, null, l1, null, false, 0.0, c)); } // Create decision maker for evacuees and connect it to the control object dmfe = new DecisionMakerForEvacuees("m", evacuees, ctrl); ctrl.connect(dmfe); // Start the simulator ctrl.run(); } }