SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
QueueEv.java
1package tutorial;
2
3import umontreal.ssj.simevents.*;
4import umontreal.ssj.rng.*;
5import umontreal.ssj.randvar.*;
6import umontreal.ssj.stat.*;
7import java.util.LinkedList;
8
9public class QueueEv {
10
11 RandomVariateGen genArr, genServ;
12 LinkedList<Customer> waitList = new LinkedList<Customer>();
13 LinkedList<Customer> servList = new LinkedList<Customer>();
14 Tally custWaits = new Tally("Waiting times");
15 Accumulate totWait = new Accumulate("Size of queue");
16
17 class Customer {
18 double arrivTime, servTime;
19 }
20
21 public QueueEv(double lambda, double mu) {
22 genArr = new ExponentialGen(new MRG32k3a(), lambda);
23 genServ = new ExponentialGen(new MRG32k3a(), mu);
24 }
25
26 public void simulate(double timeHorizon) {
27 Sim.init();
28 waitList.clear();
29 servList.clear();
30 custWaits.init();
31 totWait.init();
32 new EndOfSim().schedule(timeHorizon);
33 new Arrival().schedule(genArr.nextDouble());
34 Sim.start();
35 }
36
37 class Arrival extends Event {
38 public void actions() {
39 new Arrival().schedule(genArr.nextDouble()); // Next arrival.
40 Customer cust = new Customer(); // Cust just arrived.
41 cust.arrivTime = Sim.time();
42 cust.servTime = genServ.nextDouble();
43 if (servList.size() > 0) { // Must join the queue.
44 waitList.addLast(cust);
45 totWait.update(waitList.size());
46 } else { // Starts service.
47 custWaits.add(0.0);
48 servList.addLast(cust);
49 new Departure().schedule(cust.servTime);
50 }
51 }
52 }
53
54 class Departure extends Event {
55 public void actions() {
56 servList.removeFirst();
57 if (waitList.size() > 0) {
58 // Starts service for next one in queue.
59 Customer cust = waitList.removeFirst();
60 totWait.update(waitList.size());
61 custWaits.add(Sim.time() - cust.arrivTime);
62 servList.addLast(cust);
63 new Departure().schedule(cust.servTime);
64 }
65 }
66 }
67
68 class EndOfSim extends Event {
69 public void actions() {
70 Sim.stop();
71 }
72 }
73
74 public static void main(String[] args) {
75 double lambda = 1.0 / 10.0; // Arrival rate
76 double mu = 1.0 / 9.0; // Service rate
77 double T = 1000.0; // Time horizon
78 QueueEv queue = new QueueEv(lambda, mu);
79 queue.simulate(T);
80 System.out.println(queue.custWaits.report());
81 System.out.println(queue.totWait.report());
82 }
83}
void actions()
This is the method that is executed when this event occurs.
Definition QueueEv.java:38
void actions()
This is the method that is executed when this event occurs.
Definition QueueEv.java:55
void actions()
This is the method that is executed when this event occurs.
Definition QueueEv.java:69
This class implements random variate generators for the exponential distribution.
This is the base class for all random variate generators over the real line.
double nextDouble()
Generates a random number from the continuous distribution contained in this object.
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...
String report()
Returns a string containing a report on this collector since its last initialization.
void init()
Initializes the statistical collector and puts the current value of the corresponding variable to 0.
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 void stop()
Tells the simulation executive to stop as soon as it takes control, and to return control to the prog...
Definition Sim.java:125
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 init()
Initializes the statistical collector.
Definition Tally.java:91
String report()
Returns a formatted string that contains a report on this probe.
Definition Tally.java:397