forked from NitzanMadar/exPBS-exRHCR
-
Notifications
You must be signed in to change notification settings - Fork 1
/
epea_search.cpp
332 lines (296 loc) · 11.3 KB
/
epea_search.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
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
326
327
328
329
330
331
332
#include "epea_search.h"
/// <summary>
/// Expands a single agent in the nodes.
/// This includes:
/// - Generating the children
/// - Inserting them into OPEN
/// - Insert node into CLOSED
/// Returns the child nodes
/// </summary>
list<EPEANode*> EPEASearch::ExpandOneAgent(list<EPEANode*>& intermediateNodes, int agent_id)
{
list<EPEANode*> GeneratedNodes;
for (list< EPEANode* >::iterator it = intermediateNodes.begin(); it != intermediateNodes.end(); ++it)
{
/*if ((std::clock() - this->start_time) > TIME_LIMIT)
break;*/
EPEANode* node = *it;
// Try all legal moves of the agents
for (list< pair<int16_t, int16_t> >::iterator it = node->singleAgentDeltaFs->at(agent_id).begin(); it != node->singleAgentDeltaFs->at(agent_id).end(); ++it)
{
int next_loc = node->locs[agent_id] + moves_offset[it->first];
if(!node->IsValid(agent_id, node->locs[agent_id], next_loc/*, vertex_cons, edge_cons*/))
continue;
// Using the data that describes its delta F potential before the move.
if (it->second <= node->remainingDeltaF // last move was good
&& node->existsChildForF(agent_id + 1, node->remainingDeltaF))
{
EPEANode *childNode = new EPEANode();
childNode->deep_copy(*node); // Copy all except lookUp table
//if (agent_id == 0)
// childNode->parent = node;
//else
// childNode->parent = node->parent; // Skip temporary node objects used during expansion process.
childNode->MoveTo(agent_id, next_loc);
childNode->remainingDeltaF -= it->second; // Update target F
GeneratedNodes.push_back(childNode);
}
else
break;
}
if(agent_id > 0)
delete node;
}
intermediateNodes.clear();
return GeneratedNodes;
}
void EPEASearch::Clear()
{
for (int i = 0; i < my_heuristics.size(); i++)
delete[] my_heuristics[i];
// releaseClosedListNodes
hashtable_t::iterator it;
for (it = allNodes_table.begin(); it != allNodes_table.end(); it++) {
delete ((*it).second); // should it be .second?
}
allNodes_table.clear();
delete (empty_node);
delete (deleted_node);
}
void EPEASearch::Expand(EPEANode& node)
{
/*if (node.index % 1000 == 0)
{
cout << "Expand node [" << node.index << "] {";
int i = 0;
for (; i < num_of_agents - 1; i++)
cout << "(" << node.locs[i] / num_col << "," << node.locs[i] % num_col << "),";
cout << "(" << node.locs[i] / num_col << "," << node.locs[i] % num_col << ")} with g = " << node.g << ", h = " << node.h <<
" and target delta f = " << node.targetDeltaF << endl;
}*/
//cout << "Expand node [" << node.index << "] {";
//int i = 0;
//for (; i < num_of_agents - 1; i++)
// cout << "(" << node.locs[i] / num_col << "," << node.locs[i] % num_col << "),";
//cout << "(" << node.locs[i] / num_col << "," << node.locs[i] % num_col << ")} with g = " << node.g << ", h = " << node.h <<
// " and target delta f = " << node.targetDeltaF << endl;
//bool wasAlreadyExpanded = true;
if (!node.alreadyExpanded)
{
node.calcSingleAgentDeltaFs();
//num_expanded++;
node.alreadyExpanded = true;
//wasAlreadyExpanded = false;
//node.hBonus = 0; // Locking any hbonus that doesn't come from partial expansion
node.targetDeltaF = 0; // Assuming a consistent heuristic (as done in the paper), the min delta F is zero.
node.remainingDeltaF = 0; // Just for the following hasChildrenForCurrentDeltaF call.
while (node.targetDeltaF <= node.maxDeltaF && node.existsChildForF(0, node.remainingDeltaF) == false) // DeltaF=0 may not be possible if all agents have obstacles between their location and the goal
{
node.targetDeltaF++;
node.remainingDeltaF = node.targetDeltaF; // Just for the following hasChildrenForCurrentDeltaF call.
}
if (node.targetDeltaF > node.maxDeltaF) // Node has no possible children at all
{
node.Clear();
return;
}
}
// If this node was already expanded, notice its h was updated, so the deltaF refers to its original H
//if (node.index == 176)
// cout << "*" << endl;
list<EPEANode*> intermediateNodes;
intermediateNodes.push_back(&node);
for (int agentIndex = 0; agentIndex < num_of_agents; agentIndex++)
{
/*if ((std::clock() - this->start_time) > TIME_LIMIT)
return;*/
//cout << "Moving agent " << agentIndex << endl;
intermediateNodes = ExpandOneAgent(intermediateNodes, agentIndex);
}
list<EPEANode*> finalGeneratedNodes = intermediateNodes;
for (list< EPEANode* >::iterator it = finalGeneratedNodes.begin(); it != finalGeneratedNodes.end(); ++it)
{
/*if ((std::clock() - this->start_time) > TIME_LIMIT)
return;*/
(*it)->makespan++;
(*it)->ClearConstraintTable();
(*it)->targetDeltaF = 0;
(*it)->parent = &node;
//if (currentNode.g < currentNode.minCost)
//{
// if (currentNode.h == 0)
// currentNode.h = 2; // Otherwise waiting at goal would expand to waiting at the goal for the same too low cost,
// // which would expand to waiting at the goal, etc.
// // +2 because you need a step out of the goal and another step into it.
// currentNode.h = Math.Max(currentNode.h, currentNode.minCost - currentNode.g);
//}
}
// Enter the generated nodes into the open list
for (list< EPEANode* >::iterator child = finalGeneratedNodes.begin(); child != finalGeneratedNodes.end(); ++child)
{
if ((*child)->h + (*child)->g <= this->maxCost)
// Assuming h is an admissable heuristic, no need to generate nodes that won't get us to the goal
// within the budget
{
// try to retrieve it from the hash table
hashtable_t::iterator it = allNodes_table.find(*child);
// If in closed list - only reopen if F is lower or node is otherwise preferred
if (it != allNodes_table.end()) // Notice the agents may have gotten to their location from a different direction in this node.
{
//++this->closedListHits;
EPEANode* existing = (*it).second;
bool compare;
if((*child)->g + (*child)->h == existing->g + existing->h/* + existing->targetDeltaF*/)
compare = (*child)->h < existing->h/* + existing->targetDeltaF*/;
else
compare = (*child)->g + (*child)->h < existing->g + existing->h/* + existing->targetDeltaF*/;
if (compare)// This node has smaller f, or preferred due to other consideration.
{
existing->Update(**child);
/*cout << "Update node [" << existing->index << "] into {";
int i = 0;
for (; i < num_of_agents - 1; i++)
cout << "(" << existing->locs[i] / num_col << "," << existing->locs[i] % num_col << "),";
cout << "(" << existing->locs[i] / num_col << "," << existing->locs[i] % num_col << ")} with g = " << existing->g << " and h = " << existing->h << endl;*/
if (!existing->in_openlist) // reopen
{
existing->open_handle = this->open_list.push(existing);
existing->in_openlist = true;
}
}
delete (*child);
}
else // add the newly generated node to open_list and hash table
{
allNodes_table[(*child)] = *child;
this->num_generated++;
(*child)->index = num_generated;
(*child)->open_handle = open_list.push(*child);
(*child)->in_openlist = true;
//if((*child)->index % 10000 == 0)
{
/*cout << "Generate new node [" << (*child)->index << "] {";
int i = 0;
for (; i < num_of_agents - 1; i++)
cout << "(" << (*child)->locs[i] / num_col << "," << (*child)->locs[i] % num_col << "),";
cout << "(" << (*child)->locs[i] / num_col << "," << (*child)->locs[i] % num_col << ")} with g = " << (*child)->g << " and h = " << (*child)->h << endl;*/
}
//currentNode.expandedCountWhenGenerated = this.expanded;
}
}
}
if (node.alreadyExpanded == false)
{
// Node was cleared during expansion.
// It's unnecessary and unsafe to continue to prepare it for the next partial expansion.
return;
// TODO: Is there a prettier way to do this?
}
node.targetDeltaF++; // This delta F was exhausted
node.remainingDeltaF = node.targetDeltaF;
while (node.targetDeltaF <= node.maxDeltaF && node.existsChildForF(0, node.remainingDeltaF) == false)
{
node.targetDeltaF++;
node.remainingDeltaF = node.targetDeltaF; // Just for the following hasChildrenForCurrentDeltaF call.
}
if (node.targetDeltaF <= node.maxDeltaF && node.existsChildForF(0, node.remainingDeltaF)
&& node.h + node.g + node.targetDeltaF <= this->maxCost)
{
// Re-insert node into open list
//cout << "Re-insert node into open list with target delta f = " << node.targetDeltaF << endl;
node.open_handle = open_list.push(&node);
node.in_openlist = true;
}
else
{
node.Clear();
}
}
/// <summary>
/// Runs the algorithm until the problem is solved or memory/time is exhausted
/// </summary>
/// <returns>True if solved</returns>
bool EPEASearch::runEPEASearch()
{
cout << " EPEA: ";
// set timer
std::clock_t start;
start = std::clock();
int initialEstimate = open_list.top()->h; // g = targetDeltaF = 0 initially
int lastF = -1;
while (!open_list.empty())
{
// Check if max time has been exceeded
runtime = std::clock() - start;
if (runtime > TIME_LIMIT)
{
solution_cost = -1;
solution_depth = open_list.top()->g + open_list.top()->h + open_list.top()->targetDeltaF - initialEstimate; // A minimum estimate
cout << "TIMEOUT ; " << solution_cost << " ; " << solution_depth << " ; " <<
num_expanded << " ; " << num_generated << " ; " <<
num_expanded << " ; " << num_generated << " ; " << runtime << endl;
this->Clear();
return false;
}
EPEANode* curr = open_list.top();
open_list.pop();
lastF = curr->g + curr->h + curr->targetDeltaF;
// Check if node is the goal
if ((int)round(curr->h) == 0)
{
runtime = std::clock() - start;
this->solution_cost = curr->g;
this->paths = curr->GetPlan();
this->solution_depth = curr->g - initialEstimate;
cout << solution_cost << " ; " << solution_depth << " ; " <<
num_expanded << " ; " << num_generated << " ; " <<
num_expanded << " ; " << num_generated << " ; " << runtime << endl;
this->Clear();
return true;
}
// Expand
Expand(*curr);
num_expanded++;
}
solution_cost = -2;
this->solution_depth = lastF - initialEstimate;
runtime = std::clock() - start;
cout << "NO SOLUTION ; " << solution_cost << " ; " << solution_depth << " ; " <<
num_expanded << " ; " << num_generated << " ; " << runtime << endl;
this->Clear();
return false;
}
EPEASearch::EPEASearch(const MapLoader& ml, const AgentsLoader& al, const EgraphReader &egr):
al(al)
{
this->num_col = ml.cols;
this->num_row = ml.rows;
this->moves_offset = ml.moves_offset;
this->num_of_agents = al.num_of_agents;
// initialize allNodes_table (hash table)
empty_node = new EPEANode();
empty_node->locs[0] = -2;
deleted_node = new EPEANode();
deleted_node->locs[0] = -3;
allNodes_table.set_empty_key(empty_node);
allNodes_table.set_deleted_key(deleted_node);
// compute heuristic lookup table
my_heuristics.resize(num_of_agents);
for (int i = 0; i < num_of_agents; i++)
{
int init_loc = ml.linearize_coordinate((al.initial_locations[i]).first, (al.initial_locations[i]).second);
int goal_loc = ml.linearize_coordinate((al.goal_locations[i]).first, (al.goal_locations[i]).second);
ComputeHeuristic ch(init_loc, goal_loc, ml.get_map(), ml.rows, ml.cols, ml.moves_offset, ml.actions_offset, 1.0, &egr);
my_heuristics[i] = ch.getHVals();
}
// generate root node
EPEANode* root_node = new EPEANode(ml, al, my_heuristics);
root_node->open_handle = this->open_list.push(root_node);
root_node->in_openlist = true;
allNodes_table[root_node] = root_node;
num_generated++;
root_node->index = num_generated;
}
EPEASearch::~EPEASearch()
{
}