SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
QueueObs.java
1package tutorial;
2
3import java.util.*;
4import umontreal.ssj.stat.*;
5import umontreal.ssj.rng.*;
6import umontreal.ssj.randvar.*;
7
8public class QueueObs {
9
10 Tally waitingTimes = new Tally("Waiting times");
11 Tally averageWaits = new Tally("Average wait");
12 RandomVariateGen genArr; // For interarrival times.
13 RandomVariateGen genServ; // For service times.
14 int cust; // Number of the current customer.
15
16 public QueueObs(double lambda, double mu, int step) {
17 genArr = new ExponentialGen(new MRG32k3a(), lambda);
18 genServ = new ExponentialGen(new MRG32k3a(), mu);
19 waitingTimes.setBroadcasting(true);
20 waitingTimes.addObservationListener(new ObservationTrace(step));
21 waitingTimes.addObservationListener(new LargeWaitsCollector(2.0));
22 }
23
24 public double simulate(int numCust) {
25 waitingTimes.init();
26 double Wi = 0.0;
27 waitingTimes.add(Wi);
28 for (cust = 2; cust <= numCust; cust++) {
29 Wi += genServ.nextDouble() - genArr.nextDouble();
30 if (Wi < 0.0)
31 Wi = 0.0;
32 waitingTimes.add(Wi);
33 }
34 return waitingTimes.average();
35 }
36
37 public void simulateRuns(int n, int numCust) {
38 averageWaits.init();
39 for (int i = 0; i < n; i++)
40 averageWaits.add(simulate(numCust));
41 }
42
43 // A listener that observes each waiting time and prints every `step`th one.
44 public class ObservationTrace implements ObservationListener {
45 private int step;
46
47 public ObservationTrace(int step) {
48 this.step = step;
49 }
50
51 public void newObservation(StatProbe probe, double x) {
52 if (cust % step == 0)
53 System.out.println("Customer " + cust + " waited " + x + " time units.");
54 }
55 }
56
57 // A listener that observes waiting times and collects those larger than
58 // threshold.
59 public class LargeWaitsCollector implements ObservationListener {
60 double threshold;
61 ArrayList<Double> largeWaits = new ArrayList<Double>();
62
63 public LargeWaitsCollector(double threshold) {
64 this.threshold = threshold;
65 }
66
67 public void newObservation(StatProbe probe, double x) {
68 if (x > threshold)
69 largeWaits.add(x);
70 }
71
72 // Maybe print the list largeWaits.
73 public String formatLargeWaits() {
74 return "not yet implemented...";
75 }
76 }
77
78 public static void main(String[] args) {
79 QueueObs queue = new QueueObs(1.0, 2.0, 5);
80 queue.simulateRuns(2, 100);
81 System.out.println("\n\n" + queue.averageWaits.report());
82 }
83}
void newObservation(StatProbe probe, double x)
Receives the new observation x broadcast by probe.
Definition QueueObs.java:67
void newObservation(StatProbe probe, double x)
Receives the new observation x broadcast by probe.
Definition QueueObs.java:51
This class implements random variate generators for the exponential distribution.
This is the base class for all random variate generators over the real line.
Extends the abstract class RandomStreamBase by using as a backbone (or main) generator the combined m...
Definition MRG32k3a.java:46
The objects of this class are statistical probes or collectors, which are elementary devices for coll...
A subclass of StatProbe.
Definition Tally.java:47
Represents an object that can listen to observations broadcast by statistical probes.