Skip to content

Commit 8da3b3f

Browse files
committed
Add path result as input parameter to search function
Now single lookup can be used for path findings without additional memory allocations.
1 parent 9508aea commit 8da3b3f

File tree

9 files changed

+145
-135
lines changed

9 files changed

+145
-135
lines changed

BrainAI.Benchmarks/Program.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public enum MapSizeType
5151
private IPathfinder<Point>? pathfinder;
5252
private GridGraph? gridGraph;
5353
private StrightEdgeGraph strightEdgeGraph = new StrightEdgeGraph();
54+
private List<Point> result = new List<Point>();
5455

5556
[GlobalSetup]
5657
public void Setup()
@@ -105,11 +106,11 @@ public void Pathfinding()
105106
{
106107
var start = this.GraphType == GraphTypes.Grid ? new Point(0, 0) : new Point(0, 1);
107108
var end = this.GraphType == GraphTypes.Grid ? new Point((int)MapSize - 1, (int)MapSize - 1) : new Point((int)MapSize - 5, (int)MapSize - 4);
109+
108110
for (var i = 0; i < (int)this.RunsCount; i++)
109111
{
110-
this.pathfinder!.Search(start, end);
111-
var pathData = this.pathfinder!.ResultPath;
112-
if (pathData.Count == 0)
112+
this.pathfinder!.Search(start, end, result);
113+
if (result.Count == 0)
113114
{
114115
throw new System.Exception("Path not found.");
115116
}

BrainAI.Tests/AStarPathfinderTest.cs

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ public class AStarPathfinderTest
99
{
1010
private GridGraph graph;
1111
private AStarPathfinder<Point> pathfinder;
12+
private List<Point> result = new List<Point>();
1213

1314
[SetUp]
1415
public void Setup()
1516
{
1617
this.graph = new GridGraph(10, 10);
1718
this.pathfinder = new AStarPathfinder<Point>(graph);
19+
this.result.Clear();
1820
}
1921

2022
[Test]
@@ -33,11 +35,11 @@ public void Search_AllowDiagonal_PathFound()
3335
graph.Walls.Add(new Point(2, 1));
3436
graph.Walls.Add(new Point(1, 0));
3537
graph.Walls.Add(new Point(0, 0));
36-
pathfinder.Search(new Point(1, 1), new Point(2, 2));
38+
pathfinder.Search(new Point(1, 1), new Point(2, 2), result);
3739
CollectionAssert.AreEqual(new List<Point> {
3840
new Point(1, 1),
3941
new Point(2, 2)
40-
}, pathfinder.ResultPath);
42+
}, result);
4143
}
4244

4345
[Test]
@@ -50,12 +52,12 @@ public void Search_ForwardPath_PathFound()
5052
*/
5153
graph.Walls.Add(new Point(1, 2));
5254

53-
pathfinder.Search(new Point(1, 1), new Point(2, 2));
55+
pathfinder.Search(new Point(1, 1), new Point(2, 2), result);
5456
CollectionAssert.AreEqual(new List<Point> {
5557
new Point(1, 1),
5658
new Point(2, 1),
5759
new Point(2, 2)
58-
}, pathfinder.ResultPath);
60+
}, result);
5961
}
6062

6163
[Test]
@@ -72,7 +74,7 @@ public void Search_BackwardPath_PathFound()
7274
graph.Walls.Add(new Point(1, 0));
7375
graph.Walls.Add(new Point(0, 0));
7476

75-
pathfinder.Search(new Point(1, 1), new Point(2, 2));
77+
pathfinder.Search(new Point(1, 1), new Point(2, 2), result);
7678
CollectionAssert.AreEqual(new List<Point> {
7779
new Point(1, 1),
7880
new Point(0, 1),
@@ -81,7 +83,7 @@ public void Search_BackwardPath_PathFound()
8183
new Point(1, 3),
8284
new Point(2, 3),
8385
new Point(2, 2)
84-
}, pathfinder.ResultPath);
86+
}, result);
8587
}
8688

8789
[Test]
@@ -98,9 +100,9 @@ public void Search_NoWay_PathNull()
98100
graph.Walls.Add(new Point(1, 0));
99101
graph.Walls.Add(new Point(0, 1));
100102

101-
pathfinder.Search(new Point(1, 1), new Point(2, 2));
103+
pathfinder.Search(new Point(1, 1), new Point(2, 2), result);
102104

103-
Assert.IsEmpty(pathfinder.ResultPath);
105+
Assert.IsEmpty(result);
104106
}
105107

106108
[Test]
@@ -112,12 +114,12 @@ public void Search_MultiGoals_PathFound()
112114
_#2_
113115
*/
114116
graph.Walls.Add(new Point(1, 2));
115-
pathfinder.Search(new Point(1, 1), new HashSet<Point> { new Point(3, 2), new Point(2, 2) });
117+
pathfinder.Search(new Point(1, 1), new HashSet<Point> { new Point(3, 2), new Point(2, 2) }, result);
116118
CollectionAssert.AreEqual(new List<Point> {
117119
new Point(1, 1),
118120
new Point(2, 1),
119121
new Point(2, 2)
120-
}, pathfinder.ResultPath);
122+
}, result);
121123
}
122124

123125
[Test]
@@ -130,19 +132,19 @@ public void ContinueSearch_MultiGoals_PathFound()
130132
__3_
131133
*/
132134
graph.Walls.Add(new Point(1, 2));
133-
pathfinder.Search(new Point(1, 1), new HashSet<Point> { new Point(2, 3), new Point(2, 2) });
135+
pathfinder.Search(new Point(1, 1), new HashSet<Point> { new Point(2, 3), new Point(2, 2) }, result);
134136
CollectionAssert.AreEqual(new List<Point> {
135137
new Point(1, 1),
136138
new Point(2, 1),
137139
new Point(2, 2)
138-
}, pathfinder.ResultPath);
139-
pathfinder.ContinueSearch();
140+
}, result);
141+
pathfinder.ContinueSearch(result);
140142
CollectionAssert.AreEqual(new List<Point> {
141143
new Point(1, 1),
142144
new Point(2, 1),
143145
new Point(2, 2),
144146
new Point(2, 3)
145-
}, pathfinder.ResultPath);
147+
}, result);
146148
}
147149

148150
[Test]
@@ -155,20 +157,20 @@ public void Search_TwiceWithSuccess_PathCleared()
155157
__3_
156158
*/
157159
graph.Walls.Add(new Point(1, 2));
158-
pathfinder.Search(new Point(1, 1), new Point(2, 3));
160+
pathfinder.Search(new Point(1, 1), new Point(2, 3), result);
159161
CollectionAssert.AreEqual(new List<Point> {
160162
new Point(1, 1),
161163
new Point(2, 1),
162164
new Point(2, 2),
163165
new Point(2, 3)
164-
}, pathfinder.ResultPath);
165-
pathfinder.Search(new Point(1, 1), new Point(2, 3));
166+
}, result);
167+
pathfinder.Search(new Point(1, 1), new Point(2, 3), result);
166168
CollectionAssert.AreEqual(new List<Point> {
167169
new Point(1, 1),
168170
new Point(2, 1),
169171
new Point(2, 2),
170172
new Point(2, 3)
171-
}, pathfinder.ResultPath);
173+
}, result);
172174
}
173175
[Test]
174176
public void Search_TwiceWithFail_PathCleared()
@@ -186,12 +188,12 @@ public void Search_TwiceWithFail_PathCleared()
186188
graph.Walls.Add(new Point(2, 0));
187189
graph.Walls.Add(new Point(2, 1));
188190
graph.Walls.Add(new Point(2, 2));
189-
pathfinder.Search(new Point(1, 1), new Point(1, 3));
191+
pathfinder.Search(new Point(1, 1), new Point(1, 3), result);
190192
CollectionAssert.AreEqual(new List<Point> {
191193
new Point(1, 1),
192194
new Point(1, 2),
193195
new Point(1, 3)
194-
}, pathfinder.ResultPath);
196+
}, result);
195197

196198

197199
/*
@@ -201,8 +203,8 @@ public void Search_TwiceWithFail_PathCleared()
201203
____
202204
*/
203205
graph.Walls.Add(new Point(1, 2));
204-
pathfinder.Search(new Point(1, 1), new Point(1, 3));
205-
CollectionAssert.IsEmpty(pathfinder.ResultPath);
206+
pathfinder.Search(new Point(1, 1), new Point(1, 3), result);
207+
CollectionAssert.IsEmpty(result);
206208
}
207209

208210
[Test]
@@ -215,12 +217,12 @@ public void MoveFromBlock()
215217
__3_
216218
*/
217219
graph.Walls.Add(new Point(1, 2));
218-
pathfinder.Search(new Point(1, 2), new Point(2, 3));
220+
pathfinder.Search(new Point(1, 2), new Point(2, 3), result);
219221
CollectionAssert.AreEqual(new List<Point> {
220222
new Point(1, 2),
221223
new Point(2, 2),
222224
new Point(2, 3)
223-
}, pathfinder.ResultPath);
225+
}, result);
224226
}
225227
}
226228
}

BrainAI.Tests/BreadthFirstPathfinderTest.cs

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ public class BreadthFirstPathfinderTest
1010
{
1111
private GridGraph graph;
1212
private ICoveragePathfinder<Point> pathfinder;
13+
private List<Point> result = new List<Point>();
1314

1415
[SetUp]
1516
public void Setup()
1617
{
1718
this.graph = new GridGraph(10, 10);
1819
this.pathfinder = new BreadthFirstPathfinder<Point>(graph);
20+
this.result.Clear();
1921
}
2022

2123
[Test]
@@ -34,11 +36,11 @@ public void Search_AllowDiagonal_PathFound()
3436
graph.Walls.Add(new Point(2, 1));
3537
graph.Walls.Add(new Point(1, 0));
3638
graph.Walls.Add(new Point(0, 0));
37-
pathfinder.Search(new Point(1, 1), new Point(2, 2));
39+
pathfinder.Search(new Point(1, 1), new Point(2, 2), result);
3840
CollectionAssert.AreEqual(new List<Point> {
3941
new Point(1, 1),
4042
new Point(2, 2)
41-
}, pathfinder.ResultPath);
43+
}, result);
4244
}
4345

4446
[Test]
@@ -50,12 +52,12 @@ public void Search_ForwardPath_PathFound()
5052
_#2_
5153
*/
5254
graph.Walls.Add(new Point(1, 2));
53-
pathfinder.Search(new Point(1, 1), new Point(2, 2));
55+
pathfinder.Search(new Point(1, 1), new Point(2, 2), result);
5456
CollectionAssert.AreEqual(new List<Point> {
5557
new Point(1, 1),
5658
new Point(2, 1),
5759
new Point(2, 2)
58-
}, pathfinder.ResultPath);
60+
}, result);
5961
}
6062

6163
[Test]
@@ -71,7 +73,7 @@ public void Search_BackwardPath_PathFound()
7173
graph.Walls.Add(new Point(2, 1));
7274
graph.Walls.Add(new Point(1, 0));
7375
graph.Walls.Add(new Point(0, 0));
74-
pathfinder.Search(new Point(1, 1), new Point(2, 2));
76+
pathfinder.Search(new Point(1, 1), new Point(2, 2), result);
7577
CollectionAssert.AreEqual(new List<Point> {
7678
new Point(1, 1),
7779
new Point(0, 1),
@@ -80,7 +82,7 @@ public void Search_BackwardPath_PathFound()
8082
new Point(1, 3),
8183
new Point(2, 3),
8284
new Point(2, 2)
83-
}, pathfinder.ResultPath);
85+
}, result);
8486
}
8587

8688
[Test]
@@ -96,8 +98,8 @@ public void Search_NoWay_PathNull()
9698
graph.Walls.Add(new Point(2, 1));
9799
graph.Walls.Add(new Point(1, 0));
98100
graph.Walls.Add(new Point(0, 1));
99-
pathfinder.Search(new Point(1, 1), new Point(2, 2));
100-
Assert.IsEmpty(pathfinder.ResultPath);
101+
pathfinder.Search(new Point(1, 1), new Point(2, 2), result);
102+
Assert.IsEmpty(result);
101103
}
102104

103105
[Test]
@@ -166,12 +168,12 @@ public void Search_MultiGoals_PathFound()
166168
_#2_
167169
*/
168170
graph.Walls.Add(new Point(1, 2));
169-
pathfinder.Search(new Point(1, 1), new HashSet<Point> { new Point(3, 2), new Point(2, 2) });
171+
pathfinder.Search(new Point(1, 1), new HashSet<Point> { new Point(3, 2), new Point(2, 2) }, result);
170172
CollectionAssert.AreEqual(new List<Point> {
171173
new Point(1, 1),
172174
new Point(2, 1),
173175
new Point(2, 2)
174-
}, pathfinder.ResultPath);
176+
}, result);
175177
}
176178

177179
[Test]
@@ -184,20 +186,20 @@ public void ContinueSearch_MultiGoals_PathFound()
184186
__3_
185187
*/
186188
graph.Walls.Add(new Point(1, 2));
187-
pathfinder.Search(new Point(1, 1), new HashSet<Point> { new Point(2, 3), new Point(2, 2) });
189+
pathfinder.Search(new Point(1, 1), new HashSet<Point> { new Point(2, 3), new Point(2, 2) }, result);
188190
CollectionAssert.AreEqual(new List<Point> {
189191
new Point(1, 1),
190192
new Point(2, 1),
191193
new Point(2, 2)
192-
}, pathfinder.ResultPath);
194+
}, result);
193195

194-
pathfinder.ContinueSearch();
196+
pathfinder.ContinueSearch(result);
195197
CollectionAssert.AreEqual(new List<Point> {
196198
new Point(1, 1),
197199
new Point(2, 1),
198200
new Point(2, 2),
199201
new Point(2, 3)
200-
}, pathfinder.ResultPath);
202+
}, result);
201203
}
202204

203205
[Test]
@@ -210,20 +212,20 @@ public void Search_TwiceWithSuccess_PathCleared()
210212
__3_
211213
*/
212214
graph.Walls.Add(new Point(1, 2));
213-
pathfinder.Search(new Point(1, 1), new Point(2, 3));
215+
pathfinder.Search(new Point(1, 1), new Point(2, 3), result);
214216
CollectionAssert.AreEqual(new List<Point> {
215217
new Point(1, 1),
216218
new Point(2, 1),
217219
new Point(2, 2),
218220
new Point(2, 3)
219-
}, pathfinder.ResultPath);
220-
pathfinder.Search(new Point(1, 1), new Point(2, 3));
221+
}, result);
222+
pathfinder.Search(new Point(1, 1), new Point(2, 3), result);
221223
CollectionAssert.AreEqual(new List<Point> {
222224
new Point(1, 1),
223225
new Point(2, 1),
224226
new Point(2, 2),
225227
new Point(2, 3)
226-
}, pathfinder.ResultPath);
228+
}, result);
227229
}
228230
[Test]
229231
public void Search_TwiceWithFail_PathCleared()
@@ -241,12 +243,12 @@ public void Search_TwiceWithFail_PathCleared()
241243
graph.Walls.Add(new Point(2, 0));
242244
graph.Walls.Add(new Point(2, 1));
243245
graph.Walls.Add(new Point(2, 2));
244-
pathfinder.Search(new Point(1, 1), new Point(1, 3));
246+
pathfinder.Search(new Point(1, 1), new Point(1, 3), result);
245247
CollectionAssert.AreEqual(new List<Point> {
246248
new Point(1, 1),
247249
new Point(1, 2),
248250
new Point(1, 3)
249-
}, pathfinder.ResultPath);
251+
}, result);
250252

251253

252254
/*
@@ -256,8 +258,8 @@ public void Search_TwiceWithFail_PathCleared()
256258
____
257259
*/
258260
graph.Walls.Add(new Point(1, 2));
259-
pathfinder.Search(new Point(1, 1), new Point(1, 3));
260-
CollectionAssert.IsEmpty(pathfinder.ResultPath);
261+
pathfinder.Search(new Point(1, 1), new Point(1, 3), result);
262+
CollectionAssert.IsEmpty(result);
261263
}
262264

263265
[Test]
@@ -270,12 +272,12 @@ public void MoveFromBlock()
270272
__3_
271273
*/
272274
graph.Walls.Add(new Point(1, 2));
273-
pathfinder.Search(new Point(1, 2), new Point(2, 3));
275+
pathfinder.Search(new Point(1, 2), new Point(2, 3), result);
274276
CollectionAssert.AreEqual(new List<Point> {
275277
new Point(1, 2),
276278
new Point(2, 2),
277279
new Point(2, 3)
278-
}, pathfinder.ResultPath);
280+
}, result);
279281
}
280282
}
281283
}

0 commit comments

Comments
 (0)