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

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

#include <simplestate.h>

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

Public Member Functions

 SimpleState (bool parallel, GslRandomNumberGenerator *pRng)
 Constructor of this class, specifying whether a parallel version should be used to speed things up a bit, and specifying the random number generator. More...
 
- Public Member Functions inherited from State
 State (GslRandomNumberGenerator *pRng)
 Constructor of the class, to which the random number generator to be used internally must be specified. More...
 
bool evolve (double &tMax, int64_t &maxEvents, double startTime=0, bool initEvents=true)
 This advances the simulation state using the core mNRM. More...
 
double getTime () const
 This function returns the current time of the simulation. More...
 
GslRandomNumberGeneratorgetRandomNumberGenerator () const
 Returns the random number generator that was specified in the constructor. More...
 
- Public Member Functions inherited from errut::ErrorBase
 ErrorBase ()
 Creates an instance without an explicit object name. More...
 
 ErrorBase (const std::string &objName)
 Creates an instance with the object name set to objName. More...
 
std::string getObjectName () const
 Returns the stored object name. More...
 
std::string getErrorString () const
 Returns the currently stored error message. More...
 

Protected Member Functions

virtual const std::vector
< EventBase * > & 
getCurrentEvents () const
 This function should return the list of events that are currently scheduled. More...
 
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 SimpleState::getCurrentEvents, and this function should remove at least that event from the list. More...
 
- Protected Member Functions inherited from State
virtual void onAboutToFire (EventBase *pEvt)
 Called right before pEvt is fired. More...
 
virtual void onAlgorithmLoop (bool finished)
 Called at the end of each algorithm loop, with finished set to true if the loop will be exited. More...
 
- Protected Member Functions inherited from errut::ErrorBase
void setErrorString (const std::string &str) const
 Derived classes can use this member function to store an error message. More...
 

Detailed Description

A very naive implementation of the necessary functions from the State 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 SimpleState::getCurrentEvents and SimpleState::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);

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;
}
}

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 SimpleState as well. Otherwise the algorithm will not work properly anymore.

Constructor & Destructor Documentation

SimpleState::SimpleState ( bool  parallel,
GslRandomNumberGenerator pRng 
)

Constructor of this class, specifying whether a parallel version should be used to speed things up a bit, and specifying the random number generator.

Member Function Documentation

virtual const std::vector<EventBase *>& SimpleState::getCurrentEvents ( ) const
inlineprotectedvirtual

This function should return the list of events that are currently scheduled.

virtual void SimpleState::onFiredEvent ( EventBase pEvt,
int  position 
)
inlineprotectedvirtual

This function is called after firing the event pEvt, stored at position position in the list that was returned by SimpleState::getCurrentEvents, and this function should remove at least that event from the list.


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