SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
San13Dist.java
1package tutorial;
2
3import java.io.*;
4import java.util.Scanner;
5import umontreal.ssj.charts.HistogramChart;
6import umontreal.ssj.charts.EmpiricalChart;
7import umontreal.ssj.probdist.ContinuousDistribution;
8import umontreal.ssj.probdist.DistributionFactory;
9import umontreal.ssj.rng.*;
10import umontreal.ssj.mcqmctools.*;
11import umontreal.ssj.stat.TallyStore;
12
21
22public class San13Dist implements MonteCarloModelDouble {
23
24 int dim = 13; // model dimension (number of uniforms needed for a simulation).
25 double[] V = new double[dim];
27 // We consider the 6 paths that can lead to the sink.
28 double[] paths = new double[6]; // Path lengths.
29 double maxPath; // Length of the current longest path.
30
31 // The constructor reads link length distributions in a file.
32 public San13Dist(String fileName) throws IOException {
33 readDistributions(fileName);
34 }
35
36 public void readDistributions(String fileName) throws IOException {
37 // Reads data and construct arrays.
38 BufferedReader input = new BufferedReader(new FileReader(fileName));
39 Scanner scan = new Scanner(input);
40 for (int k = 0; k < 13; k++) {
41 dist[k] = DistributionFactory.getContinuousDistribution(scan.nextLine());
42 }
43 scan.close();
44 }
45
46 public int getDimension() {
47 return dim;
48 }
49
50 public double deterministicLengths() {
51 for (int k = 0; k < 13; k++) {
52 V[k] = dist[k].getMean();
53 if (V[k] < 0.0)
54 V[k] = 0.0;
55 }
56 return computePathsAndT();
57 }
58
59 public void simulate(RandomStream stream) {
60 for (int k = 0; k < 13; k++) {
61 V[k] = dist[k].inverseF(stream.nextDouble());
62 if (V[k] < 0.0)
63 V[k] = 0.0;
64 }
65 computePathsAndT();
66 }
67
68 // Compute the lengths of all paths and returns the longest length T
69 public double computePathsAndT() {
70 // Path lengths
71 paths[0] = V[1] + V[5] + V[10];
72 paths[1] = V[0] + V[2] + V[5] + V[10];
73 paths[2] = V[0] + V[4] + V[10];
74 paths[3] = V[0] + V[3] + V[7] + V[9] + V[10];
75 paths[4] = V[0] + V[3] + V[7] + V[8] + V[12];
76 paths[5] = V[0] + V[3] + V[6] + V[11] + V[12];
77 maxPath = paths[0];
78 for (int p = 1; p < 6; p++)
79 if (paths[p] > maxPath)
80 maxPath = paths[p];
81 return maxPath;
82 }
83
84 // Returns the length T of longest path.
85 public double getPerformance() {
86 return maxPath;
87 }
88
89 public String toString() {
90 String s = "SAN network with 9 nodes and 13 links, from Elmaghraby (1977)\n"
91 + "Estimate distribution of length T of longest path \n";
92 return s;
93 }
94
95 public String getTag() {
96 return "San13Dist";
97 }
98
99 public static void main(String[] args) throws IOException {
100 int n = 100000;
101 int groupSize = 100;
102 San13Dist san = new San13Dist("src/main/docs/examples/tutorial/san13a.dat");
103 TallyStore statT = new TallyStore("TallyStore for SAN13 example");
104 System.out.println(MonteCarloExperiment.simulateRunsDefaultReportStudent(san, n, new LFSR113(), statT, 0.95, 4));
105 statT.quickSort();
106
107 Writer file;
108 TallyStore statTaggregated = statT;
109 if (groupSize > 1)
110 statTaggregated = statT.aggregate(groupSize);
111 double[] aggreg = statTaggregated.getArray();
112 EmpiricalChart cdf = new EmpiricalChart("Empirical cdf of $T$", "Values of $T$", "cdf", aggreg,
113 statTaggregated.numberObs());
114 double[] bounds2 = { 0, 200, 0, 1.0 };
115 cdf.setManualRange(bounds2);
116 cdf.view(800, 500);
117 // String cdfLatex = cdf.toLatex(12.0, 8.0);
118 // file = new FileWriter("src/main/docs/examples/tutorial/san13cdf.tex");
119 // file.write(cdfLatex);
120 // file.close();
121
122 HistogramChart hist = new HistogramChart("Distribution of $T$", "Values of $T$", "Frequency", statT.getArray(),
123 n);
124 double[] bounds = { 0, 200, 0, 12000 };
125 hist.setManualRange(bounds);
126 (hist.getSeriesCollection()).setBins(0, 40, 0, 200);
127 hist.view(800, 500);
128 String histLatex = hist.toLatex(12.0, 8.0);
129 file = new FileWriter("src/main/docs/examples/tutorial/san13chart.tex");
130 file.write(histLatex);
131 file.close();
132
133 // Print p-th quantile
134 double p = 0.99;
135 int index = (int) Math.round(p * n);
136 double xip = statT.getArray()[index];
137 System.out.printf("%5.3g -th quantile: %9.6g \n", p, xip);
138 }
139}
This class simulates a specific stochastic activity network with 9 nodes and 13 links,...
double getPerformance()
Recovers and returns the realization of the performance measure, of type double.
String toString()
Returns a description of the model and its parameters.
String getTag()
Returns a short model name (tag) to be used in reports.
void simulate(RandomStream stream)
Simulates the model for one run.
Provides tools to create and manage empirical plots, which are used to plot empirical distributions.
JFrame view(int width, int height)
Displays chart on the screen using Swing.
void setManualRange(double[] range)
Sets the and ranges of the chart using the format: range = [xmin, xmax, ymin, ymax].
Definition XYChart.java:261
Provides generic tools to perform simple Monte Carlo experiments with a simulation model that impleme...
static String simulateRunsDefaultReportStudent(MonteCarloModelDouble model, int n, RandomStream stream, Tally statValue, double level, int d, Chrono timer)
Performs n independent runs using n substreams of stream, collects statistics in statValue,...
Classes implementing continuous distributions should inherit from this base class.
This class implements a string API for the package probdist.
static ContinuousDistribution getContinuousDistribution(String str)
Uses the Java Reflection API to construct a.
Extends RandomStreamBase using a composite linear feedback shift register (LFSR) (or Tausworthe) RNG ...
Definition LFSR113.java:47
This class is a variant of Tally for which the individual observations are stored in a list implement...
void quickSort()
Sorts the elements of this probe using the quicksort from Colt.
TallyStore aggregate(int gsize)
Returns a new TallyStore instance that contains aggregate observations from this TallyStore.
double[] getArray()
Returns the observations stored in this probe.
int numberObs()
Returns the number of observations given to this probe since its last initialization.
Definition Tally.java:143
An interface for a very simple simulation model for which Monte Carlo (MC) and RQMC experiments are t...
This interface defines the basic structures to handle multiple streams of uniform (pseudo)random numb...
double nextDouble()
Returns a (pseudo)random number from the uniform distribution over the interval , using this stream,...