-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasesimulation.cpp
165 lines (131 loc) · 3.95 KB
/
basesimulation.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//============================================================================
// Name : microsimulator.cpp
// Author : Nathan Geffen
// Version :
// Copyright : Copyright Nathan Geffen, 2011
// Description : Microsimulator of epidemics
//============================================================================
#include <vector>
#include <iostream>
#include <algorithm>
#include "simutils.h"
#include "basesimulation.h"
using namespace microsimulator;
const double DEFAULT_TRANSITION_PROBABILITY = 0.009;
const double DEFAULT_TRANSITION_BACK_PROBABILITY = 0.7;
void BaseSimulation::prepare()
{
// If there are no states, insert a default StateAge
if (states_.size() == 0) {
addState("age", new StateAge(), true);
}
// Prepare states
for ( auto itState : states_ ) {
itState->prepare(timePeriod_);
}
// Initialize individuals
for ( int i=0; i<nIndividuals_; ++i ) {
Individual individual;
for ( auto state : states_ ) {
individual.initializeStateValue(state->initialize());
}
individuals_.push_back(individual);
}
};
void BaseSimulation::simulate() {
for (int i=0; i<nIterations_; ++i) {
for (auto& individual : individuals_) {
for (int j=0; j < stateCounter_; ++j) {
if (passesFilters(*individual.getStateValues(),
*states_[j]->getFilterFunctions())) {
double stateValue = individual.getStateValue(j);
stateValue = states_[j]->transition(stateValue,
states_,
individuals_,
individual);
individual.setStateValue(j, stateValue);
}
}
}
}
};
AnalysisOutput BaseSimulation::analyze()
{
AnalysisOutput output;
OutputDescriptor o;
for (auto descriptor : analysisDescriptors_) {
// Get state name and filter out unneeded individuals
string outputDescription = get<0>(descriptor);
int stateIndex = get<1>(descriptor);
vector<On> filters = get<3>(descriptor);
IndividualVector filteredIndividuals(individuals_.size());
auto itend = remove_copy_if(
individuals_.begin(), individuals_.end(),
filteredIndividuals.begin(),
[filters](Individual& i){
return !passesFilters(*i.getStateValues(), filters);
});
filteredIndividuals.erase(itend, filteredIndividuals.end());
o.description = outputDescription;
o.value = get<2>(descriptor)(stateIndex, filteredIndividuals);
output.push_back(o);
}
o.description = "population";
o.value = (double) individuals_.size();
output.push_back(o);
return output;
};
// Getters and setters
int BaseSimulation::getIndividuals() const
{
return nIndividuals_;
}
int BaseSimulation::getIterations() const
{
return nIterations_;
}
double BaseSimulation::getTimePeriod() const
{
return timePeriod_;
}
void BaseSimulation::setIndividuals(int nIndividuals_)
{
this->nIndividuals_ = nIndividuals_;
}
void BaseSimulation::setIterations(int nIterations_)
{
this->nIterations_ = nIterations_;
}
void BaseSimulation::setTimePeriod(double timePeriod_)
{
this->timePeriod_ = timePeriod_;
}
int BaseSimulation::addState(string name,
State* state,
bool markForDeletion)
{
int stateCounter = stateCounter_;
state->setName(name);
state->setId(stateCounter);
states_.push_back(state);
if (markForDeletion) {
statesToDelete_.push_back(stateCounter);
}
++stateCounter_;
return stateCounter;
}
void BaseSimulation::addAnalysisDescriptor(
string analysisName,
int stateIndex,
AnalysisFunction analysisFunction,
vector<On> filters)
{
analysisDescriptors_.push_back(AnalysisDescriptor(analysisName,
stateIndex, analysisFunction, filters));
}
BaseSimulation::~BaseSimulation()
{
for (auto index : statesToDelete_) {
delete states_[index];
}
}