forked from leethomason/MicroPather
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeed.cpp
354 lines (299 loc) · 9.84 KB
/
speed.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
Copyright (c) 2000-2012 Lee Thomason (www.grinninglizard.com)
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any
damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must
not claim that you wrote the original software. If you use this
software in a product, an acknowledgment in the product documentation
would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#include <ctype.h>
#include <stdio.h>
#include <memory.h>
#include <math.h>
#include <time.h>
#include <limits.h>
#include <vector>
#include "micropather.h"
using namespace micropather;
//#define PROFILING_RUN
#ifdef _MSC_VER
typedef unsigned __int64 U64;
#else
typedef unsigned long long U64;
#endif
typedef unsigned int U32;
#ifdef _MSC_VER
inline U64 FastTime()
{
union
{
U64 result;
struct
{
U32 lo;
U32 hi;
} split;
} u;
u.result = 0;
_asm {
//pushad; // don't need - aren't using "emit"
cpuid; // force all previous instructions to complete - else out of order execution can confuse things
rdtsc;
mov u.split.hi, edx;
mov u.split.lo, eax;
//popad;
}
return u.result;
}
#else
#define rdtscll(val) __asm__ __volatile__ ("rdtsc" : "=A" (val))
inline U64 FastTime() {
U64 val;
rdtscll( val );
return val;
}
#endif
inline int TimeDiv( U64 time, int count )
{
if ( count == 0 ) count = 1;
U64 timePer = time / (U64)count;
MPASSERT( timePer < INT_MAX );
return (int) timePer / 1000;
}
const int MAPX = 90;
const int MAPY = 20;
const char gMap[MAPX*MAPY+1] =
//"012345678901234567890123456789"
" | | | | || | | | |"
" | |----+ | + | ||---+ | + | |----+ | +"
"---+ +--- -+ +--+--+ ---+ +--- -+| +--+--+ ---+ +--- -+ +--+--+ "
" | +-- + | || +-- + | +-- +"
" +----+ +---+ || +---+ +----+ +---+ "
"---+ + + + | ---+ + | 2---+ + + + | "
" | | +----+ +----+ +--+ | | || +----+ +--+322| | +----+ +----+ +--+"
" | | | | || | | 222232 | | | "
" | +-------+ +-+ |------------------+| +-+ |--+ 2223| +-------+ +-+ |--+ "
"---+ | || | 222+---+ | +"
" | | | || 22233| | | |"
" | |----+ ++ ||---+3333| 22+ | |----+ | +"
"---+ +--- -+ +--+-------------------||22223+--+--+ ---+ +--- -+ +--+--+ "
" | +-- + | 22223333 +-- + | +-- +"
" +----+ +---+ +---+| +---+ 222 +----+ +---+ "
"---+ + + + | ---+ + + +| |222---+ + + + | "
" | | +----+ +----+ +--+ | | +---+| 22+----+ +--+233|2| +----+ +----+ +--+"
" | | | | ||2222| | 2223333| | | "
" | +-------+ +-+ |--+ | +------++ +-+ |--+ |2+-------+ +-+ |--+ "
"---+ | +---+ || | +---+ | ";
class Dungeon : public Graph
{
public:
MPVector<void*> path;
MicroPather* aStar;
int maxDir;
Dungeon() {
aStar = new MicroPather( this, MAPX*MAPY, 6 );
maxDir = 4;
}
virtual ~Dungeon() {
delete aStar;
}
int Passable( int nx, int ny )
{
if ( nx >= 0 && nx < MAPX
&& ny >= 0 && ny < MAPY )
{
int index = ny*MAPX+nx;
char c = gMap[ index ];
if ( c == ' ' )
return 1;
else if ( c >= '1' && c <= '9' ) {
int val = c-'0';
MPASSERT( val > 0 );
return val;
}
}
return 0;
}
void NodeToXY( void* node, int* x, int* y )
{
int index = (int)node;
*y = index / MAPX;
*x = index - *y * MAPX;
}
void* XYToNode( int x, int y )
{
return (void*) ( y*MAPX + x );
}
virtual float LeastCostEstimate( void* nodeStart, void* nodeEnd )
{
int xStart, yStart, xEnd, yEnd;
NodeToXY( nodeStart, &xStart, &yStart );
NodeToXY( nodeEnd, &xEnd, &yEnd );
int dx = xStart - xEnd;
int dy = yStart - yEnd;
return (float) sqrt( (double)(dx*dx) + (double)(dy*dy) );
}
virtual void AdjacentCost( void* node, MPVector< StateCost > *neighbors )
{
int x, y;
// E N W S NE NW SW SE
const int dx[8] = { 1, 0, -1, 0, 1, -1, -1, 1 };
const int dy[8] = { 0, -1, 0, 1, -1, -1, 1, 1 };
const float cost[8] = { 1.0f, 1.0f, 1.0f, 1.0f,
1.41f, 1.41f, 1.41f, 1.41f };
NodeToXY( node, &x, &y );
for( int i=0; i<maxDir; ++i ) {
int nx = x + dx[i];
int ny = y + dy[i];
int pass = Passable( nx, ny );
if ( pass > 0 ) {
// Normal floor
StateCost nodeCost = { XYToNode( nx, ny ), cost[i] * (float)(pass) };
neighbors->push_back( nodeCost );
}
}
}
virtual void PrintStateInfo( void* node )
{
int x, y;
NodeToXY( node, &x, &y );
printf( "(%2d,%2d)", x, y );
}
};
int main( int argc, const char* argv[] )
{
Dungeon dungeon;
#ifdef PROFILING_RUN
const int NUM_TEST = 117;
#else
const int NUM_TEST = 389;
#endif
int indexArray[ NUM_TEST ];
float costArray[ NUM_TEST ];
U64 timeArray[ NUM_TEST ];
int resultArray[ NUM_TEST ];
int i;
bool useBinaryHash = false;
bool useList = false;
bool debug = false;
#ifdef DEBUG
debug = true;
#endif
#ifdef USE_BINARY_HASH
useBinaryHash = true;
#endif
#ifdef USE_LIST
useList = true;
#endif
printf( "SpeedTest binaryHash=%s list=%s debug=%s\n",
useBinaryHash ? "true" : "false",
useList ? "true" : "false",
debug ? "true" : "false" );
// Set up the test indices. Distribute, then randomize.
for( i=0; i<NUM_TEST; ++i ) {
indexArray[i] = (MAPX*MAPY) * i / NUM_TEST;
costArray[i] = 0.0f;
int y = indexArray[i] / MAPX;
int x = indexArray[i] - MAPX*y;
while ( !dungeon.Passable( x, y ) ) {
indexArray[i] += 1;
y = indexArray[i] / MAPX;
x = indexArray[i] - MAPX*y;
}
}
for( i=0; i<NUM_TEST; ++i )
{
int swapWith = rand()%NUM_TEST;
int temp = indexArray[i];
indexArray[i] = indexArray[swapWith];
indexArray[swapWith] = temp;
}
int compositeScore = 0;
for ( int numDir=4; numDir<=8; numDir+=4 )
{
dungeon.maxDir = numDir;
dungeon.aStar->Reset();
for( int reset=0; reset<=1; ++reset )
{
clock_t clockStart = clock();
for( i=0; i<NUM_TEST; ++i )
{
if ( reset )
dungeon.aStar->Reset();
int startState = indexArray[i];
int endState = indexArray[ (i==(NUM_TEST-1)) ? 0 : i+1];
U64 start = FastTime();
resultArray[i] = dungeon.aStar->Solve( (void*)startState, (void*)endState, &dungeon.path, &costArray[i] );
U64 end = FastTime();
timeArray[i] = end-start;
}
clock_t clockEnd = clock();
#ifndef PROFILING_RUN
// -------- Results ------------ //
const float shortPath = (float)(MAPX / 4);
const float medPath = (float)(MAPX / 2 );
int count[5] = { 0, 0, 0, 0 }; // short, med, long, fail short, fail long
U64 time[5] = { 0, 0, 0, 0 };
for( i=0; i<NUM_TEST; ++i )
{
if ( resultArray[i] == MicroPather::SOLVED ) {
if ( costArray[i] < shortPath ) {
++count[0];
time[0] += timeArray[i];
}
else if ( costArray[i] < medPath ) {
++count[1];
time[1] += timeArray[i];
}
else {
++count[2];
time[2] += timeArray[i];
}
}
else if ( resultArray[i] == MicroPather::NO_SOLUTION ) {
int startState = indexArray[i];
int endState = indexArray[ (i==(NUM_TEST-1)) ? 0 : i+1];
int startX, startY, endX, endY;
dungeon.NodeToXY( (void*)startState, &startX, &startY );
dungeon.NodeToXY( (void*)endState, &endX, &endY );
int distance = abs( startX - endX ) + abs( startY - endY );
if ( distance < shortPath ) {
++count[3];
time[3] += timeArray[i];
}
else {
++count[4];
time[4] += timeArray[i];
}
}
}
printf( "Average of %d runs. Reset=%s. Dir=%d.\n",
NUM_TEST, reset ? "true" : "false", numDir );
printf( "short(%4d, cutoff=%.1f)= %d\n", count[0], shortPath, TimeDiv( time[0], count[0] ));
printf( "med (%4d, cutoff=%.1f)= %d\n", count[1], medPath, TimeDiv( time[1], count[1] ));
printf( "long (%4d) = %d\n", count[2], TimeDiv( time[2], count[2] ));
printf( "fail (%4d) short = %d\n", count[3], TimeDiv( time[3], count[3] ));
printf( "fail (%4d) long = %d\n", count[4], TimeDiv( time[4], count[4] ));
int total = 0;
for( int k=0; k<5; ++k ) {
total += TimeDiv( time[k], count[k] );
}
printf( "Average = %d\n", total / 5 );
compositeScore += total;
//printf( "Average time / test = %.3f ms\n\n", 1000.0f * float(clockEnd-clockStart)/float( NUM_TEST*CLOCKS_PER_SEC ) );
#endif
//dungeon.aStar->DumpHashTable();
}
}
printf( "Composite average = %d\n", compositeScore / (5*4) );
return 0;
}