15 template <
class state,
class action,
class environment>
22 const state& from,
const state& to, std::vector<state> &thePath);
28 const state& from,
const state& to, std::vector<state> &thePath);
33 virtual const char *
GetName() {
return "BOAStar"; }
57 void SetPhi(std::function<
double(
double,
double)> p)
61 double Phi(
double h,
double g)
const
67 phi = [w](
double h,
double g){
return g/w+h; };
78 void AddState(
const state &s,
size_t parent);
79 size_t Lookup(
const state &s);
116 std::function<double(
double,
double)>
phi;
119 template <
class state,
class action,
class environment>
121 const state& from,
const state& to, std::vector<state> &thePath)
123 if (!InitializeSearch(env1, env2, h1, h2, from, to, thePath))
127 while (!DoSingleSearchStep(thePath))
132 template <
class state,
class action,
class environment>
135 const state& from,
const state& to, std::vector<state> &thePath)
149 allStates.push_back({start, 0, 0,
150 (float)h1->
HCost(start, goal),
151 (float)h2->
HCost(start, goal),
156 openDrawing.resize(e1->GetMaxHash());
157 std::fill(openDrawing.begin(), openDrawing.end(), -1);
158 openDrawing[e1->GetStateHash(start)]+=2;
163 template <
class state,
class action,
class environment>
167 size_t which = GetBestStateOnOpen();
169 if (which == allStates.size())
171 std::cout <<
"Search done; " << GetNodesExpanded() <<
" expanded;" << goals.size() <<
" paths found\n";
176 allStates[which].open =
false;
177 openDrawing[e1->GetStateHash(allStates[which].s)]--;
183 if (allStates[which].gCost2 >= hashTable[e1->GetStateHash(allStates[which].s)].gmin)
191 if (allStates[which].fCost2() >= hashTable[e1->GetStateHash(goal)].gmin)
197 hashTable[e1->GetStateHash(allStates[which].s)].gmin = allStates[which].gCost2;
200 if (e1->GoalTest(allStates[which].s, goal))
202 std::cout <<
"Adding goal " << allStates[which].s <<
" with costs {" <<
to_string_with_precision(allStates[which].gCost1, 10) <<
", " << allStates[which].gCost2 <<
"}\n";
203 goals.push_back(allStates[which]);
204 assert(allStates[which].open ==
false);
214 template <
class state,
class action,
class environment>
219 for (
size_t x = 0; x < allStates.size(); x++)
221 if (allStates[x].open)
229 return allStates.size();
231 for (
size_t x = which+1; x < allStates.size(); x++)
233 if (allStates[x].open &&
234 (
fless(allStates[x].fCost1(), allStates[which].fCost1()) ||
235 (
fequal(allStates[x].fCost1(), allStates[which].fCost1()) &&
236 fless(allStates[x].fCost2(), allStates[which].fCost2()))
249 template <
class state,
class action,
class environment>
253 e1->GetSuccessors(allStates[which].s, neighbors);
254 for (
int x = 0; x < neighbors.size(); x++)
259 static_cast<float>(allStates[which].gCost1+e1->GCost(allStates[which].s, neighbors[x])),
260 static_cast<float>(allStates[which].gCost2+e2->GCost(allStates[which].s, neighbors[x])),
261 static_cast<float>(h1->HCost(neighbors[x], goal)),
262 static_cast<float>(h2->HCost(neighbors[x], goal)),
268 if (i.
gCost2 >= hashTable[e1->GetStateHash(neighbors[x])].gmin)
275 if (i.
fCost2() >= hashTable[e1->GetStateHash(goal)].gmin)
281 allStates.push_back(i);
282 auto hash = e1->GetStateHash(i.
s);
284 if (openDrawing[hash] == 0)
290 template <
class state,
class action,
class environment>
294 for (
int x = 0; x < e1->GetMaxHash(); x++)
295 maxVal =
std::max(openDrawing[x], maxVal);
296 for (
int x = 0; x < e1->GetMaxHash(); x++)
298 if (openDrawing[x] == -1)
301 e1->GetStateFromHash(x, s);
302 if (openDrawing[x] > 0)
305 c *= openDrawing[x]/(float)maxVal;
330 template <
class state,
class action,
class environment>
333 static std::string s;
334 s = std::to_string(goals.size())+
" goals";
339 static std::string s;
344 goalDrawLocs.resize(0);
345 float minf1 = (float)h1->HCost(start, goal);
346 float maxf1 = (float)h1->HCost(start, goal);
347 float minf2 = (float)h2->HCost(start, goal);
348 float maxf2 = (float)h2->HCost(start, goal);
349 for (
int x = 0; x < goals.size(); x++)
351 minf1 =
std::min(minf1, goals[x].fCost1());
352 maxf1 =
std::max(maxf1, goals[x].fCost1());
353 minf2 =
std::min(minf2, goals[x].fCost2());
354 maxf2 =
std::max(maxf2, goals[x].fCost2());
360 const char o2[] =
"O\0b\0j\0e\0c\0t\0i\0v\0e\0 \0002\0";
361 for (
int y = 0; y < 11; y++)
373 for (
int x = 0; x < goals.size(); x++)
376 if (!
fequal(maxf1, minf1))
377 denom1 = maxf1-minf1;
379 if (!
fequal(maxf2, minf2))
380 denom2 = maxf2-minf2;
381 Graphics::point p(-1+2*(goals[x].fCost1()-minf1)/(denom1), 1-2*((goals[x].fCost2()-minf2)/(denom2)));
386 goalDrawLocs.emplace_back(p);
407 template <
class state,
class action,
class environment>
412 for (
int x = 0; x < goals.size(); x++)
414 int current = goals[x].parent;
415 e1->DrawLine(
d, goals[x].s, allStates[current].s, 4);
418 e1->DrawLine(
d, allStates[current].s, allStates[allStates[current].parent].s, 4);
419 current = allStates[current].parent;
424 template <
class state,
class action,
class environment>
431 int current = goals[x].parent;
432 e1->DrawLine(
d, goals[x].s, allStates[current].s);
435 e1->DrawLine(
d, allStates[current].s, allStates[allStates[current].parent].s,
width);
436 current = allStates[current].parent;
441 template <
class state,
class action,
class environment>
444 if (goals.size() < 1)
447 float dist = (goalDrawLocs[0]-p).squaredLength();
448 for (
int x = 1; x < goals.size(); x++)
450 float d = (goalDrawLocs[x]-p).squaredLength();
457 if (
fless(dist, limit*limit))