Class SimuLauncher
onActivation() for a detailed description
of the simulation initialization process.
This class is intended to be extended by the user to define the simulation engine, if
the default setup should customized. The user can define the simulation environment,
model, and scheduler classes by overriding the onLaunchEnvironment(),
onLaunchModel(), and onLaunchScheduler() methods, respectively. The
user can also define the simulation agents and viewers by overriding the
onLaunchSimulatedAgents() and onLaunchViewers() methods,
respectively. The user can also define the simulation startup behavior by overriding
the onSetupSimulation() method.
Crucially, this class is also responsible for initializing the pseudo random number
generator (PRNG) that has to be used by the simulation agents for ensuring the
reproducibility of the simulation. The PRNG is initialized with a seed that can be set
by the user. The seed is a long integer that can be set by the user by overriding the
onInitializeSimulationSeedIndex() method. By default, the seed index is 0.
By default its logger level is set to Level.INFO.
-
Nested Class Summary
Nested classes/interfaces inherited from class Agent
Agent.ReturnCode -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionReturns the name of the simulation community.getEngineAgentsArgsFrom(Class<?> target) Gets the engine agents args from.Returns the engine group associated with the simulation.<E extends SimuEnvironment>
EGets the environment.<M extends SimuModel>
MgetModel()Gets the model.Returns the model group associated with the simulation.intReturns the index used to create the PRNG seed.<S extends Scheduler<?>>
SGets the scheduler.Returns the the viewers that are actually running.protected voidThis method is called when the simulation engine is activated.protected RandomGeneratorCreates the pseudo random number generator that has to be used by the simulation.protected voidonEnd()Called when the simulation ends, as the launcher is killed by the scheduler.protected voidInitializes the simulation seed index.protected <E extends SimuEnvironment>
ELaunches the simulation environment agent and logs the event.protected <M extends SimuModel>
MLaunches the simulation model agent and logs the event.protected <S extends Scheduler<?>>
SLaunches the simulation scheduler agent and logs the event.protected voidLaunches the simulation agents.protected voidLaunches the simulation viewers agents and logs their launch.voidCalled just before the simulation starts.prng()Returns the pseudo random number generator that has to be used by the simulation agents.voidsetPrngSeedIndex(int seedIndex) Sets the seed which is used to create a PRNG.voidsetRandomGnerator(RandomGenerator randomGenerator) Sets the pseudo random number generator that has to be used by the simulation.protected voidStart simulation.Methods inherited from class Watcher
addProbe, getProbes, removeAllProbes, removeProbe, toStringMethods inherited from class SimuAgent
createSimuGroup, getLauncher, getNextEventDate, getSimuTimer, launchAgent, leaveRole, leaveSimuGroup, leaveSimuRole, playRole, requestSimuRoleMethods inherited from class Agent
activate, broadcast, broadcastWithRole, broadcastWithRoleWaitForReplies, checkAgentAddress, createGroup, createGroup, createGroup, end, equals, executeThisAgent, executeThisAgent, exitOnKill, getAgentsWithRole, getAgentWithRole, getKernelAddress, getKernelConfig, getLogger, getMailbox, getName, getNetworkID, getOrganization, getReplyTo, handleRequestActionMessage, hashCode, isAlive, killAgent, killAgent, launchAgent, launchAgent, leaveGroup, leaveRole, live, nextMessage, onLive, pause, proceedEnumMessage, receiveMessage, reload, reply, replyWithRole, requestRole, requestRole, send, send, sendWaitReply, sendWaitReply, sendWithRole, sendWithRole, sendWithRoleWaitReply, sendWithRoleWaitReply, setupDefaultGUI, waitAnswer, waitAnswer, waitNextMessage, waitNextMessage
-
Constructor Details
-
SimuLauncher
protected SimuLauncher()Default constructor. It initializes the simulation community name to the class name of the simulation engine.
-
-
Method Details
-
onActivation
protected void onActivation()This method is called when the simulation engine is activated. It initializes the simulation community, creates the engine and model groups, and requests the roleSimuOrganization.LAUNCHER_ROLEin the groupSimuOrganization.ENGINE_GROUP.Then, it initiates the simulation by first creating the pseudo random number generator (prng) by calling the
onCreateRandomGenerator().Then, it launches the engine agents of the simulation in the following order:
- the model agent by calling the
onLaunchModel()method - the environment agent by calling the
onLaunchEnvironment()method - the scheduler agent by calling the
onLaunchScheduler()method - the viewers agents by calling the
onLaunchViewers()method - the simulated agents by calling the
onLaunchSimulatedAgents()method
Then, it calls the
onSetupSimulation()method.Finally, if the start switch is passed on the command line or through the arguments of the main method, it automatically starts the simulation by calling the
startSimulation(), which, by default, send the starting message to the scheduler.- Overrides:
onActivationin classWatcher
- the model agent by calling the
-
onCreateRandomGenerator
Creates the pseudo random number generator that has to be used by the simulation. The seed index is taken usingfrom the kernel configuration. If the seed index is not set, the default value is 0.invalid reference
#getSeedIndex()- Returns:
- the pseudo random number generator that will be used by the simulation
-
setPrngSeedIndex
public void setPrngSeedIndex(int seedIndex) Sets the seed which is used to create a PRNG. The actual seed that will be used will be computed by adding seedIndex to the built-in long (0xFEDCBA0987654321L), which is used as initial seed. This is done so that the obtained long respects the many seed bits characteristic. Moreover it is known that a good practice, considering how seeds should be chosen, is to take them in sequence. See this blog: Random number generator seed mistakes So a simulation suite can be obtained by using this method with a consecutive list of int: 1, 2, 3...- Parameters:
seedIndex- the seed index to set. Privilege the use of sequence of integers such as 0, 1, 2...
-
getPrngSeedIndex
public int getPrngSeedIndex()Returns the index used to create the PRNG seed.- Returns:
- the index used to create the PRNG seed
-
onInitializeSimulationSeedIndex
protected void onInitializeSimulationSeedIndex()Initializes the simulation seed index. By default, the seed index is taken from the kernel configuration, and if not set the seed index is set 0, which means that the seed used to create the PRNG will be the built-in long (0xFEDCBA0987654321L) plus 0, and thus the same for all simulations. This allows to have a reproducible simulation when the seed index is not set, and to have different simulations by using different seed indices, for example by using a sequence of integers such as 0, 1, 2... This method can be overridden by the user to define a custom seed index initialization. -
onSetupSimulation
public void onSetupSimulation()Called just before the simulation starts. By default, it calls in the following order, theSimuAgent.onSetupSimulation(),SimuAgent.onSetupSimulation(),Scheduler.onSetupSimulation(), andSimuAgent.onSetupSimulation()methods for each viewer.This method can be overridden by the user to define fine tuning of the simulation initialization.
It is worth noting that this method is the latest method called by the launcher on all the engine agents before giving to the scheduler the control of the simulation. This provided that, at this point of the launching process, the simulation is ready to start, i.e.: all the agents participating in the simulation have been launched, and already have their
Agent.onActivation()method called.By default, this method is not called on the simulated agents, which are not considered as engine agents, and thus not known by the launcher. However, the simulated agents have already been launched and have already had their
Agent.onActivation()method called, so they are ready to start the simulation as well.So, it is possible to override this method to call the
SimuAgent.onSetupSimulation()method on the simulated agents as well, if needed.Beware that calling this method programmatically or using the GUI after the simulation has started will break reproducibility of the simulation. This facility is provided for testing purposes, and should be used with caution. The only way to reproduce a simulation is to relaunch it from scratch with the same seed index.
- Overrides:
onSetupSimulationin classSimuAgent
-
onEnd
protected void onEnd()Called when the simulation ends, as the launcher is killed by the scheduler. By default, the launcher then kills the model, environment, and viewers, so that their onEnd() methods are called.This method can be overridden by the user to define fine tuning of the simulation ending process.
-
onLaunchModel
Launches the simulation model agent and logs the event. Defaultly, the model class is taken from the annotationEngineAgentsif it is defined on the class, or from the kernel configuration. If none of these sources provide a model class, the fallback mode is used, which means that the model class is set toSimuModel. This method could be overridden by the user to define a custom model class or to customize the launch process of the model agent.- Type Parameters:
M- the type of the model- Returns:
- the model agent for this simulation
-
onLaunchEnvironment
Launches the simulation environment agent and logs the event. Defaultly, the environment class is taken from the annotationEngineAgentsif it is defined on the class, or from the kernel configuration. If none of these sources provide an environment class, the fallback mode is used, which means that the environment class is set toSimuEnvironment. This method could be overridden by the user to define a custom environment class or to customize the launch process of the environment agent.- Type Parameters:
E- the type of the environment- Returns:
- the environment agent for this simulation
-
onLaunchScheduler
Launches the simulation scheduler agent and logs the event. Defaultly, the scheduler class is taken from the annotationEngineAgentsif it is defined on the class, or from the kernel configuration. If none of these sources provide a scheduler class, the fallback mode is used, which means that the scheduler class is set toTickBasedScheduler. This method could be overridden by the user to define a custom scheduler class or to customize the launch process of the scheduler agent.- Type Parameters:
S- the type of the scheduler- Returns:
- the scheduler agent for this simulation
-
onLaunchViewers
protected void onLaunchViewers()Launches the simulation viewers agents and logs their launch. Defaultly, the viewers classes are taken from the annotationEngineAgentsif it is defined on the class, or from the kernel configuration. If none of these sources provide viewers classes, the fallback mode is used, which means that no viewer is launched. This method could be overridden by the user to define custom viewers classes or to customize the launch process of the viewers agents. -
onLaunchSimulatedAgents
protected void onLaunchSimulatedAgents()Launches the simulation agents. Defaultly, no simulated agent is launched. This method could be overridden by the user to define custom simulated agents or to customize the launch process of the simulated agents. -
startSimulation
protected void startSimulation()Start simulation. -
getScheduler
Gets the scheduler.- Overrides:
getSchedulerin classSimuAgent- Type Parameters:
S- the generic type- Returns:
- the scheduler
-
getEnvironment
Gets the environment.- Overrides:
getEnvironmentin classSimuAgent- Type Parameters:
E- the element type- Returns:
- the environment
-
getModel
-
getEngineAgentsArgsFrom
public static final List<String> getEngineAgentsArgsFrom(Class<?> target) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, SecurityException Gets the engine agents args from.- Parameters:
target- the target- Returns:
- the engine agents args from
- Throws:
IllegalAccessException- the illegal access exceptionInvocationTargetException- the invocation target exceptionNoSuchMethodException- the no such method exceptionSecurityException- the security exception
-
getModelGroup
Returns the model group associated with the simulation.- Overrides:
getModelGroupin classSimuAgent- Returns:
- the model group
-
getCommunity
Returns the name of the simulation community.- Overrides:
getCommunityin classSimuAgent- Returns:
- the community
-
getEngineGroup
Returns the engine group associated with the simulation.- Overrides:
getEngineGroupin classSimuAgent- Returns:
- the name of the engine group
-
getViewers
Returns the the viewers that are actually running.- Overrides:
getViewersin classSimuAgent- Returns:
- the running viewers
-
prng
Returns the pseudo random number generator that has to be used by the simulation agents. -
setRandomGnerator
Sets the pseudo random number generator that has to be used by the simulation.- Parameters:
randomGenerator- the randomGenerator to set
-