-
Notifications
You must be signed in to change notification settings - Fork 0
/
Path.cpp
289 lines (214 loc) · 5.48 KB
/
Path.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
* Path.cpp
*
* Created on: Jan 28, 2010
* Author: mikee
*/
#include "Path.h"
Step::Step() {
time = -1.0;
state = 0;
site = 0;
}
Step::Step(double t, int s, int x) {
time = t;
state = s;
site = x;
}
bool Step::operator<(const Step &A) const {
//std::cout << "operator<\n";
//TODO: get merge to use the operator> properly
if (time > A.time) return true;
else if (time == A.time && site < A.site) return true;
else return false;
}
bool Step::operator>(const Step &A) const {
//std::cout << "operator>\n";
if (time > A.time) return true;
else return false;
}
Path::Path()
{
}
Path::~Path()
{
}
Path::Path(Path &p) {
clone(p);
}
void Path::clearPathHistory(void)
{
//pathHistory.clear();
int i = pathHistory.size() - 1;
while (pathHistory.empty() == false)
{
clearSitePathHistory(i);
pathHistory.pop_back();
i--;
}
}
void Path::clearSitePathHistory(int x)
{
while (pathHistory[x].empty() == false)
{
pathHistory[x].pop_back();
}
}
void Path::clone(const Path &p) {
pathHistory = p.pathHistory;
}
Path& Path::operator=(const Path &p) {
if (this != &p)
clone(p);
return *this;
}
std::vector<Step> Path::getSeqState() {
int i = 0;
std::vector<Step> seqState(pathHistory.size());
for (std::vector<std::vector<Step> >::iterator it = pathHistory.begin(); it != pathHistory.end(); it++)
{
seqState[i] = (*it)[0];
i++;
}
return seqState;
}
void Path::setPathSize(int numCodonSites)
{
pathHistory.resize(numCodonSites);
}
void Path::pushStep(Step s, int x) {
pathHistory[x].push_back(s);
}
void Path::addNextStep(Step s, int x) {
// inserts an element after the first position
std::vector<Step>::iterator it = pathHistory[x].begin();
it++;
pathHistory[x].insert(it, s);
}
void Path::printPathHistory()
{
double oldTime = -1.0;
// create vector to hold events sorted by time
std::vector<Step> printVector;
// add & sort all events to printVector
for (std::vector<std::vector<Step> >::iterator it1 = pathHistory.begin(); it1 != pathHistory.end(); it1++)
{
for (std::vector<Step>::iterator it2 = (*it1).begin(); it2 != (*it1).end(); it2++)
{
printVector.push_back(*it2);
}
}
sort(printVector.begin(), printVector.end());
// print events from printVector
for (std::vector<Step>::iterator it = printVector.begin(); it != printVector.end(); it++)
{
if ((*it).getTime() == oldTime)
std::cout << std::setw(2) << (*it).getState() << " ";
else {
std::cout << std::endl;
std::cout << "t" << std::setw(24) << std::setprecision(16) << (*it).getTime() << ": ";
for (int i = 0; i < (*it).getSite(); i++)
std::cout << " ";
std::cout << std::setw(2) << (*it).getState() << " ";
}
oldTime = (*it).getTime();
}
std::cout << "\n";
}
void Path::printSitePathHistory(int x) {
double oldTime = -1.0;
std::vector<Step> printVector = pathHistory[x];
// print events from printVector
for (std::vector<Step>::iterator it = printVector.begin(); it != printVector.end(); it++)
{
if ((*it).getTime() == oldTime)
std::cout << std::setw(2) << (*it).getState() << " ";
else {
std::cout << std::endl;
std::cout << "t" << std::setw(24) << std::setprecision(16) << (*it).getTime() << ": ";
for (int i = 0; i < (*it).getSite(); i++)
std::cout << " ";
std::cout << std::setw(2) << (*it).getState() << " ";
}
oldTime = (*it).getTime();
}
std::cout << "\n";
}
void Path::printSeqState() {
double oldTime = (*(*(pathHistory.begin())).begin()).getTime();
std::cout << "t" << std::setw(24) << std::setprecision(16) << oldTime << ": ";
for (std::vector<std::vector<Step> >::iterator it1 = pathHistory.begin(); it1 != pathHistory.end(); it1++)
{
Step s = *(*it1).begin();
std::cout << std::setw(2) << s.getState() << " ";
}
std::cout << std::endl;
}
History::History(void) {
activeState = 0;
numAttemptedChanges = 0;
numAcceptedChanges = 0;
paths[0] = new Path();
paths[1] = new Path(*paths[0]);
}
History::~History(void) {
delete paths[0];
delete paths[1];
}
void History::initPathSize(int numCodonSites)
{
paths[0]->setPathSize(numCodonSites);
paths[1]->setPathSize(numCodonSites);
}
void History::keep(void) {
*paths[getInactiveState()] = *paths[activeState];
}
void History::restore(void) {
*paths[activeState] = *paths[getInactiveState()];
}
int History::getActiveState(void) {
if (activeState == 1) return 1;
else return 0;
}
int History::getInactiveState(void) {
if (activeState == 1) return 0;
else return 1;
}
/*
void Path::printSeqAtNode() {
double oldTime = (*(eventHistory.begin())).getTime();
std::cout << "t" << std::setw(24) << std::setprecision(16) << oldTime << ": ";
for (std::list<Step>::iterator it = eventHistory.begin(); it != eventHistory.end(); ++it)
{
if ((*it).getTime() == oldTime)
std::cout << std::setw(2) << (*it).getState() << " ";
oldTime = (*it).getTime();
}
std::cout << std::endl;
}
void Path::printEventHistory()
{
int j = 0;
std::cout << "PATH HISTORY!" << std::endl;
//for (int j = 0; (unsigned int)j < eventHistory.size(); j++) // for each step
for (std::list<Step>::iterator it = eventHistory.begin(); it != eventHistory.end(); ++it)
{
std::cout << " (step#" << j+1 << ", site#" << (*it).getSite() << ", s" << (*it).getState() << ", t" << (*it).getTime() << ", 0x= " << (*it).getNextStep() << ")";
std::cout << std::endl;
j++;
}
std::cout << std::endl;
}
*/
/*
void Path::pushStep(Step s) {
eventHistory.push_back(s);
}
*/
/*
void Path::pushEventHistory(std::list<Step> p)
{
reverse(p.begin(), p.end());
eventHistory.merge(p);
}
*/