-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMAPPGridState.hpp
103 lines (90 loc) · 2.99 KB
/
MAPPGridState.hpp
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
#pragma once
#include "Agent.hpp"
#include "Wall.hpp"
#include "porting.hpp"
using namespace porting;
class MAPPGridState
{
/*
* The grid state represents the actual state of the grid. It needs a list
* of agents, a list of wall positions, the x size of the grid and the
* y size of the grid.
*/
public:
MAPPGridState( vector<Agent>&agents, unsigned int xsize, unsigned int ysize, unsigned int cost );
inline unsigned int getF() const
{
return currentCost + currentHeuristic;
}
inline unsigned int getH() const
{
return currentHeuristic;
}
inline void setCost( unsigned int c ){ currentCost = c; }
inline unsigned int getXsize()const{ return xsize; }
inline unsigned int getYsize()const{ return ysize; }
static bool hasWallAt( int x, int y )
{
/*
* Check if there are walls at specific location
*/
for( const auto &i : walls )
{
if( i.getX() == x && i.getY() == y )
{
return true;
}
}
return false;
}
void succCoords( vector<Agent> &ret, const Agent &a ) const;
inline bool sameCoord( const Agent &a, const Agent &b )
{
/*
* Test if two agents have the same coordinates
*/
if( ( a.getX() == b.getX() ) && ( a.getY() == b.getY() ) )
{
return true;
}
return false;
}
bool goodSuccessor( const vector<Agent> &successor );
vector<MAPPGridState> successors();
void show() const;
bool operator ==( const MAPPGridState &a)const;
static vector<Wall>walls;
friend less<MAPPGridState>;
friend hash<MAPPGridState>;
private:
vector<Agent>agents;
unsigned int xsize, ysize, numberAgents, currentCost, currentHeuristic;
};
namespace std
{
/* Template specialisation for MAPPGridState priority_queue comparison */
template<>
struct less<MAPPGridState>
{
bool operator()( MAPPGridState const &s1, MAPPGridState const &s2 )
{
return s1.getF() > s2.getF();
}
};
/* Template specialisation for MAPPGridState hash function*/
template<>
struct hash<MAPPGridState>
{
size_t operator()(const MAPPGridState &k) const
{
size_t hash = 0;
unsigned int xsize = k.getXsize();
unsigned int ysize = k.getYsize();
for( const Agent &agent : k.agents )
{
hash = hash * (xsize * ysize) + agent.getX() + xsize * agent.getY();
}
return hash;
}
};
}