// Copyright 1999 Regents of The University of Michigan // This makes all public classes defined for the simulator package available to this class import umich.coabs.simulator.*; /** This class is a decision maker that controls the single transport in its entities list. The transport explores the network by avoiding locations it has already visited, moving randomly otherwise, and stopping at each location to allow evacuees to embark or disembark. It has a very simple state machine and can get stuck if one of its actions fails. @author Brad Clement, bradc@umich.edu */ public class SmarterTransportDM extends DecisionMaker { int state = 0; // the state of the state machine Location destination; // global variable for keeping track of where // it wants to go LinkedList visitedLocations; /** Just passes basics up to the costructor of the parent class, DecisionMaker */ public SmarterTransportDM(String i, LinkedList e, Control c) { super(i, e, c); visitedLocations = new LinkedList(); } /** This is the method that is called on each decision cycle by the Control object of the simulator. It controls the behavior of the transport in its entities list. Sometimes the transports will not successfully move to a location. The results of these action failures are defined in the Transport class. The default is nothing changes, and a message is printed. Additionally, this decision maker often winds up in the wrong state and gets stuck when an action fails. For example, this can happen when two transports try to go opposite ways on a one-lane road or when a transport tries to traverse a route that has been destroyed. */ public void TakeStep() { ListElement elem, elem1; // for stepping through lists LinkedList candidates = new LinkedList(); // candidate routes to take int i, n; Route r; // the chosen route to traverse Route tr; // temporary route var double time = 0.0; // used to wait for a specified time Transport me = (Transport)entities.head.data; // get the transport at // the front of the // entities list // Take step for state 0 if (state == 0) { visitedLocations.Add(me.getCurLocation()); // Every route that has not been traveled before is a candidate for (elem=me.getCurLocation().getRoutes().head; elem!=null; elem=elem.next) { tr = (Route)elem.data; if (tr.getEndpt1() == me.getCurLocation()) { if (visitedLocations.Find(tr.getEndpt2()) == null) candidates.Add(tr); } else if (visitedLocations.Find(tr.getEndpt1()) == null) candidates.Add(tr); } // If have been to all surrounding locations, all routes are candidates if (candidates.size == 0) { candidates = me.getCurLocation().getRoutes(); } // Pick a random route to traverse; assign it to r n = (int) (candidates.size * java.lang.Math.random()); if (n == candidates.size) --n; elem = candidates.head; for (i = 0; i < n; i++) elem = elem.next; r = (Route) elem.data; // Get the location at the other end of the route if (r.getEndpt1() == me.getCurLocation()) destination = r.getEndpt2(); else destination = r.getEndpt1(); // Move the transport along the chosen route; additionally destroy // the route with 0.0 probability if (java.lang.Math.random() < 1.0) me.Move(destination, r); else me.MoveAndDestroy(destination, r); // Go to next state state = 1; } else // Take step for state 1 if (state == 1) { // If transport has arrived at its destination, set a timer to // wait at the current location to let evacuees embark or disembark if (me.getCurLocation() == destination) { time = ctrl.getTime(); state = 2; } } else // Take step for state 2 if (state == 2) { // If time is up go back to state 0 to move to another location if (ctrl.getTime() - time >= 4.0) state = 0; } } }