Class ValueServer
- java.lang.Object
-
- org.apache.commons.math3.random.ValueServer
-
public class ValueServer extends java.lang.ObjectGenerates values for use in simulation applications.How values are generated is determined by the
modeproperty.Supported
modevalues are:- DIGEST_MODE -- uses an empirical distribution
- REPLAY_MODE -- replays data from
valuesFileURL - UNIFORM_MODE -- generates uniformly distributed random values with
mean =
mu - EXPONENTIAL_MODE -- generates exponentially distributed random values
with mean =
mu - GAUSSIAN_MODE -- generates Gaussian distributed random values with
mean =
muand standard deviation =sigma - CONSTANT_MODE -- returns
muevery time.
-
-
Field Summary
Fields Modifier and Type Field Description static intCONSTANT_MODEAlways return mustatic intDIGEST_MODEUse empirical distribution.private EmpiricalDistributionempiricalDistributionEmpirical probability distribution for use with DIGEST_MODE.static intEXPONENTIAL_MODEExponential random deviates with mean = μ.private java.io.BufferedReaderfilePointerFile pointer for REPLAY_MODE.static intGAUSSIAN_MODEGaussian random deviates with mean = μ, std dev = σ.private intmodemode determines how values are generated.private doublemuMean for use with non-data-driven modes.private RandomDataGeneratorrandomDataRandomDataImpl to use for random data generation.static intREPLAY_MODEReplay data from valuesFilePath.private doublesigmaStandard deviation for use with GAUSSIAN_MODE.static intUNIFORM_MODEUniform random deviates with mean = μ.private java.net.URLvaluesFileURLURI to raw data values.
-
Constructor Summary
Constructors Constructor Description ValueServer()Creates new ValueServerValueServer(RandomDataImpl randomData)Deprecated.ValueServer(RandomGenerator generator)Construct a ValueServer instance using a RandomGenerator as its source of random data.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidcloseReplayFile()ClosesvaluesFileURLafter use in REPLAY_MODE.voidcomputeDistribution()Computes the empirical distribution using values from the file invaluesFileURL, using the default number of bins.voidcomputeDistribution(int binCount)Computes the empirical distribution using values from the file invaluesFileURLandbinCountbins.voidfill(double[] values)Fills the input array with values generated using getNext() repeatedly.double[]fill(int length)Returns an array of lengthlengthwith values generated using getNext() repeatedly.EmpiricalDistributiongetEmpiricalDistribution()Returns theEmpiricalDistributionused when operating in 0.intgetMode()Returns the data generation mode.doublegetMu()doublegetNext()Returns the next generated value, generated according to the mode value (see MODE constants).private doublegetNextDigest()Gets a random value in DIGEST_MODE.private doublegetNextExponential()Gets an exponentially distributed random value with mean = mu.private doublegetNextGaussian()Gets a Gaussian distributed random value with mean = mu and standard deviation = sigma.private doublegetNextReplay()Gets next sequential value from thevaluesFileURL.private doublegetNextUniform()Gets a uniformly distributed random value with mean = mu.doublegetSigma()Returns the standard deviation used when operating inGAUSSIAN_MODE.java.net.URLgetValuesFileURL()Returns the URL for the file used to build the empirical distribution when usingDIGEST_MODE.voidreSeed(long seed)Reseeds the random data generator.voidresetReplayFile()Resets REPLAY_MODE file pointer to the beginning of thevaluesFileURL.voidsetMode(int mode)Sets the data generation mode.voidsetMu(double mu)Sets themeanused in data generation.voidsetSigma(double sigma)Sets thestandard deviationused inGAUSSIAN_MODE.voidsetValuesFileURL(java.lang.String url)Sets thevalues file URLusing a string URL representation.voidsetValuesFileURL(java.net.URL url)Sets the thevalues file URL.
-
-
-
Field Detail
-
DIGEST_MODE
public static final int DIGEST_MODE
Use empirical distribution.- See Also:
- Constant Field Values
-
REPLAY_MODE
public static final int REPLAY_MODE
Replay data from valuesFilePath.- See Also:
- Constant Field Values
-
UNIFORM_MODE
public static final int UNIFORM_MODE
Uniform random deviates with mean = μ.- See Also:
- Constant Field Values
-
EXPONENTIAL_MODE
public static final int EXPONENTIAL_MODE
Exponential random deviates with mean = μ.- See Also:
- Constant Field Values
-
GAUSSIAN_MODE
public static final int GAUSSIAN_MODE
Gaussian random deviates with mean = μ, std dev = σ.- See Also:
- Constant Field Values
-
CONSTANT_MODE
public static final int CONSTANT_MODE
Always return mu- See Also:
- Constant Field Values
-
mode
private int mode
mode determines how values are generated.
-
valuesFileURL
private java.net.URL valuesFileURL
URI to raw data values.
-
mu
private double mu
Mean for use with non-data-driven modes.
-
sigma
private double sigma
Standard deviation for use with GAUSSIAN_MODE.
-
empiricalDistribution
private EmpiricalDistribution empiricalDistribution
Empirical probability distribution for use with DIGEST_MODE.
-
filePointer
private java.io.BufferedReader filePointer
File pointer for REPLAY_MODE.
-
randomData
private final RandomDataGenerator randomData
RandomDataImpl to use for random data generation.
-
-
Constructor Detail
-
ValueServer
public ValueServer()
Creates new ValueServer
-
ValueServer
@Deprecated public ValueServer(RandomDataImpl randomData)
Deprecated.Construct a ValueServer instance using a RandomDataImpl as its source of random data.- Parameters:
randomData- the RandomDataImpl instance used to source random data- Since:
- 3.0
-
ValueServer
public ValueServer(RandomGenerator generator)
Construct a ValueServer instance using a RandomGenerator as its source of random data.- Parameters:
generator- source of random data- Since:
- 3.1
-
-
Method Detail
-
getNext
public double getNext() throws java.io.IOException, MathIllegalStateException, MathIllegalArgumentExceptionReturns the next generated value, generated according to the mode value (see MODE constants).- Returns:
- generated value
- Throws:
java.io.IOException- in REPLAY_MODE if a file I/O error occursMathIllegalStateException- if mode is not recognizedMathIllegalArgumentException- if the underlying random generator thwrows one
-
fill
public void fill(double[] values) throws java.io.IOException, MathIllegalStateException, MathIllegalArgumentExceptionFills the input array with values generated using getNext() repeatedly.- Parameters:
values- array to be filled- Throws:
java.io.IOException- in REPLAY_MODE if a file I/O error occursMathIllegalStateException- if mode is not recognizedMathIllegalArgumentException- if the underlying random generator thwrows one
-
fill
public double[] fill(int length) throws java.io.IOException, MathIllegalStateException, MathIllegalArgumentExceptionReturns an array of lengthlengthwith values generated using getNext() repeatedly.- Parameters:
length- length of output array- Returns:
- array of generated values
- Throws:
java.io.IOException- in REPLAY_MODE if a file I/O error occursMathIllegalStateException- if mode is not recognizedMathIllegalArgumentException- if the underlying random generator thwrows one
-
computeDistribution
public void computeDistribution() throws java.io.IOException, ZeroException, NullArgumentExceptionComputes the empirical distribution using values from the file invaluesFileURL, using the default number of bins.valuesFileURLmust exist and be readable by *this at runtime.This method must be called before using
getNext()withmode = DIGEST_MODE- Throws:
java.io.IOException- if an I/O error occurs reading the input fileNullArgumentException- if thevaluesFileURLhas not been setZeroException- if URL contains no data
-
computeDistribution
public void computeDistribution(int binCount) throws NullArgumentException, java.io.IOException, ZeroExceptionComputes the empirical distribution using values from the file invaluesFileURLandbinCountbins.valuesFileURLmust exist and be readable by this process at runtime.This method must be called before using
getNext()withmode = DIGEST_MODE- Parameters:
binCount- the number of bins used in computing the empirical distribution- Throws:
NullArgumentException- if thevaluesFileURLhas not been setjava.io.IOException- if an error occurs reading the input fileZeroException- if URL contains no data
-
getMode
public int getMode()
Returns the data generation mode. Seethe class javadocfor description of the valid values of this property.- Returns:
- Value of property mode.
-
setMode
public void setMode(int mode)
Sets the data generation mode.- Parameters:
mode- New value of the data generation mode.
-
getValuesFileURL
public java.net.URL getValuesFileURL()
Returns the URL for the file used to build the empirical distribution when usingDIGEST_MODE.- Returns:
- Values file URL.
-
setValuesFileURL
public void setValuesFileURL(java.lang.String url) throws java.net.MalformedURLExceptionSets thevalues file URLusing a string URL representation.- Parameters:
url- String representation for new valuesFileURL.- Throws:
java.net.MalformedURLException- if url is not well formed
-
setValuesFileURL
public void setValuesFileURL(java.net.URL url)
Sets the thevalues file URL.The values file must be an ASCII text file containing one valid numeric entry per line.
- Parameters:
url- URL of the values file.
-
getEmpiricalDistribution
public EmpiricalDistribution getEmpiricalDistribution()
Returns theEmpiricalDistributionused when operating in 0.- Returns:
- EmpircalDistribution built by
computeDistribution()
-
resetReplayFile
public void resetReplayFile() throws java.io.IOExceptionResets REPLAY_MODE file pointer to the beginning of thevaluesFileURL.- Throws:
java.io.IOException- if an error occurs opening the filejava.lang.NullPointerException- if thevaluesFileURLhas not been set.
-
closeReplayFile
public void closeReplayFile() throws java.io.IOExceptionClosesvaluesFileURLafter use in REPLAY_MODE.- Throws:
java.io.IOException- if an error occurs closing the file
-
getMu
public double getMu()
Returns the mean used when operating inGAUSSIAN_MODE,EXPONENTIAL_MODEorUNIFORM_MODE. When operating inCONSTANT_MODE, this is the constant value always returned. CallingcomputeDistribution()sets this value to the overall mean of the values in thevalues file.- Returns:
- Mean used in data generation.
-
setMu
public void setMu(double mu)
Sets themeanused in data generation. Note that calling this method aftercomputeDistribution()has been called will have no effect on data generated inDIGEST_MODE.- Parameters:
mu- new Mean value.
-
getSigma
public double getSigma()
Returns the standard deviation used when operating inGAUSSIAN_MODE. CallingcomputeDistribution()sets this value to the overall standard deviation of the values in thevalues file. This property has no effect when the data generation mode is notGAUSSIAN_MODE.- Returns:
- Standard deviation used when operating in
GAUSSIAN_MODE.
-
setSigma
public void setSigma(double sigma)
Sets thestandard deviationused inGAUSSIAN_MODE.- Parameters:
sigma- New standard deviation.
-
reSeed
public void reSeed(long seed)
Reseeds the random data generator.- Parameters:
seed- Value with which to reseed theRandomDataImplused to generate random data.
-
getNextDigest
private double getNextDigest() throws MathIllegalStateExceptionGets a random value in DIGEST_MODE.Preconditions:
- Before this method is called,
computeDistribution()must have completed successfully; otherwise anIllegalStateExceptionwill be thrown
- Returns:
- next random value from the empirical distribution digest
- Throws:
MathIllegalStateException- if digest has not been initialized
- Before this method is called,
-
getNextReplay
private double getNextReplay() throws java.io.IOException, MathIllegalStateExceptionGets next sequential value from thevaluesFileURL.Throws an IOException if the read fails.
This method will open the
valuesFileURLif there is no replay file open.The
valuesFileURLwill be closed and reopened to wrap around from EOF to BOF if EOF is encountered. EOFException (which is a kind of IOException) may still be thrown if thevaluesFileURLis empty.- Returns:
- next value from the replay file
- Throws:
java.io.IOException- if there is a problem reading from the fileMathIllegalStateException- if URL contains no datajava.lang.NumberFormatException- if an invalid numeric string is encountered in the file
-
getNextUniform
private double getNextUniform() throws MathIllegalArgumentExceptionGets a uniformly distributed random value with mean = mu.- Returns:
- random uniform value
- Throws:
MathIllegalArgumentException- if the underlying random generator thwrows one
-
getNextExponential
private double getNextExponential() throws MathIllegalArgumentExceptionGets an exponentially distributed random value with mean = mu.- Returns:
- random exponential value
- Throws:
MathIllegalArgumentException- if the underlying random generator thwrows one
-
getNextGaussian
private double getNextGaussian() throws MathIllegalArgumentExceptionGets a Gaussian distributed random value with mean = mu and standard deviation = sigma.- Returns:
- random Gaussian value
- Throws:
MathIllegalArgumentException- if the underlying random generator thwrows one
-
-