-
Notifications
You must be signed in to change notification settings - Fork 54
/
heuristic.cpp
50 lines (45 loc) · 1.34 KB
/
heuristic.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
#include "heuristic.h"
void Heuristic::init(unsigned int size, unsigned int agents)
{
h_values.clear();
h_values.resize(size);
for(unsigned int i = 0; i < size; i++)
h_values[i].resize(agents, -1);
}
void Heuristic::count(const Map &map, Agent agent)
{
Node curNode(agent.goal_id, 0, 0, agent.goal_i, agent.goal_j), newNode;
open.clear();
open.insert(curNode);
while(!open.empty())
{
curNode = find_min();
h_values[curNode.id][agent.id] = curNode.g;
std::vector<Node> valid_moves = map.get_valid_moves(curNode.id);
for(auto move: valid_moves)
{
newNode.i = move.i;
newNode.j = move.j;
newNode.id = move.id;
newNode.g = curNode.g + dist(curNode, newNode);
if(h_values[newNode.id][agent.id] < 0)
{
auto it = open.get<1>().find(newNode.id);
if(it != open.get<1>().end())
{
if(it->g > newNode.g)
open.get<1>().erase(it);
else
continue;
}
open.insert(newNode);
}
}
}
}
Node Heuristic::find_min()
{
Node min = *open.begin();
open.erase(open.begin());
return min;
}