1 | package desmoj.core.dist; |
2 | |
3 | import desmoj.core.simulator.NamedObject; |
4 | |
5 | /** |
6 | * Controls all distributions used during an experiment. Provides the service of |
7 | * automatic seed generation for all distributions registered at the |
8 | * distributionmanager. Note that all distributions register at instantiation |
9 | * time at the experiment's distributionmanager automatically. |
10 | * |
11 | * @version DESMO-J, Ver. 2.3.3 copyright (c) 2011 |
12 | * @author Tim Lechler |
13 | * |
14 | * Licensed under the Apache License, Version 2.0 (the "License"); you |
15 | * may not use this file except in compliance with the License. You may |
16 | * obtain a copy of the License at |
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 |
22 | * implied. See the License for the specific language governing |
23 | * permissions and limitations under the License. |
24 | * |
25 | */ |
26 | public class DistributionManager extends NamedObject { |
27 | |
28 | /** |
29 | * The current default random number generator to be used by newly created |
30 | * distributions. |
31 | */ |
32 | private Class<? extends UniformRandomGenerator> _currentDefaultGenerator; |
33 | |
34 | /** |
35 | * Value sets antithetic mode for all distributions registering at the |
36 | * distributionmanager. |
37 | */ |
38 | private boolean _antitheticMode; |
39 | |
40 | /** |
41 | * Keeps references to all distributions of this experiment |
42 | */ |
43 | private java.util.ArrayList<Distribution> _distributions; |
44 | |
45 | /** |
46 | * Produces all starting seeds for registered distributions. |
47 | */ |
48 | private UniformRandomGenerator _seedGenerator; |
49 | |
50 | /** |
51 | * Creates a new distributionManager with the given name and the given |
52 | * initial seed for the seed-generator. |
53 | * |
54 | * @param name |
55 | * java.lang.String : The distributionmanager's name |
56 | * @param seed |
57 | * long : The initial seed for the seedgenerator |
58 | */ |
59 | public DistributionManager(String name, long seed) { |
60 | |
61 | super(name + "_DistributionManager"); // create the NamedObject |
62 | |
63 | _antitheticMode = false; // set antithetic mode to false by default |
64 | _seedGenerator = new LinearCongruentialRandomGenerator(seed); // create |
65 | // seed |
66 | // generator |
67 | _currentDefaultGenerator = LinearCongruentialRandomGenerator.class; |
68 | _distributions = new java.util.ArrayList<Distribution>(); // init List |
69 | // for dist |
70 | |
71 | } |
72 | |
73 | /** |
74 | * De-registers a distribution from the experiment. |
75 | * |
76 | * @param dist |
77 | * desmoj.dist.Distribution : The distribution to be deregistered |
78 | */ |
79 | public void deRegister(Distribution dist) { |
80 | |
81 | _distributions.remove(dist); // remove from List |
82 | |
83 | } |
84 | |
85 | /** |
86 | * Provides all registered distributions with new seed values, thus |
87 | * resetting all distribution statistics at the same time. |
88 | */ |
89 | public void newSeedAll() { |
90 | |
91 | for (Distribution d : _distributions) { |
92 | d.setSeed(nextSeed()); |
93 | } |
94 | } |
95 | |
96 | /** |
97 | * Returns a new seed value to be used as an initial seed for registered |
98 | * distributions. |
99 | * |
100 | * @return long : A new seed value for a registered distribution |
101 | */ |
102 | public long nextSeed() { |
103 | |
104 | // get a positive seed value |
105 | return (long) (_seedGenerator.nextDouble() * 100000000); |
106 | |
107 | } |
108 | |
109 | /** |
110 | * Registers a new distribution at the experiment to control antithetic mode |
111 | * and set random seed values. |
112 | * |
113 | * @param dist |
114 | * desmoj.dist.Distribution : The distribution to be registered |
115 | */ |
116 | public void register(Distribution dist) { |
117 | |
118 | dist.setAntithetic(_antitheticMode); // set antithetic mode to default |
119 | dist.setSeed(nextSeed()); // set new seed |
120 | _distributions.add(dist); // add to Vector |
121 | |
122 | } |
123 | |
124 | /** |
125 | * Resets all registered distributions. Just calls all distribution's |
126 | * individual reset method. |
127 | */ |
128 | public void resetAll() { |
129 | |
130 | for (Distribution d : _distributions) { |
131 | d.reset(); |
132 | } |
133 | } |
134 | |
135 | /** |
136 | * Sets antithetic mode to true on all registered distributions regardless |
137 | * of their previous status. No reset of statistical counters. |
138 | * |
139 | * @param antitheticMode |
140 | * boolean : The new status of antithetic mode |
141 | */ |
142 | public void setAntitheticAll(boolean antitheticMode) { |
143 | |
144 | for (Distribution d : _distributions) { |
145 | d.setAntithetic(antitheticMode); |
146 | } |
147 | } |
148 | |
149 | /** |
150 | * Sets the seed of the SeedGenerator to the given value. If the seed is not |
151 | * set here, its default is zero, unless specified in the experimentoptions. |
152 | * |
153 | * @param newSeed |
154 | * long : The new seed for the seedgenerator |
155 | */ |
156 | public void setSeed(long newSeed) { |
157 | |
158 | _seedGenerator.setSeed(newSeed); // go ahead and set it! |
159 | |
160 | } |
161 | |
162 | /** |
163 | * Sets the underlying pseudo random number generator to be used by all |
164 | * distributions created from now on. The default generator is |
165 | * LinearCongruentialRandomGenerator; any other generator to be used must |
166 | * implement the interface UniformRandomGenerator. |
167 | * |
168 | * @see desmoj.core.dist.LinearCongruentialRandomGenerator |
169 | * @see desmoj.core.dist.UniformRandomGenerator |
170 | * |
171 | * @param randomNumberGenerator |
172 | * Class : The random number generator class to be used |
173 | */ |
174 | public void setRandomNumberGenerator( |
175 | Class<? extends UniformRandomGenerator> randomNumberGenerator) { |
176 | |
177 | this._currentDefaultGenerator = randomNumberGenerator; |
178 | |
179 | } |
180 | |
181 | /** |
182 | * Returns the underlying pseudo random number generator to be used by all |
183 | * distributions. This method is intended for internal use (i.e. called by |
184 | * Distribution) only. |
185 | * |
186 | * @see desmoj.core.dist.LinearCongruentialRandomGenerator |
187 | * @see desmoj.core.dist.MersenneTwisterRandomGenerator |
188 | * @see desmoj.core.dist.UniformRandomGenerator |
189 | * |
190 | */ |
191 | protected Class<? extends UniformRandomGenerator> getRandomNumberGenerator() { |
192 | |
193 | return this._currentDefaultGenerator; |
194 | |
195 | } |
196 | |
197 | /** |
198 | * Returns a list containing all distributions. |
199 | */ |
200 | public java.util.List<Distribution> getDistributions() { |
201 | return new java.util.ArrayList<Distribution>(this._distributions); |
202 | } |
203 | } |