Discrete Event SimulaVon Discrete event simulaVon

Discrete Event Simula2on University of Massachuse9s Amherst ECE 242 – Data Structures and Algorithms Lecture 30 ECE 242 – Fall 2014 © 2014 Tilman Wolf & Mike Zink 1 Discrete event simula2on •  Simula2on important in engineering –  Systems may be too complex for analysis –  Need quan2ta2ve results •  Simula2on occurs in 2me steps –  What is suitable interval of 2me steps? •  Too small: simula2on takes too long •  Too large: simula2on too coarse •  Discrete event simula2on simulates events –  Time steps are aligned with events in system ECE 242 – Fall 2014 © 2014 Tilman Wolf & Mike Zink 2 1 Simula2on example •  What are suitable events to simulate… –  … an airport? –  … a processor? –  … a baseball game? –  … a circuit? •  Con2nuous phenomena are difficult to simulate with discrete event simula2on –  Need discrete events ECE 242 – Fall 2014 © 2014 Tilman Wolf & Mike Zink 3 How to build a simulator? •  What step to simulate when? ECE 242 – Fall 2014 © 2014 Tilman Wolf & Mike Zink 4 2 How to build a simulator? •  What step to simulate when? –  Order events by 2me –  Earliest event is simulated next •  Events processing –  Event may create new event(s) –  New events must be in future –  New events are added to list of events to be processed ECE 242 – Fall 2014 © 2014 Tilman Wolf & Mike Zink 5 How to build a simulator? •  What data structure for finding next event? –  Heap with earliest event on top •  How to represent event? –  Time of event –  Type of event •  Components of simulated system implement event handling –  Common interface to process event ECE 242 – Fall 2014 © 2014 Tilman Wolf & Mike Zink 6 3 Input to simulator •  Events can be triggered by ini2al events –  Simula2ons sets ini2al condi2ons –  Random behavior may be simulated •  Trace-­‐driven simula2on –  Record of real events is part of event 2meline ECE 242 – Fall 2014 © 2014 Tilman Wolf & Mike Zink 7 Example •  Very simple machine repairman problem –  1 machine that ini2ally works –  Machine may stop working randomly –  1 Repairman available to repair machine –  Repair takes random amount of 2me –  Aaer repair, machine con2nues working un2l next failure •  1 user checks every 60 2me units on machine –  What percentage of 2me does user find machine ok? ECE 242 – Fall 2014 © 2014 Tilman Wolf & Mike Zink 8 4 Example •  What en22es need to be simulated? •  What events do we need? ECE 242 – Fall 2014 © 2014 Tilman Wolf & Mike Zink 9 Example •  What en22es need to be simulated? –  Machine –  Repairman –  User •  What events do we need? –  Machine breaks/repairman starts repair –  Repairman finishes repair/machine works –  User checks machine ECE 242 – Fall 2014 © 2014 Tilman Wolf & Mike Zink 10 5 Example •  What does event sequence look like? check works 2me event triggers next event for machine check fails 2me event triggers next event for machine check works ECE 242 – Fall 2014 2me © 2014 Tilman Wolf & Mike Zink 11 Example •  What does event sequence look like? check works 2me event triggers next event for machine fails check 2me event triggers next event for user © 2014 Tilman Wolf & Mike Zink check fails ECE 242 – Fall 2014 2me 12 6 Code •  Event: public class Event {
private double time;
private int type;
private EventHandler handler;
...
•  EventHandler: interface EventHandler {
public void respondToEvent(Event e, Simulation s); }
ECE 242 – Fall 2014 © 2014 Tilman Wolf & Mike Zink 13 Code •  Machine: public class Machine implements EventHandler {
...
public void respondToEvent(Event e, Simulation s) {
if (e.getType()==s.working) {
System.out.println(e.getTime()+" machine working");
working=true;
double timeToNextFailure = Math.abs(s.generator.nextGaussian()*MTTFvariance+MTTF);
e.setTime(s.now+timeToNextFailure);
e.setType(s.failure);
s.scheduleEvent(e);
return;
}
if (e.getType()==s.failure) {
System.out.println(e.getTime()+" machine failure");
working=false;
e.setTime(s.now);
e.setHandler(s.r);
e.setType(s.startRepair);
s.scheduleEvent(e);
return;
}
}
}
ECE 242 – Fall 2014 © 2014 Tilman Wolf & Mike Zink 14 7 Code •  Simula2on: –  Main loop: public void run(double maxTime) {
while (!h.isEmpty() && h.peek().getTime()<=maxTime) {
Event nextEvent = h.remove();
now = nextEvent.getTime();
nextEvent.getHandler().respondToEvent(nextEvent, this);
}
}
ECE 242 – Fall 2014 © 2014 Tilman Wolf & Mike Zink 15 Code •  Simula2on setup: public void setup() {
Event machineEvent = new Event();
machineEvent.setHandler(m);
machineEvent.setType(working);
machineEvent.setTime(0);
scheduleEvent(machineEvent);
Event userEvent = new Event();
userEvent.setHandler(u);
userEvent.setType(userCheck);
userEvent.setTime(60);
scheduleEvent(userEvent);
return;
}
ECE 242 – Fall 2014 © 2014 Tilman Wolf & Mike Zink 16 8 Output • 
• 
• 
• 
• 
• 
• 
• 
• 
• 
• 
• 
• 
• 
• 
• 
• 
• 
0.0 machine working
22.588145853143633 machine failure
22.588145853143633 starting repair
29.936322779217726 finishing repair
29.936322779217726 machine working
60.0 user check: working 100%
62.278181963082304 machine failure
62.278181963082304 starting repair
...
9937.589327385185 machine failure
9937.589327385185 starting repair
9944.742518109793 finishing repair
9944.742518109793 machine working
9960.0 user check: working 74%
9979.121482973369 machine failure
9979.121482973369 starting repair
9993.010992163434 finishing repair
9993.010992163434 machine working ECE 242 – Fall 2014 © 2014 Tilman Wolf & Mike Zink 17 Next Steps •  Project 4 –  Deadline extended to Friday 11 p.m. (hard) •  Homework 6 posted –  Official deadline Wednesday 11 p.m. (submissions accepted un2l Thursday 11 p.m.) •  Lecture on Monday –  Hash tables – very important ECE 242 – Fall 2014 © 20134 Tilman Wolf & Mike Zink 18 9