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 //#define POPULATIONEVENT_FAKEDELETE
18 
19 class PersonBase;
21 
63 class PopulationEvent : public EventBase
64 {
65 public:
73 
75  PopulationEvent(PersonBase *pPerson);
76 
78  PopulationEvent(PersonBase *pPerson1, PersonBase *pPerson2);
79  ~PopulationEvent();
80 
81  // These are for internal use
82  void setGlobalEventPerson(PersonBase *pDummyPerson);
83  void setEventID(int64_t id);
84  int64_t getEventID() const;
85 
90  int getNumberOfPersons() const;
91 
94  PersonBase *getPerson(int idx) const;
95 
98  virtual bool isEveryoneAffected() const { return false; }
99 
102  virtual void markOtherAffectedPeople(const PopulationStateInterface &population) { }
103 
106  virtual bool areGlobalEventsAffected() const { return false; }
107 
112  virtual std::string getDescription(double tNow) const { return std::string("No description given"); }
113 
114  // TODO: shield these from the user somehow? These functions are used internally
115  // by the algorithm and should not be called directly by the user. */
116  void setEventIndex(PersonBase *pPerson, int idx);
117  int getEventIndex(PersonBase *pPerson) const;
118 
119  void setScheduledForRemoval() { m_scheduledForRemoval = true; }
120  bool isScheduledForRemoval() const { return m_scheduledForRemoval; }
121 
122  bool isNoLongerUseful(const PopulationStateInterface &population);
123 
124  // For debugging
125 #if !defined(NDEBUG) && defined(POPULATIONEVENT_FAKEDELETE)
126  void setDeleted() { m_deleted = true; }
127  bool isDeleted() const { return m_deleted; }
128 #else
129  void setDeleted() { }
130  bool isDeleted() const { return false; }
131 #endif
132  PersonBase *getPersonWithoutChecking(int idx) const;
133 protected:
138  virtual bool isUseless(const PopulationStateInterface &population) { return false; }
139 private:
140  void commonConstructor();
141 
142  PersonBase *m_pPersons[POPULATIONEVENT_MAXPERSONS];
143  int m_eventIndex[POPULATIONEVENT_MAXPERSONS];
144  int8_t m_numPersons;
145 
146  bool m_scheduledForRemoval;
147  int64_t m_eventID;
148 
149 #ifdef POPULATIONEVENT_FAKEDELETE
150  bool m_deleted;
151 #endif // POPULATIONEVENT_FAKEDELETE
152 };
153 
154 inline void PopulationEvent::setEventIndex(PersonBase *pPerson, int idx)
155 {
156  assert(pPerson != 0);
157  int num = m_numPersons;
158 
159  for (int i = 0 ; i < num ; i++)
160  {
161  assert(m_pPersons[i] != 0);
162 
163  if (pPerson == m_pPersons[i])
164  {
165  m_eventIndex[i] = idx;
166  return;
167  }
168  }
169 
170  std::cerr << "PopulationEvent::setEventIndex: Consistency error: invalid Person object in setEventIndex" << std::endl;
171  abort();
172 }
173 
174 inline int PopulationEvent::getEventIndex(PersonBase *pPerson) const
175 {
176  assert(pPerson != 0);
177  int num = m_numPersons;
178 
179  for (int i = 0 ; i < num ; i++)
180  {
181  assert(m_pPersons[i] != 0);
182 
183  if (pPerson == m_pPersons[i])
184  return m_eventIndex[i];
185  }
186 
187  std::cerr << "PopulationEvent::getEventIndex: Consistency error: invalid Person object in getEventIndex" << std::endl;
188  abort();
189 }
190 
191 #ifdef NDEBUG
192 inline PersonBase *PopulationEvent::getPerson(int idx) const
193 {
194  return m_pPersons[idx];
195 }
196 #endif
197 
198 inline PersonBase *PopulationEvent::getPersonWithoutChecking(int idx) const
199 {
200  return m_pPersons[idx];
201 }
202 
203 inline void PopulationEvent::setEventID(int64_t id)
204 {
205 #ifdef POPULATIONEVENT_FAKEDELETE
206  assert(!m_deleted);
207 #endif
208  assert(m_eventID < 0);
209  assert(id >= 0);
210  m_eventID = id;
211 }
212 
213 inline int64_t PopulationEvent::getEventID() const
214 {
215 #ifdef POPULATIONEVENT_FAKEDELETE
216  assert(!m_deleted);
217 #endif
218  return m_eventID;
219 }
220 
222 {
223 #ifdef POPULATIONEVENT_FAKEDELETE
224  assert(!m_deleted);
225 #endif
226  assert(m_numPersons >= 0 && m_numPersons <= POPULATIONEVENT_MAXPERSONS);
227  return (int)m_numPersons;
228 }
229 
230 #endif // POPULATIONEVENT_H
This is the base class for events in the mNRM algorithm.
Definition: eventbase.h:56
This is the base class for a person in a population-based simulation.
Definition: personbase.h:24
This is the base class for events in population-based simulations.
Definition: populationevent.h:64
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:93
virtual bool areGlobalEventsAffected() const
If global events (not referring to a particular person) are affected, this function can be overridden...
Definition: populationevent.h:106
virtual bool isEveryoneAffected() const
If the entire population is affected by this event (should be avoided!), this function can be overrid...
Definition: populationevent.h:98
virtual bool isUseless(const PopulationStateInterface &population)
This function can be used to inform the algorithm that an event is no longer of any use and should be...
Definition: populationevent.h:138
int getNumberOfPersons() const
Returns the number of people specified during the creation of the event (will be one for global event...
Definition: populationevent.h:221
PopulationEvent()
Constructs a 'global' event.
Definition: populationevent.cpp:30
virtual std::string getDescription(double tNow) const
Returns a short description of the event, can be useful for logging/debugging purposes.
Definition: populationevent.h:112
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:102
Interface for a simulation state for the population-based algorithm, specifying member functions that...
Definition: populationinterfaces.h:27