Simpact Cyan
Population based event driven simulation using mNRM
populationevent.h
Go to the documentation of this file.
1 #ifndef POPULATIONEVENT_H
2 
3 #define POPULATIONEVENT_H
4 
9 #include "eventbase.h"
10 #include <assert.h>
11 #include <stdlib.h>
12 #include <string>
13 #include <iostream>
14 
15 #define POPULATIONEVENT_MAXPERSONS 2
16 
17 class PersonBase;
19 
61 class PopulationEvent : public EventBase
62 {
63 public:
71 
73  PopulationEvent(PersonBase *pPerson);
74 
76  PopulationEvent(PersonBase *pPerson1, PersonBase *pPerson2);
77  ~PopulationEvent();
78 
79  // These are for internal use
80  void setGlobalEventPerson(PersonBase *pDummyPerson);
81  void setEventID(int64_t id) { assert(m_eventID < 0); assert(id >= 0); m_eventID = id; }
82  int64_t getEventID() const { return m_eventID; }
83 
88  int getNumberOfPersons() const { assert(m_numPersons >= 0 && m_numPersons <= POPULATIONEVENT_MAXPERSONS); return (int)m_numPersons; }
89 
92  PersonBase *getPerson(int idx) const;
93 
96  virtual bool isEveryoneAffected() const { return false; }
97 
100  virtual void markOtherAffectedPeople(const PopulationStateInterface &population) { }
101 
104  virtual bool areGlobalEventsAffected() const { return false; }
105 
110  virtual std::string getDescription(double tNow) const { return std::string("No description given"); }
111 
112  // TODO: shield these from the user somehow? These functions are used internally
113  // by the algorithm and should not be called directly by the user. */
114  void setEventIndex(PersonBase *pPerson, int idx);
115  int getEventIndex(PersonBase *pPerson) const;
116 
117  void setScheduledForRemoval() { m_scheduledForRemoval = true; }
118  bool isScheduledForRemoval() const { return m_scheduledForRemoval; }
119 
120  bool isNoLongerUseful();
121 protected:
126  virtual bool isUseless() { return false; }
127 private:
128  void commonConstructor();
129 
130  PersonBase *m_pPersons[POPULATIONEVENT_MAXPERSONS];
131  int m_eventIndex[POPULATIONEVENT_MAXPERSONS];
132  int8_t m_numPersons;
133 
134  bool m_scheduledForRemoval;
135  int64_t m_eventID;
136 };
137 
138 inline void PopulationEvent::setEventIndex(PersonBase *pPerson, int idx)
139 {
140  assert(pPerson != 0);
141  int num = m_numPersons;
142 
143  for (int i = 0 ; i < num ; i++)
144  {
145  assert(m_pPersons[i] != 0);
146 
147  if (pPerson == m_pPersons[i])
148  {
149  m_eventIndex[i] = idx;
150  return;
151  }
152  }
153 
154  std::cerr << "PopulationEvent::setEventIndex: Consistency error: invalid Person object in setEventIndex" << std::endl;
155  abort();
156 }
157 
158 inline int PopulationEvent::getEventIndex(PersonBase *pPerson) const
159 {
160  assert(pPerson != 0);
161  int num = m_numPersons;
162 
163  for (int i = 0 ; i < num ; i++)
164  {
165  assert(m_pPersons[i] != 0);
166 
167  if (pPerson == m_pPersons[i])
168  return m_eventIndex[i];
169  }
170 
171  std::cerr << "PopulationEvent::getEventIndex: Consistency error: invalid Person object in getEventIndex" << std::endl;
172  abort();
173 }
174 
175 #ifdef NDEBUG
176 inline PersonBase *PopulationEvent::getPerson(int idx) const
177 {
178  return m_pPersons[idx];
179 }
180 #endif
181 
182 #endif // POPULATIONEVENT_H
virtual bool isUseless()
This function can be used to inform the algorithm that an event is no longer of any use and should be...
Definition: populationevent.h:126
int getNumberOfPersons() const
Returns the number of people specified during the creation of the event (will be one for global event...
Definition: populationevent.h:88
PersonBase * getPerson(int idx) const
Returns a person that was specified when the event was constructed, where idx can range from 0 to Pop...
Definition: populationevent.cpp:82
PopulationEvent()
Constructs a 'global' event.
Definition: populationevent.cpp:26
This is the base class for events in population-based simulations.
Definition: populationevent.h:61
virtual bool areGlobalEventsAffected() const
If global events (not referring to a particular person) are affected, this function can be overridden...
Definition: populationevent.h:104
This is the base class for a person in a population-based simulation.
Definition: personbase.h:23
virtual std::string getDescription(double tNow) const
Returns a short description of the event, can be useful for logging/debugging purposes.
Definition: populationevent.h:110
virtual void markOtherAffectedPeople(const PopulationStateInterface &population)
If other people than the one(s) mentioned in the constructor are also affected by this event...
Definition: populationevent.h:100
virtual bool isEveryoneAffected() const
If the entire population is affected by this event (should be avoided!), this function can be overrid...
Definition: populationevent.h:96
This is the base class for events in the mNRM algorithm.
Definition: eventbase.h:55
Interface for a simulation state for the population-based algorithm, specifying member functions that...
Definition: populationinterfaces.h:26