HOG2
SearchAlgorithm.cpp
Go to the documentation of this file.
1 /*
2  * $Id: SearchAlgorithm.cpp
3  * hog2
4  *
5  * Created by Nathan Sturtevant on 9/28/04.
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 //#ifdef OS_MAC
13 //#include <CoreServices/CoreServices.h>
14 //#endif
15 
16 #include "SearchAlgorithm.h"
17 #include <sys/time.h>
18 #include <sys/resource.h>
19 #include <stdint.h>
20 
21 using namespace GraphAbstractionConstants;
22 using namespace std;
23 
24 static const int verbose = 0;
25 
26 void DoRandomPath(GraphAbstraction *aMap, SearchAlgorithm *sa, bool repeat)
27 {
28  static double lastLength, lastTime;
29  static node *r1 = 0, *r2 = 0;
30  Graph *g = aMap->GetAbstractGraph(0);
31  //if (verbose) cout << "Clearing marked nodes" << endl;
32  //aMap->ClearMarkedNodes();
33 
34  // r1 = ((MapAbstraction*)aMap)->GetNodeFromMap(257, 449);
35  // r2 = ((MapAbstraction*)aMap)->GetNodeFromMap(319, 458);
36  if ((!repeat) || (r1 == 0) || (r2 == 0))
37  {
38  lastLength = 0;
39  lastTime = 1;
40  do {
41  // do {
42  r1 = g->GetRandomNode();
43  // } while (aMap->GetMap()->GetTerrainType((long)r1->GetLabelL(kFirstData), (long)r1->GetLabelL(kFirstData+1)) == kOutOfBounds);
44  // do {
45  r2 = g->GetRandomNode();
46  // } while (aMap->GetMap()->GetTerrainType((long)r2->GetLabelL(kFirstData), (long)r2->GetLabelL(kFirstData+1)) == kOutOfBounds);
47  } while (!aMap->Pathable(r1, r2));
48  }
49 
50  if (verbose)
51  {
52  cout << "Attempting path between nodes:" << endl;
53  cout << (*r1) << endl << (*r2) << endl;
54  }
55 
56  // while (r1 != r2)
57  // {
58  // r1 = getPathStep(r1, r2);
59  // if (verbose)
60  // cout << "Stepping to " << (*r1) << endl;
61  // }
62 
63  // ignoring return value! Leaking memory!
64 
65 #ifdef OS_MAC
66  AbsoluteTime startTime = UpTime();
67 #else
68  clock_t startTime, endTime;
69  long double duration;
70  startTime = clock();
71 
72 #endif
73 
74 
75  path *p;
76 
77  p = sa->GetPath(aMap, r1, r2);
78  // if (optimal)
79  // //p = getLibraryPath(r1, r2);
80  // p = GetPath(r1, r2);
81  // else
82  // //p = getLibraryPath(r1, r2);
83  // p = getApproximatePath(r1, r2);
84  // // while (r1 != r2)
85  // // r1 = getPathStep(r1, r2);
86 
87 
88 #ifdef OS_MAC
89  AbsoluteTime stopTime = UpTime();
90  Nanoseconds diff = AbsoluteDeltaToNanoseconds(stopTime, startTime);
91  uint64_t nanosecs = UnsignedWideToUInt64(diff);
92  //cout << nanosecs << " ns elapsed (" << (double)nanosecs/1000000.0 << " ms)" << endl;
93 #else
94  endTime = clock();
95  duration=(long double)(endTime-startTime)/CLOCKS_PER_SEC;
96  //cout << duration << " seconds elapsed" << endl;
97 #endif
98 
99  int cnt = 0;
100  double length = 0;
101  for (path *q = p; q; q = q->next)
102  {
103  if (q && q->next)
104  {
105  double t1, t2;
106  t1 = q->n->GetLabelL(kFirstData)-q->next->n->GetLabelL(kFirstData);
107  t2 = q->n->GetLabelL(kFirstData+1)-q->next->n->GetLabelL(kFirstData+1);
108  length += sqrt(t1*t1+t2*t2);
109  }
110  cnt++;
111  }
112 
113 #ifdef OS_MAC
114  cout << "Steps: " << cnt << ", len: " << length << ", time: " << (double)nanosecs/1000000.0
115  ;//<< ", time/step: " << (double)nanosecs/(1000*cnt) << ", time/unit: " << (double)nanosecs/(1000*length);
116  cout << "ms, h() = " << aMap->h(r1, r2) << ", nodes: " << sa->GetNodesExpanded() << endl;
117 
118  //cout << "DATA\t" << nanosecs << "\t" << length << endl;
119  if (!repeat)
120  {
121  lastLength = length;
122  lastTime = (double)nanosecs;
123  } else {
124  cout << "Comparison: " << lastLength/length << "x longer; but " << (double)nanosecs/lastTime << "x faster." << endl;
125  }
126 #endif
127 
128  //aMap->clearDisplayLists();
129 }
GraphAbstraction
A generic class for basic operations on Graph abstractions.
Definition: GraphAbstraction.h:63
GraphAbstractionConstants
Definition: GraphAbstraction.h:22
verbose
static const int verbose
Definition: SearchAlgorithm.cpp:24
GraphAbstraction::GetAbstractGraph
Graph * GetAbstractGraph(int level)
return the abstract Graph at the given level
Definition: GraphAbstraction.h:74
SearchAlgorithm.h
SearchAlgorithm::GetNodesExpanded
uint64_t GetNodesExpanded()
Definition: SearchAlgorithm.h:31
Graph
A generic Graph class.
Definition: Graph.h:66
GraphAbstraction::h
virtual double h(node *a, node *b)=0
heuristic cost between any two nodes
GraphAbstraction::Pathable
virtual bool Pathable(node *from, node *to)=0
is there a legal path between these 2 nodes?
GraphAbstractionConstants::kFirstData
@ kFirstData
Definition: GraphAbstraction.h:34
SearchAlgorithm::GetPath
virtual path * GetPath(GraphAbstraction *aMap, node *from, node *to, reservationProvider *rp=0)=0
DoRandomPath
void DoRandomPath(GraphAbstraction *aMap, SearchAlgorithm *sa, bool repeat)
Definition: SearchAlgorithm.cpp:26
std
Definition: CanonicalGraphEnvironment.h:26
Graph::GetRandomNode
node * GetRandomNode()
Definition: Graph.cpp:281
SearchAlgorithm
A generic algorithm which can be used for pathfinding.
Definition: SearchAlgorithm.h:25
path::next
path * next
Definition: Path.h:23
path
A linked list of nodes which form a continuous path.
Definition: Path.h:20
node
Nodes to be stored within a Graph.
Definition: Graph.h:170