SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
BankEv.java
1package tutorial;
2
3import umontreal.ssj.simevents.*;
4import umontreal.ssj.rng.*;
5import umontreal.ssj.randvar.*;
6import umontreal.ssj.stat.*;
7
8public class BankEv {
9 static final double minute = 1.0 / 60.0;
10 int nbTellers; // Number of tellers.
11 int nbBusy; // Number of tellers busy.
12 int nbWait; // Queue length.
13 int nbServed; // Number of customers served so far
14 double meanDelay; // Mean time between arrivals.
15 Event nextArriv = new Arrival(); // The next arrival.
16 RandomStream streamArr = new MRG32k3a(); // Customer's arrivals
17 ErlangGen genServ = new ErlangConvolutionGen(new MRG32k3a(), 2, 1.0 / minute);
18 RandomStream streamTeller = new MRG32k3a(); // Number of tellers
19 RandomStream streamBalk = new MRG32k3a(); // Balking decisions
20 Tally statServed = new Tally("Nb. served per day");
21 Tally avWait = new Tally("Average wait per day (hours)");
22 Accumulate wait = new Accumulate("cumulated wait for this day");
23
24 Event e9h45 = new Event() {
25 public void actions() {
26 meanDelay = 2.0 * minute;
27 nextArriv.schedule(ExponentialGen.nextDouble(streamArr, 1.0 / meanDelay));
28 }
29 };
30
31 Event e10h = new Event() {
32 public void actions() {
33 double u = streamTeller.nextDouble();
34 if (u >= 0.2)
35 nbTellers = 3;
36 else if (u < 0.05)
37 nbTellers = 1;
38 else
39 nbTellers = 2;
40 while (nbWait > 0 && nbBusy < nbTellers) {
41 nbBusy++;
42 nbWait--;
43 new Departure().schedule(genServ.nextDouble());
44 }
45 wait.update(nbWait);
46 }
47 };
48
49 Event e11h = new Event() {
50 public void actions() {
51 nextArriv.reschedule((nextArriv.time() - Sim.time()) / 2.0);
52 meanDelay = minute;
53 }
54 };
55
56 Event e14h = new Event() {
57 public void actions() {
58 nextArriv.reschedule((nextArriv.time() - Sim.time()) * 2.0);
59 meanDelay = 2.0 * minute;
60 }
61 };
62
63 Event e15h = new Event() {
64 public void actions() {
65 nextArriv.cancel();
66 }
67 };
68
69 private boolean balk() {
70 return (nbWait > 9) || (nbWait > 5 && (5.0 * streamBalk.nextDouble() < nbWait - 5));
71 }
72
73 class Arrival extends Event {
74 public void actions() {
75 nextArriv.schedule(ExponentialGen.nextDouble(streamArr, 1.0 / meanDelay));
76 if (nbBusy < nbTellers) {
77 nbBusy++;
78 new Departure().schedule(genServ.nextDouble());
79 } else if (!balk()) {
80 nbWait++;
81 wait.update(nbWait);
82 }
83 }
84 }
85
86 class Departure extends Event {
87 public void actions() {
88 nbServed++;
89 if (nbWait > 0) {
90 new Departure().schedule(genServ.nextDouble());
91 nbWait--;
92 wait.update(nbWait);
93 } else
94 nbBusy--;
95 }
96 };
97
98 public void simulOneDay() {
99 Sim.init();
100 wait.init();
101 nbTellers = 0;
102 nbBusy = 0;
103 nbWait = 0;
104 nbServed = 0;
105 e9h45.schedule(9.75);
106 e10h.schedule(10.0);
107 e11h.schedule(11.0);
108 e14h.schedule(14.0);
109 e15h.schedule(15.0);
110 Sim.start();
111 statServed.add(nbServed);
112 wait.update();
113 avWait.add(wait.sum());
114 }
115
116 public void simulateDays(int numDays) {
117 for (int i = 1; i <= numDays; i++)
118 simulOneDay();
119 System.out.println(statServed.report());
120 System.out.println(avWait.report());
121 }
122
123 public static void main(String[] args) {
124 new BankEv().simulateDays(100);
125 }
126}
void actions()
This is the method that is executed when this event occurs.
Definition BankEv.java:74
void actions()
This is the method that is executed when this event occurs.
Definition BankEv.java:87
This class implements Erlang random variate generators using the convolution method.
This class implements random variate generators for the Erlang distribution with parameters and .
This class implements random variate generators for the exponential distribution.
static double nextDouble(RandomStream s, double lambda)
Uses inversion to generate a new exponential variate with parameter.
Extends the abstract class RandomStreamBase by using as a backbone (or main) generator the combined m...
Definition MRG32k3a.java:46
A subclass of umontreal.ssj.stat.StatProbe, for collecting statistics on a variable that evolves in s...
double sum()
Returns the sum cumulated so far for this probe.
void update()
Updates the accumulator using the last value passed to update(double).
void init()
Initializes the statistical collector and puts the current value of the corresponding variable to 0.
This abstract class provides event scheduling tools.
Definition Event.java:53
boolean cancel()
Cancels this event before it occurs.
Definition Event.java:231
void reschedule(double delay)
Cancels this event and reschedules it to happen in delay time units.
Definition Event.java:215
Event()
Constructs a new event instance, which can be placed afterwards into the event list of the default si...
Definition Event.java:126
void schedule(double delay)
Schedules this event to happen in delay time units, i.e., at time sim.time() + delay,...
Definition Event.java:153
This static class contains the executive of a discrete-event simulation.
Definition Sim.java:48
static double time()
Returns the current value of the simulation clock.
Definition Sim.java:59
static void start()
Starts the simulation executive.
Definition Sim.java:113
static void init()
Reinitializes the simulation executive by clearing up the event list, and resetting the simulation cl...
Definition Sim.java:69
A subclass of StatProbe.
Definition Tally.java:47
void add(double x)
Gives a new observation x to the statistical collector.
Definition Tally.java:109
String report()
Returns a formatted string that contains a report on this probe.
Definition Tally.java:397
This interface defines the basic structures to handle multiple streams of uniform (pseudo)random numb...