SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
Accumulate.java
1/*
2 * Class: Accumulate
3 * Description: collects statistics on a variable that evolves in
4 simulation time
5 * Environment: Java
6 * Software: SSJ
7 * Copyright (C) 2001 Pierre L'Ecuyer and Universite de Montreal
8 * Organization: DIRO, Universite de Montreal
9 * @author
10 * @since
11 *
12 *
13 * Licensed under the Apache License, Version 2.0 (the "License");
14 * you may not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS,
21 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
24 *
25 */
26package umontreal.ssj.simevents;
27// This class doesn't belong to package stat because objects of this class
28
29// always depend of Simulator
30
31import java.util.Observable;
32import umontreal.ssj.util.PrintfFormat;
33import umontreal.ssj.stat.StatProbe;
34
44public class Accumulate extends StatProbe implements Cloneable {
45
46 private double initTime; // Initialization time.
47 private double lastTime; // Last update time.
48 private double lastValue; // Value since last update.
49 private Simulator sim;
50
55 public Accumulate() {
56 super();
58 init();
59 }
60
67 public Accumulate(Simulator inSim) {
68 super();
69 if (inSim == null)
70 throw new NullPointerException();
71 sim = inSim;
72 init();
73 }
74
79 public Accumulate(String name) {
80 super();
82 this.name = name;
83 init();
84 }
85
93 public Accumulate(Simulator inSim, String name) {
94 super();
95 if (inSim == null)
96 throw new NullPointerException();
97 sim = inSim;
98 this.name = name;
99 init();
100 }
101
108 public void init() {
109 maxValue = Double.MIN_VALUE;
110 minValue = Double.MAX_VALUE;
111 lastValue = 0.0;
112 sumValue = 0.0;
113 // May start the accumulator at t > 0; for ex., a warm-up period or
114 // other reasons
115 initTime = lastTime = sim.time();
116 }
117
123 public void init(double x) {
124 init();
125 update(x);
126 }
127
131 public void update() {
132 update(lastValue);
133 }
134
143 public void update(double x) {
144 if (collect) {
145 double time = sim.time();
146 if (x < minValue)
147 minValue = x;
148 if (x > maxValue)
149 maxValue = x;
150 sumValue += lastValue * (time - lastTime);
151 lastValue = x;
152 lastTime = time;
153 }
154 if (broadcast) {
155 // setChanged();
157 }
158 }
159
160 public double sum() {
161 update(lastValue);
162 return sumValue;
163 }
164
169 public double average() {
170 update(lastValue);
171 double periode = lastTime - initTime;
172 if (periode > 0.0)
173 return sumValue / periode;
174 else
175 return 0.0;
176 }
177
178 public String shortReportHeader() {
179 PrintfFormat pf = new PrintfFormat();
180 pf.append(-9, "from time").append(" ");
181 pf.append(-9, "to time").append(" ");
182 pf.append(-8, " min").append(" ");
183 pf.append(-8, " max").append(" ");
184 pf.append(-10, " average");
185 return pf.toString();
186 }
187
188 public String shortReport() {
189 update();
190 PrintfFormat pf = new PrintfFormat();
191 pf.append(9, 2, 2, getInitTime()).append(" ");
192 pf.append(9, 2, 2, getLastTime()).append(" ");
193 pf.append(8, 3, 2, min()).append(" ");
194 pf.append(8, 3, 2, max()).append(" ");
195 pf.append(10, 3, 2, average());
196 return pf.toString();
197 }
198
203 public String report() {
204 update(lastValue);
205 PrintfFormat str = new PrintfFormat();
206 str.append("REPORT on Accumulate stat. collector ==> " + name);
207 str.append(PrintfFormat.NEWLINE + " from time to time min max");
208 str.append(" average").append(PrintfFormat.NEWLINE);
209 str.append(12, 2, 2, initTime);
210 str.append(13, 2, 2, lastTime);
211 str.append(11, 3, 2, minValue);
212 str.append(12, 3, 2, (double) maxValue);
213 str.append(14, 3, 2, (double) average()).append(PrintfFormat.NEWLINE);
214
215 return str.toString();
216 }
217
224 public double getInitTime() {
225 return initTime;
226 }
227
235 public double getLastTime() {
236 return lastTime;
237 }
238
245 public double getLastValue() {
246 return lastValue;
247 }
248
255 return sim;
256 }
257
264 public void setSimulator(Simulator sim) {
265 if (sim == null)
266 throw new NullPointerException();
267 this.sim = sim;
268 }
269
273 public Accumulate clone() {
274 try {
275 return (Accumulate) super.clone();
276 } catch (CloneNotSupportedException e) {
277 throw new IllegalStateException("Accumulate can't clone");
278 }
279 }
280
281}
double getLastTime()
Returns the last update time for this object.
double getLastValue()
Returns the value passed to this probe by the last call to its update method (or the initial value if...
Accumulate(Simulator inSim, String name)
Constructs-initializes a new Accumulate statistical probe with name name and initial time 0.
double average()
Returns the time-average since the last initialization to the last call to update.
double sum()
Returns the sum cumulated so far for this probe.
Accumulate(Simulator inSim)
Constructs a new Accumulate statistical probe linked to the given simulator, and initializes it by in...
void update(double x)
Gives a new observation x to the statistical collector.
String report()
Returns a string containing a report on this collector since its last initialization.
void update()
Updates the accumulator using the last value passed to update(double).
Accumulate()
Constructs a new Accumulate statistical probe using the default simulator and initializes it by invok...
Simulator simulator()
Returns the simulator associated with this statistical probe.
Accumulate(String name)
Constructs and initializes a new Accumulate statistical probe with name name and initial time 0,...
String shortReport()
Formats and returns a short, one-line report about this statistical probe.
void init(double x)
Same as init followed by update(x).
void setSimulator(Simulator sim)
Sets the simulator associated with this probe to sim.
void init()
Initializes the statistical collector and puts the current value of the corresponding variable to 0.
String shortReportHeader()
Returns a string containing the name of the values returned in the report strings.
double getInitTime()
Returns the initialization time for this object.
Accumulate clone()
Clone this object.
Represents the executive of a discrete-event simulator.
static Simulator getDefaultSimulator()
Returns the default simulator instance used by the deprecated class.
The objects of this class are statistical probes or collectors, which are elementary devices for coll...
void notifyListeners(double x)
Notifies the observation x to all registered observers if broadcasting is ON.
double min()
Returns the smallest value taken by the variable since the last initialization of this probe.
double max()
Returns the largest value taken by the variable since the last initialization of this probe.
This class acts like a StringBuffer which defines new types of append methods.
String toString()
Converts the buffer into a String.
PrintfFormat append(String str)
Appends str to the buffer.
static final String NEWLINE
End-of-line symbol or line separator.