-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathCostTreeNodeSolver.cs
478 lines (427 loc) · 17.2 KB
/
CostTreeNodeSolver.cs
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
using System.Collections.Generic;
using System.Linq;
namespace mapf;
/// <summary>
/// This class tries to find a solution to a problem instance of a given cost.
/// (this is used in the CostTreeSearchSolver)
/// </summary>
abstract class CostTreeNodeSolver : IConflictReporting
{
protected MDD[] allMDDs;
int maxCost;
AgentState[] startingPos;
public int totalCost;
public int generated;
public int expanded;
protected Run runner;
protected ProblemInstance problem;
public CostTreeSearchSolver solver;
public int conflictsNotAvoided;
public int matchCounter; // For debugging
public CostTreeNodeSolver(ProblemInstance problem, Run runner, CostTreeSearchSolver solver)
{
this.runner = runner;
this.problem = problem;
this.solver = solver;
this.allMDDs = new MDD[problem.GetNumOfAgents()];
}
/// <summary>
///
/// </summary>
/// <param name="problem"></param>
/// <param name="runner"></param>
/// <param name="solver"></param>
/// <param name="agentNums"></param>
/// <param name="costsNode">Of all the agents, not just the ones selected</param>
/// <param name="reserved"></param>
public CostTreeNodeSolver(ProblemInstance problem, Run runner, CostTreeSearchSolver solver,
int[] agentNums, CostTreeNode costsNode, ISet<TimedMove> reserved)
{
this.runner = runner;
this.problem = problem;
this.solver = solver;
this.allMDDs = new MDD[agentNums.Length];
this.Setup(agentNums, costsNode, reserved);
}
/// <summary>
/// Automatically calls Setup with the given costsNode
/// </summary>
/// <param name="problem"></param>
/// <param name="costNode">TODO: Maybe just pass the array of costs here?</param>
/// <param name="runner"></param>
/// <param name="solver"></param>
public CostTreeNodeSolver(ProblemInstance problem, CostTreeNode costNode, Run runner,
CostTreeSearchSolver solver, ISet<TimedMove> reserved) // Make sure agent numbers are in the correct order
: this(problem, runner, solver)
{
this.Setup(costNode, reserved);
}
public virtual void Setup(CostTreeNode costsNode, ISet<TimedMove> reserved)
{
this.startingPos = problem.agents;
this.totalCost = costsNode.costs.Sum();
this.maxCost = costsNode.costs.Max();
for (int i = 0; i < this.allMDDs.Length; i++)
{
this.allMDDs[i] = new MDD(i, startingPos[i].agent.agentNum, startingPos[i].lastMove,
costsNode.costs[i], maxCost, startingPos.Length, problem, reserved: reserved);
}
this.expanded = 0;
this.generated = 0;
this.matchCounter = 0;
}
public virtual void Setup(int[] agentNums, CostTreeNode costsNode, ISet<TimedMove> reserved)
{
this.startingPos = new AgentState[agentNums.Length];
int index = 0;
foreach (var agentNum in agentNums)
{
while (problem.agents[index].agent.agentNum != agentNum)
++index;
this.startingPos[index] = this.problem.agents[index];
}
this.totalCost = costsNode.costs.Sum();
this.maxCost = costsNode.costs.Max();
for (int i = 0; i < this.allMDDs.Length; i++)
{
this.allMDDs[i] = new MDD(agentNums[i], startingPos[i].agent.agentNum, startingPos[i].lastMove,
costsNode.costs[i], maxCost, startingPos.Length, problem, reserved: reserved);
}
this.expanded = 0;
this.generated = 0;
this.matchCounter = 0;
}
/// <summary>
/// Tries to find a solution for the agents with the given cost.
/// </summary>
/// <returns>The solution if found or null otherwise</returns>
public abstract SinglePlan[] Solve(ConflictAvoidanceTable CAT);
public int getGenerated() { return generated; }
protected Dictionary<int, int> conflictCounts;
protected Dictionary<int, List<int>> conflictTimes;
public Dictionary<int, int> GetExternalConflictCounts()
{
return conflictCounts;
}
public Dictionary<int, List<int>> GetConflictTimes()
{
return conflictTimes;
}
}
public class CostTreeNode
{
public int[] costs;
public CostTreeNode(int[] costs)
{
this.costs = costs;
}
public void Expand(Queue<CostTreeNode> openList, HashSet<CostTreeNode> closedList)
{
for (int j = 0; j < costs.Length; j++)
{
int[] newCosts = this.costs.ToArray<int>();
newCosts[j]++;
var child = new CostTreeNode(newCosts);
if (!closedList.Contains(child))
{
closedList.Add(child);
openList.Enqueue(child);
}
}
}
public int Sum(int from, int to)
{
int ans = 0;
for (int i = from; i < to; i++)
{
ans += costs[i];
}
return ans;
}
public override int GetHashCode()
{
unchecked
{
int ans = 0;
for (int i = 0; i < costs.Length; i++)
{
ans += costs[i] * Constants.PRIMES_FOR_HASHING[i % Constants.PRIMES_FOR_HASHING.Length];
}
return ans;
}
}
public override bool Equals(object obj)
{
if (obj == null)
return false;
CostTreeNode check = (CostTreeNode)obj;
return this.costs.SequenceEqual(check.costs);
}
}
/// <summary>
/// Use this one!
/// </summary>
class CostTreeNodeSolverOldMatching : CostTreeNodeSolver
{
/// <summary>
/// Currently the only supported values are 3 and non-3. This is equivalent to bool checkTriples.
/// </summary>
int syncSize;
public CostTreeNodeSolverOldMatching(ProblemInstance problem, Run runner, CostTreeSearchSolver solver)
: base(problem, runner, solver) { }
public CostTreeNodeSolverOldMatching(ProblemInstance problem, CostTreeNode costNode, Run runner, CostTreeSearchSolver solver,
int syncSize, ISet<TimedMove> reserved)
: base(problem, costNode, runner, solver, reserved) { this.syncSize = syncSize; }
public void Setup(CostTreeNode costNode, int syncSize, ISet<TimedMove> reserved)
{
base.Setup(costNode, reserved);
this.syncSize = syncSize;
}
/// <summary>
/// Prunes the individual agent MDDs and, if possible, matches them to return a non-conflicting configuration of
/// paths of the given costs.
/// </summary>
/// <param name="CAT"></param>
/// <returns>
/// null if no solution is found.
/// </returns>
public override SinglePlan[] Solve(ConflictAvoidanceTable CAT)
{
if (this.Prune()) // Everything was pruned
return null;
this.solver.survivedPruningHL++;
A_Star_MDDs findSolution = new A_Star_MDDs(allMDDs, runner, CAT);
SinglePlan[] ans = findSolution.Solve();
this.generated = findSolution.generated;
this.expanded = findSolution.expanded;
this.conflictsNotAvoided = findSolution.conflictCount;
this.conflictCounts = findSolution.GetExternalConflictCounts();
this.conflictTimes = findSolution.GetConflictTimes();
return ans;
}
/// <summary>
/// Prunes the individual agent MDDs and prepares data for matching them
/// </summary>
/// <returns>Whether everything was pruned</returns>
public bool Prune()
{
for (int i = allMDDs.Length - 1; i >= 0; i--)
{
for (int j = i + 1; j < allMDDs.Length; j++)
{
(MDD.PruningDone pruningDone, int matchCounterIncrement) = allMDDs[i].SyncMDDs(allMDDs[j], this.syncSize == 3 && this.allMDDs.Length > 2);
this.matchCounter += matchCounterIncrement;
if (pruningDone == MDD.PruningDone.EVERYTHING)
return true;
}
}
if (allMDDs[0].levels == null)
return true;
return false;
}
}
class CostTreeNodeSolverDDBF : CostTreeNodeSolver
{
public CostTreeNodeSolverDDBF(ProblemInstance problem, Run runner, CostTreeSearchSolver solver)
: base(problem, runner, solver) { }
public CostTreeNodeSolverDDBF(ProblemInstance problem, CostTreeNode costNode, Run runner, CostTreeSearchSolver solver,
ISet<TimedMove> reserved)
: base(problem, costNode, runner, solver, reserved) { }
public override void Setup(CostTreeNode costNode, ISet<TimedMove> reserved)
{
base.Setup(costNode, reserved);
}
public override SinglePlan[] Solve(ConflictAvoidanceTable CAT)
{
for (int i = 0; i < allMDDs.Length; i++)
if (allMDDs[i].levels == null)
return null;
A_Star_MDDs findSolution = new A_Star_MDDs(allMDDs, runner, CAT);
SinglePlan[] ans = findSolution.Solve();
generated = findSolution.generated;
expanded = findSolution.expanded;
conflictsNotAvoided = findSolution.conflictCount;
conflictCounts = findSolution.GetExternalConflictCounts();
conflictTimes = findSolution.GetConflictTimes();
return ans;
}
}
class CostTreeNodeSolverKSimpleMatching : CostTreeNodeSolver
{
int maxGroupChecked;
public CostTreeNodeSolverKSimpleMatching(ProblemInstance problem, Run runner, CostTreeSearchSolver solver)
: base(problem, runner, solver) { }
public CostTreeNodeSolverKSimpleMatching(ProblemInstance problem, CostTreeNode costNode,
Run runner, CostTreeSearchSolver solver, int maxGroupChecked,
ISet<TimedMove> reserved)
: base(problem, costNode, runner, solver, reserved) { this.maxGroupChecked = maxGroupChecked; }
public void Setup(CostTreeNode costNode, int maxGroupChecked, ISet<TimedMove> reserved)
{
base.Setup(costNode, reserved);
this.maxGroupChecked = maxGroupChecked;
}
public override SinglePlan[] Solve(ConflictAvoidanceTable CAT)
{
A_Star_MDDs findSolution;
SinglePlan[] subCheck;
MDD[] match;
MddMatchAndPrune matcher = new MddMatchAndPrune(runner, this);
foreach (MDD checkValid in allMDDs)
{
if (checkValid.levels == null)
return null;
}
if (maxGroupChecked >= 2)
{
match = new MDD[2];
for (int i = allMDDs.Length - 1; i >= 0; i--)
{
for (int j = i + 1; j < allMDDs.Length; j++)
{
match[0] = allMDDs[i];
match[1] = allMDDs[j];
//matcher.initialize(match);
//if (matcher.pruneMDDs() == false)
findSolution = new A_Star_MDDs(match, runner, CAT);
subCheck = findSolution.Solve();
if (subCheck == null || subCheck[0] == null)
{
return null;
}
}
}
}
if (maxGroupChecked >= 3)
{
match = new MDD[3];
for (int i = allMDDs.Length - 2; i >= 0; i--)
{
for (int j = i + 1; j < allMDDs.Length - 1; j++)
{
for (int t = j + 1; t < allMDDs.Length; t++)
{
match[0] = allMDDs[i];
match[1] = allMDDs[j];
match[2] = allMDDs[t];
//matcher.initialize(match);
//if (matcher.pruneMDDs() == false)
findSolution = new A_Star_MDDs(match, runner, CAT);
subCheck = findSolution.Solve();
if (subCheck == null || subCheck[0] == null)
{
return null;
}
}
}
}
}
if (maxGroupChecked >= 4)
{
match = new MDD[4];
for (int i = allMDDs.Length - 3; i >= 0; i--)
{
for (int j = i + 1; j < allMDDs.Length - 2; j++)
{
for (int t = j + 1; t < allMDDs.Length - 1; t++)
{
for (int m = t + 1; m < allMDDs.Length; m++)
{
match[0] = allMDDs[i];
match[1] = allMDDs[j];
match[2] = allMDDs[t];
match[3] = allMDDs[m];
//matcher.initialize(match);
//if (matcher.pruneMDDs() == false)
findSolution = new A_Star_MDDs(match, runner, CAT);
subCheck = findSolution.Solve();
if (subCheck == null || subCheck[0] == null)
{
return null;
}
}
}
}
}
}
this.solver.survivedPruningHL++;
if (allMDDs[0].levels == null)
return null;
findSolution = new A_Star_MDDs(allMDDs, runner, CAT);
SinglePlan[] ans = findSolution.Solve();
generated = findSolution.generated;
expanded = findSolution.expanded;
conflictsNotAvoided = findSolution.conflictCount;
conflictCounts = findSolution.GetExternalConflictCounts();
conflictTimes = findSolution.GetConflictTimes();
return ans;
}
}
class CostTreeNodeSolverRepeatedMatching : CostTreeNodeSolver
{
int syncSize;
public CostTreeNodeSolverRepeatedMatching(ProblemInstance problem, Run runner, CostTreeSearchSolver solver)
: base(problem, runner, solver) { }
public CostTreeNodeSolverRepeatedMatching(ProblemInstance problem, CostTreeNode costNode,
Run runner, CostTreeSearchSolver solver, int syncSize,
ISet<TimedMove> reserved)
: base(problem, costNode, runner, solver, reserved) { this.syncSize = syncSize; }
public void setup(CostTreeNode costNode, int syncSize, ISet<TimedMove> reserved)
{
base.Setup(costNode, reserved);
this.syncSize = syncSize;
}
public override SinglePlan[] Solve(ConflictAvoidanceTable CAT)
{
MDD[] match = new MDD[2];
bool Converging = true;
int[] changed = new int[allMDDs.Length];
int currentIteration = 0;
MDD.PruningDone conflictStatus = MDD.PruningDone.NOTHING;
while (Converging)
{
currentIteration++;
Converging = false;
for (int i = allMDDs.Length - 1; i >= 0; i--)
{
for (int j = i + 1; j < allMDDs.Length; j++)
{
if (changed[i] >= currentIteration - 1 || changed[j] >= currentIteration - 1) // If at least one of the two MDDs was changed during the last iteration
{
int matchCounterIncrement;
(conflictStatus, matchCounterIncrement) = allMDDs[i].SyncMDDs(allMDDs[j], this.syncSize == 3);
this.matchCounter += matchCounterIncrement;
if (conflictStatus == MDD.PruningDone.EVERYTHING)
{
return null;
}
else if (conflictStatus == MDD.PruningDone.SOME)
{
changed[i] = currentIteration;
Converging = true;
}
(conflictStatus, matchCounterIncrement) = allMDDs[i].SyncMDDs(allMDDs[j], this.syncSize == 3);
this.matchCounter += matchCounterIncrement;
if (conflictStatus == MDD.PruningDone.EVERYTHING)
{
return null;
}
else if (conflictStatus == MDD.PruningDone.SOME)
{
changed[i] = currentIteration;
Converging = true;
}
}
}
}
}
this.solver.survivedPruningHL++;
if (allMDDs[0].levels == null)
return null;
A_Star_MDDs findSolution = new A_Star_MDDs(allMDDs, runner, CAT);
SinglePlan[] ans = findSolution.Solve();
generated = findSolution.generated;
expanded = findSolution.expanded;
conflictsNotAvoided = findSolution.conflictCount;
return ans;
}
}