Class RandomVariable
- java.lang.Object
-
- jsl.simulation.ModelElement
-
- jsl.modeling.elements.variable.RandomVariable
-
- All Implemented Interfaces:
RandomElementIfc
,ObservableIfc
,StreamOptionIfc
,GetNameIfc
,GetValueIfc
,IdentityIfc
,RandomIfc
,GetRandomNumberStreamIfc
,RNStreamControlIfc
,SetRandomNumberStreamIfc
,SampleIfc
- Direct Known Subclasses:
NHPPTimeBtwEventRV
public class RandomVariable extends ModelElement implements RandomIfc, RandomElementIfc
A random variable (RandomVariable) is a function that maps a probability space to a real number. A random variable uses a RandomIfc to provide the underlying mapping to a real number via the getValue() method.To construct a RandomVariable the user must provide an instance of a class that implements the RandomIfc interface as the initial random source. This source is used to initialize the source of randomness for each replication.
WARNING: For efficiency, this class uses a direct reference to the supplied initial random source. It simply wraps the supplied object reference to a random source so that it can be utilized within the JSL model. Because of the direct reference to the random source, a change to the state of the random source will be reflected in the use of that instance within this class. Thus, mutating the state of the random source will also see those mutations reflected in the usage of this class. This may or not be what is expected by the client. For example, mutating the state of the initial random source during a replication may cause each replication to start with different initial conditions.
Using the setRandomSource() method allows the user to change the source of randomness during a replication. The source of randomness during a replication is set to the reference of the initial random source at the beginning of each replication. This ensures that each replication uses the same random source during the replication. However, the user may call the setRandomSource() method to immediately change the source of randomness during the replication. This change is in effect only during the current replication. At the beginning of each replication, the source of randomness is set to the reference to the initial random source. This ensures that each replication uses the same random source. For this reason, the use of setInitialRandomSource() should be limited to before or after running a simulation experiment.
The initial source is used to set up the source used during the replication. If the client changes the reference to the initial source, this change does not become effective until the beginning of the next replication. In other words, the random source used during the replication is unaffected. However, the client might mutate the initial random source during a replication. If this occurs, those changes are immediately reflected within the current replication and all subsequent replications. Again, mutating the initial random source during a replication is generally a bad idea unless you really know what you are doing.
Changing the initial random source between experiments is very common. For example, to set up an experiment that has different random characteristics the client can and should change the source of randomness (either by mutating the random source or by supplying a reference to a different random source.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class jsl.simulation.ModelElement
ModelElement.EventBuilderIfc<T>, ModelElement.EventScheduler<T>, ModelElement.ModelElementComparator, ModelElement.RequestBuilder, ModelElement.RequestDurationIfc, ModelElement.RequestTimeIfc, ModelElement.RequestUsingIfc, ModelElement.TimedUpdateEventAction, ModelElement.TimeUnitIfc<T>, ModelElement.WarmUpEventAction
-
-
Field Summary
Fields Modifier and Type Field Description protected boolean
myAdvanceToNextSubStreamOption
indicates whether or not the random variable's distribution has it stream reset to the next sub-stream stream, or not, prior to each replication.protected boolean
myCaptureResponseFlag
A flag to indicate whether or not a ResponseVariable should be created and used to capture the randomly generated values.protected RandomIfc
myInitialRandomSource
RandomIfc provides a reference to the underlying source of randomness to initialize each replication.protected boolean
myInitialRandomSourceChangeWarning
Controls whether warning of changing the initial random source during a replication is logged, default is true.protected RandomIfc
myRandomSource
RandomIfc provides a reference to the underlying source of randomness during the replicationprotected boolean
myResetStartStreamOption
indicates whether or not the random variable's distribution has it stream reset to the default stream, or not prior to each experiment.protected ResponseVariable
myResponse
Used to capture the randomly generated values if the capture response flag is true-
Fields inherited from class jsl.simulation.ModelElement
AFTER_EXPERIMENT, AFTER_REPLICATION, BEFORE_EXPERIMENT, BEFORE_REPLICATION, CONDITIONAL_ACTION_REGISTRATION, DEFAULT_TIMED_EVENT_PRIORITY, INITIALIZED, MONTE_CARLO, myAfterExperimentOption, myAfterReplicationOption, myBeforeExperimentOption, myBeforeReplicationOption, myInitializationOption, myLengthOfWarmUp, myModelElements, myMonteCarloOption, myObservableComponent, myReplicationEndedOption, myTimedUpdateActionListener, myTimedUpdateEvent, myTimedUpdateInterval, myTimedUpdateOption, myTimedUpdatePriority, myWarmUpActionListener, myWarmUpEvent, myWarmUpIndicator, myWarmUpOption, myWarmUpPriority, NONE, REMOVED_FROM_MODEL, REPLICATION_ENDED, TIME_UNIT_DAY, TIME_UNIT_HOUR, TIME_UNIT_MILLISECOND, TIME_UNIT_MINUTE, TIME_UNIT_SECOND, TIME_UNIT_WEEK, TIMED_UPDATE, UPDATE, WARMUP
-
-
Constructor Summary
Constructors Constructor Description RandomVariable(ModelElement parent, RandomIfc initialSource)
Constructs a RandomVariable given the supplied reference to the underlying source of randomness Throws a NullPointerException if the supplied randomness is nullRandomVariable(ModelElement parent, RandomIfc initialSource, java.lang.String name)
Constructs a RandomVariable given the supplied reference to the underlying source of randomness Throws a NullPointerException if the supplied randomness is null
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
advanceToNextSubstream()
Positions the RNG at the beginning of its next substreamprotected void
afterReplication()
after each replication reset the underlying random number generator to the next sub-streamjava.lang.String
asString()
Allows sub-classes to provide more detail than toString() to represent the ModelElement as a Stringprotected void
beforeExperiment()
before any replications reset the underlying random number generator to the starting streamboolean
getAntitheticOption()
java.util.Optional<ResponseVariable>
getCapturedResponse()
If the capturing of the random variable generated values is turned on (turnOnResponseCapture()), then this allows the response to be retrieved.boolean
getCaptureResponseFlag()
RandomIfc
getInitialRandomSource()
Gets the underlying RVariableIfc for the RandomVariable.boolean
getInitialRandomSourceChangeWarningOption()
RNStreamIfc
getRandomNumberStream()
RandomIfc
getRandomSource()
Gets the underlying RandomIfc for the RandomVariable currently being used during the replicationboolean
getResetNextSubStreamOption()
Gets the current reset next sub-stream option true means, that it is set to jump to the next sub-stream after each replicationboolean
getResetStartStreamOption()
Gets the current Reset Start Stream Optiondouble
getSumOfValues(int n)
Returns the sum of n random draws of the random variable if n <= 0, then the sum is 0.0double
getValue()
Each call to getValue() returns a new observationprotected void
removedFromModel()
This method should be overridden by subclasses that need actions performed when a model element is removed from a modelvoid
resetStartStream()
The resetStartStream method will position the RNG at the beginning of its stream.void
resetStartSubstream()
Resets the position of the RNG at the start of the current substreamdouble
sample()
void
setAntitheticOption(boolean flag)
Tells the stream to start producing antithetic variatesvoid
setInitialRandomSource(RandomIfc source)
Sets the underlying RandomIfc source for the RandomVariable.void
setInitialRandomSourceChangeWarningOption(boolean flag)
Controls whether or not the change of the initial random source will be logged if made during the replicationvoid
setRandomNumberStream(RNStreamIfc stream)
Changes the stream for the *initial random source*.void
setRandomSource(RandomIfc source)
Sets the underlying RandomIfc source for the RandomVariable.void
setResetNextSubStreamOption(boolean b)
Sets the current reset next sub-stream option true means, that it is set to jump to the next sub-stream after each replicationvoid
setResetStartStreamOption(boolean b)
Sets the reset start stream option, true means that it will be reset to the starting streamvoid
turnOnResponseCapture()
Turns on the capturing of the generated random values with a ResponseVariable-
Methods inherited from class jsl.simulation.ModelElement
addObserver, addObserverToModelElements, afterExperiment, afterExperiment_, afterReplication_, beforeExperiment_, beforeReplication, beforeReplication_, buildEventGenerator, cancelTimedUpdateEvent, cancelWarmUpEvent, changeParentModelElement, checkForAfterExperiment, checkForAfterReplication, checkForBeforeExperiment, checkForBeforeReplication, checkForConditionalActionRegistration, checkForInitialize, checkForMonteCarlo, checkForRemoveFromModel, checkForReplicationEnded, checkForTimedUpdate, checkForUpdate, checkForWarmUp, checkObserverState, checkWarmUpOption, clearModelElementObservers, contains, countObservers, createEntity, createEntity, createQObject, day, deleteObserver, deleteObserverFromModelElements, deleteObservers, findModelElementWithWarmUpEvent, getAfterExperimentOption, getAfterReplicationOption, getAllCounters, getAllModelElements, getAllRandomElements, getAllResponseVariables, getAllVariables, getBeforeExperimentOption, getBeforeReplicationOption, getChildModelElementIterator, getCurrentReplicationNumber, getDefaultEntityType, getExecutive, getExperiment, getId, getInitializationOption, getInitializationOrderAsString, getLeftPreOrderTraversalCount, getLengthOfWarmUp, getModel, getModelElementCompartor, getModelElementsAsString, getModelElementsAsString, getModelElementsAsString, getMonteCarloOption, getName, getNumberOfModelElements, getObserverState, getParentModelElement, getPreviousObserverState, getReplicationEndedOption, getRightPreOrderTraversalCount, getSimulation, getSpatialModel, getStringLabel, getThisElementsCounters, getThisElementsModelElements, getThisElementsRandomVariables, getThisElementsResponseVariables, getThisElementsVariables, getTime, getTimedUpdateInterval, getTimedUpdateOption, getUpdateNotificationFlag, getVariable, getWarmUpEventTime, getWarmUpOption, hour, indent, initialize, initialize_, isTimedUpdateEventScheduled, isWarmedUp, isWarmUpEventScheduled, isWarmUpScheduled, millisecond, minute, montecarlo, montecarlo_, notifyAfterExperimentObservers, notifyAfterReplicationObservers, notifyBeforeExperimentObservers, notifyBeforeReplicationObservers, notifyConditionalActionRegistrationObservers, notifyInitializationObservers, notifyMonteCarloObservers, notifyObservers, notifyObservers, notifyRemovingFromModelObservers, notifyReplicationEndedObservers, notifyTimedUpdateObservers, notifyUpdateObservers, notifyWarmUpObservers, registerConditionalActions, registerConditionalActions_, removeFromModel, replicationEnded, replicationEnded_, schedule, second, seize, setAfterExperimentOption, setAfterExperimentOptionForModelElements, setAfterReplicationOption, setAfterReplicationOptionForModelElements, setBeforeExperimentOption, setBeforeExperimentOptionForModelElements, setBeforeReplicationOption, setBeforeReplicationOptionForModelElements, setInitializationOption, setInitializationOptionForModelElements, setLengthOfWarmUp, setModel, setMonteCarloOption, setMonteCarloOptionForModelElements, setName, setObserverState, setParentModelElement, setReplicationEndedOption, setReplicationEndedOptionForModelElements, setSpatialModel, setStringLabel, setTimedUpdateInterval, setTimedUpdateOption, setTimedUpdateOptionForModelElements, setUpdateNotificationFlag, setWarmUpOption, setWarmUpOptionForModelElements, stopExecutive, stopExecutive, timedUpdate, timedUpdate_, toString, update, useControls, warmUp, warmUp_, week
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface jsl.utilities.random.rng.GetRandomNumberStreamIfc
getStreamNumber
-
Methods inherited from interface jsl.utilities.random.rng.SetRandomNumberStreamIfc
setRandomNumberStream
-
-
-
-
Field Detail
-
myResetStartStreamOption
protected boolean myResetStartStreamOption
indicates whether or not the random variable's distribution has it stream reset to the default stream, or not prior to each experiment. Resetting allows each experiment to use the same underlying random numbers i.e. common random numbers, this is the defaultSetting it to true indicates that it does reset
-
myAdvanceToNextSubStreamOption
protected boolean myAdvanceToNextSubStreamOption
indicates whether or not the random variable's distribution has it stream reset to the next sub-stream stream, or not, prior to each replication. Resetting allows each replication to better ensure that each replication will be start at the same place in the sub-streams, thereby, improving synchronization when using common random numbers.Setting it to true indicates that it does jump to the next sub-stream, true is the default
-
myRandomSource
protected RandomIfc myRandomSource
RandomIfc provides a reference to the underlying source of randomness during the replication
-
myInitialRandomSource
protected RandomIfc myInitialRandomSource
RandomIfc provides a reference to the underlying source of randomness to initialize each replication.
-
myCaptureResponseFlag
protected boolean myCaptureResponseFlag
A flag to indicate whether or not a ResponseVariable should be created and used to capture the randomly generated values. Useful for control variate implementation
-
myResponse
protected ResponseVariable myResponse
Used to capture the randomly generated values if the capture response flag is true
-
myInitialRandomSourceChangeWarning
protected boolean myInitialRandomSourceChangeWarning
Controls whether warning of changing the initial random source during a replication is logged, default is true.
-
-
Constructor Detail
-
RandomVariable
public RandomVariable(ModelElement parent, RandomIfc initialSource)
Constructs a RandomVariable given the supplied reference to the underlying source of randomness Throws a NullPointerException if the supplied randomness is null- Parameters:
parent
- The parent ModelElementinitialSource
- The reference to the underlying source of randomness
-
RandomVariable
public RandomVariable(ModelElement parent, RandomIfc initialSource, java.lang.String name)
Constructs a RandomVariable given the supplied reference to the underlying source of randomness Throws a NullPointerException if the supplied randomness is null- Parameters:
parent
- The parent ModelElementinitialSource
- The reference to the underlying source of randomnessname
- A string to label the RandomVariable
-
-
Method Detail
-
getInitialRandomSourceChangeWarningOption
public final boolean getInitialRandomSourceChangeWarningOption()
- Returns:
- true means the option will be logged
-
setInitialRandomSourceChangeWarningOption
public final void setInitialRandomSourceChangeWarningOption(boolean flag)
Controls whether or not the change of the initial random source will be logged if made during the replication- Parameters:
flag
- true means warning will be logged
-
setRandomNumberStream
public void setRandomNumberStream(RNStreamIfc stream)
Changes the stream for the *initial random source*.- Specified by:
setRandomNumberStream
in interfaceSetRandomNumberStreamIfc
- Parameters:
stream
- the reference to the random number stream, must not be null
-
getRandomNumberStream
public RNStreamIfc getRandomNumberStream()
- Specified by:
getRandomNumberStream
in interfaceGetRandomNumberStreamIfc
- Returns:
- the random number stream associated with the initial random source
-
getResetStartStreamOption
public final boolean getResetStartStreamOption()
Description copied from interface:StreamOptionIfc
Gets the current Reset Start Stream Option- Specified by:
getResetStartStreamOption
in interfaceStreamOptionIfc
- Returns:
- the option
-
setResetStartStreamOption
public final void setResetStartStreamOption(boolean b)
Description copied from interface:StreamOptionIfc
Sets the reset start stream option, true means that it will be reset to the starting stream- Specified by:
setResetStartStreamOption
in interfaceStreamOptionIfc
- Parameters:
b
- true means reset
-
getResetNextSubStreamOption
public final boolean getResetNextSubStreamOption()
Description copied from interface:StreamOptionIfc
Gets the current reset next sub-stream option true means, that it is set to jump to the next sub-stream after each replication- Specified by:
getResetNextSubStreamOption
in interfaceStreamOptionIfc
- Returns:
- the option
-
setResetNextSubStreamOption
public final void setResetNextSubStreamOption(boolean b)
Description copied from interface:StreamOptionIfc
Sets the current reset next sub-stream option true means, that it is set to jump to the next sub-stream after each replication- Specified by:
setResetNextSubStreamOption
in interfaceStreamOptionIfc
- Parameters:
b
- true means reset
-
setAntitheticOption
public final void setAntitheticOption(boolean flag)
Description copied from interface:RNStreamControlIfc
Tells the stream to start producing antithetic variates- Specified by:
setAntitheticOption
in interfaceRNStreamControlIfc
- Parameters:
flag
- true means that it produces antithetic variates.
-
getAntitheticOption
public boolean getAntitheticOption()
- Specified by:
getAntitheticOption
in interfaceRNStreamControlIfc
- Returns:
- true means on
-
advanceToNextSubstream
public final void advanceToNextSubstream()
Description copied from interface:RNStreamControlIfc
Positions the RNG at the beginning of its next substream- Specified by:
advanceToNextSubstream
in interfaceRNStreamControlIfc
-
resetStartStream
public final void resetStartStream()
Description copied from interface:RNStreamControlIfc
The resetStartStream method will position the RNG at the beginning of its stream. This is the same location in the stream as assigned when the RNG was created and initialized.- Specified by:
resetStartStream
in interfaceRNStreamControlIfc
-
resetStartSubstream
public final void resetStartSubstream()
Description copied from interface:RNStreamControlIfc
Resets the position of the RNG at the start of the current substream- Specified by:
resetStartSubstream
in interfaceRNStreamControlIfc
-
getInitialRandomSource
public final RandomIfc getInitialRandomSource()
Gets the underlying RVariableIfc for the RandomVariable. This is the source to which each replication will be initialized- Returns:
- a RVariableIfc
-
setInitialRandomSource
public final void setInitialRandomSource(RandomIfc source)
Sets the underlying RandomIfc source for the RandomVariable. This is the source to which each replication will be initialized. This is only used when the replication is initialized. Changing the reference has no effect during a replication, since the random variable will continue to use the reference returned by getRandomSource(). Please also see the discussion in the class documentation.WARNING: If this is used during an experiment to change the characteristics of the random source, then each replication may not necessarily start in the same initial state. It is recommended that this be used only prior to executing experiments.
- Parameters:
source
- the reference to the random source, must not be null
-
turnOnResponseCapture
public final void turnOnResponseCapture()
Turns on the capturing of the generated random values with a ResponseVariable
-
getCapturedResponse
public final java.util.Optional<ResponseVariable> getCapturedResponse()
If the capturing of the random variable generated values is turned on (turnOnResponseCapture()), then this allows the response to be retrieved.- Returns:
- an optional holding the ResponseVariable
-
getCaptureResponseFlag
public final boolean getCaptureResponseFlag()
- Returns:
- true means that the values are being captured via a ResponseVariable
-
getRandomSource
public final RandomIfc getRandomSource()
Gets the underlying RandomIfc for the RandomVariable currently being used during the replication- Returns:
- a RandomIfc
-
setRandomSource
public final void setRandomSource(RandomIfc source)
Sets the underlying RandomIfc source for the RandomVariable. This changes the source for the current replication only. The random variable will start to use this source immediately; however if a replication is started after this method is called, the random source will be reassigned to the initial random source before the next replication is executed.To set the random source for the entire experiment (all replications) use the setInitialRandomSource() method
- Parameters:
source
- the reference to the random source, must not be null
-
getValue
public double getValue()
Each call to getValue() returns a new observation- Specified by:
getValue
in interfaceGetValueIfc
- Returns:
- The value.
-
getSumOfValues
public double getSumOfValues(int n)
Returns the sum of n random draws of the random variable if n <= 0, then the sum is 0.0- Parameters:
n
- the number to sum- Returns:
- the sum
-
sample
public final double sample()
-
asString
public java.lang.String asString()
Description copied from class:ModelElement
Allows sub-classes to provide more detail than toString() to represent the ModelElement as a String- Overrides:
asString
in classModelElement
- Returns:
- a detailed String representation
-
removedFromModel
protected void removedFromModel()
Description copied from class:ModelElement
This method should be overridden by subclasses that need actions performed when a model element is removed from a model- Overrides:
removedFromModel
in classModelElement
-
beforeExperiment
protected void beforeExperiment()
before any replications reset the underlying random number generator to the starting stream- Overrides:
beforeExperiment
in classModelElement
-
afterReplication
protected void afterReplication()
after each replication reset the underlying random number generator to the next sub-stream- Overrides:
afterReplication
in classModelElement
-
-