-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStatesContainer.h
325 lines (305 loc) · 11 KB
/
StatesContainer.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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#ifndef multi_index_H
#define multi_index_H
#include "structs.h"
#include "LineOfSight.h"
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/composite_key.hpp>
#include <boost/tuple/tuple.hpp>
using namespace boost::multi_index;
struct by_ij;
struct by_fg;
struct by_cons;
typedef multi_index_container<
Node,
indexed_by<
ordered_unique<
tag<by_ij>,
composite_key<
Node,
member<Node, int, &Node::i>,
member<Node, int, &Node::j>,
member<Node, double, &Node::interval_begin>
>
>,
ordered_non_unique<
tag<by_fg>,
composite_key<
Node,
member<Node, bool, &Node::expanded>,
member<Node, double, &Node::F>,
member<Node, double, &Node::g>
>
>,
ordered_non_unique<
tag<by_cons>,
member<Node, int, &Node::consistent>
>
>
> multi_index;
class StatesContainer
{
public:
LineOfSight los;
const cMap* map;
multi_index states;
std::vector<int> divisors;
struct updateExpand
{
updateExpand(){}
void operator()(Node& n)
{
n.expanded = true;
n.consistent = 1;
n.g = n.best_g;
n.Parent = n.best_Parent;
}
};
struct addParent
{
addParent(const Node* parent, double new_g):parent(parent), new_g(new_g){}
void operator()(Node& n)
{
if(n.parents.empty())
n.parents.push_front({parent, new_g});
else if(n.parents.back().second - new_g < CN_EPSILON)
n.parents.push_back({parent, new_g});
else
for(auto it = n.parents.begin(); it != n.parents.end(); it++)
if(it->second - new_g > CN_EPSILON)
{
n.parents.insert(it,{parent, new_g});
break;
}
}
private:
const Node* parent;
double new_g;
};
struct updateFG
{
updateFG(double g, const Node* parent):g(g), parent(parent){}
void operator()(Node& n)
{
n.F = g + n.h;
n.g = g;
if(n.consistent == 0)
n.consistent = 2;
n.Parent = parent;
}
private:
double g;
const Node* parent;
};
struct updateBest
{
updateBest(double g, const Node* parent):g(g),parent(parent){}
void operator()(Node& n)
{
n.best_Parent = parent;
n.best_g = g;
}
private:
double g;
const Node* parent;
};
struct cutParents
{
cutParents(double best_g):best_g(best_g){}
void operator()(Node& n)
{
while(n.parents.back().second >= best_g)
n.parents.pop_back();
}
private:
double best_g;
};
struct pop_parent
{
pop_parent(){}
void operator()(Node& n)
{
if(!n.parents.empty())
n.parents.pop_front();
}
};
Node getMin()
{
typedef multi_index::index<by_fg>::type fg_index;
fg_index & fg = states.get<by_fg>();
return *(fg.begin());
}
void expand(const Node& curNode)
{
typedef multi_index::index<by_ij>::type ij_index;
ij_index & ij = states.get<by_ij>();
auto it = ij.find(boost::tuple<int, int, double>(curNode.i, curNode.j, curNode.interval_begin));
ij.modify(it, updateExpand());
}
const Node* getParentPtr()
{
typedef multi_index::index<by_fg>::type fg_index;
fg_index & fg = states.get<by_fg>();
return &(*(fg.begin()));
}
void insert(const Node& curNode)
{
if(curNode.F>=CN_INFINITY)
std::cout<<curNode.i<<" "<<curNode.j<<" "<<curNode.g<<" "<<curNode.interval_begin<<" "<<curNode.interval_end<<" insert\n";
states.insert(curNode);
}
void update(const Node &curNode, bool best)
{
typedef multi_index::index<by_ij>::type ij_index;
ij_index & ij = states.get<by_ij>();
std::list<std::pair<const Node*, double>> parents(0);
auto it = ij.find(boost::tuple<int, int, double>(curNode.i, curNode.j, curNode.interval_begin));
ij.modify(it, pop_parent());
if(best)
ij.modify(it, updateBest(curNode.g, curNode.Parent));
if(curNode.consistent == 0)
{
parents = this->findParents(curNode);
for(auto pit = parents.begin(); pit!= parents.end(); pit++)
ij.modify(it, addParent(&(*pit->first), pit->second));
}
else if(best)
ij.modify(it, cutParents(curNode.best_g));
if(!it->parents.empty() && it->parents.begin()->second < curNode.best_g)
ij.modify(it, updateFG(it->parents.begin()->second, it->parents.begin()->first));
else
ij.modify(it, updateFG(curNode.best_g, curNode.best_Parent));
}
std::list<std::pair<const Node*, double>> findParents(const Node& curNode)
{
std::list<std::pair<const Node*, double>> parents;
parents.clear();
typedef multi_index::index<by_cons>::type cons_index;
cons_index & cons = states.get<by_cons>();
auto range = cons.equal_range(1);
double dist;
bool found = false;
for(auto it = range.first; it != range.second; it++)
{
if(!found)
if(it->i == curNode.Parent->i && it->j == curNode.Parent->j && fabs(it->interval_begin - curNode.Parent->interval_begin) < CN_EPSILON)
{
found = true;
it++;
if(it == range.second)
break;
}
if(hasMiddleState(it->i, it->j, curNode.i, curNode.j))
continue;
dist = sqrt(pow(it->i - curNode.i, 2) + pow(it->j - curNode.j, 2));
if(it->g + dist < curNode.g)
if(it->g + dist >= curNode.interval_begin)
{
if(it->g + dist <= curNode.interval_end && los.checkLine(curNode.i,curNode.j,it->i,it->j,*map))
parents.push_back({&(*it), it->g + dist});
}
else if(it->interval_end + dist >= curNode.interval_begin && los.checkLine(curNode.i,curNode.j,it->i,it->j,*map))
parents.push_front({&(*it), curNode.interval_begin});
}
return parents;
}
int gcd (int a, int b)
{
return b ? gcd(b, a % b) : a;
}
bool hasMiddleState(int i1, int j1, int i2, int j2)
{
int di(abs(i1-i2)),dj(abs(j1-j2));
if(di==1 || dj==1)
return false;
else if(di==0 || dj==0 || gcd(di,dj)>1)
return true;
return false;
}
void updateNonCons(const Node& curNode)
{
typedef multi_index::index<by_ij>::type ij_index;
ij_index & ij = states.get<by_ij>();
auto parent = &(*ij.find(boost::tuple<int, int, double>(curNode.i, curNode.j, curNode.interval_begin)));
typedef multi_index::index<by_cons>::type nc_index;
nc_index & non_cons = states.get<by_cons>();
auto range = non_cons.equal_range(2);
double dist, new_g;
for(auto it = range.first; it != range.second; it++)
{
if(hasMiddleState(it->i, it->j, curNode.i, curNode.j))
continue;
dist = sqrt(pow(it->i - curNode.i,2) + pow(it->j - curNode.j,2));
new_g = dist + curNode.best_g;
if(new_g < it->best_g)
if(new_g >= it->interval_begin)
{
if(new_g <= it->interval_end && los.checkLine(curNode.i,curNode.j,it->i,it->j,*map))
{
non_cons.modify(it, addParent(parent, new_g));
if(it->g - new_g > CN_EPSILON)
non_cons.modify(it, updateFG(new_g, parent));
}
}
else if(curNode.interval_end + dist >= it->interval_begin && los.checkLine(curNode.i,curNode.j,it->i,it->j,*map))
{
non_cons.modify(it, addParent(parent, it->interval_begin));
non_cons.modify(it, updateFG(it->interval_begin, parent));
}
}
}
void findAndPrint(int i, int j)
{
typedef multi_index::index<by_ij>::type ij_index;
ij_index & ij = states.get<by_ij>();
auto range = ij.equal_range(boost::make_tuple(i,j));
for(auto it = range.first; it != range.second; it++)
{
std::cout<<it->i<<" "<<it->j<<" "<<it->g<<" "<<it->F<<" "<<it->best_g<<"\n";
for(auto p_it = it->parents.begin(); p_it!=it->parents.end(); p_it++)
std::cout<<p_it->first->i<<" "<<p_it->first->j<<" "<<p_it->first->g<<" "<<p_it->first->F<<" "<<p_it->first->best_g<<" "<<p_it->second<<" parent\n";
}
}
void printByFG()
{
typedef multi_index::index<by_fg>::type fg_index;
fg_index & fg = states.get<by_fg>();
for(auto it=fg.begin(); it!=fg.end(); it++)
std::cout<<it->i<<" "<<it->j<<" "<<it->interval_begin<<" "<<it->interval_end<<" "<<it->g<<" "<<it->F<<" "<<it->best_g<<" "<<it->parents.size()<<" "<<it->expanded<<" "<<it->consistent<<"\n";
}
void printStats()
{
typedef multi_index::index<by_cons>::type cons_index;
cons_index & cons = states.get<by_cons>();
std::cout<<cons.count(0)<<" "<<cons.count(1)<<" "<<cons.count(2)<<" ";
std::ofstream out("optimal_vs_point_warehouse.txt",std::ios::app);
out<<cons.count(0)<<" "<<cons.count(1)<<" "<<cons.count(2)<<" ";
auto range = cons.equal_range(0);
//for(auto it=range.first;it!=range.second; it++)
// std::cout<<it->i<<" "<<it->j<<" "<<it->g<<" "<<it->F<<" not_checked\n";
}
std::pair<std::vector<Node>,std::vector<Node>> getExpanded()
{
std::vector<Node> closed(0), open(0);
typedef multi_index::index<by_cons>::type cons_index;
cons_index & cons = states.get<by_cons>();
auto range = cons.equal_range(1);
for(auto it=range.first; it!=range.second; it++)
closed.push_back(*it);
range = cons.equal_range(2);
for(auto it=range.first; it!=range.second; it++)
open.push_back(*it);
return {open, closed};
}
void clear()
{
states.clear();
}
int size()
{
return states.size();
}
};
#endif // multi_index_H