Simpact Cyan
Population based event driven simulation using mNRM
Public Member Functions | List of all members
PopulationAlgorithmAdvanced Class Reference

This class provides functions for a population-based simulation using the modified Next Reaction Method (mNRM). More...

#include <populationalgorithmadvanced.h>

Inheritance diagram for PopulationAlgorithmAdvanced:
Inheritance graph
[legend]
Collaboration diagram for PopulationAlgorithmAdvanced:
Collaboration graph
[legend]

Public Member Functions

 PopulationAlgorithmAdvanced (PopulationStateAdvanced &state, GslRandomNumberGenerator &rng, bool parallel)
 Constructor of the class, indicating if a parallel version should be used, which random number generator should be used and which simulation state.
 
bool_t init ()
 Abstract function to initialize the implementation used.
 
bool_t run (double &tMax, int64_t &maxEvents, double startTime=0)
 This should be called to actually start the simulation, do not call Algorithm::evolve for this. More...
 
void onNewEvent (PopulationEvent *pEvt)
 When a new event has been created, it must be injected into the simulation using this function.
 
double getTime () const
 Must return the simulation tilme of the algorithm.
 
GslRandomNumberGeneratorgetRandomNumberGenerator () const
 Must return the random number generator used by the algorithm.
 
void setAboutToFireAction (PopulationAlgorithmAboutToFireInterface *pAction)
 Allows you to set the action that needs to be performed before firing an event dynamically. More...
 
- Public Member Functions inherited from Algorithm
 Algorithm (State &state, GslRandomNumberGenerator &rng)
 Constructor of the class, to which the simulation state must be specified as well as the random number generator to be used internally.
 
bool_t evolve (double &tMax, int64_t &maxEvents, double startTime=0, bool initEvents=true)
 This advances the simulation state specified in the constructor using the core mNRM. More...
 
double getTime () const
 This function returns the current time of the simulation.
 
GslRandomNumberGeneratorgetRandomNumberGenerator () const
 Returns the random number generator that was specified in the constructor.
 

Additional Inherited Members

- Protected Member Functions inherited from Algorithm
StategetState () const
 Returns the simulation state instance that was specified in the constructor.
 
virtual void onFiredEvent (EventBase *pEvt)
 Called after pEvt is fired.
 

Detailed Description

This class provides functions for a population-based simulation using the modified Next Reaction Method (mNRM).

Being population-based, the state of the simulation mostly consists of the (living) people.

This implementation uses an algorithm by deriving from Algorithm directly and overriding the functions Algorithm::getNextScheduledEvent and Algorithm::advanceEventTimes. It is always good to compare the results of the ones from PopulationAlgorithmSimple to assure the correct working of this more optimized version.

Before calling PopulationAlgorithmInterface::run, you'll need to introduce initial events (see PopulationAlgorithmInterface::onNewEvent) and most likely also an initial set of persons in the simulation state you're using (see PopulationStateInterface::addNewPerson).

Events for this type of simulation should derive from the PopulationEvent class instead of using the EventBase base class directly.

Advanced algorithm

To know how to use this population based algorithm, it can be useful to understand the way it works. The figure below illustrates how everything is organized. In essence, a population (stored in an instance of PopulationStateAdvanced) is just a collection of people (represented by some class derived from PersonBase), and each person stores a list of events that are relevant to him.

When you construct a new PopulationEvent based instance, you need to specify the persons involved in this event, and the event gets stored in these persons' lists. As the figure shows, it is very well possible that a single event appears in the lists of different people: for example a relationship formation event would involve two persons and would therefore be present in two lists. To be able to have global events, events that in principle don't affect people that are known in advance, a 'dummy' person is introduced. This 'dummy' person, neither labelled as a 'Man' nor as a 'Woman', only has such global events in its event list. By definition, these events will not be present in any other person's list. Note that this implies that PopulationEvent::getNumberOfPersons will also return 1 for global events.

When an event fires, the algorithm assumes that the persons which have the event in their lists are affected and that their events will require a recalculation of the fire times. In case other people are affected as well (who you don't know beforehand), this can be specified using the functions PopulationEvent::isEveryoneAffected or PopulationEvent::markOtherAffectedPeople. If such additional people are specified as well, those people's event fire times will be recalculated as well. Using PopulationEvent::areGlobalEventsAffected you can indicate that the fire times of global events should be recalculated.

Before recalculating an event fire time, it is checked if the event is still relevant. If one of the persons specified in the PopulationEvent constructor has died, the event is deemed useless and will be discarded. In case it's possible that an event becomes useless because of some other criteria, the PopulationEvent::isUseless function should be reimplemented to inform the algorithm about this. But note that this is only called before recalculating an event fire time, which in turn is only done for people affected by the event.

Each person keeps track of which event in his list will fire first. To know which event in the entire simulation will fire first, the algorithm then just needs to check the first event times for all the people.

Member Function Documentation

◆ run()

bool_t PopulationAlgorithmAdvanced::run ( double &  tMax,
int64_t &  maxEvents,
double  startTime = 0 
)
virtual

This should be called to actually start the simulation, do not call Algorithm::evolve for this.

Parameters
tMaxStop the simulation if the simulation time exceeds the specified time. Upon completion of the function, this variable will contain the actual simulation time stopped.
maxEventsIf positive, the simulation will stop if this many events have been executed. Set to a negative value to disable this limit. At the end of the simulation, this variable will contain the number of events executed.
startTimeThe start time of the simulation, can be used to continue where a previous call to this function left off.

Implements PopulationAlgorithmInterface.

◆ setAboutToFireAction()

void PopulationAlgorithmAdvanced::setAboutToFireAction ( PopulationAlgorithmAboutToFireInterface pAction)
inlinevirtual

Allows you to set the action that needs to be performed before firing an event dynamically.

When implementing a new population based algorithm you must make sure that this way the action performed by Algorithm::onAboutToFire can be changed at run time.

Implements PopulationAlgorithmInterface.


The documentation for this class was generated from the following files: