Package madkit.kernel
Class Scheduler
java.lang.Object
madkit.kernel.AbstractAgent
madkit.kernel.Agent
madkit.kernel.Scheduler
- All Implemented Interfaces:
Comparable<AbstractAgent>
- Direct Known Subclasses:
DateBasedDiscreteEventScheduler
Scheduler is the core agent for defining multi-agent based simulations. This class defines a generic threaded agent which is in charge of activating the simulated agents usingActivator
.Activator
are tool objects which are able to trigger any available method belonging to the agents that have a role within a group. The purpose of this approach is to allow the manipulation of agents regardless of their concrete Java classes. So a scheduler holds a collection of activators that target specific groups and roles and thus allow to define very complex scheduling policies if required. A default behavior is implemented and corresponds to the triggering of the activators according to the order in which they have been added to the scheduler engine usingaddActivator(Activator)
. The default state of a scheduler isScheduler.SimulationState.PAUSED
. The default delay between two simulation steps is 0 milliseconds (max speed). Default GUI components are defined for this agent and they could be easily integrated in any GUI. As of MaDKit 5.3, two different temporal schemes could used:
BigDecimal
which is
incremented at will. BigDecimal
is used to avoid rounding errors that
may happen when working with double values. This is the preferred choice if
the simulation is based on simple loop following a discrete time approach.
LocalDateTime
. This is
far more convenient when the model refers to a real-world case for which
representing usual temporal units such as hours or weeks is required (the
default used unit is ChronoUnit.SECONDS
). This mode also allow to
handle agents that evolve considering different time scales or during
specific period of the day.
Those two modes are exclusive and can be selected using the corresponding
constructor of the scheduler- Since:
- MaDKit 2.0
- Version:
- 5.3
- Author:
- Fabien Michel
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enum
A simulation state.static interface
Nested classes/interfaces inherited from class madkit.kernel.AbstractAgent
AbstractAgent.ReturnCode, AbstractAgent.State
-
Constructor Summary
ConstructorsConstructorDescriptionConstructs aScheduler
using a tick-basedScheduler.SimulationTime
.Scheduler
(double endTick) Constructs aScheduler
using a tick-based that will end the simulation for the specified tick.Scheduler
(LocalDateTime initialDate) Constructs aScheduler
using a date-based time which relies onLocalDateTime
-
Method Summary
Modifier and TypeMethodDescriptionvoid
addActivator
(Activator<? extends AbstractAgent> activator) Adds an activator to the simulation engine.protected void
Changes my state according to aSchedulingMessage
and sends a reply to the sender as acknowledgment.void
Defines a default simulation loop which is automatically during the scheduler's life.protected void
end()
This method corresponds to the last behavior which is called by the MaDKit kernel.void
executeAndLog
(Activator<? extends AbstractAgent> activator, Object... args) Triggers the execute method of thisactivator
and logs it using theLevel.FINER
logging levelint
getDelay()
Returns the delay between two simulation stepsdouble
getGVT()
Deprecated.Returns a label giving the simulation timeReturns a menu which could be used in any GUI.Returns a label giving some information on the simulation processReturns a toolbar which could be used in any GUI.double
Deprecated.as of MDK 5.3, replaced byAbstractAgent.getSimulationTime()
available methodsThe state of the simulation.protected void
live()
Scheduler's default behavior. default code is:void
Logs the current simulation step value.protected void
paused()
RunscheckMail(Message)
every 1000 ms.void
removeActivator
(Activator<? extends AbstractAgent> activator) Removes an activator from the simulation engine.void
Remove all the activators which have been previously addedvoid
setActivatorPriority
(Activator<? extends AbstractAgent> activator, int priority) Sets the priority of anActivator
.void
setDelay
(int delay) Sets the delay between two simulation steps.void
setGVT
(double value) Deprecated.as of MaDKit 5.3, replaced byAbstractAgent.getSimulationTime()
void
setSimulationDuration
(double endTime) Deprecated.as of MDK 5.3, replaced byAbstractAgent.getSimulationTime()
available methodsprotected void
setSimulationState
(Scheduler.SimulationState newState) Changes the state of the schedulervoid
setupFrame
(AgentFrame frame) Setup the default Scheduler GUI when launched with the default MaDKit GUI mechanism.Methods inherited from class madkit.kernel.Agent
broadcastMessageWithRoleAndWaitForReplies, getThreadPriority, isDaemon, killAgent, pause, sendMessageAndWaitForReply, sendMessageAndWaitForReply, sendMessageAndWaitForReply, sendMessageAndWaitForReply, sendMessageWithRoleAndWaitForReply, sendMessageWithRoleAndWaitForReply, sendMessageWithRoleAndWaitForReply, sendMessageWithRoleAndWaitForReply, sendReplyAndWaitForReply, sendReplyAndWaitForReply, sendReplyWithRoleAndWaitForReply, sendReplyWithRoleAndWaitForReply, setThreadPriority, waitAnswer, waitAnswer, waitNextMessage, waitNextMessage, waitNextMessage, waitNextMessage
Methods inherited from class madkit.kernel.AbstractAgent
activate, broadcastMessage, broadcastMessageWithRole, bucketModeCreateGroup, bucketModeRequestRole, checkAgentAddress, compareTo, createGroup, createGroup, createGroup, createGroupIfAbsent, createGroupIfAbsent, createGroupIfAbsent, createGUIOnStartUp, destroyCommunity, destroyGroup, destroyRole, executeThisAgent, executeThisAgent, executeThisAgent, getAgentAddressIn, getAgentsWithRole, getAgentsWithRole, getAgentWithRole, getDistantAgentWithRole, getExistingCommunities, getExistingGroups, getExistingRoles, getKernelAddress, getLastReceivedMessage, getLastReceivedMessage, getLogger, getMadkitConfig, getMadkitProperty, getMadkitProperty, getMyGroups, getMyRoles, getName, getNetworkID, getNextEventDate, getOrganizationSnapShot, getReplyTo, getServerInfo, getSimpleNetworkID, getSimulationTime, getState, hasGUI, hashCode, hasRole, isAlive, isCommunity, isGroup, isKernelOnline, isMadkitPropertyTrue, isMessageBoxEmpty, isRole, killAgent, launchAgent, launchAgent, launchAgent, launchAgent, launchAgent, launchAgent, launchAgent, launchAgent, launchAgentBucket, launchAgentBucket, launchAgentBucket, launchAgentBucket, launchNode, launchXmlAgents, leaveGroup, leaveRole, nextMessage, nextMessage, nextMessages, proceedEnumMessage, purgeMailbox, receiveMessage, reload, requestRole, requestRole, sendMessage, sendMessage, sendMessageWithRole, sendMessageWithRole, sendReply, sendReplyWithRole, setLogLevel, setMadkitProperty, setMadkitProperty, setName, toString
-
Constructor Details
-
Scheduler
public Scheduler()Constructs aScheduler
using a tick-basedScheduler.SimulationTime
. -
Scheduler
public Scheduler(double endTick) Constructs aScheduler
using a tick-based that will end the simulation for the specified tick.- Parameters:
endTick
- the tick at which the simulation will automatically stop
-
Scheduler
Constructs aScheduler
using a date-based time which relies onLocalDateTime
- Parameters:
initialDate
- the date at which the simulation should begin e.g.LocalDateTime.of(1, 1, 1, 0, 0)
the initial date of the simulation
-
-
Method Details
-
getDelay
public int getDelay()Returns the delay between two simulation steps- Returns:
- the delay between two simulation steps.
-
setDelay
public void setDelay(int delay) Sets the delay between two simulation steps. That is the real time pause between two calls ofdoSimulationStep()
. Using the Scheduler's GUI, the value can be adjusted from 0 to 400.- Parameters:
delay
- the real time pause between two steps in milliseconds, an integer between 0 and 400: O is max speed.
-
getGVT
public double getGVT()Deprecated.as of MaDKit 5.3, replaced byAbstractAgent.getSimulationTime()
Returns the simulation current tick.- Returns:
- the gVT
-
setGVT
public void setGVT(double value) Deprecated.as of MaDKit 5.3, replaced byAbstractAgent.getSimulationTime()
Sets the simulation global virtual time.- Parameters:
value
- the current simulation time
-
setupFrame
Setup the default Scheduler GUI when launched with the default MaDKit GUI mechanism.- Overrides:
setupFrame
in classAbstractAgent
- Parameters:
frame
- the default frame which has been created by MaDKit for this agent.- See Also:
-
addActivator
Adds an activator to the simulation engine. This has to be done to make an activator work properly.- Parameters:
activator
- an activator.- Since:
- MaDKit 5.0.0.8
-
setActivatorPriority
Sets the priority of anActivator
.- Parameters:
activator
-priority
-
-
removeActivator
Removes an activator from the simulation engine.- Parameters:
activator
- an activator.
-
doSimulationStep
public void doSimulationStep()Defines a default simulation loop which is automatically during the scheduler's life. This method should be overridden to define a customized scheduling policy. By default, it executes all the activators in the order they have been added, usingActivator.execute(Object...)
, and then increments the simulation time by one unit. Default implementation is:logActivationStep(); for (final Activator<? extends AbstractAgent> activator : activators) { executeAndLog(activator); } getSimulationTime().addOneTimeUnit();
By default logs are displayed only ifAbstractAgent.getLogger()
is set aboveLevel.FINER
. -
logActivationStep
public void logActivationStep()Logs the current simulation step value. logs are displayed only ifAbstractAgent.getLogger()
is set aboveLevel.FINER
. -
executeAndLog
Triggers the execute method of thisactivator
and logs it using theLevel.FINER
logging level- Parameters:
activator
- the activator to triggerargs
- the args that will be passed to the targeted method
-
end
protected void end()Description copied from class:AbstractAgent
This method corresponds to the last behavior which is called by the MaDKit kernel. This call occurs when a threaded agent normally exits its live method or when the agent is killed. Usually a good place to release taken resources or log what has to be logged. It has to be noted that the kernel automatically takes care of removing the agent from the organizations it is in. However, this cleaning is not logged by the agent. Therefore it could be of interest for the agent to do that itself.Here is a typical example:
@Override protected void end() { AbstractAgent.ReturnCode returnCode = leaveRole("a community", "a group", "my role"); if (returnCode == AbstractAgent.ReturnCode.SUCCESS){ if(logger != null) logger.info("I am leaving the artificial society"); } else{ if(logger != null) logger.warning("something wrong when ending, return code is "+returnCode); } if(logger != null) logger.info("I am done"); } }
- Overrides:
end
in classAbstractAgent
-
getSimulationState
The state of the simulation.- Returns:
- the state in which the simulation is.
- See Also:
-
setSimulationState
Changes the state of the scheduler- Parameters:
newState
- the new state
-
live
protected void live()Scheduler's default behavior. default code is:while (isAlive()) { if (GVT > getSimulationDuration()) { if (logger != null) logger.info("Simulation has reached end time -> " + getSimulationDuration()); return; } pause(getDelay()); checkMail(nextMessage()); switch (getSimulationState()) { case RUNNING: doSimulationStep(); break; case PAUSED: paused(); break; case STEP: simulationState = PAUSED; doSimulationStep(); break; case SHUTDOWN: return; // shutdown default: getLogger().severe("state not handled " + getSimulationState); } }
-
checkMail
Changes my state according to aSchedulingMessage
and sends a reply to the sender as acknowledgment.- Parameters:
m
- the received message
-
paused
protected void paused()RunscheckMail(Message)
every 1000 ms. -
removeAllActivators
public void removeAllActivators()Remove all the activators which have been previously added -
setSimulationDuration
Deprecated.as of MDK 5.3, replaced byAbstractAgent.getSimulationTime()
available methodsSets the simulation time for which the scheduler should end the simulation.- Parameters:
endTime
- the end time to set
-
getSimulationDuration
public double getSimulationDuration()Deprecated.as of MDK 5.3, replaced byAbstractAgent.getSimulationTime()
available methods- Returns:
- the simulationDuration
-
getSchedulerToolBar
Returns a toolbar which could be used in any GUI.- Returns:
- a toolBar controlling the scheduler's actions
-
getSchedulerMenu
Returns a menu which could be used in any GUI.- Returns:
- a menu controlling the scheduler's actions
-
getSchedulerStatusLabel
Returns a label giving some information on the simulation process- Returns:
- a label giving some information on the simulation process
-
getGVTLabel
Returns a label giving the simulation time- Returns:
- a label giving the simulation time
-
AbstractAgent.getSimulationTime()