HOG2
RadiusAbstraction.cpp
Go to the documentation of this file.
1 /*
2  * $Id: RadiusAbstraction.cpp
3  * hog2
4  *
5  * Created by Nathan Sturtevant on 6/3/05.
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 #include "RadiusAbstraction.h"
13 #include "Graph.h"
14 
15 using namespace GraphAbstractionConstants;
16 
18 :MapAbstraction(_m), radius(_radius)
19 {
20  assert(_m!=NULL);
21 
23 }
24 
26 {
27 }
28 
30 {
31  while (from != to)
32  {
33  if ((!from) || (!to) ||
34  (abstractions[from->GetLabelL(kAbstractionLevel)]->GetNumEdges() == 0))
35  return false;
36 
37  from = abstractions[from->GetLabelL(kAbstractionLevel)+1]->
38  GetNode(from->GetLabelL(kParent));
39  to = abstractions[to->GetLabelL(kAbstractionLevel)+1]->
40  GetNode(to->GetLabelL(kParent));
41  }
42  if ((from == 0) || (to == 0))
43  return false;
44  return true;
45 }
46 
47 // utility functions
50 {
51 }
52 
53 // hierarchical modifications
56 {
57 }
58 
60 void RadiusAbstraction::RemoveEdge(edge *, unsigned int)
61 {
62 }
63 
66 {
67  // add it to the
68  assert(false);
69 }
70 
72 void RadiusAbstraction::AddEdge(edge *, unsigned int)
73 {
74  assert(false);
75  // if each end is already connected who cares -- doesn't mess anything up -- just add it throughout
76 }
77 
81 {
82 }
83 
85 {
86  //inefficient for the moment
87  abstractions.push_back(GetMapGraph(GetMap()));
88  while (abstractions.back()->GetNumEdges() > 0)
89  {
90  Graph *g = new Graph();
91  addNodes(g);
92  addEdges(g);
93  abstractions.push_back(g);
94  }
95 }
96 
98 {
99  int count = abstractions.back()->GetNumNodes();
100  while (count > 0)
101  {
102  // select a random node
103  node *next = abstractions.back()->GetRandomNode();
104  assert(next!=NULL);
105  // if it isn't abstracted, do a bfs according to the depth and abstract these nodes together
106  if (next->GetLabelL(kParent) == -1)
107  {
108  node *parent;
109  g->AddNode(parent = new node("??"));
110  assert(parent!=NULL);
111  parent->SetLabelL(kAbstractionLevel, next->GetLabelL(kAbstractionLevel)+1); // level in abstraction tree
112  parent->SetLabelL(kNumAbstractedNodes, 0); // number of abstracted nodes
113  parent->SetLabelL(kParent, -1); // parent of this node in abstraction hierarchy
115  parent->SetLabelL(kNodeBlocked, 0);
116  abstractionBFS(next, parent, radius);
117  count-=parent->GetLabelL(kNumAbstractedNodes);
118  }
119  }
120 }
121 
123 {
124  Graph *g = abstractions.back();
125  edge_iterator ei = g->getEdgeIter();
126  for (edge *e = g->edgeIterNext(ei); e; e = g->edgeIterNext(ei))
127  {
128  int from = g->GetNode(e->getFrom())->GetLabelL(kParent);
129  int to = g->GetNode(e->getTo())->GetLabelL(kParent);
130  edge *f=0;
131 
132  if ((from != to) && (!(f = aGraph->FindEdge(to, from))))
133  {
134  double weight = h(aGraph->GetNode(from), aGraph->GetNode(to));
135  f = new edge(from, to, weight);
136  f->SetLabelL(kEdgeCapacity, 1);
137  aGraph->AddEdge(f);
138  }
139  else if (f) f->SetLabelL(kEdgeCapacity, f->GetLabelL(kEdgeCapacity)+1);
140  }
141 }
142 
143 void RadiusAbstraction::abstractionBFS(node *which, node *parent, int depth) // depth in edges...should we try literal distance?
144 {
145  if ((which == 0) || (which->GetLabelL(kParent) != -1))
146  return;
147  buildNodeIntoParent(which, parent);
148  if (depth < 0)
149  return;
150  neighbor_iterator ni = which->getNeighborIter();
151  for (long tmp = which->nodeNeighborNext(ni); tmp != -1; tmp = which->nodeNeighborNext(ni))
152  {
153  abstractionBFS(abstractions.back()->GetNode(tmp), parent, depth-1);
154  }
155 }
156 
158 {
159  assert(GetAbstractionLevel(n)+1 == GetAbstractionLevel(parent));
160  n->SetLabelL(kParent, parent->GetNum());
161  parent->SetLabelL(kFirstData+parent->GetLabelL(kNumAbstractedNodes), n->GetNum());
164 }
Graph::AddNode
int AddNode(node *)
Definition: Graph.cpp:136
GraphAbstractionConstants
Definition: GraphAbstraction.h:22
node::SetLabelL
void SetLabelL(unsigned int index, long val) const
Definition: Graph.cpp:702
Graph.h
node::SetLabelF
void SetLabelF(unsigned int index, double val) const
Definition: Graph.cpp:687
Graph::FindEdge
edge * FindEdge(unsigned int from, unsigned int to)
Finds an edge between nodes with ids from and to, no matter which direction.
Definition: Graph.cpp:230
neighbor_iterator
unsigned int neighbor_iterator
Definition: Graph.h:34
RadiusAbstraction::~RadiusAbstraction
~RadiusAbstraction()
Definition: RadiusAbstraction.cpp:25
RadiusAbstraction::addEdges
void addEdges(Graph *g)
Definition: RadiusAbstraction.cpp:122
Graph::AddEdge
void AddEdge(edge *)
Definition: Graph.cpp:170
RadiusAbstraction::RemoveNode
virtual void RemoveNode(node *n)
remove node from abstraction
Definition: RadiusAbstraction.cpp:55
edge_iterator
std::vector< edge * >::const_iterator edge_iterator
Definition: Graph.h:30
GraphAbstractionConstants::kEdgeCapacity
@ kEdgeCapacity
Definition: GraphAbstraction.h:53
Graph
A generic Graph class.
Definition: Graph.h:66
Graph::GetNode
node * GetNode(unsigned long num)
Definition: Graph.cpp:152
Graph::edgeIterNext
edge * edgeIterNext(edge_iterator &) const
Definition: Graph.cpp:320
GraphAbstractionConstants::kNumAbstractedNodes
@ kNumAbstractedNodes
Definition: GraphAbstraction.h:26
RadiusAbstraction::AddNode
virtual void AddNode(node *n)
add node to abstraction
Definition: RadiusAbstraction.cpp:65
edge::GetLabelL
long GetLabelL(unsigned int index) const
Definition: Graph.h:138
RadiusAbstraction::buildNodeIntoParent
void buildNodeIntoParent(node *n, node *parent)
Definition: RadiusAbstraction.cpp:157
GetMapGraph
Graph * GetMapGraph(Map *m)
GetMapGraph(map)
Definition: MapAbstraction.cpp:312
RadiusAbstraction::Pathable
virtual bool Pathable(node *from, node *to)
Definition: RadiusAbstraction.cpp:29
Graph::getEdgeIter
edge_iterator getEdgeIter() const
Definition: Graph.cpp:315
GraphAbstractionConstants::kFirstData
@ kFirstData
Definition: GraphAbstraction.h:34
edge::SetLabelL
void SetLabelL(unsigned int index, long val)
Definition: Graph.cpp:598
RadiusAbstraction::addNodes
void addNodes(Graph *g)
Definition: RadiusAbstraction.cpp:97
RadiusAbstraction.h
GraphAbstractionConstants::kAbstractionLevel
@ kAbstractionLevel
Definition: GraphAbstraction.h:25
node::nodeNeighborNext
int nodeNeighborNext(neighbor_iterator &) const
Definition: Graph.cpp:807
GraphAbstractionConstants::kXCoordinate
@ kXCoordinate
Definition: GraphAbstraction.h:30
GraphAbstractionConstants::kNodeBlocked
@ kNodeBlocked
Definition: GraphAbstraction.h:33
node::GetNum
unsigned int GetNum() const
Definition: Graph.h:176
RadiusAbstraction::buildAbstraction
void buildAbstraction()
Definition: RadiusAbstraction.cpp:84
RadiusAbstraction::VerifyHierarchy
virtual void VerifyHierarchy()
verify that the hierarchy is consistent
Definition: RadiusAbstraction.cpp:49
RadiusAbstraction::abstractionBFS
void abstractionBFS(node *which, node *parent, int depth)
Definition: RadiusAbstraction.cpp:143
RadiusAbstraction::radius
int radius
Definition: RadiusAbstraction.h:49
GraphAbstractionConstants::kParent
@ kParent
Definition: GraphAbstraction.h:27
node::GetLabelL
long GetLabelL(unsigned int index) const
Definition: Graph.h:220
node::getNeighborIter
neighbor_iterator getNeighborIter() const
Definition: Graph.cpp:802
GraphAbstractionConstants::kUnknownPosition
const double kUnknownPosition
Definition: GraphAbstraction.h:56
RadiusAbstraction::RemoveEdge
virtual void RemoveEdge(edge *e, unsigned int absLevel)
remove edge from abstraction
Definition: RadiusAbstraction.cpp:60
RadiusAbstraction::RadiusAbstraction
RadiusAbstraction(Map *, int)
Definition: RadiusAbstraction.cpp:17
RadiusAbstraction::RepairAbstraction
virtual void RepairAbstraction()
This must be called after any of the above add/remove operations.
Definition: RadiusAbstraction.cpp:80
RadiusAbstraction::AddEdge
virtual void AddEdge(edge *e, unsigned int absLevel)
add edge to abstraction
Definition: RadiusAbstraction.cpp:72
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