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

A very naive implementation of the necessary functions from the Algorithm class. More...

#include <simplealgorithm.h>

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

Public Member Functions

 SimpleAlgorithm (State &state, GslRandomNumberGenerator &rng, bool parallel)
 Constructor of this class, specifying whether a parallel version should be used to speed things up a bit, specifying the random number generator as well as the simulation state to use.
 
- 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.
 

Protected Member Functions

virtual const std::vector< EventBase * > & getCurrentEvents () const
 This function should return the list of events that are currently scheduled.
 
virtual void onFiredEvent (EventBase *pEvt, int position)
 This function is called after firing the event pEvt, stored at position position in the list that was returned by SimpleAlgorithm::getCurrentEvents, and this function should remove at least that event from the list.
 
- Protected Member Functions inherited from Algorithm
StategetState () const
 Returns the simulation state instance that was specified in the constructor.
 
virtual void onAboutToFire (EventBase *pEvt)
 Called right before pEvt is fired.
 
virtual void onAlgorithmLoop (bool finished)
 Called at the end of each algorithm loop, with finished set to true if the loop will be exited.
 

Detailed Description

A very naive implementation of the necessary functions from the Algorithm class.

In this implementation, all event times are recalculated each time an event has fired. This is slow, but a good reference implemenation to check an optimized version against. To create a simulation with this version of the algorithm, you need to provide implementations for the SimpleAlgorithm::getCurrentEvents and SimpleAlgorithm::onFiredEvent functions. The first function must return a list of all events in the system, the second one can modify the list, for example removing the fired event if no longer necessary.

The code that implements State::initEventTimes looks as follow:

const std::vector<EventBase *> &events = getCurrentEvents();
for (int i = 0 ; i < events.size() ; i++)
events[i]->generateNewInternalTimeDifference(pRndGen, this);
virtual const std::vector< EventBase * > & getCurrentEvents() const
This function should return the list of events that are currently scheduled.
Definition: simplealgorithm.h:96

The code for the State::getNextScheduledEvent implementation looks as follows:

const std::vector<EventBase *> &events = getCurrentEvents();
double curTime = getTime();
double dtMin = MAX_DOUBLE;
int eventPos = -1;
for (int i = 0 ; i < events.size() ; i++)
{
// This function calcutates the real-world interval that corresponds
// to the stored internal time interval.
double dt = events[i]->solveForRealTimeInterval(this, curTime);
if (dt < dtMin)
{
dtMin = dt;
eventPos = i;
}
}
double getTime() const
This function returns the current time of the simulation.
Definition: algorithm.h:155

Here, the key calculation is the solveForRealTimeInterval one, which solves for $ dt $ in the integral

\[ \Delta T = \int_{\rm curTime }^{{\rm curTime} + dt} h(X({\rm curTime}),s) ds \]

where $ \Delta T $ is the current (remaining) internal time interval for an event.

Finally, the State::advanceEventTimes implementation is the following:

double t1 = curTime + dtMin;
for (int i = 0 ; i < events.size() ; i++)
{
if (i != eventPos)
// This function subtracts from the internal time the
// amount that corresponds to advancing the real world
// time to t1
events[i]->subtractInternalTimeInterval(this, t1);
}

Here, the internal time $ \Delta T $ for an event is replaced by

\[ \Delta T - \int_{t^c}^{t_1} h(X(t^c),s) ds \]

where $ t_1 = {\rm curTime} + {\rm dtMin} $ and $t_c$ is the time at which the mapping between internal time interval and real world fire time was last calculated (see main page).

Note that if you still need to implement the State::onFiredEvent function yourself, you must call the corresponding implementation of SimpleAlgorithm as well. Otherwise the algorithm will not work properly anymore.


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