1 16 17 package org.apache.commons.math.random; 18 import java.io.BufferedReader ; 19 import java.io.InputStreamReader ; 20 import java.io.IOException ; 21 import java.net.URL ; 22 import java.net.MalformedURLException ; 23 24 45 public class ValueServer { 46 47 private int mode = 5; 48 49 50 private URL valuesFileURL = null; 51 52 53 private double mu = 0.0; 54 55 56 private double sigma = 0.0; 57 58 59 private EmpiricalDistribution empiricalDistribution = null; 60 61 62 private BufferedReader filePointer = null; 63 64 65 private RandomData randomData = new RandomDataImpl(); 66 67 69 70 public static final int DIGEST_MODE = 0; 71 72 73 public static final int REPLAY_MODE = 1; 74 75 76 public static final int UNIFORM_MODE = 2; 77 78 79 public static final int EXPONENTIAL_MODE = 3; 80 81 82 public static final int GAUSSIAN_MODE = 4; 83 84 85 public static final int CONSTANT_MODE = 5; 86 87 88 public ValueServer() { 89 } 90 91 98 public double getNext() throws IOException { 99 switch (mode) { 100 case DIGEST_MODE: return getNextDigest(); 101 case REPLAY_MODE: return getNextReplay(); 102 case UNIFORM_MODE: return getNextUniform(); 103 case EXPONENTIAL_MODE: return getNextExponential(); 104 case GAUSSIAN_MODE: return getNextGaussian(); 105 case CONSTANT_MODE: return mu; 106 default: throw new IllegalStateException 107 ("Bad mode: " + mode); 108 } 109 } 110 111 117 public void fill(double[] values) throws IOException { 118 for (int i = 0; i < values.length; i++) { 119 values[i] = getNext(); 120 } 121 } 122 123 131 public double[] fill(int length) throws IOException { 132 double[] out = new double[length]; 133 for (int i = 0; i < length; i++) { 134 out[i] = getNext(); 135 } 136 return out; 137 } 138 139 151 public void computeDistribution() throws IOException { 152 empiricalDistribution = new EmpiricalDistributionImpl(); 153 empiricalDistribution.load(valuesFileURL); 154 } 155 156 170 public void computeDistribution(int binCount) 171 throws IOException { 172 empiricalDistribution = new EmpiricalDistributionImpl(binCount); 173 empiricalDistribution.load(valuesFileURL); 174 mu = empiricalDistribution.getSampleStats().getMean(); 175 sigma = empiricalDistribution.getSampleStats().getStandardDeviation(); 176 } 177 178 181 public int getMode() { 182 return mode; 183 } 184 185 188 public void setMode(int mode) { 189 this.mode = mode; 190 } 191 192 196 public URL getValuesFileURL() { 197 return valuesFileURL; 198 } 199 200 205 public void setValuesFileURL(String url) throws MalformedURLException { 206 this.valuesFileURL = new URL (url); 207 } 208 209 213 public void setValuesFileURL(URL url) { 214 this.valuesFileURL = url; 215 } 216 217 220 public EmpiricalDistribution getEmpiricalDistribution() { 221 return empiricalDistribution; 222 } 223 224 229 public void resetReplayFile() throws IOException { 230 if (filePointer != null) { 231 try { 232 filePointer.close(); 233 filePointer = null; 234 } catch (IOException ex) { 235 } 237 } 238 filePointer = new BufferedReader (new InputStreamReader (valuesFileURL.openStream())); 239 } 240 241 246 public void closeReplayFile() throws IOException { 247 if (filePointer != null) { 248 filePointer.close(); 249 filePointer = null; 250 } 251 } 252 253 256 public double getMu() { 257 return mu; 258 } 259 260 263 public void setMu(double mu) { 264 this.mu = mu; 265 } 266 267 270 public double getSigma() { 271 return sigma; 272 } 273 274 277 public void setSigma(double sigma) { 278 this.sigma = sigma; 279 } 280 281 283 293 private double getNextDigest() { 294 if ((empiricalDistribution == null) || 295 (empiricalDistribution.getBinStats().size() == 0)) { 296 throw new IllegalStateException ("Digest not initialized"); 297 } 298 return empiricalDistribution.getNextValue(); 299 } 300 301 315 private double getNextReplay() throws IOException { 316 String str = null; 317 if (filePointer == null) { 318 resetReplayFile(); 319 } 320 if ((str = filePointer.readLine()) == null) { 321 closeReplayFile(); 322 resetReplayFile(); 323 str = filePointer.readLine(); 324 } 325 return new Double (str).doubleValue(); 326 } 327 328 333 private double getNextUniform() { 334 return randomData.nextUniform(0, 2 * mu); 335 } 336 337 342 private double getNextExponential() { 343 return randomData.nextExponential(mu); 344 } 345 346 352 private double getNextGaussian() { 353 return randomData.nextGaussian(mu, sigma); 354 } 355 356 363 public ValueServer(RandomData randomData) { 364 super(); 365 this.randomData = randomData; 366 } 367 } 368 | Popular Tags |