HOG2
ClusterAbstraction.h
Go to the documentation of this file.
1 /*
2  * $Id: ClusterAbstraction.h
3  * hog2
4  *
5  * Created by Renee Jansen on 06/06/06.
6  * Modified by Nathan Sturtevant on 02/29/20.
7  *
8  * This file is part of HOG2. See https://github.com/nathansttt/hog2 for licensing information.
9  *
10  */
11 
12 #ifndef CLUSTERABSTRACTION_H
13 #define CLUSTERABSTRACTION_H
14 
15 #include "MapAbstraction.h"
16 #include "Graph.h"
17 #include "Path.h"
18 
19 #include <unordered_map>
20 #include <vector>
21 
23 
24 const int MAX_ENTRANCE_WIDTH = 6;
25 
26 /*
27  * Set up hash map to cache paths. These are used to easily find a map-level path
28  * from an abstract path. Paths are hashed by pointers to the abstract edges.
29  */
30 namespace clusterUtil {
31 
32  struct EdgeEqual {
33  bool operator()(const edge* e1, const edge* e2) const
34  {return e1->getEdgeNum() == e2->getEdgeNum();}
35  };
36 
37  struct EdgeHash {
38  size_t operator()(const edge *e) const
39  { return (size_t)(e->getEdgeNum()); }
40  };
41 
42  typedef std::unordered_map<edge*,path*,
45 }
46 
47 class Cluster {
48 public:
49  Cluster(int id, int row, int col, int horizOrigin, int vertOrigin,int width, int height)
50  :m_id(id),m_row(row),m_column(col),
51  m_horizOrigin(horizOrigin),m_vertOrigin(vertOrigin),
53  {}
54 
55  void AddNode(int);
56  int getIthNodeNum(int i) const { return nodes[i];}
57  int GetNumNodes() const { return nodes.size(); }
58  int getHOrig() const { return m_horizOrigin; }
59  int getVOrig() const { return m_vertOrigin; }
60  int GetHeight() const { return m_height; }
61  int getWidth() const { return m_width; }
62  void addParent(node* n) { parents.push_back(n); }
63  std::vector<node*>& getParents() { return parents; }
64  std::vector<node*> parents; // each connected component gets its own parent
65 
66 private:
67  int m_id;
68  int m_row; // abstract row of this cluster (e.g., 1 for the second clusters horizontally)
69  int m_column; // abstract col of this cluster (e.g., 1 for the second clusters vertically)
70  int m_horizOrigin; // horizontal/column origin
71  int m_vertOrigin; //vertical / row origin
72  int m_width; // width of this cluster
73  int m_height; // high of this cluster
74  std::vector<int> nodes; // node numbers in abstract Graph of the entrances
75 
76 
77 };
78 
79 class Entrance {
80 public:
81  Entrance(int center1Row, int center1Col,
82  int cl1Id, int cl2Id,
83  int center1Id, int center2Id,
84  int row, int col, int length,
85  Orientation orientation)
86  :m_center1Row(center1Row), m_center1Col(center1Col),
87  m_cluster1Id(cl1Id), m_cluster2Id(cl2Id),
88  m_center1Id(center1Id), m_center2Id(center2Id),
89  m_row(row), m_col(col), m_length(length),
90  m_orientation(orientation) {}
91 
92  int getOrientation() {return m_orientation;}
93  int getRow() {return m_row;}
94  int getCol() {return m_col;}
95  void setCluster1Id(int id) {m_cluster1Id=id;}
96  void setCluster2Id(int id) {m_cluster2Id=id;}
97  int getCenter1Row() { return m_center1Row; }
98  int getCenter1Col() { return m_center1Col; }
99  int getCluster1Id() { return m_cluster1Id; }
100  int getCluster2Id() { return m_cluster2Id; }
101 
102 
103 private:
104  int m_center1Row; //y-coordinate of entrance in top / left cluster
105  int m_center1Col; //x-coordinate of entrance in top / left cluster
106  int m_cluster1Id; //top / left cluster's id
107  int m_cluster2Id; //bottom / right cluster's id
108  int m_center1Id; //node number of the entrance in the top / left cluster
109  int m_center2Id; //node number of the entrance in the bottom / right cluster
110  int m_row; // abstract row of the leftmost adjacent cluster
111  int m_col; // abstract col of the uppermost adjacent cluster
112  int m_length; // length of the entrance
114 
115 };
116 
117 
122 class ClusterAbstraction : public MapAbstraction {
123 public:
124  ClusterAbstraction(Map *map, int _clusterSize);
126  MapAbstraction* Clone(Map* map)
127  { return new ClusterAbstraction(map, clusterSize); }
128 
129  int getClusterSize() { return clusterSize; };
130  bool Pathable(node* start, node* goal);
131  void VerifyHierarchy() {}
132  void removeNodes(node* start, node* goal);
133  void RemoveNode(node*) {}
134  void RemoveEdge(edge*, unsigned int) {}
135  void AddNode(node*) {}
136  void AddEdge(edge*, unsigned int) {}
138  node* insertNode(node* n, int& expanded, int& touched);
139  path* getCachedPath(edge* e);
140  node* getLowLevelNode(node* abstract);
141  int getClusterIdFromNode(node* n);
142 
143 
144  //move back to private later
145  int getClusterIdFromCoord(int row, int col) const;
146  void printMapCoord(node* n);
147  void printPathAsCoord(path* p);
148  virtual void OpenGLDraw() const;
149 
150 private:
151  int min(int, int);
153  void createAbstractGraph();
154  void addCluster(Cluster c);
155  void createHorizEntrances(int, int, int, int, int);
156  void createVertEntrances(int, int, int, int, int);
158  void addAbsNodes(Graph* g);
159  void computeClusterPaths(Graph* g);
160  void addEntrance(Entrance e);
161  int getClusterId(int row, int col) const;
162 
163  Cluster& getCluster(int id);
164 
166  int rows; //rows of clusters
167  int columns; //columns of clusters
168 
169  std::vector<Cluster> clusters;
170  std::vector<Entrance> entrances;
173  std::vector<path*> newPaths;
174  int nodeExists(const Cluster& c,double x,double y, Graph* g);
175  void setUpParents(Graph* g);
176 
177  void buildNodeIntoParent(node *n, node *parent);
178  void abstractionBFS(node *which, node *parent, int cluster,int numOrigNodes,int numNodesAfter);
180  void connectedBFS(node *which, node *parent);
181 };
182 
183 
184 
185 #endif
ClusterAbstraction::computeClusterPaths
void computeClusterPaths(Graph *g)
Definition: ClusterAbstraction.cpp:528
Entrance::setCluster2Id
void setCluster2Id(int id)
Definition: ClusterAbstraction.h:96
Entrance::getCol
int getCol()
Definition: ClusterAbstraction.h:94
ClusterAbstraction::addCluster
void addCluster(Cluster c)
Definition: ClusterAbstraction.cpp:202
Graph.h
clusterUtil::EdgeEqual::operator()
bool operator()(const edge *e1, const edge *e2) const
Definition: ClusterAbstraction.h:33
Entrance::getCluster1Id
int getCluster1Id()
Definition: ClusterAbstraction.h:99
ClusterAbstraction::getClusterSize
int getClusterSize()
Definition: ClusterAbstraction.h:129
ClusterAbstraction::RepairAbstraction
void RepairAbstraction()
Definition: ClusterAbstraction.h:137
ClusterAbstraction::getClusterIdFromCoord
int getClusterIdFromCoord(int row, int col) const
given a node's coordinates, find the cluster it's in
Definition: ClusterAbstraction.cpp:624
ClusterAbstraction::getLowLevelNode
node * getLowLevelNode(node *abstract)
get the map-level node that is at the (x,y) coordinates of the abstract node
Definition: ClusterAbstraction.cpp:1274
ClusterAbstraction::nodeExists
int nodeExists(const Cluster &c, double x, double y, Graph *g)
check if a node already exists in a certain cluster with a particular set of coordinates
Definition: ClusterAbstraction.cpp:649
ClusterAbstraction
Cluster abstraction for HPA* algorithm as described in (Botea,Mueller,Schaeffer 2004).
Definition: ClusterAbstraction.h:122
ClusterAbstraction::getCluster
Cluster & getCluster(int id)
get the cluster from it's ID
Definition: ClusterAbstraction.cpp:635
ClusterAbstraction::setUpParents
void setUpParents(Graph *g)
Nodes are assigned the closest entrance node in the abstract Graph as their parent.
Definition: ClusterAbstraction.cpp:695
ClusterAbstraction::createClustersAndEntrances
void createClustersAndEntrances()
create clusters on the abstract level and create corresponding entrances.
Definition: ClusterAbstraction.cpp:125
Entrance::getCenter1Col
int getCenter1Col()
Definition: ClusterAbstraction.h:98
ClusterAbstraction::rows
int rows
Definition: ClusterAbstraction.h:166
ClusterAbstraction::Pathable
bool Pathable(node *start, node *goal)
Check if there is a path between two nodes.
Definition: ClusterAbstraction.cpp:984
Cluster::m_height
int m_height
Definition: ClusterAbstraction.h:73
ClusterAbstraction::RemoveEdge
void RemoveEdge(edge *, unsigned int)
Definition: ClusterAbstraction.h:134
Cluster::getParents
std::vector< node * > & getParents()
Definition: ClusterAbstraction.h:63
Path.h
ClusterAbstraction::Clone
MapAbstraction * Clone(Map *map)
Definition: ClusterAbstraction.h:126
Cluster::Cluster
Cluster(int id, int row, int col, int horizOrigin, int vertOrigin, int width, int height)
Definition: ClusterAbstraction.h:49
ClusterAbstraction::linkEntrancesAndClusters
void linkEntrancesAndClusters()
from original HPA* abstiling.cpp
Definition: ClusterAbstraction.cpp:351
Cluster::AddNode
void AddNode(int)
Add an abstract node corresponding to an entrance to the cluster.
Definition: ClusterAbstraction.cpp:86
clusterUtil::PathLookupTable
std::unordered_map< edge *, path *, clusterUtil::EdgeHash, clusterUtil::EdgeEqual > PathLookupTable
Definition: ClusterAbstraction.h:44
Cluster::getHOrig
int getHOrig() const
Definition: ClusterAbstraction.h:58
ClusterAbstraction::createConnectivityGraph
void createConnectivityGraph()
Add a 2nd level to the Graph.
Definition: ClusterAbstraction.cpp:933
Entrance::Entrance
Entrance(int center1Row, int center1Col, int cl1Id, int cl2Id, int center1Id, int center2Id, int row, int col, int length, Orientation orientation)
Definition: ClusterAbstraction.h:81
ClusterAbstraction::connectedBFS
void connectedBFS(node *which, node *parent)
Definition: ClusterAbstraction.cpp:965
width
int width
Definition: SFML_HOG.cpp:54
ClusterAbstraction::clusterSize
int clusterSize
Definition: ClusterAbstraction.h:165
VERTICAL
@ VERTICAL
Definition: ClusterAbstraction.h:22
Entrance::m_center2Id
int m_center2Id
Definition: ClusterAbstraction.h:109
ClusterAbstraction::createAbstractGraph
void createAbstractGraph()
Definition: ClusterAbstraction.cpp:180
Graph
A generic Graph class.
Definition: Graph.h:66
Entrance::m_cluster2Id
int m_cluster2Id
Definition: ClusterAbstraction.h:107
Entrance::m_row
int m_row
Definition: ClusterAbstraction.h:110
clusterUtil::EdgeHash
Definition: ClusterAbstraction.h:37
Cluster::parents
std::vector< node * > parents
Definition: ClusterAbstraction.h:64
ClusterAbstraction::VerifyHierarchy
void VerifyHierarchy()
Definition: ClusterAbstraction.h:131
ClusterAbstraction::buildNodeIntoParent
void buildNodeIntoParent(node *n, node *parent)
Definition: ClusterAbstraction.cpp:999
ClusterAbstraction::columns
int columns
Definition: ClusterAbstraction.h:167
Cluster::getVOrig
int getVOrig() const
Definition: ClusterAbstraction.h:59
ClusterAbstraction::addEntrance
void addEntrance(Entrance e)
Definition: ClusterAbstraction.cpp:210
Cluster::m_horizOrigin
int m_horizOrigin
Definition: ClusterAbstraction.h:70
Cluster::m_column
int m_column
Definition: ClusterAbstraction.h:69
ClusterAbstraction::AddNode
void AddNode(node *)
Definition: ClusterAbstraction.h:135
Cluster::m_width
int m_width
Definition: ClusterAbstraction.h:72
Cluster::m_id
int m_id
Definition: ClusterAbstraction.h:67
Entrance::m_center1Col
int m_center1Col
Definition: ClusterAbstraction.h:105
Entrance::m_col
int m_col
Definition: ClusterAbstraction.h:111
Entrance::getOrientation
int getOrientation()
Definition: ClusterAbstraction.h:92
ClusterAbstraction::createHorizEntrances
void createHorizEntrances(int, int, int, int, int)
based on the function from HPA* abswizard.cpp with the same name
Definition: ClusterAbstraction.cpp:221
ClusterAbstraction::createVertEntrances
void createVertEntrances(int, int, int, int, int)
based on the function from HPA* abstiling.cpp with the same name
Definition: ClusterAbstraction.cpp:284
ClusterAbstraction::OpenGLDraw
virtual void OpenGLDraw() const
Definition: ClusterAbstraction.cpp:1323
Cluster::addParent
void addParent(node *n)
Definition: ClusterAbstraction.h:62
ClusterAbstraction::newPaths
std::vector< path * > newPaths
Definition: ClusterAbstraction.h:173
clusterUtil::EdgeHash::operator()
size_t operator()(const edge *e) const
Definition: ClusterAbstraction.h:38
ClusterAbstraction::abstractionBFS
void abstractionBFS(node *which, node *parent, int cluster, int numOrigNodes, int numNodesAfter)
'borrowed' from MapSectorAbstraction.cpp
Definition: ClusterAbstraction.cpp:911
Entrance::m_orientation
Orientation m_orientation
Definition: ClusterAbstraction.h:113
height
int height
Definition: SFML_HOG.cpp:54
clusterUtil::EdgeEqual
Definition: ClusterAbstraction.h:32
Entrance::setCluster1Id
void setCluster1Id(int id)
Definition: ClusterAbstraction.h:95
ClusterAbstraction::removeNodes
void removeNodes(node *start, node *goal)
remove a start or goal node after the search has been completed.
Definition: ClusterAbstraction.cpp:1169
ClusterAbstraction::RemoveNode
void RemoveNode(node *)
Definition: ClusterAbstraction.h:133
edge::getEdgeNum
int getEdgeNum() const
Definition: Graph.h:152
ClusterAbstraction::entrances
std::vector< Entrance > entrances
Definition: ClusterAbstraction.h:170
ClusterAbstraction::getClusterId
int getClusterId(int row, int col) const
given a cluster row and column (NOT map row/column), return the cluster's ID.
Definition: ClusterAbstraction.cpp:615
Entrance::m_length
int m_length
Definition: ClusterAbstraction.h:112
Orientation
Orientation
Definition: ClusterAbstraction.h:22
ClusterAbstraction::ClusterAbstraction
ClusterAbstraction(Map *map, int _clusterSize)
create a cluster abstraction for the given map.
Definition: ClusterAbstraction.cpp:95
Cluster::m_row
int m_row
Definition: ClusterAbstraction.h:68
ClusterAbstraction::temp
clusterUtil::PathLookupTable temp
Definition: ClusterAbstraction.h:172
Entrance::m_cluster1Id
int m_cluster1Id
Definition: ClusterAbstraction.h:106
Cluster::GetNumNodes
int GetNumNodes() const
Definition: ClusterAbstraction.h:57
ClusterAbstraction::AddEdge
void AddEdge(edge *, unsigned int)
Definition: ClusterAbstraction.h:136
Cluster::getWidth
int getWidth() const
Definition: ClusterAbstraction.h:61
Entrance
Definition: ClusterAbstraction.h:79
MAX_ENTRANCE_WIDTH
const int MAX_ENTRANCE_WIDTH
Definition: ClusterAbstraction.h:24
Cluster::GetHeight
int GetHeight() const
Definition: ClusterAbstraction.h:60
MapAbstraction.h
Entrance::getRow
int getRow()
Definition: ClusterAbstraction.h:93
Entrance::m_center1Row
int m_center1Row
Definition: ClusterAbstraction.h:104
ClusterAbstraction::~ClusterAbstraction
~ClusterAbstraction()
Definition: ClusterAbstraction.cpp:104
Entrance::getCluster2Id
int getCluster2Id()
Definition: ClusterAbstraction.h:100
Cluster::getIthNodeNum
int getIthNodeNum(int i) const
Definition: ClusterAbstraction.h:56
Cluster
Definition: ClusterAbstraction.h:47
ClusterAbstraction::printPathAsCoord
void printPathAsCoord(path *p)
Definition: ClusterAbstraction.cpp:1307
ClusterAbstraction::paths
clusterUtil::PathLookupTable paths
Definition: ClusterAbstraction.h:171
ClusterAbstraction::getClusterIdFromNode
int getClusterIdFromNode(node *n)
Given a map node, return the ID of the cluster it's in.
Definition: ClusterAbstraction.cpp:671
ClusterAbstraction::getCachedPath
path * getCachedPath(edge *e)
get the map-level path that corresponds to abstract edge e
Definition: ClusterAbstraction.cpp:1263
path
A linked list of nodes which form a continuous path.
Definition: Path.h:20
clusterUtil
Definition: ClusterAbstraction.h:30
Entrance::m_center1Id
int m_center1Id
Definition: ClusterAbstraction.h:108
ClusterAbstraction::printMapCoord
void printMapCoord(node *n)
Definition: ClusterAbstraction.cpp:1286
Cluster::nodes
std::vector< int > nodes
Definition: ClusterAbstraction.h:74
HORIZONTAL
@ HORIZONTAL
Definition: ClusterAbstraction.h:22
ClusterAbstraction::min
int min(int, int)
Definition: ClusterAbstraction.cpp:168
Entrance::getCenter1Row
int getCenter1Row()
Definition: ClusterAbstraction.h:97
ClusterAbstraction::insertNode
node * insertNode(node *n, int &expanded, int &touched)
insert a start or goal node in to the abstract Graph (for searching purposes).
Definition: ClusterAbstraction.cpp:1015
ClusterAbstraction::addAbsNodes
void addAbsNodes(Graph *g)
Add a node for cluster for each entrance.
Definition: ClusterAbstraction.cpp:392
ClusterAbstraction::clusters
std::vector< Cluster > clusters
Definition: ClusterAbstraction.h:169
node
Nodes to be stored within a Graph.
Definition: Graph.h:170
Map
A tile-based representation of the world.
Definition: Map.h:142
edge
Edge class for connections between node in a Graph.
Definition: Graph.h:129
Cluster::m_vertOrigin
int m_vertOrigin
Definition: ClusterAbstraction.h:71