-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatetransitiontable.cpp
More file actions
153 lines (134 loc) · 4.38 KB
/
Copy pathstatetransitiontable.cpp
File metadata and controls
153 lines (134 loc) · 4.38 KB
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
/*
* statetransitiontable.cpp
*
* Created on: 16 Nov 2011
* Author: nathan
*/
#include <iostream>
#include "simutils.h"
#include "statetransitiontable.h"
#include "simplejson/JSON.h"
using namespace microsimulator;
void StateTransitionTable::loadStateTransitionTable(
string inputTable,
SimulationFormat tableFormat)
{
switch( tableFormat ) {
case JSON:
cout << "JSON Table" << endl;
loadJsonTransitionTable(inputTable);
break;
case SQLITE:
throw SimulationException("SQLITE state transition table"
"not implemented yet", __LINE__, __FILE__);
break;
default:
throw SimulationException("Unknown state transition table format",
__LINE__, __FILE__);
break;
}
}
void StateTransitionTable::loadJsonTransitionTable(string inputTable)
{
// Parse example data
cout << "Input Table " << inputTable;
JSONValue *value = JSON::Parse(inputTable.c_str());
// Did it go wrong?
if (value == NULL)
{
throw SimulationException("JSON transition table did not parse",
__LINE__, __FILE__);
}
else
{
// Retrieve the main object
JSONObject root;
if (value->IsObject() == false)
{
throw SimulationException("The root element is not an object while "
"parsing Json table.",
__LINE__,
__FILE__);
}
root = value->AsObject();
if ( root.find(L"state") != root.end()
&& root[L"state"]->IsObject() ) {
wcout << L"STATE 1" << endl;
wcout << root[L"state"]->Stringify() << endl << endl;
}
if ( root.find(L"state") != root.end()
&& root[L"state"]->IsObject() ) {
wcout << L"STATE 2" << endl;
wcout << root[L"state"]->Stringify() << endl << endl;
}
}
}
double StateTransitionTable::transitionUsingTransitionTable(
double value,
StateVector& states,
IndividualVector& individuals,
Individual& individual)
{
for ( auto record : transitionTable_ ) {
bool match = false;
for ( auto entry : record.entries ) {
switch ( entry.matchFunction ) {
case ( EQ ):
if ( value == entry.lower )
match = true;
break;
case ( GTE_LTE ):
if ( value >= entry.lower && value <= entry.upper )
match = true;
case ( GTE_LT ):
if ( value >= entry.lower && value < entry.upper )
match = true;
break;
case ( GT_LTE ):
if ( value > entry.lower && value <= entry.upper )
match = true;
break;
case ( GT_LT ):
if ( value > entry.lower && value < entry.upper )
match = true;
break;
}
if ( !match ) break;
}
if ( match ) {
if ( frand() < record.normalizedProbability ) {
if ( record.assignFunction == ASSIGN ) {
return record.normalizedValue;
} else {
return value + record.normalizedValue;
}
} else {
// Event did not occur, then just return the state value unchanged
return value;
}
}
}
// No match, then just return the state value unchanged.
return value;
}
void StateTransitionTable::prepare(double toTimePeriod)
{
for ( auto record : transitionTable_ ) {
if ( record.valueTimePeriod != toTimePeriod ) {
record.normalizedValue = record.valueNormalizeFunction(
record.newValue,
record.valueTimePeriod,
toTimePeriod );
} else {
record.normalizedValue = record.newValue;
}
if ( record.probabilityTimePeriod != toTimePeriod ) {
record.normalizedProbability = record.probabilityNormalizeFunction(
record.probability,
record.probabilityTimePeriod,
toTimePeriod );
} else {
record.normalizedProbability = record.probability;
}
}
}