001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.math3.random;
019import java.io.BufferedReader;
020import java.io.IOException;
021import java.io.InputStreamReader;
022import java.net.MalformedURLException;
023import java.net.URL;
024
025import org.apache.commons.math3.exception.MathIllegalArgumentException;
026import org.apache.commons.math3.exception.MathIllegalStateException;
027import org.apache.commons.math3.exception.NullArgumentException;
028import org.apache.commons.math3.exception.ZeroException;
029import org.apache.commons.math3.exception.util.LocalizedFormats;
030
031/**
032 * Generates values for use in simulation applications.
033 * <p>
034 * How values are generated is determined by the <code>mode</code>
035 * property.</p>
036 * <p>
037 * Supported <code>mode</code> values are: <ul>
038 * <li> DIGEST_MODE -- uses an empirical distribution </li>
039 * <li> REPLAY_MODE -- replays data from <code>valuesFileURL</code></li>
040 * <li> UNIFORM_MODE -- generates uniformly distributed random values with
041 *                      mean = <code>mu</code> </li>
042 * <li> EXPONENTIAL_MODE -- generates exponentially distributed random values
043 *                         with mean = <code>mu</code></li>
044 * <li> GAUSSIAN_MODE -- generates Gaussian distributed random values with
045 *                       mean = <code>mu</code> and
046 *                       standard deviation = <code>sigma</code></li>
047 * <li> CONSTANT_MODE -- returns <code>mu</code> every time.</li></ul></p>
048 *
049 * @version $Id: ValueServer.java 1517416 2013-08-26 03:04:38Z dbrosius $
050 *
051 */
052public class ValueServer {
053
054    /** Use empirical distribution.  */
055    public static final int DIGEST_MODE = 0;
056
057    /** Replay data from valuesFilePath. */
058    public static final int REPLAY_MODE = 1;
059
060    /** Uniform random deviates with mean = &mu;. */
061    public static final int UNIFORM_MODE = 2;
062
063    /** Exponential random deviates with mean = &mu;. */
064    public static final int EXPONENTIAL_MODE = 3;
065
066    /** Gaussian random deviates with mean = &mu;, std dev = &sigma;. */
067    public static final int GAUSSIAN_MODE = 4;
068
069    /** Always return mu */
070    public static final int CONSTANT_MODE = 5;
071
072    /** mode determines how values are generated. */
073    private int mode = 5;
074
075    /** URI to raw data values. */
076    private URL valuesFileURL = null;
077
078    /** Mean for use with non-data-driven modes. */
079    private double mu = 0.0;
080
081    /** Standard deviation for use with GAUSSIAN_MODE. */
082    private double sigma = 0.0;
083
084    /** Empirical probability distribution for use with DIGEST_MODE. */
085    private EmpiricalDistribution empiricalDistribution = null;
086
087    /** File pointer for REPLAY_MODE. */
088    private BufferedReader filePointer = null;
089
090    /** RandomDataImpl to use for random data generation. */
091    private final RandomDataImpl randomData;
092
093    // Data generation modes ======================================
094
095    /** Creates new ValueServer */
096    public ValueServer() {
097        randomData = new RandomDataImpl();
098    }
099
100    /**
101     * Construct a ValueServer instance using a RandomDataImpl as its source
102     * of random data.
103     *
104     * @param randomData the RandomDataImpl instance used to source random data
105     * @since 3.0
106     * @deprecated use {@link #ValueServer(RandomGenerator)}
107     */
108    @Deprecated
109    public ValueServer(RandomDataImpl randomData) {
110        this.randomData = randomData;
111    }
112
113    /**
114     * Construct a ValueServer instance using a RandomGenerator as its source
115     * of random data.
116     *
117     * @since 3.1
118     * @param generator source of random data
119     */
120    public ValueServer(RandomGenerator generator) {
121        this.randomData = new RandomDataImpl(generator);
122    }
123
124    /**
125     * Returns the next generated value, generated according
126     * to the mode value (see MODE constants).
127     *
128     * @return generated value
129     * @throws IOException in REPLAY_MODE if a file I/O error occurs
130     * @throws MathIllegalStateException if mode is not recognized
131     * @throws MathIllegalArgumentException if the underlying random generator thwrows one
132     */
133    public double getNext() throws IOException, MathIllegalStateException, MathIllegalArgumentException {
134        switch (mode) {
135            case DIGEST_MODE: return getNextDigest();
136            case REPLAY_MODE: return getNextReplay();
137            case UNIFORM_MODE: return getNextUniform();
138            case EXPONENTIAL_MODE: return getNextExponential();
139            case GAUSSIAN_MODE: return getNextGaussian();
140            case CONSTANT_MODE: return mu;
141            default: throw new MathIllegalStateException(
142                    LocalizedFormats.UNKNOWN_MODE,
143                    mode,
144                    "DIGEST_MODE",   DIGEST_MODE,   "REPLAY_MODE",      REPLAY_MODE,
145                    "UNIFORM_MODE",  UNIFORM_MODE,  "EXPONENTIAL_MODE", EXPONENTIAL_MODE,
146                    "GAUSSIAN_MODE", GAUSSIAN_MODE, "CONSTANT_MODE",    CONSTANT_MODE);
147        }
148    }
149
150    /**
151     * Fills the input array with values generated using getNext() repeatedly.
152     *
153     * @param values array to be filled
154     * @throws IOException in REPLAY_MODE if a file I/O error occurs
155     * @throws MathIllegalStateException if mode is not recognized
156     * @throws MathIllegalArgumentException if the underlying random generator thwrows one
157     */
158    public void fill(double[] values)
159        throws IOException, MathIllegalStateException, MathIllegalArgumentException {
160        for (int i = 0; i < values.length; i++) {
161            values[i] = getNext();
162        }
163    }
164
165    /**
166     * Returns an array of length <code>length</code> with values generated
167     * using getNext() repeatedly.
168     *
169     * @param length length of output array
170     * @return array of generated values
171     * @throws IOException in REPLAY_MODE if a file I/O error occurs
172     * @throws MathIllegalStateException if mode is not recognized
173     * @throws MathIllegalArgumentException if the underlying random generator thwrows one
174     */
175    public double[] fill(int length)
176        throws IOException, MathIllegalStateException, MathIllegalArgumentException {
177        double[] out = new double[length];
178        for (int i = 0; i < length; i++) {
179            out[i] = getNext();
180        }
181        return out;
182    }
183
184    /**
185     * Computes the empirical distribution using values from the file
186     * in <code>valuesFileURL</code>, using the default number of bins.
187     * <p>
188     * <code>valuesFileURL</code> must exist and be
189     * readable by *this at runtime.</p>
190     * <p>
191     * This method must be called before using <code>getNext()</code>
192     * with <code>mode = DIGEST_MODE</code></p>
193     *
194     * @throws IOException if an I/O error occurs reading the input file
195     * @throws NullArgumentException if the {@code valuesFileURL} has not been set
196     * @throws ZeroException if URL contains no data
197     */
198    public void computeDistribution() throws IOException, ZeroException, NullArgumentException {
199        computeDistribution(EmpiricalDistribution.DEFAULT_BIN_COUNT);
200    }
201
202    /**
203     * Computes the empirical distribution using values from the file
204     * in <code>valuesFileURL</code> and <code>binCount</code> bins.
205     * <p>
206     * <code>valuesFileURL</code> must exist and be readable by this process
207     * at runtime.</p>
208     * <p>
209     * This method must be called before using <code>getNext()</code>
210     * with <code>mode = DIGEST_MODE</code></p>
211     *
212     * @param binCount the number of bins used in computing the empirical
213     * distribution
214     * @throws NullArgumentException if the {@code valuesFileURL} has not been set
215     * @throws IOException if an error occurs reading the input file
216     * @throws ZeroException if URL contains no data
217     */
218    public void computeDistribution(int binCount) throws NullArgumentException, IOException, ZeroException {
219        empiricalDistribution = new EmpiricalDistribution(binCount, randomData);
220        empiricalDistribution.load(valuesFileURL);
221        mu = empiricalDistribution.getSampleStats().getMean();
222        sigma = empiricalDistribution.getSampleStats().getStandardDeviation();
223    }
224
225    /**
226     * Returns the data generation mode. See {@link ValueServer the class javadoc}
227     * for description of the valid values of this property.
228     *
229     * @return Value of property mode.
230     */
231    public int getMode() {
232        return mode;
233    }
234
235    /**
236     * Sets the data generation mode.
237     *
238     * @param mode New value of the data generation mode.
239     */
240    public void setMode(int mode) {
241        this.mode = mode;
242    }
243
244    /**
245     * Returns the URL for the file used to build the empirical distribution
246     * when using {@link #DIGEST_MODE}.
247     *
248     * @return Values file URL.
249     */
250    public URL getValuesFileURL() {
251        return valuesFileURL;
252    }
253
254    /**
255     * Sets the {@link #getValuesFileURL() values file URL} using a string
256     * URL representation.
257     *
258     * @param url String representation for new valuesFileURL.
259     * @throws MalformedURLException if url is not well formed
260     */
261    public void setValuesFileURL(String url) throws MalformedURLException {
262        this.valuesFileURL = new URL(url);
263    }
264
265    /**
266     * Sets the the {@link #getValuesFileURL() values file URL}.
267     *
268     * <p>The values file <i>must</i> be an ASCII text file containing one
269     * valid numeric entry per line.</p>
270     *
271     * @param url URL of the values file.
272     */
273    public void setValuesFileURL(URL url) {
274        this.valuesFileURL = url;
275    }
276
277    /**
278     * Returns the {@link EmpiricalDistribution} used when operating in {@value #DIGEST_MODE}.
279     *
280     * @return EmpircalDistribution built by {@link #computeDistribution()}
281     */
282    public EmpiricalDistribution getEmpiricalDistribution() {
283        return empiricalDistribution;
284    }
285
286    /**
287     * Resets REPLAY_MODE file pointer to the beginning of the <code>valuesFileURL</code>.
288     *
289     * @throws IOException if an error occurs opening the file
290     */
291    public void resetReplayFile() throws IOException {
292        if (filePointer != null) {
293            try {
294                filePointer.close();
295                filePointer = null;
296            } catch (IOException ex) { //NOPMD
297                // ignore
298            }
299        }
300        filePointer = new BufferedReader(new InputStreamReader(valuesFileURL.openStream(), "UTF-8"));
301    }
302
303    /**
304     * Closes {@code valuesFileURL} after use in REPLAY_MODE.
305     *
306     * @throws IOException if an error occurs closing the file
307     */
308    public void closeReplayFile() throws IOException {
309        if (filePointer != null) {
310            filePointer.close();
311            filePointer = null;
312        }
313    }
314
315    /**
316     * Returns the mean used when operating in {@link #GAUSSIAN_MODE}, {@link #EXPONENTIAL_MODE}
317     * or {@link #UNIFORM_MODE}.  When operating in {@link #CONSTANT_MODE}, this is the constant
318     * value always returned.  Calling {@link #computeDistribution()} sets this value to the
319     * overall mean of the values in the {@link #getValuesFileURL() values file}.
320     *
321     * @return Mean used in data generation.
322     */
323    public double getMu() {
324        return mu;
325    }
326
327    /**
328     * Sets the {@link #getMu() mean} used in data generation.  Note that calling this method
329     * after {@link #computeDistribution()} has been called will have no effect on data
330     * generated in {@link #DIGEST_MODE}.
331     *
332     * @param mu new Mean value.
333     */
334    public void setMu(double mu) {
335        this.mu = mu;
336    }
337
338    /**
339     * Returns the standard deviation used when operating in {@link #GAUSSIAN_MODE}.
340     * Calling {@link #computeDistribution()} sets this value to the overall standard
341     * deviation of the values in the {@link #getValuesFileURL() values file}.  This
342     * property has no effect when the data generation mode is not
343     * {@link #GAUSSIAN_MODE}.
344     *
345     * @return Standard deviation used when operating in {@link #GAUSSIAN_MODE}.
346     */
347    public double getSigma() {
348        return sigma;
349    }
350
351    /**
352     * Sets the {@link #getSigma() standard deviation} used in {@link #GAUSSIAN_MODE}.
353     *
354     * @param sigma New standard deviation.
355     */
356    public void setSigma(double sigma) {
357        this.sigma = sigma;
358    }
359
360    /**
361     * Reseeds the random data generator.
362     *
363     * @param seed Value with which to reseed the {@link RandomDataImpl}
364     * used to generate random data.
365     */
366    public void reSeed(long seed) {
367        randomData.reSeed(seed);
368    }
369
370    //------------- private methods ---------------------------------
371
372    /**
373     * Gets a random value in DIGEST_MODE.
374     * <p>
375     * <strong>Preconditions</strong>: <ul>
376     * <li>Before this method is called, <code>computeDistribution()</code>
377     * must have completed successfully; otherwise an
378     * <code>IllegalStateException</code> will be thrown</li></ul></p>
379     *
380     * @return next random value from the empirical distribution digest
381     * @throws MathIllegalStateException if digest has not been initialized
382     */
383    private double getNextDigest() throws MathIllegalStateException {
384        if ((empiricalDistribution == null) ||
385            (empiricalDistribution.getBinStats().size() == 0)) {
386            throw new MathIllegalStateException(LocalizedFormats.DIGEST_NOT_INITIALIZED);
387        }
388        return empiricalDistribution.getNextValue();
389    }
390
391    /**
392     * Gets next sequential value from the <code>valuesFileURL</code>.
393     * <p>
394     * Throws an IOException if the read fails.</p>
395     * <p>
396     * This method will open the <code>valuesFileURL</code> if there is no
397     * replay file open.</p>
398     * <p>
399     * The <code>valuesFileURL</code> will be closed and reopened to wrap around
400     * from EOF to BOF if EOF is encountered. EOFException (which is a kind of
401     * IOException) may still be thrown if the <code>valuesFileURL</code> is
402     * empty.</p>
403     *
404     * @return next value from the replay file
405     * @throws IOException if there is a problem reading from the file
406     * @throws MathIllegalStateException if URL contains no data
407     * @throws NumberFormatException if an invalid numeric string is
408     *   encountered in the file
409     */
410    private double getNextReplay() throws IOException, MathIllegalStateException {
411        String str = null;
412        if (filePointer == null) {
413            resetReplayFile();
414        }
415        if ((str = filePointer.readLine()) == null) {
416            // we have probably reached end of file, wrap around from EOF to BOF
417            closeReplayFile();
418            resetReplayFile();
419            if ((str = filePointer.readLine()) == null) {
420                throw new MathIllegalStateException(LocalizedFormats.URL_CONTAINS_NO_DATA,
421                                                    valuesFileURL);
422            }
423        }
424        return Double.parseDouble(str);
425    }
426
427    /**
428     * Gets a uniformly distributed random value with mean = mu.
429     *
430     * @return random uniform value
431     * @throws MathIllegalArgumentException if the underlying random generator thwrows one
432     */
433    private double getNextUniform() throws MathIllegalArgumentException {
434        return randomData.nextUniform(0, 2 * mu);
435    }
436
437    /**
438     * Gets an exponentially distributed random value with mean = mu.
439     *
440     * @return random exponential value
441     * @throws MathIllegalArgumentException if the underlying random generator thwrows one
442     */
443    private double getNextExponential() throws MathIllegalArgumentException {
444        return randomData.nextExponential(mu);
445    }
446
447    /**
448     * Gets a Gaussian distributed random value with mean = mu
449     * and standard deviation = sigma.
450     *
451     * @return random Gaussian value
452     * @throws MathIllegalArgumentException if the underlying random generator thwrows one
453     */
454    private double getNextGaussian() throws MathIllegalArgumentException {
455        return randomData.nextGaussian(mu, sigma);
456    }
457
458}