SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
RepSim.java
1/*
2 * Class: RepSim
3 * Description: simulation using independent runs or replications
4 * Environment: Java
5 * Software: SSJ
6 * Copyright (C) 2001 Pierre L'Ecuyer and Universite de Montreal
7 * Organization: DIRO, Universite de Montreal
8 * @author Éric Buist
9 * @since 2007
10
11 * SSJ is free software: you can redistribute it and/or modify it under
12 * the terms of the GNU General Public License (GPL) as published by the
13 * Free Software Foundation, either version 3 of the License, or
14 * any later version.
15
16 * SSJ is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20
21 * A copy of the GNU General Public License is available at
22 <a href="http://www.gnu.org/licenses">GPL licence site</a>.
23 */
24package umontreal.ssj.simexp;
25
26import umontreal.ssj.simevents.Simulator;
27
47public abstract class RepSim extends SimExp {
48 private int minReps;
49 private int maxReps;
50 private int targetReps;
51 private int doneReps;
52
61 public RepSim(int minReps) {
62 this(minReps, Integer.MAX_VALUE);
63 }
64
76 public RepSim(int minReps, int maxReps) {
77 this(Simulator.getDefaultSimulator(), minReps, maxReps);
78 }
79
88 public RepSim(Simulator sim, int minReps) {
89 this(sim, minReps, Integer.MAX_VALUE);
90 }
91
102 public RepSim(Simulator sim, int minReps, int maxReps) {
103 super(sim);
104 if (minReps <= 0)
105 throw new IllegalArgumentException("minReps <= 0");
106 if (minReps > maxReps)
107 throw new IllegalArgumentException("minReps > maxReps");
108 this.minReps = minReps;
109 this.maxReps = maxReps;
110 targetReps = minReps;
111 }
112
119 public int getMinReplications() {
120 return minReps;
121 }
122
133 public void setMinReplications(int minReps) {
134 if (minReps <= 0)
135 throw new IllegalArgumentException("minReps <= 0");
136 this.minReps = minReps;
137 if (maxReps < minReps)
138 maxReps = minReps;
139 }
140
148 public int getMaxReplications() {
149 return maxReps;
150 }
151
160 public void setMaxReplications(int maxReps) {
161 if (maxReps < minReps)
162 throw new IllegalArgumentException("maxReps < minReps");
163 this.maxReps = maxReps;
164 }
165
175 return targetReps;
176 }
177
189 public void setTargetReplications(int targetReps) {
190 if (targetReps < minReps)
191 throw new IllegalArgumentException("Target number of replications too small");
192 if (targetReps > maxReps)
193 throw new IllegalArgumentException("Target number of replications too large");
194 this.targetReps = targetReps;
195 }
196
204 return doneReps;
205 }
206
211 public abstract void initReplicationProbes();
212
223 public void performReplication(int r) {
224 simulator().init();
226 simulator().start();
229 }
230
235 protected void replicationDone() {
236 ++doneReps;
237 }
238
248 public abstract void initReplication(int r);
249
256 public abstract void addReplicationObs(int r);
257
268 return 0;
269 }
270
276 public void init() {
277 if (simulating)
278 throw new IllegalStateException("Already simulating");
279 doneReps = 0;
280 if (targetReps < minReps)
281 // minReps has been increased, so increase targetReps too
282 targetReps = minReps;
284 }
285
295 public void adjustTargetReplications(int numNewReplications) {
296 if (numNewReplications < 0)
297 throw new IllegalArgumentException("numReplications < 0");
298 if (numNewReplications == 0)
299 return;
300 targetReps = doneReps + numNewReplications;
301 if (targetReps > maxReps)
302 targetReps = maxReps;
303 }
304
316 public void simulate() {
317 init();
318 simulating = true;
319 try {
320 while (doneReps < targetReps) {
321 for (int i = 0; i < targetReps; i++)
324 }
325 } finally {
326 simulating = false;
327 }
328 }
329
330 public String toString() {
331 StringBuffer sb = new StringBuffer(getClass().getName());
332 sb.append('[');
333 sb.append("minimal number of replications: ").append(minReps);
334 if (maxReps < Integer.MAX_VALUE)
335 sb.append(", maximal number of replications: ").append(maxReps);
336 sb.append(", target number of replications: ").append(targetReps);
337 if (simulating)
338 sb.append(", simulation in progress");
339 else
340 sb.append(", simulation stopped");
341 sb.append(", number of completed replications: ").append(doneReps);
342 sb.append(']');
343 return sb.toString();
344 }
345}
Represents the executive of a discrete-event simulator.
void start()
Starts the simulation executive.
void init()
Reinitializes the simulation executive by clearing up the event list, and resetting the simulation cl...
static Simulator getDefaultSimulator()
Returns the default simulator instance used by the deprecated class.
int getMaxReplications()
Returns the maximal number of replications to be simulated before an error check.
Definition RepSim.java:148
int getCompletedReplications()
Returns the total number of completed replications for the current experiment.
Definition RepSim.java:203
void setTargetReplications(int targetReps)
Sets the target number of simulated replications before an error check to targetReps.
Definition RepSim.java:189
RepSim(Simulator sim, int minReps, int maxReps)
Equivalent to the second constructor, with the given simulator sim.
Definition RepSim.java:102
void setMaxReplications(int maxReps)
Sets the maximal number of replications required before an error check to maxReps.
Definition RepSim.java:160
RepSim(Simulator sim, int minReps)
Equivalent to the first constructor, with the given simulator sim.
Definition RepSim.java:88
int getMinReplications()
Returns the minimal number of replications to be simulated before an error check.
Definition RepSim.java:119
RepSim(int minReps)
Constructs a new replications-based simulator with a minimal number of runs, minReps,...
Definition RepSim.java:61
void simulate()
Simulates several independent simulation replications of a system.
Definition RepSim.java:316
abstract void addReplicationObs(int r)
Adds statistical observations for the replication&#160;r.
abstract void initReplicationProbes()
Initializes any statistical collector used to collect values for replications.
abstract void initReplication(int r)
Initializes the simulation model for a new replication&#160;r.
int getRequiredNewReplications()
Returns the approximate number of additional replications to meet an experiment-specific stopping cri...
Definition RepSim.java:267
RepSim(int minReps, int maxReps)
Constructs a new replications-based simulator with a minimal number of runs minReps,...
Definition RepSim.java:76
void setMinReplications(int minReps)
Sets the minimal number of replications required before an error check to minReps.
Definition RepSim.java:133
void replicationDone()
Increments by one the number of completed replications.
Definition RepSim.java:235
void init()
Initializes this simulator for a new experiment.
Definition RepSim.java:276
int getTargetReplications()
Returns the actual target number of replications to be simulated before an error check.
Definition RepSim.java:174
void adjustTargetReplications(int numNewReplications)
Adjusts the target number of replications to simulate numNewReplications additional replications.
Definition RepSim.java:295
void performReplication(int r)
Contains the necessary logic to perform the rth replication of the simulation.
Definition RepSim.java:223
boolean simulating
Determines if the simulation is in progress.
Definition SimExp.java:50
SimExp()
Constructs a new object for performing experiments using the default simulator returned by Simulator....
Definition SimExp.java:56
final Simulator simulator()
Returns the simulator linked to this experiment object.
Definition SimExp.java:77