CS 342 Lecture -*- Outline -*- * discrete-event simulation example (section 7.4) ad: computer models (simulations) of reality are useful for planning (business) exploring consequences of hypotheses especially if no easy mathematical way to get answer validating theories that are dangereous or hard to test (supernova explosions, galaxy formation) ** problem 2 terminals students can use terminal for "t" minutes, then go to end of line what is the best value for t? minimize average amount of time waiting in line per student ** discrete-event simulation in general clock holds simulated time event queue holds list of events (and when they should happen) loop: take off head of event queue (next event to happen) update clock to its time do the event (will place new events on queue) stop when run to a certain simulated time ** example simulation (7.4.1) *** events in example DRAW PICTURE arrival of student in lab what happens? enter line or sit at terminal schedule timeout schedule next arrival students time running out at terminal what happens? stay if more time and no one waiting leave if done go to end of line, new student seated schedule timeout *** designing the example THINK OF CLASS-RESPONSIBILITY-COLLABORATOR cards where are the objects? event queue keeps track of next event simulation clock (can just be an integer variable?) knows simulated time student knows how long needs to do her job knows whether in line or not the lab room (!) knows the state of the two terminals will use other classes to organize code represent computational ideas (e.g., queues) in a way that might permit reuse. *** the code **** simulation framework and labSimulation class Simulation is a "framework" for writing this kind of simulation it's an abstract class run sets up global variables, calls initialize, then loops until timelimit, then calls report LabSimulation (fig. 7.16 on page 307) concrete subclass of Simulation initialize sets up a lot of global variables global so can be used to communicate among different objects (students, the lab, ...) otherwise would have to have 2 methods for each such variable (get and set) Running the similation: (p. 314) make a LabSimulation object, then send it the "run" message. **** Lab knows about state of terminals protocol: (page 307) check and change status of terminals **** Queues protocol enqueue: puts someone at end of the line impl. need different version of add:, so inherited code makes Queues instead of lists look at newEmptyCollection in Queue, inherits initList **** eventQueues, priority queues priority queue like a list of pairs (associations) sorted by first element in each pair look at scheduleEvent method in EventQueue... event queue a priority queue of events (i.e., students) prioritized by time the event will occur protocol scheduleEvent doNextEvent note the takeAction send here, this makes the event "happen" (side effects) used in class Simulation, method run why isn't event queue a subclass of queue? because it doesn't do them in order enqueued, but by time how are priority queues used in event queue class? as a client **** Student, page 311 states (status instance variable: p. 310) -1 scheduled to arrive 0 on the queue (for terminal) 1 or 2, on terminal 1 or terminal 2 3 finished these numbers should perhaps be defined as constants but only have meaning within the class Student (info hiding) instance variables number is student's id, used in report timeNeeded is total necessary, never changes protocol takeAction (the key method), sent by LabSimulation, in method run inherited from Simulation which does doNextEvent which actually sends it. calls all the other methods arrive grabs terminal if can (thereby scheduling leaving) otherwise puts self in terminal queue leaveTerminal handles details of leaving terminal release-terminal line contains pun on status and terminal number scheduleLeaveTerminal put self on event queue, to leave when either done or when time slice is up responsibility to keep herself on some queue or other, if not done. invariant: if timeStillNeeded > 0, student is either on event queue or terminal queue other invariants? arrival always schedules another arrival a student with status >= 0 is either on the terminal queue or working on a terminal or has status 3 a student with status 3 has timeStillNeeded = 0 ... responsibility to record various statistics (time wasted, finishing) distinction between scheduling (putting on event queue) and occurrence for an event scheduleArrival **** WaitTimeList and ServiceTimeList (p. 313) schedule of arrivals and service times protocol respond to "next" (not first, why?) WaitTimeList is list of times when students will arrive these are in deltas (not absolute, but relative to now) ServiceTimeList is list of how much time students will need to complete their task 2 implementations: fig 7.24 (files ...1.smt in public directory) all students arrive when lab opens (waitTime is 0), each needs 120 minutes (service time is 120) fig 7.25 (files ...2.smt) students arrive every 30 minutes time needed alternates between 30 and 120