HOG2
PathGeneration.cpp
Go to the documentation of this file.
1 /*
2  * $Id: pathGeneration.cpp
3  * hog2
4  *
5  * Created by Nathan Sturtevant on 11/30/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 "PathGeneration.h"
13 #include "Map.h"
14 #include "AStar.h"
15 #include "MapFlatAbstraction.h"
16 
17 using namespace GraphAbstractionConstants;
18 
19 // outputs algorithm/path length/time per step
20 void generatePaths(char *_map, int mapSizeX, int mapSizeY, int numBuckets, int bucketSize, int pathsPerBucket)
21 {
22 // int numBuckets = (int)(mapSize/4.0);
23 // const int pathsPerBucket = 10;
24 
25  int pathsLeft = numBuckets*pathsPerBucket;
26  int iterations = 0;
27  int maxIterations = 10*numBuckets*pathsPerBucket;
28  Map *map = new Map(_map);
29  if ((mapSizeX != -1) && (mapSizeY != -1))
30  map->Scale(mapSizeX, mapSizeY);
31  else {
32  mapSizeX = map->GetMapWidth();
33  mapSizeY = map->GetMapHeight();
34  }
35 
36  MapFlatAbstraction *absMap = new MapFlatAbstraction(map);
37  Graph *g = absMap->GetAbstractGraph(0);
38 
39  node *r1, *r2;
40  // 10 maps/bigMaps/600.map 48 151 55 152 7.4142135624
41  // bucket, map, sizex, sizey, startx, starty, endx, endy, a* length
42  printf("# bucket\tmap\tsizex\tsizey\tfromx\tfromy\ttox\ttoy\tA*len\n");
43  std::vector<int> buckets(numBuckets);
44  for (int x = 0; x < numBuckets; x++)
45  buckets[x] = 0;
46  while ((pathsLeft > 0) && (iterations < maxIterations))
47  {
48  iterations++;
49  // try to find two valid points
50  do {
51  r1 = g->GetRandomNode();
52  r2 = g->GetRandomNode();
53  } while (!absMap->Pathable(r1, r2) || (r1 == r2) ||
54  ((iterations > maxIterations/2) && (absMap->h(r1, r2) > numBuckets*bucketSize)));
55 
56  aStar a;
57 
58  path *p;
59  p = a.GetPath(absMap, r1, r2);
60 
61  int cnt = 0;
62  double length = 0;
63  for (path *q = p; q; q = q->next)
64  {
65  if (q && q->next)
66  {
67  double t1, t2;
68  t1 = q->n->GetLabelL(kFirstData)-q->next->n->GetLabelL(kFirstData);
69  t2 = q->n->GetLabelL(kFirstData+1)-q->next->n->GetLabelL(kFirstData+1);
70  length += sqrt(t1*t1+t2*t2);
71  }
72  cnt++;
73  }
74  if ((length > numBuckets*bucketSize) ||
75  ((iterations > maxIterations/2) && (buckets[(int)length/bucketSize] == pathsPerBucket)))
76  {
77  while (p && p->next)
78  {
79  double t1, t2;
81  t2 = p->n->GetLabelL(kFirstData+1)-p->next->n->GetLabelL(kFirstData+1);
82  length -= sqrt(t1*t1+t2*t2);
83  path *t = p;
84  p = p->next;
85  t->next = 0; delete t;
86  if (p->next == 0)
87  {
88  delete p;
89  p = 0;
90  }
91  if (p == 0)
92  break;
93  if (buckets[(int)length/numBuckets] < pathsPerBucket)
94  break;
95  }
96  if (p && p->next)
97  {
98  r1 = p->n;
99  for (path *q = p; q; q = q->next)
100  {
101  r2 = q->n;
102  }
103  delete p;
104 
105  p = a.GetPath(absMap, r1, r2);
106 
107  length = 0; cnt = 0;
108  for (path *q = p; q; q = q->next)
109  {
110  if (q && q->next)
111  {
112  double t1, t2;
113  t1 = q->n->GetLabelL(kFirstData)-q->next->n->GetLabelL(kFirstData);
114  t2 = q->n->GetLabelL(kFirstData+1)-q->next->n->GetLabelL(kFirstData+1);
115  length += sqrt(t1*t1+t2*t2);
116  }
117  cnt++;
118  }
119  }
120  }
121  if (p == 0)
122  continue;
123  if ((length/bucketSize < numBuckets) && (buckets[(int)length/bucketSize] < pathsPerBucket))
124  {
125  buckets[(int)length/bucketSize]++;
126  pathsLeft--;
127  }
128  else {
129  continue;
130  }
131 
132  int x1, x2, y1, y2;
133  x1 = r1->GetLabelL(kFirstData); y1 = r1->GetLabelL(kFirstData+1);
134  x2 = r2->GetLabelL(kFirstData); y2 = r2->GetLabelL(kFirstData+1);
135  printf("%d\t%s\t%d\t%d\t%d\t%d\t%d\t%d\t%1.2f\n",
136  (int)(length/bucketSize), _map, mapSizeX, mapSizeY, x1, y1, x2, y2, length);
137  delete p;
138  }
139  delete absMap;
140 }
GraphAbstractionConstants
Definition: GraphAbstraction.h:22
MapFlatAbstraction
Definition: MapFlatAbstraction.h:17
path::n
node * n
Definition: Path.h:22
aStar
Definition: AStar.h:81
Graph
A generic Graph class.
Definition: Graph.h:66
Map::Scale
void Scale(long newWidth, long newHeight)
Definition: Map.cpp:177
GraphAbstractionConstants::kFirstData
@ kFirstData
Definition: GraphAbstraction.h:34
Map::GetMapWidth
long GetMapWidth() const
return the width of the map
Definition: Map.h:163
numBuckets
const int numBuckets
Definition: EnvUtil.h:20
PathGeneration.h
MapFlatAbstraction::Pathable
virtual bool Pathable(node *from, node *to)
Definition: MapFlatAbstraction.cpp:59
Graph::GetRandomNode
node * GetRandomNode()
Definition: Graph.cpp:281
Map::GetMapHeight
long GetMapHeight() const
return the height of the map
Definition: Map.h:165
aStar::GetPath
path * GetPath(GraphAbstraction *aMap, node *from, node *to, reservationProvider *rp=0)
Definition: AStar.cpp:31
Map.h
path::next
path * next
Definition: Path.h:23
node::GetLabelL
long GetLabelL(unsigned int index) const
Definition: Graph.h:220
path
A linked list of nodes which form a continuous path.
Definition: Path.h:20
AStar.h
generatePaths
void generatePaths(char *_map, int mapSizeX, int mapSizeY, int numBuckets, int bucketSize, int pathsPerBucket)
Definition: PathGeneration.cpp:20
MapFlatAbstraction.h
node
Nodes to be stored within a Graph.
Definition: Graph.h:170
Map
A tile-based representation of the world.
Definition: Map.h:142