Simpact Cyan
Population based event driven simulation using mNRM
populationalgorithmsimple.h
Go to the documentation of this file.
1 #ifndef POPULATIONALGORITHMSIMPLE_H
2 
3 #define POPULATIONALGORITHMSIMPLE_H
4 
9 #include "simplealgorithm.h"
10 #include "populationinterfaces.h"
11 #include "populationevent.h"
12 #include <assert.h>
13 
14 #ifdef STATE_SHOW_EVENTS
15 #include <iostream>
16 #endif // STATE_SHOW_EVENTS
17 
19 class PersonBase;
20 class PopulationEvent;
22 
44 {
45 public:
51 
52  bool_t init();
53  bool_t run(double &tMax, int64_t &maxEvents, double startTime = 0);
54  void onNewEvent(PopulationEvent *pEvt);
55 
56  // TODO: shield these from the user somehow? These functions should not be used
57  // directly by the user, they are used internally by the algorithm
58  void scheduleForRemoval(PopulationEvent *pEvt);
59 
60  double getTime() const { return SimpleAlgorithm::getTime(); }
61 
62  void setAboutToFireAction(PopulationAlgorithmAboutToFireInterface *pAction) { m_pOnAboutToFire = pAction; }
64 private:
65  bool_t initEventTimes() const;
66  const std::vector<EventBase *> &getCurrentEvents() const { return m_allEvents; }
67  void onFiredEvent(EventBase *pEvt, int position);
68  int64_t getNextEventID();
69  void onAboutToFire(EventBase *pEvt);
70 
71  std::vector<EventBase *> m_allEvents;
72  PopulationStateSimple &m_popState;
73  bool m_init;
74 
75 #ifdef ALGORITHM_SHOW_EVENTS
76  void showEvents(); // FOR DEBUGGING
77 #endif // ALGORITHM_SHOW_EVENTS
78  void onAlgorithmLoop(bool finished);
79 
80  std::vector<EventBase *> m_eventsToRemove;
81 
82  // For the parallel version
83  bool m_parallelRequested;
84  int64_t m_nextEventID;
85 
87 };
88 
89 inline int64_t PopulationAlgorithmSimple::getNextEventID()
90 {
91  int64_t id = m_nextEventID++;
92  return id;
93 }
94 
95 inline void PopulationAlgorithmSimple::onAboutToFire(EventBase *pEvt)
96 {
97 #ifdef STATE_SHOW_EVENTS
98  std::cerr << getTime() << "\t" << static_cast<PopulationEvent *>(pEvt)->getDescription(getTime()) << std::endl;
99 #endif // STATE_SHOW_EVENTS
100 
101  if (m_pOnAboutToFire)
102  m_pOnAboutToFire->onAboutToFire(static_cast<PopulationEvent *>(pEvt));
103 }
104 
105 #endif // POPULATIONALGORITHMSIMPLE_H
GslRandomNumberGenerator * getRandomNumberGenerator() const
Returns the random number generator that was specified in the constructor.
Definition: algorithm.h:158
double getTime() const
This function returns the current time of the simulation.
Definition: algorithm.h:155
This is the base class for events in the mNRM algorithm.
Definition: eventbase.h:56
This class allows you to generate random numbers, and uses the GNU Scientific Library for this.
Definition: gslrandomnumbergenerator.h:17
This is the base class for a person in a population-based simulation.
Definition: personbase.h:24
An interface to allow a member function PopulationAlgorithmAboutToFireInterface::onAboutToFire to be ...
Definition: populationinterfaces.h:90
virtual void onAboutToFire(PopulationEvent *pEvt)=0
If set using PopulationAlgorithmInterface::setAboutToFireAction, this function will be called right b...
An interface for a population based mNRM algorithm.
Definition: populationinterfaces.h:102
This class provides functions for a population-based simulation using the modified Next Reaction Meth...
Definition: populationalgorithmsimple.h:44
bool_t init()
Abstract function to initialize the implementation used.
Definition: populationalgorithmsimple.cpp:29
void setAboutToFireAction(PopulationAlgorithmAboutToFireInterface *pAction)
Allows you to set the action that needs to be performed before firing an event dynamically.
Definition: populationalgorithmsimple.h:62
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.
Definition: populationalgorithmsimple.cpp:57
void onNewEvent(PopulationEvent *pEvt)
When a new event has been created, it must be injected into the simulation using this function.
Definition: populationalgorithmsimple.cpp:89
double getTime() const
Must return the simulation tilme of the algorithm.
Definition: populationalgorithmsimple.h:60
PopulationAlgorithmSimple(PopulationStateSimple &state, GslRandomNumberGenerator &rng, bool parallel)
Constructor of the class, indicating if a parallel version should be used, which random number genera...
Definition: populationalgorithmsimple.cpp:16
GslRandomNumberGenerator * getRandomNumberGenerator() const
Must return the random number generator used by the algorithm.
Definition: populationalgorithmsimple.h:63
This is the base class for events in population-based simulations.
Definition: populationevent.h:64
Population state to be used when simulating with the straightforward algorithm in PopulationAlgorithm...
Definition: populationstatesimple.h:29
A very naive implementation of the necessary functions from the Algorithm class.
Definition: simplealgorithm.h:87
Type to return true/false with error description.
Definition: booltype.h:26