HOG2
MapLineAbstraction.cpp
Go to the documentation of this file.
1 /*
2  * MapLineAbstraction.cpp
3  * hog
4  *
5  * Created by Nathan Sturtevant on 11/10/06.
6  * Copyright 2006 __MyCompanyName__. All rights reserved.
7  *
8  */
9 
10 #include "MapLineAbstraction.h"
11 
12 using namespace GraphAbstractionConstants;
13 
14 MapLineAbstraction::MapLineAbstraction(Map *_map, int dist, bool uniform)
15 :MapAbstraction(_map), lineDistance(dist), abstractUniformly(uniform)
16 {
18 }
19 
21 {
22 }
23 
24 bool MapLineAbstraction::MapLineAbstraction::Pathable(node *from, node *to)
25 {
26  while (from != to)
27  {
28  if ((!from) || (!to) ||
29  (abstractions[from->GetLabelL(kAbstractionLevel)]->GetNumEdges() == 0))
30  return false;
31 
32  from = abstractions[from->GetLabelL(kAbstractionLevel)+1]->
33  GetNode(from->GetLabelL(kParent));
34  to = abstractions[to->GetLabelL(kAbstractionLevel)+1]->
35  GetNode(to->GetLabelL(kParent));
36  }
37  if ((from == 0) || (to == 0))
38  return false;
39  return true;
40 }
41 
42 // utility functions
45 {
46  assert(false); // not implemented
47 }
48 
50 {
51  assert(false); // not implemented
52 }
53 
54 void MapLineAbstraction::RemoveEdge(edge *, unsigned int)
55 {
56  assert(false); // not implemented
57 }
58 
60 {
61  assert(false); // not implemented
62 }
63 
64 void MapLineAbstraction::AddEdge(edge *, unsigned int)
65 {
66  assert(false); // not implemented
67 }
68 
70 {
71  assert(false); // not implemented
72 }
73 
75 {
76  abstractions.push_back(GetMapGraph(GetMap()));
77  while (abstractions.back()->GetNumEdges() > 0)
78  {
79 // printf("%d nodes and %d edges in Graph %d\n",
80 // abstractions.back()->GetNumNodes(),
81 // abstractions.back()->GetNumEdges(),
82 // abstractions.size()-1);
83  fflush(stdout);
84  Graph *g = new Graph();
85  addNodes(g);
86  addEdges(g);
87  abstractions.push_back(g);
88  }
89 // printf("%d nodes and %d edges in Graph %d\n",
90 // abstractions.back()->GetNumNodes(),
91 // abstractions.back()->GetNumEdges(),
92 // abstractions.size()-1);
93 }
94 
96 {
97  int count = abstractions.back()->GetNumNodes();
98  //printf("Initial count: %d\n", count);
99  int xstep = pow(lineDistance,((abstractions.size()+1)/2));
100  int ystep = pow(lineDistance,((abstractions.size())/2));
101  int zstep = pow(lineDistance,((abstractions.size()-1)/2));
102  Graph *toAbstract = abstractions.back();
103  //printf("Size: %d.[%d] XStep: %d, YStep: %d, ZStep %d\n", abstractions.size(), lineDistance, xstep, ystep, zstep);
104 
105  if (abstractUniformly)
106  {
107  for (int x = 0; x < GetMap()->GetMapWidth(); x += xstep)
108  {
109  for (int y = 0; y < GetMap()->GetMapHeight(); y+= ystep)
110  {
111  //printf("Next check starts from (%d, %d)\n", x, y);
112  std::vector<node *> nodes;
113  std::vector<node *> parents;
114  for (int z = 0; z < lineDistance; z++)
115  {
116  if ((abstractions.size()%2) != 0) // vertical abstraction
117  {
118  //printf("->(%d, %d)\n", x+z*zstep, y);
119  nodes.push_back(GetNodeFromMap(x+z*zstep, y));
120  }
121  else {
122  //printf("->(%d, %d)\n", x, y+z*zstep);
123  nodes.push_back(GetNodeFromMap(x, y+z*zstep));
124  }
125 
126  if (nodes.back() == 0)
127  {
128  //printf("(null)\n");
129  nodes.resize(0);
130  break;
131  }
132  parents.push_back(GetNthParent(nodes.back(), abstractions.size()-1));
133  if ((parents.back() == 0) ||
134  (parents.back()->GetLabelL(kParent) != -1) ||
135  ((z > 0) && (!toAbstract->FindEdge(parents[z]->GetNum(), parents[z-1]->GetNum()))))
136  {
137  //if (parents.back() == 0) printf("(null)\n");
138  //else if (parents.back()->GetLabelL(kParent) != -1) printf("(parent)\n");
139  //else printf("(disconnected)\n");
140  parents.resize(0);
141  break;
142  }
143  }
144  if ((nodes.size() == 0) || (parents.size() == 0))
145  continue;
146  //printf("Match!\n");
147  node *parent = createParent(g, parents[0]);
148  //printf("-->Creating parent (%d)\n", parent->GetNum());
149  for (unsigned int z = 0; z < parents.size(); z++)
150  {
151  if (parents[z]->GetLabelL(kParent) != -1)
152  break;
153  buildNodeIntoParent(parents[z], parent);
154  count--;
155  //printf("Abstracting %d, count now %d\n", parents[z]->GetNum(), count);
156  }
157  }
158  }
159  }
160 
161 // node_iterator ni = toAbstract->getNodeIter();
162 // for (node *n = toAbstract->nodeIterNext(ni); n; n = toAbstract->nodeIterNext(ni))
163  std::vector<node *> stopList; // nodes with no parents aren't abstracted
164  while (count > 0)
165  {
166  // select a random node
167  node *n = abstractions.back()->GetRandomNode();
168  assert(n!=NULL);
169  if ((n->GetLabelL(kParent) == -1) && (n->GetNumEdges() != 0))
170  {
171  node *parent = createParent(g, n);
172  //printf("==>Creating parent (%d)\n", parent->GetNum());
173  buildNodeIntoParent(n, parent);
174  count -= 1;
175  //printf("Abstracting %d, count now %d\n", n->GetNum(), count);
176 
177  for (int x = 0; x < lineDistance - 1; x++)
178  {
179  node *choice = 0;
180  double prob = 0;
182  for (long tmp = n->nodeNeighborNext(nbi); tmp != -1; tmp = n->nodeNeighborNext(nbi))
183  {
184  node *neighbor = toAbstract->GetNode(tmp);
185  if (neighbor->GetLabelL(kParent) == -1)
186  {
187  prob++;
188  if ((random()%100) < 100.0/prob)
189  {
190  choice = neighbor;
191  }
192  }
193  }
194  if (choice)
195  {
196  buildNodeIntoParent(choice, parent);
197  count -= 1;
198  n = choice;
199  //printf("Abstracting %d, count now %d\n", n->GetNum(), count);
200  }
201  else {
202  break;
203  }
204  }
205 
206  }
207  else if (n->GetNumEdges() == 0)
208  {
209  if ((n->key < stopList.size()) && (stopList[n->key] == n))
210  {
211  //printf("Already Removed %d from consideration\n", n->GetNum());
212  }
213  else {
214  //printf("Removing %d from consideration\n", n->GetNum());
215  count--;
216  n->key = stopList.size();
217  stopList.push_back(n);
218  //printf("Abstracting [%d], count now %d\n", n->GetNum(), count);
219  }
220  }
221  }
222 }
223 
225 {
226  Graph *g = abstractions.back();
227  edge_iterator ei = g->getEdgeIter();
228  for (edge *e = g->edgeIterNext(ei); e; e = g->edgeIterNext(ei))
229  {
230  int from = g->GetNode(e->getFrom())->GetLabelL(kParent);
231  int to = g->GetNode(e->getTo())->GetLabelL(kParent);
232  edge *f=0;
233 
234  if ((from == -1) || (to == -1))
235  {
236  printf("Error, (%d parent %d) or (%d parent %d) didn't get abstracted!\n",
237  e->getFrom(), from, e->getTo(), to);
238  assert(false);
239  }
240  if ((from != to) && (!(f = aGraph->FindEdge(to, from))))
241  {
242  double weight = h(aGraph->GetNode(from), aGraph->GetNode(to));
243  f = new edge(from, to, weight);
244  f->SetLabelL(kEdgeCapacity, 1);
245  aGraph->AddEdge(f);
246  }
247  else if (f) f->SetLabelL(kEdgeCapacity, f->GetLabelL(kEdgeCapacity)+1);
248  }
249 }
250 
252 {
253  assert(GetAbstractionLevel(n)+1 == GetAbstractionLevel(parent));
254  n->SetLabelL(kParent, parent->GetNum());
255  parent->SetLabelL(kFirstData+parent->GetLabelL(kNumAbstractedNodes), n->GetNum());
258 }
259 
261 {
262  node *parent;
263  g->AddNode(parent = new node("??"));
264  assert(parent!=NULL);
265  parent->SetLabelL(kAbstractionLevel, n->GetLabelL(kAbstractionLevel)+1); // level in abstraction tree
266  parent->SetLabelL(kNumAbstractedNodes, 0); // number of abstracted nodes
267  parent->SetLabelL(kParent, -1); // parent of this node in abstraction hierarchy
269  parent->SetLabelL(kNodeBlocked, 0);
270  return parent;
271 }
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
MapLineAbstraction::createParent
node * createParent(Graph *g, node *n)
Definition: MapLineAbstraction.cpp:260
node::SetLabelF
void SetLabelF(unsigned int index, double val) const
Definition: Graph.cpp:687
graphMove
Definition: GraphEnvironment.h:34
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
graph_object::key
unsigned int key
Definition: Graph.h:54
MapLineAbstraction::abstractUniformly
bool abstractUniformly
Definition: MapLineAbstraction.h:49
Graph::AddEdge
void AddEdge(edge *)
Definition: Graph.cpp:170
MapLineAbstraction::AddNode
virtual void AddNode(node *n)
add node to abstraction
Definition: MapLineAbstraction.cpp:59
MapLineAbstraction::lineDistance
int lineDistance
Definition: MapLineAbstraction.h:48
edge_iterator
std::vector< edge * >::const_iterator edge_iterator
Definition: Graph.h:30
MapLineAbstraction::buildNodeIntoParent
void buildNodeIntoParent(node *n, node *parent)
Definition: MapLineAbstraction.cpp:251
GraphAbstractionConstants::kEdgeCapacity
@ kEdgeCapacity
Definition: GraphAbstraction.h:53
neighbor
graphMove neighbor
Definition: RoadMap.h:17
MapLineAbstraction::RepairAbstraction
virtual void RepairAbstraction()
This must be called after any of the above add/remove operations.
Definition: MapLineAbstraction.cpp:69
MapLineAbstraction::addEdges
void addEdges(Graph *g)
Definition: MapLineAbstraction.cpp:224
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
MapLineAbstraction.h
edge::GetLabelL
long GetLabelL(unsigned int index) const
Definition: Graph.h:138
GetMapGraph
Graph * GetMapGraph(Map *m)
GetMapGraph(map)
Definition: MapAbstraction.cpp:312
Graph::getEdgeIter
edge_iterator getEdgeIter() const
Definition: Graph.cpp:315
MapLineAbstraction::AddEdge
virtual void AddEdge(edge *e, unsigned int absLevel)
add edge to abstraction
Definition: MapLineAbstraction.cpp:64
GraphAbstractionConstants::kFirstData
@ kFirstData
Definition: GraphAbstraction.h:34
MapLineAbstraction::RemoveNode
virtual void RemoveNode(node *n)
remove node from abstraction
Definition: MapLineAbstraction.cpp:49
edge::SetLabelL
void SetLabelL(unsigned int index, long val)
Definition: Graph.cpp:598
MapLineAbstraction::VerifyHierarchy
virtual void VerifyHierarchy()
verify that the hierarchy is consistent
Definition: MapLineAbstraction.cpp:44
GraphAbstractionConstants::kAbstractionLevel
@ kAbstractionLevel
Definition: GraphAbstraction.h:25
MapLineAbstraction::RemoveEdge
virtual void RemoveEdge(edge *e, unsigned int absLevel)
remove edge from abstraction
Definition: MapLineAbstraction.cpp:54
MapLineAbstraction::buildAbstraction
void buildAbstraction()
Definition: MapLineAbstraction.cpp:74
MapLineAbstraction::~MapLineAbstraction
~MapLineAbstraction()
Definition: MapLineAbstraction.cpp:20
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
MapLineAbstraction::addNodes
void addNodes(Graph *g)
Definition: MapLineAbstraction.cpp:95
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
MapLineAbstraction::MapLineAbstraction
MapLineAbstraction(Map *, int dist=2, bool uniform=true)
Definition: MapLineAbstraction.cpp:14
node::GetNumEdges
int GetNumEdges()
Definition: Graph.h:204
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