-
Notifications
You must be signed in to change notification settings - Fork 5
/
OGraphTests.m
398 lines (350 loc) · 15.1 KB
/
OGraphTests.m
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
//
// OGraphTests.m
// OGraphTests
//
// Created by Matthew McGlincy on 5/6/11.
// Copyright 2011 n/a. All rights reserved.
//
#import "GraphEdge.h"
#import "GraphNode.h"
#import "GraphSearchAStar.h"
#import "GraphSearchBFS.h"
#import "GraphSearchDFS.h"
#import "GraphSearchDijkstra.h"
#import "Heuristic.h"
#import "HeuristicEuclid.h"
#import "HeuristicManhattan.h"
#import "NavGraphNode.h"
#import "SparseGraph.h"
#import "OGraphTests.h"
@interface OGraphTests()
+ (SparseGraph *)minimalDigraph;
+ (SparseGraph *)minimalNavGraph;
+ (SparseGraph *)sampleUndirectedGraph;
+ (SparseGraph *)twoRouteCostedDigraph;
+ (SparseGraph *)complicatedCostedDigraph;
+ (SparseGraph *)threeByThreeNavGraph;
- (void)aStarWithHeuristic:(id<Heuristic>)heuristic;
@end
@implementation OGraphTests
- (void)setUp {
[super setUp];
}
- (void)tearDown {
[super tearDown];
}
+ (SparseGraph *)minimalDigraph {
// simple digraph with 2 nodes and 1 edge
SparseGraph *graph = [[SparseGraph alloc] initWithIsDigraph:YES];
[graph addNodeWithIndex:0];
[graph addNodeWithIndex:1];
[graph addEdgeFrom:0 to:1];
return [graph autorelease];
}
+ (SparseGraph *)minimalNavGraph {
SparseGraph *graph = [[SparseGraph alloc] initWithIsDigraph:NO];
NavGraphNode *node1 = [[NavGraphNode alloc] initWithIndex:0 x:0 y:0];
NavGraphNode *node2 = [[NavGraphNode alloc] initWithIndex:1 x:200.0 y:200.0];
[graph addNode:node1];
[graph addNode:node2];
[graph addEdgeFrom:0 to:1];
[node1 release];
[node2 release];
return [graph autorelease];
}
- (void)testSparseGraph {
SparseGraph *graph = [OGraphTests minimalDigraph];
// simple existence checks
STAssertEquals(graph.numNodes, 2U, nil);
STAssertEquals(graph.numActiveNodes, 2U, nil);
STAssertEquals(graph.numEdges, 1U, nil);
STAssertTrue([graph isNodePresentWithIndex:0], nil);
STAssertTrue([graph isNodePresentWithIndex:1], nil);
STAssertFalse([graph isNodePresentWithIndex:3], nil);
STAssertTrue([graph isEdgePresentWithFrom:0 to:1], nil);
STAssertFalse([graph isEdgePresentWithFrom:1 to:0], nil);
// remove an edge
[graph removeEdgeWithFrom:0 to:1];
STAssertEquals(graph.numEdges, 0U, nil);
STAssertFalse([graph isEdgePresentWithFrom:0 to:1], nil);
STAssertEquals(graph.numEdges, 0U, nil);
// put it back
GraphEdge *e0 = [[GraphEdge alloc] initWithFrom:0 to:1];
[graph addEdge:e0];
[e0 release];
STAssertTrue([graph isEdgePresentWithFrom:0 to:1], nil);
STAssertEquals(graph.numEdges, 1U, nil);
// remove a node, which should only deactivate the node
[graph removeNodeWithIndex:1];
STAssertEquals(graph.numNodes, 2U, nil);
STAssertEquals(graph.numActiveNodes, 1U, nil);
// but, it should nuke our edge, too
STAssertEquals(graph.numEdges, 0U, nil);
STAssertFalse([graph isEdgePresentWithFrom:0 to:1], nil);
// clear
[graph clear];
STAssertEquals(graph.numNodes, 0U, nil);
STAssertEquals(graph.numActiveNodes, 0U, nil);
STAssertEquals(graph.numEdges, 0U, nil);
STAssertFalse([graph isNodePresentWithIndex:0], nil);
STAssertFalse([graph isNodePresentWithIndex:1], nil);
STAssertFalse([graph isEdgePresentWithFrom:0 to:1], nil);
}
- (void)testSimpleDirectedDFS {
// simple digraph with 2 nodes and 1 edge
SparseGraph *graph = [OGraphTests minimalDigraph];
GraphSearchDFS *dfs = [[GraphSearchDFS alloc] initWithGraph:graph sourceNodeIndex:0 targetNodeIndex:1];
STAssertTrue(dfs.pathToTargetFound, nil);
NSArray *path = [dfs getPathToTarget];
// path should be of 2 elements, from node 0 (source) to node 1 (target)
STAssertEquals([path count], 2U, nil);
STAssertEqualObjects([path objectAtIndex:0], [NSNumber numberWithInt:0], nil);
STAssertEqualObjects([path objectAtIndex:1], [NSNumber numberWithInt:1], nil);
[graph release];
}
+ (SparseGraph *)sampleUndirectedGraph {
// create the undirected graph from The Secret Life of Graphs chapter
SparseGraph *graph = [[SparseGraph alloc] initWithIsDigraph:NO];
[graph addNodeWithIndex:0];
[graph addNodeWithIndex:1];
[graph addNodeWithIndex:2];
[graph addNodeWithIndex:3];
[graph addNodeWithIndex:4];
[graph addNodeWithIndex:5];
[graph addEdgeFrom:0 to:1];
[graph addEdgeFrom:0 to:2];
[graph addEdgeFrom:1 to:4];
[graph addEdgeFrom:2 to:3];
[graph addEdgeFrom:3 to:4];
[graph addEdgeFrom:3 to:5];
[graph addEdgeFrom:4 to:5];
return [graph autorelease];
}
- (void)testUndirectedDFS {
SparseGraph *graph = [OGraphTests sampleUndirectedGraph];
GraphSearchDFS *dfs = [[GraphSearchDFS alloc] initWithGraph:graph sourceNodeIndex:4 targetNodeIndex:2];
STAssertTrue(dfs.pathToTargetFound, nil);
NSArray *path = [dfs getPathToTarget];
// note that the order we add edges will affect the order edges are pushed
// onto our search stack, and thus ultimately the order of the DFS.
// With the edge-adding order above, we end up with 4=>5=>3=>2
STAssertEquals([path count], 4U, nil);
STAssertEqualObjects([path objectAtIndex:0], [NSNumber numberWithInt:4], nil);
STAssertEqualObjects([path objectAtIndex:1], [NSNumber numberWithInt:5], nil);
STAssertEqualObjects([path objectAtIndex:2], [NSNumber numberWithInt:3], nil);
STAssertEqualObjects([path objectAtIndex:3], [NSNumber numberWithInt:2], nil);
[dfs release];
}
- (void)testUndirectedBFS {
SparseGraph *graph = [OGraphTests sampleUndirectedGraph];
GraphSearchBFS *bfs = [[GraphSearchBFS alloc] initWithGraph:graph sourceNodeIndex:4 targetNodeIndex:2];
STAssertTrue(bfs.pathToTargetFound, nil);
NSArray *path = [bfs getPathToTarget];
// BFS path is 4=>3=>2
STAssertEquals([path count], 3U, nil);
STAssertEqualObjects([path objectAtIndex:0], [NSNumber numberWithInt:4], nil);
STAssertEqualObjects([path objectAtIndex:1], [NSNumber numberWithInt:3], nil);
STAssertEqualObjects([path objectAtIndex:2], [NSNumber numberWithInt:2], nil);
[bfs release];
}
+ (SparseGraph *)twoRouteCostedDigraph {
// simple digraph with 4 nodes, with a "cheaper" way to get from
// source n0 to target n3
SparseGraph *graph = [[SparseGraph alloc] initWithIsDigraph:YES];
[graph addNodeWithIndex:0];
[graph addNodeWithIndex:1];
[graph addNodeWithIndex:2];
[graph addNodeWithIndex:3];
[graph addEdgeFrom:0 to:1 cost:1.0];
[graph addEdgeFrom:0 to:2 cost:1.0];
[graph addEdgeFrom:1 to:3 cost:0.5];
[graph addEdgeFrom:2 to:3 cost:1.0];
return [graph autorelease];
}
+ (SparseGraph *)complicatedCostedDigraph {
// a more complicated digraph, as used in the chapter
// The Secret Life of Graphs, pg233.
SparseGraph *graph = [[SparseGraph alloc] initWithIsDigraph:YES];
[graph addNodeWithIndex:0];
[graph addNodeWithIndex:1];
[graph addNodeWithIndex:2];
[graph addNodeWithIndex:3];
[graph addNodeWithIndex:4];
[graph addNodeWithIndex:5];
[graph addEdgeFrom:0 to:4 cost:2.9];
[graph addEdgeFrom:0 to:5 cost:1.0];
[graph addEdgeFrom:1 to:2 cost:3.1];
[graph addEdgeFrom:2 to:4 cost:0.8];
[graph addEdgeFrom:3 to:2 cost:3.7];
[graph addEdgeFrom:4 to:1 cost:1.9];
[graph addEdgeFrom:4 to:5 cost:3.0];
[graph addEdgeFrom:5 to:3 cost:1.1];
return [graph autorelease];
}
- (void)testEasyDijkstra {
SparseGraph *graph = [OGraphTests twoRouteCostedDigraph];
GraphSearchDijkstra *dij = [[GraphSearchDijkstra alloc] initWithGraph:graph sourceNodeIndex:0 targetNodeIndex:3];
NSArray *path = [dij getPathToTarget];
// our test digraph has 2 routes from 0=>3: 0=>2=>3 costing 2.0, and 0=>1=>3 costing 1.5.
// Make sure our search picks the cheaper route of 1.5.
STAssertEquals([path count], 3U, nil);
STAssertEqualObjects([path objectAtIndex:0], [NSNumber numberWithInt:0], nil);
STAssertEqualObjects([path objectAtIndex:1], [NSNumber numberWithInt:1], nil);
STAssertEqualObjects([path objectAtIndex:2], [NSNumber numberWithInt:3], nil);
STAssertEquals([dij getCostToTarget], 1.5, nil);
[dij release];
}
- (void)testComplicatedDijkstra {
SparseGraph *graph = [OGraphTests complicatedCostedDigraph];
GraphSearchDijkstra *dij = [[GraphSearchDijkstra alloc] initWithGraph:graph sourceNodeIndex:4 targetNodeIndex:2];
NSArray *path = [dij getPathToTarget];
// cheapest path from 4=>2 is 4=>1=>2, with a total cost of 5.0
STAssertEquals([path count], 3U, nil);
STAssertEqualObjects([path objectAtIndex:0], [NSNumber numberWithInt:4], nil);
STAssertEqualObjects([path objectAtIndex:1], [NSNumber numberWithInt:1], nil);
STAssertEqualObjects([path objectAtIndex:2], [NSNumber numberWithInt:2], nil);
STAssertEquals([dij getCostToTarget], 5.0, nil);
[dij release];
}
+ (SparseGraph *)threeByThreeNavGraph {
// make a small 3x3 grid of nodes
// 0 - 1 - 2
// | X | X |
// 3 - 4 - 5
// | X | X |
// 6 - 7 - 8
SparseGraph *graph = [[SparseGraph alloc] initWithIsDigraph:NO];
NSUInteger idx = 0;
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
// give then positions, assuming 50px grid tile size
CGPoint pos = CGPointMake(col * 50.0, row * 50.0);
NavGraphNode *n = [[NavGraphNode alloc] initWithIndex:idx position:pos];
[graph addNode:n];
[n release];
idx++;
}
}
[graph addEdgeFrom:0 to:1];
[graph addEdgeFrom:0 to:3];
[graph addEdgeFrom:0 to:4];
[graph addEdgeFrom:1 to:2];
[graph addEdgeFrom:1 to:3];
[graph addEdgeFrom:1 to:4];
[graph addEdgeFrom:1 to:4];
[graph addEdgeFrom:2 to:4];
[graph addEdgeFrom:2 to:5];
[graph addEdgeFrom:3 to:4];
[graph addEdgeFrom:3 to:6];
[graph addEdgeFrom:3 to:7];
[graph addEdgeFrom:4 to:6];
[graph addEdgeFrom:4 to:7];
[graph addEdgeFrom:4 to:8];
[graph addEdgeFrom:5 to:7];
[graph addEdgeFrom:5 to:8];
[graph addEdgeFrom:6 to:7];
[graph addEdgeFrom:7 to:8];
return [graph autorelease];
}
// Convenience method to test AStar with a given heuristic
- (void)aStarWithHeuristic:(id<Heuristic>)heuristic {
SparseGraph *graph = [OGraphTests threeByThreeNavGraph];
// find a path from our bottom center node (7) to our top center (1)
GraphSearchAStar *aStar = [[GraphSearchAStar alloc] initWithGraph:graph
sourceNodeIndex:7
targetNodeIndex:1
heuristic:heuristic];
NSArray *path = [aStar getPathToTarget];
// best path will be straight up the middle column: 7=>4=>1
STAssertEquals([path count], 3U, nil);
STAssertEqualObjects([path objectAtIndex:0], [NSNumber numberWithInt:7], nil);
STAssertEqualObjects([path objectAtIndex:1], [NSNumber numberWithInt:4], nil);
STAssertEqualObjects([path objectAtIndex:2], [NSNumber numberWithInt:1], nil);
// do another search
[aStar release];
// find a path from our lower-left (6) to our upper right (2)
aStar = [[GraphSearchAStar alloc] initWithGraph:graph
sourceNodeIndex:6
targetNodeIndex:2
heuristic:heuristic];
path = [aStar getPathToTarget];
// best path will be the diagonal: 6=>4=>2
STAssertEquals([path count], 3U, nil);
STAssertEqualObjects([path objectAtIndex:0], [NSNumber numberWithInt:6], nil);
STAssertEqualObjects([path objectAtIndex:1], [NSNumber numberWithInt:4], nil);
STAssertEqualObjects([path objectAtIndex:2], [NSNumber numberWithInt:2], nil);
[aStar release];
}
- (void)testAStarWithEuclid {
HeuristicEuclid *heuristic = [[HeuristicEuclid alloc] init];
[self aStarWithHeuristic:heuristic];
[heuristic release];
}
- (void)testAStarWithManhattan {
HeuristicManhattan *heuristic = [[HeuristicManhattan alloc] init];
[self aStarWithHeuristic:heuristic];
[heuristic release];
}
- (void)testArchiveAndNewFromFile {
// our test target uses a different bundle than [NSBundle mainBundle]
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
NSString *archivePath = [[bundle bundlePath] stringByAppendingPathComponent:@"sgArchive.data"];
// archive a test graph to a file
SparseGraph *archive = [OGraphTests minimalDigraph];
[archive archiveToFile:archivePath];
// then load it
SparseGraph *graph = [SparseGraph newFromFile:archivePath];
// make sure it's the graph we expect
STAssertNotNil(graph, nil);
STAssertEquals(graph.numNodes, 2U, nil);
STAssertEquals(graph.numActiveNodes, 2U, nil);
STAssertEquals(graph.numEdges, 1U, nil);
STAssertTrue([graph isNodePresentWithIndex:0], nil);
STAssertTrue([graph isNodePresentWithIndex:1], nil);
STAssertFalse([graph isNodePresentWithIndex:2], nil);
STAssertTrue([graph isEdgePresentWithFrom:0 to:1], nil);
STAssertFalse([graph isEdgePresentWithFrom:1 to:0], nil);
[graph release];
// remove test file
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager removeItemAtPath:archivePath error:NULL];
}
- (void)testArchiveAndNewFromFileWithNavGraphNode {
// our test target uses a different bundle than [NSBundle mainBundle]
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
NSString *archivePath = [[bundle bundlePath] stringByAppendingPathComponent:@"sgArchive.data"];
// archive a test graph to a file
SparseGraph *archive = [OGraphTests minimalNavGraph];
[archive archiveToFile:archivePath];
// then load it
SparseGraph *graph = [SparseGraph newFromFile:archivePath];
// make sure it's the graph we expect
STAssertNotNil(graph, nil);
STAssertEquals(graph.numNodes, 2U, nil);
STAssertEquals(graph.numActiveNodes, 2U, nil);
// undirected graph, so edges to and from
STAssertEquals(graph.numEdges, 2U, nil);
STAssertTrue([graph isNodePresentWithIndex:0], nil);
STAssertTrue([graph isNodePresentWithIndex:1], nil);
STAssertFalse([graph isNodePresentWithIndex:2], nil);
STAssertTrue([graph isEdgePresentWithFrom:0 to:1], nil);
STAssertTrue([graph isEdgePresentWithFrom:1 to:0], nil);
[graph release];
// remove test file
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager removeItemAtPath:archivePath error:NULL];
}
- (void)testMediumMapGraph {
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
SparseGraph *graph = [SparseGraph newFromFile:[bundle pathForResource:@"mediumMapGraph" ofType:@"data"]];
HeuristicManhattan *heuristic = [[HeuristicManhattan alloc] init];
GraphSearchAStar *aStar = [[GraphSearchAStar alloc] initWithGraph:graph
sourceNodeIndex:0
targetNodeIndex:870
heuristic:heuristic];
NSArray *path = [aStar getPathToTarget];
STAssertNotNil(path, nil);
[heuristic release];
[graph release];
}
@end