AStarPathFinderTests.cs
7.05 KB
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
using System.Reflection;
using Microsoft.Extensions.Logging.Abstractions;
using Rcs.Application.Services.PathFind.Models;
using Rcs.Domain.Entities;
using Rcs.Domain.Enums;
using Rcs.Infrastructure.PathFinding.Services;
using Xunit;
namespace Rcs.Infrastructure.Tests;
public class AStarPathFinderTests
{
private const double OppositeDirectionCost = 9999999.0;
[Fact]
public void FindPath_AppliesSameDirectionCost_999()
{
var pathFinder = CreatePathFinder();
var graph = CreateSimpleGraph();
var request = CreateSimpleRequest(graph);
var otherRobotId = Guid.NewGuid();
var context = new GlobalPathContext
{
RobotPlannedEdges =
{
[otherRobotId] = new List<PlannedEdge>
{
new()
{
FromNodeId = GetStartNode(graph),
ToNodeId = GetEndNode(graph),
FromNodeCode = "N1",
ToNodeCode = "N2",
Length = 1,
TraveledDistance = 0,
CurrentSpeed = 1,
AverageSpeed = 1,
EstimatedArrivalTime = 10
}
}
}
};
var result = pathFinder.FindPath(request, graph, context);
Assert.True(result.Success);
Assert.Equal(1000d, result.TotalCost, 6);
}
[Fact]
public void FindPath_AppliesSweepEdgePenalty_AsOppositeDirectionCost()
{
var pathFinder = CreatePathFinder();
var graph = CreateSimpleGraph();
var request = CreateSimpleRequest(graph);
var otherRobotId = Guid.NewGuid();
var context = new GlobalPathContext
{
RobotSweepLockedEdgeCodes =
{
[otherRobotId] = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
{
"E1"
}
}
};
var result = pathFinder.FindPath(request, graph, context);
Assert.True(result.Success);
Assert.Equal(1d + OppositeDirectionCost, result.TotalCost, 6);
}
[Fact]
public void FindPath_AppliesSweepNodePenalty_AsOppositeDirectionCost()
{
var pathFinder = CreatePathFinder();
var graph = CreateSimpleGraph();
var request = CreateSimpleRequest(graph);
var otherRobotId = Guid.NewGuid();
var context = new GlobalPathContext
{
RobotSweepLockedNodeCodes =
{
[otherRobotId] = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
{
"N1"
}
}
};
var result = pathFinder.FindPath(request, graph, context);
Assert.True(result.Success);
Assert.Equal(1d + OppositeDirectionCost, result.TotalCost, 6);
}
[Fact]
public void FindPath_SweepEdgeAndNodeFromSameRobot_PenalizesOnlyOnce()
{
var pathFinder = CreatePathFinder();
var graph = CreateSimpleGraph();
var request = CreateSimpleRequest(graph);
var otherRobotId = Guid.NewGuid();
var context = new GlobalPathContext
{
RobotSweepLockedNodeCodes =
{
[otherRobotId] = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
{
"N1"
}
},
RobotSweepLockedEdgeCodes =
{
[otherRobotId] = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
{
"E1"
}
}
};
var result = pathFinder.FindPath(request, graph, context);
Assert.True(result.Success);
Assert.Equal(1d + OppositeDirectionCost, result.TotalCost, 6);
}
[Fact]
public void FindPath_WithoutPlannedAndSweepConflicts_DoesNotAddExtraPenalty()
{
var pathFinder = CreatePathFinder();
var graph = CreateSimpleGraph();
var request = CreateSimpleRequest(graph);
var result = pathFinder.FindPath(request, graph, new GlobalPathContext());
Assert.True(result.Success);
Assert.Equal(1d, result.TotalCost, 6);
}
[Fact]
public void ExtractLockCodesByMap_OnlyReturnsCodesInCurrentMap()
{
var method = typeof(AgvPathService).GetMethod(
"ExtractLockCodesByMap",
BindingFlags.NonPublic | BindingFlags.Static);
Assert.NotNull(method);
var keys = new[]
{
"rcs:lock:node:MAP-A:N1",
"rcs:lock:node:MAP-B:N2",
"rcs:lock:edge:MAP-A:E1",
"rcs:lock:edge:MAP-C:E9"
};
var result = (HashSet<string>)method!.Invoke(null, new object[] { keys, "MAP-A" })!;
Assert.Contains("N1", result);
Assert.Contains("E1", result);
Assert.DoesNotContain("N2", result);
Assert.DoesNotContain("E9", result);
}
private static AStarPathFinder CreatePathFinder()
{
return new AStarPathFinder(NullLogger<AStarPathFinder>.Instance);
}
private static PathRequest CreateSimpleRequest(PathGraph graph)
{
return new PathRequest
{
RobotId = Guid.NewGuid(),
MapId = graph.MapId,
StartNodeId = GetStartNode(graph),
EndNodeId = GetEndNode(graph),
CurrentTheta = 0,
MovementType = MovementType.Differential
};
}
private static PathGraph CreateSimpleGraph()
{
var start = Guid.NewGuid();
var end = Guid.NewGuid();
var edgeId = Guid.NewGuid();
var startNode = new PathNode
{
NodeId = start,
NodeCode = "N1",
X = 0,
Y = 0,
Active = true,
AllowRotate = true
};
var endNode = new PathNode
{
NodeId = end,
NodeCode = "N2",
X = 1,
Y = 0,
Active = true,
AllowRotate = true
};
var edge = new PathEdge
{
EdgeId = edgeId,
EdgeCode = "E1",
FromNodeId = start,
ToNodeId = end,
FromNodeCode = "N1",
ToNodeCode = "N2",
Active = true,
Cost = 1,
Length = 1,
OrientationRads = new List<double> { 0, Math.PI },
StartTangentRad = 0,
DirectionRad = 0
};
startNode.OutEdges.Add(edge);
endNode.InEdges.Add(edge);
var graph = new PathGraph
{
MapId = Guid.NewGuid(),
MapCode = "MAP-A"
};
graph.Nodes[start] = startNode;
graph.Nodes[end] = endNode;
graph.Edges[edgeId] = edge;
return graph;
}
private static Guid GetStartNode(PathGraph graph)
{
return graph.Nodes.Values.Single(node => node.NodeCode == "N1").NodeId;
}
private static Guid GetEndNode(PathGraph graph)
{
return graph.Nodes.Values.Single(node => node.NodeCode == "N2").NodeId;
}
}