-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimutils.h
150 lines (123 loc) · 3.94 KB
/
simutils.h
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
/*
* simutils.h
*
* Created on: 16 Oct 2011
* Author: nathan
*/
#ifndef SIMUTILS_H_
#define SIMUTILS_H_
#include <string>
#include <vector>
#include <map>
#include <random>
#include <iostream>
#include <exception>
#define PARM(X) (*stateParameters_[Parameter(X)])()
#define MATCH(X, Y) stateParameters_.insert(pair<int, StateParameter*>( \
Parameter(X), \
&Y))
using namespace std;
namespace microsimulator {
struct OutputDescriptor {
string description;
double value;
};
struct StateDescriptor {
string stateName;
string description;
};
class StateParameter;
class State;
class Individual;
class On;
typedef map<string, StateParameter> ParameterMap;
typedef int StateIndex; // Not yet in use
typedef vector<State*> StateVector;
typedef map<string, double> StateValueMap;
//typedef bool (*FilterFunction)(const StateValueMap&);
typedef vector <On> FilterFunctionList;
typedef double (*InitializeFunction)();
typedef double (*NormalizeFunction)(double, double, double);
typedef vector<Individual> IndividualVector;
typedef double (*AnalysisFunction) (int, IndividualVector&);
typedef tuple <string, int, AnalysisFunction, FilterFunctionList>
AnalysisDescriptor;
typedef vector < AnalysisDescriptor > AnalysisDescriptorVector;
typedef vector<OutputDescriptor> AnalysisOutput;
typedef double (*TransitionFunction) (double, StateVector&, IndividualVector&,
Individual&);
typedef vector <double> StateValueVector;
typedef vector <StateDescriptor> StateDescriptorVector;
enum SimulationFormat {
JSON,
SQLITE,
};
const double SECOND = 1.0 / (24.0 * 60.0 * 60.0);
const double MINUTE = 1.0 / (24.0 * 60.0);
const double HOUR = 1.0 / 24.0;
const double DAY = 1.0;
const double WEEK = 7.0;
const double MONTH = 365.0 / 12.0;
const double YEAR = 365.0;
const int defaultNumberIndividuals = 10000;
const int defaultNumberIterations = 70;
static mt19937 mersenne_twister;
// Utility functions
double always_true();
double frand(uniform_real_distribution<> dist=uniform_real_distribution<>(0,1));
template <class FilterContainer>
bool passesFilters(const StateValueVector& stateValueVector,
const FilterContainer& filters)
{
for (auto f : filters) {
if (not f(stateValueVector)) {
return false;
}
}
return true;
}
// Common filters
class On
{
public:
On(int stateIndex, bool on=true) : stateIndex_(stateIndex), on_(on) {};
bool operator()(const StateValueVector& stateValues);
protected:
int stateIndex_;
bool on_;
};
#define Off(x) On(x, false)
double normalize_linear_proportion(double proportion,
double fromTimePeriod,
double toTimePeriod);
double normalize_compounded_proportion(double proportion,
double fromTimePeriod,
double toTimePeriod);
double normalize_identity(double proportion,
double fromTimePeriod,
double toTimePeriod);
double count(int stateIndex, IndividualVector& individuals);
double mean(int stateIndex, IndividualVector& individuals);
double median(int stateIndex, IndividualVector& individuals);
double defaultTransitionFunction(double value,
StateVector& states,
ParameterMap& parameters);
double aliveStateTransition(double value,
StateVector& states,
ParameterMap& parameters);
double ageStateTransition(double value,
StateVector& states,
ParameterMap& parameters);
}
// Exceptions
class SimulationException : public exception {
public:
SimulationException(string message, int line=0, string file="");
virtual const char* what() const throw();
~SimulationException() throw () {};
private:
string message_;
int line_;
string file_;
};
#endif /* SIMUTILS_H_ */