HOG2
NodeLimitAbstraction.cpp
Go to the documentation of this file.
1 /*
2  * NodeLimitAbstraction.cpp
3  * hog
4  *
5  * Created by Nathan Sturtevant on 3/24/07.
6  * Copyright 2007 __MyCompanyName__. All rights reserved.
7  *
8  * This file is part of HOG.
9  *
10  * HOG is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * HOG is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with HOG; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23  *
24  */
25 
26 
27 #include "NodeLimitAbstraction.h"
28 #include "Graph.h"
29 
30 using namespace GraphAbstractionConstants;
31 
33 :MapAbstraction(_m), nodeLimit(_NodeLimit)
34 {
35  assert(_m!=NULL);
36 
38 }
39 
41 {
42 }
43 
45 {
46  while (from != to)
47  {
48  if ((!from) || (!to) ||
49  (abstractions[from->GetLabelL(kAbstractionLevel)]->GetNumEdges() == 0))
50  return false;
51 
52  from = abstractions[from->GetLabelL(kAbstractionLevel)+1]->
53  GetNode(from->GetLabelL(kParent));
54  to = abstractions[to->GetLabelL(kAbstractionLevel)+1]->
55  GetNode(to->GetLabelL(kParent));
56  }
57  if ((from == 0) || (to == 0))
58  return false;
59  return true;
60 }
61 
62 // utility functions
65 {
66 }
67 
68 // hierarchical modifications
71 {
72 }
73 
76 {
77 }
78 
81 {
82  // add it to the
83  assert(false);
84 }
85 
87 void NodeLimitAbstraction::AddEdge(edge *, unsigned int)
88 {
89  assert(false);
90  // if each end is already connected who cares -- doesn't mess anything up -- just add it throughout
91 }
92 
96 {
97 }
98 
100 {
101  //inefficient for the moment
102  abstractions.push_back(GetMapGraph(GetMap()));
103  while (abstractions.back()->GetNumEdges() > 0)
104  {
105  Graph *g = new Graph();
106  addNodes(g);
107  addEdges(g);
108  abstractions.push_back(g);
109  }
110 }
111 
113 {
114  int count = abstractions.back()->GetNumNodes();
115  while (count > 0)
116  {
117  // select a random node
118  node *next = abstractions.back()->GetRandomNode();
119  assert(next!=NULL);
120  // if it isn't abstracted, do a bfs according to the depth and abstract these nodes together
121  if (next->GetLabelL(kParent) == -1)
122  {
123  node *parent;
124  g->AddNode(parent = new node("??"));
125  assert(parent!=NULL);
126  parent->SetLabelL(kAbstractionLevel, next->GetLabelL(kAbstractionLevel)+1); // level in abstraction tree
127  parent->SetLabelL(kNumAbstractedNodes, 0); // number of abstracted nodes
128  parent->SetLabelL(kParent, -1); // parent of this node in abstraction hierarchy
130  parent->SetLabelL(kNodeBlocked, 0);
131  abstractionBFS(next, parent, nodeLimit);
132  count-=parent->GetLabelL(kNumAbstractedNodes);
133  }
134  }
135 }
136 
138 {
139  Graph *g = abstractions.back();
140  edge_iterator ei = g->getEdgeIter();
141  for (edge *e = g->edgeIterNext(ei); e; e = g->edgeIterNext(ei))
142  {
143  int from = g->GetNode(e->getFrom())->GetLabelL(kParent);
144  int to = g->GetNode(e->getTo())->GetLabelL(kParent);
145  edge *f=0;
146 
147  if ((from != to) && (!(f = aGraph->FindEdge(to, from))))
148  {
149  double weight = h(aGraph->GetNode(from), aGraph->GetNode(to));
150  f = new edge(from, to, weight);
151  f->SetLabelL(kEdgeCapacity, 1);
152  aGraph->AddEdge(f);
153  }
154  else if (f) f->SetLabelL(kEdgeCapacity, f->GetLabelL(kEdgeCapacity)+1);
155  }
156 }
157 
158 void NodeLimitAbstraction::abstractionBFS(node *which, node *parent, int count)
159 {
160  std::vector<node *> q;
161  Graph *g = GetAbstractGraph(which);
162 
163  buildNodeIntoParent(which, parent);
164  count--;
165  q.push_back(which);
166  which->key = 0;
167 
168  for (unsigned int x = 0; x < q.size(); x++)
169  {
170  if (count <= 0)
171  break;
172 
173  neighbor_iterator ni = q[x]->getNeighborIter();
174  for (int next = q[x]->nodeNeighborNext(ni); next != -1; next = q[x]->nodeNeighborNext(ni))
175  {
176  if (count <= 0)
177  break;
178 
179  node *neighbor = g->GetNode(next);
180  // already in our q and handled
181  if ((neighbor->key < q.size()) && (q[neighbor->key] == neighbor))
182  {
183  continue;
184  }
185  // doesn't yet have a parent
186  if (neighbor->GetLabelL(kParent) == -1)
187  {
188  buildNodeIntoParent(neighbor, parent);
189  count--;
190  neighbor->key = q.size();
191  q.push_back(neighbor);
192  }
193  }
194  }
195 
196 // if ((which == 0) || (which->GetLabelL(kParent) != -1))
197 // return;
198 // buildNodeIntoParent(which, parent);
199 // count--;
200 // if (count < 0)
201 // return;
202 // neighbor_iterator ni = which->getNeighborIter();
203 // for (long tmp = which->nodeNeighborNext(ni); tmp != -1; tmp = which->nodeNeighborNext(ni))
204 // {
205 // abstractionBFS(abstractions.back()->GetNode(tmp), parent, depth-1);
206 // }
207 }
208 
210 {
211  assert(GetAbstractionLevel(n)+1 == GetAbstractionLevel(parent));
212  n->SetLabelL(kParent, parent->GetNum());
213  parent->SetLabelL(kFirstData+parent->GetLabelL(kNumAbstractedNodes), n->GetNum());
216 }
217 
NodeLimitAbstraction::buildAbstraction
void buildAbstraction()
Definition: NodeLimitAbstraction.cpp:99
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
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
NodeLimitAbstraction.h
graph_object::key
unsigned int key
Definition: Graph.h:54
NodeLimitAbstraction::nodeLimit
int nodeLimit
Definition: NodeLimitAbstraction.h:63
Graph::AddEdge
void AddEdge(edge *)
Definition: Graph.cpp:170
NodeLimitAbstraction::buildNodeIntoParent
void buildNodeIntoParent(node *n, node *parent)
Definition: NodeLimitAbstraction.cpp:209
edge_iterator
std::vector< edge * >::const_iterator edge_iterator
Definition: Graph.h:30
GraphAbstractionConstants::kEdgeCapacity
@ kEdgeCapacity
Definition: GraphAbstraction.h:53
NodeLimitAbstraction::addEdges
void addEdges(Graph *g)
Definition: NodeLimitAbstraction.cpp:137
NodeLimitAbstraction::RepairAbstraction
virtual void RepairAbstraction()
This must be called after any of the above add/remove operations.
Definition: NodeLimitAbstraction.cpp:95
NodeLimitAbstraction::Pathable
virtual bool Pathable(node *from, node *to)
Definition: NodeLimitAbstraction.cpp:44
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
NodeLimitAbstraction::NodeLimitAbstraction
NodeLimitAbstraction(Map *, int nodeLimit)
Definition: NodeLimitAbstraction.cpp:32
GraphAbstractionConstants::kNumAbstractedNodes
@ kNumAbstractedNodes
Definition: GraphAbstraction.h:26
edge::GetLabelL
long GetLabelL(unsigned int index) const
Definition: Graph.h:138
NodeLimitAbstraction::RemoveEdge
virtual void RemoveEdge(edge *e, unsigned int absLevel)
remove edge from abstraction
Definition: NodeLimitAbstraction.cpp:75
GetMapGraph
Graph * GetMapGraph(Map *m)
GetMapGraph(map)
Definition: MapAbstraction.cpp:312
Graph::getEdgeIter
edge_iterator getEdgeIter() const
Definition: Graph.cpp:315
GraphAbstractionConstants::kFirstData
@ kFirstData
Definition: GraphAbstraction.h:34
NodeLimitAbstraction::abstractionBFS
void abstractionBFS(node *which, node *parent, int depth)
Definition: NodeLimitAbstraction.cpp:158
NodeLimitAbstraction::VerifyHierarchy
virtual void VerifyHierarchy()
verify that the hierarchy is consistent
Definition: NodeLimitAbstraction.cpp:64
edge::SetLabelL
void SetLabelL(unsigned int index, long val)
Definition: Graph.cpp:598
GraphAbstractionConstants::kAbstractionLevel
@ kAbstractionLevel
Definition: GraphAbstraction.h:25
NodeLimitAbstraction::~NodeLimitAbstraction
~NodeLimitAbstraction()
Definition: NodeLimitAbstraction.cpp:40
NodeLimitAbstraction::AddNode
virtual void AddNode(node *n)
add node to abstraction
Definition: NodeLimitAbstraction.cpp:80
GraphAbstractionConstants::kXCoordinate
@ kXCoordinate
Definition: GraphAbstraction.h:30
NodeLimitAbstraction::addNodes
void addNodes(Graph *g)
Definition: NodeLimitAbstraction.cpp:112
GraphAbstractionConstants::kNodeBlocked
@ kNodeBlocked
Definition: GraphAbstraction.h:33
node::GetNum
unsigned int GetNum() const
Definition: Graph.h:176
GraphAbstractionConstants::kParent
@ kParent
Definition: GraphAbstraction.h:27
node::GetLabelL
long GetLabelL(unsigned int index) const
Definition: Graph.h:220
NodeLimitAbstraction::AddEdge
virtual void AddEdge(edge *e, unsigned int absLevel)
add edge to abstraction
Definition: NodeLimitAbstraction.cpp:87
GraphAbstractionConstants::kUnknownPosition
const double kUnknownPosition
Definition: GraphAbstraction.h:56
NodeLimitAbstraction::RemoveNode
virtual void RemoveNode(node *n)
remove node from abstraction
Definition: NodeLimitAbstraction.cpp:70
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