-
Notifications
You must be signed in to change notification settings - Fork 3
/
A_Star_MDDs.cs
724 lines (640 loc) · 29.7 KB
/
A_Star_MDDs.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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
using System;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
using ExtensionMethods;
namespace mapf
{
/// <summary>
/// Finds the solution with the least number of conflicts, given a set of MDDs
/// </summary>
class A_Star_MDDs : IConflictReporting
{
MDD[] problem;
Dictionary<A_Star_MDDs_Node, A_Star_MDDs_Node> closedList;
Run runner;
BinaryHeap<A_Star_MDDs_Node> openList;
public int expanded;
public int generated;
public int conflictCount;
ConflictAvoidanceTable CAT;
public A_Star_MDDs(MDD[] problem, Run runner, ConflictAvoidanceTable CAT)
{
this.expanded = 0;
this.generated = 0;
A_Star_MDDs_Node root;
this.problem = problem;
this.runner = runner;
this.CAT = CAT;
this.closedList = new Dictionary<A_Star_MDDs_Node, A_Star_MDDs_Node>();
this.openList = new BinaryHeap<A_Star_MDDs_Node>();
MDDNode[] sRoot = new MDDNode[problem.Length];
for (int i = 0; i < problem.Length; i++)
{
sRoot[i] = problem[i].levels[0].First.Value;
}
root = new A_Star_MDDs_Node(sRoot, null);
openList.Add(root);
closedList.Add(root, root); // There will never be a hit. This is only done for consistancy
conflictCount = 0;
}
public SinglePlan[] Solve()
{
A_Star_MDDs_Node currentNode;
//A_Star_MDDs_Expander expander = new A_Star_MDDs_Expander();
while (openList.Count > 0)
{
if (runner.ElapsedMilliseconds() > Constants.MAX_TIME)
{
return null;
}
currentNode = openList.Remove();
// Check if node is the goal
if (this.GoalTest(currentNode))
{
this.conflictCount = currentNode.conflictCount;
this.conflictCounts = currentNode.conflictCounts;
this.conflictTimes = currentNode.conflictTimes;
return GetAnswer(currentNode);
}
// Expand
expanded++; // TODO: don't count re-expansions as expansions?
Expand(currentNode);
//expander.Setup(currentNode);
//Expand(expander); // TODO: the expander just generates all children. EPEA* its ass!!
}
return null;
}
public void Expand(A_Star_MDDs_Node node)
{
if (node.IsAlreadyExpanded() == false)
{
node.calcSingleAgentDeltaConflictCounts(this.CAT);
node.alreadyExpanded = true;
node.targetDeltaConflictCount = 0;
node.remainingDeltaConflictCount = node.targetDeltaConflictCount; // Just for the following hasChildrenForCurrentDeltaConflictCount call.
while (node.hasMoreChildren() && node.hasChildrenForCurrentDeltaConflictCount() == false) // DeltaConflictCount==0 may not be possible if all agents have obstacles between their location and the goal
{
node.targetDeltaConflictCount++;
node.remainingDeltaConflictCount = node.targetDeltaConflictCount;
}
if (node.hasMoreChildren() == false) // Node has no possible children at all
{
node.ClearExpansionData();
return;
}
}
var intermediateNodes = new List<A_Star_MDDs_Node>() { node };
for (int mddIndex = 0; mddIndex < this.problem.Length && intermediateNodes.Count != 0; ++mddIndex)
{
if (runner.ElapsedMilliseconds() > Constants.MAX_TIME)
return;
intermediateNodes = ExpandOneAgent(intermediateNodes, mddIndex);
}
var finalGeneratedNodes = intermediateNodes;
foreach (var child in finalGeneratedNodes)
{
child.conflictCount = node.conflictCount + node.targetDeltaConflictCount;
// Accumulating the conflicts count from parent to child
// We're counting conflicts along the entire path, so the parent's conflicts count is added to the child's:
child.conflictCounts = new Dictionary<int, int>(child.prev.conflictCounts);
child.conflictTimes = new Dictionary<int, List<int>>();
foreach (var kvp in child.prev.conflictTimes)
child.conflictTimes[kvp.Key] = new List<int>(kvp.Value);
child.IncrementConflictCounts(this.CAT); // We're counting conflicts along the entire path, so the parent's conflicts count
// is added to the child's.
bool was_closed = this.closedList.ContainsKey(child);
if (was_closed)
{
A_Star_MDDs_Node inClosedList = this.closedList[child];
if (inClosedList.conflictCount > child.conflictCount)
{
closedList.Remove(inClosedList);
openList.Remove(inClosedList);
was_closed = false;
}
}
if (!was_closed)
{
this.openList.Add(child);
this.closedList.Add(child, child);
generated++;
}
}
// Prepare the node for the next partial expansion:
if (node.IsAlreadyExpanded() == false)
{
// Node was cleared during expansion.
// It's unnecessary and unsafe to continue to prepare it for the next partial expansion.
return;
}
node.targetDeltaConflictCount++; // This delta F was exhausted
node.remainingDeltaConflictCount = node.targetDeltaConflictCount;
while (node.hasMoreChildren() && node.hasChildrenForCurrentDeltaConflictCount() == false)
{
node.targetDeltaConflictCount++;
node.remainingDeltaConflictCount = node.targetDeltaConflictCount; // Just for the following hasChildrenForCurrentDeltaF call.
}
if (node.hasMoreChildren() && node.hasChildrenForCurrentDeltaConflictCount())
{
// Re-insert node into open list
openList.Add(node);
}
else
node.ClearExpansionData();
}
protected List<A_Star_MDDs_Node> ExpandOneAgent(List<A_Star_MDDs_Node> intermediateNodes, int mddIndex)
{
var generated = new List<A_Star_MDDs_Node>();
// Expand the mdd node
foreach (A_Star_MDDs_Node node in intermediateNodes)
{
// Try all the children of this MDD node
foreach ((int childIndex, MDDNode childMddNode) in node.allSteps[mddIndex].children.Enumerate())
{
if (node.currentMoves != null && childMddNode.move.IsColliding(node.currentMoves)) // Can happen. We only prune partially, we don't build the full k-agent MDD.
continue;
var childNode = new A_Star_MDDs_Node(node, mddIndex != node.allSteps.Length - 1);
childNode.allSteps[mddIndex] = childMddNode;
// Update target conflict count and prune nodes that can't get to the target conflict count
childNode.UpdateRemainingDeltaConflictCount(mddIndex, childIndex);
if (childNode.remainingDeltaConflictCount == ushort.MaxValue || // Last move was bad - not sure this can happen here
(childNode.hasChildrenForCurrentDeltaConflictCount(mddIndex + 1) == false)) // No children that can reach the target
continue;
if (mddIndex < node.allSteps.Length - 1) // More MDD nodes need to choose a child
childNode.currentMoves.Add(childMddNode.move);
else // Moved the last agent
childNode.currentMoves = null; // To reduce memory load and lookup times
// Set the node's prev to its real parent, skipping over the intermediate nodes.
if (mddIndex != 0)
childNode.prev = node.prev;
else
childNode.prev = node;
generated.Add(childNode);
}
}
return generated;
}
protected Dictionary<int, int> conflictCounts;
protected Dictionary<int, List<int>> conflictTimes;
/// <summary>
/// </summary>
/// <returns>Map each external agent to the number of conflicts with their path the solution has</returns>
public Dictionary<int, int> GetExternalConflictCounts()
{
return this.conflictCounts;
}
/// <summary>
/// </summary>
/// <returns>Map each external agent to a list of times the solution has a conflict with theirs</returns>
public Dictionary<int, List<int>> GetConflictTimes()
{
return this.conflictTimes;
}
public void Expand(A_Star_MDDs_Expander currentNode)
{
while (true)
{
A_Star_MDDs_Node child = currentNode.GetNextChild();
if (child == null)
break;
if (IsLegalMove(child))
{
child.conflictCount = child.prev.conflictCount;
child.UpdateConflicts(CAT);
bool was_closed = this.closedList.ContainsKey(child);
if (was_closed)
{
A_Star_MDDs_Node inClosedList = this.closedList[child];
if (inClosedList.conflictCount > child.conflictCount)
{
closedList.Remove(inClosedList);
openList.Remove(inClosedList);
was_closed = false;
}
}
if (!was_closed)
{
this.openList.Add(child);
this.closedList.Add(child, child);
generated++;
}
}
}
}
public int GetGenerated() { return this.generated; }
public int GetExpanded() { return this.expanded; }
private bool GoalTest(A_Star_MDDs_Node toCheck)
{
if (toCheck.GetDepth() == problem[0].levels.Length - 1)
return true;
return false;
}
private SinglePlan[] GetAnswer(A_Star_MDDs_Node finish)
{
// TODO: Move the construction of the SinglePlans to a static method in SinglePlan
var routes = new LinkedList<Move>[problem.Length];
for (int i = 0; i < routes.Length; i++)
routes[i] = new LinkedList<Move>();
A_Star_MDDs_Node current = finish;
while (current != null)
{
for (int i = 0; i < problem.Length; i++)
{
routes[i].AddFirst(new Move(current.allSteps[i].move));
}
current = current.prev;
}
var ans = new SinglePlan[problem.Length];
for (int i = 0; i < ans.Length; i++)
ans[i] = new SinglePlan(routes[i], i);
return ans;
}
private bool CheckIfLegal(MDDNode to1, MDDNode to2)
{
return to1.move.IsColliding(to2.move) == false;
}
private bool IsLegalMove(A_Star_MDDs_Node to)
{
if (to == null)
return false;
if (to.prev == null)
return true;
for (int i = 0; i < problem.Length; i++)
{
for (int j = i + 1; j < to.allSteps.Length; j++)
{
if (CheckIfLegal(to.allSteps[i], to.allSteps[j]) == false)
return false;
}
}
return true;
}
}
class A_Star_MDDs_Node : IComparable<IBinaryHeapItem>, IBinaryHeapItem
{
/// <summary>
/// The last move of all agents that have already moved in this turn.
/// Used for making sure the next agent move doesn't collide with moves already made.
/// Used while generating this node, nullified when done.
/// </summary>
public HashSet<TimedMove> currentMoves;
public MDDNode[] allSteps;
public A_Star_MDDs_Node prev;
public int conflictCount;
public Dictionary<int, int> conflictCounts;
public Dictionary<int, List<int>> conflictTimes;
int binaryHeapIndex;
public bool alreadyExpanded;
/// <summary>
/// Starts at zero, incremented after a node is expanded once. Set on Expand.
/// </summary>
public ushort targetDeltaConflictCount = 0;
/// <summary>
/// Remaining delta conflict count towards targetDeltaConflictCount. Reset on Expand.
/// </summary>
public ushort remainingDeltaConflictCount;
/// <summary>
/// For each MDD node and each child it has, the effect of that choosing that child on the conflict count.
/// byte.MaxValue means this is an illegal move. Only computed on demand.
/// </summary>
protected byte[][] singleAgentDeltaConflictCounts;
/// <summary>
/// Only computed on demand
/// </summary>
protected ushort maxDeltaConflictCount;
/// <summary>
/// Per each MDD node and delta conflict count, has 1 if that delta F is achievable by choosing child MDD nodes starting from this one on,
/// -1 if it isn't, and 0 if we don't know yet.
/// Only computed on demand
/// </summary>
protected sbyte[][] conflictCountLookup;
/// <summary>
/// From generated nodes. Allows expansion table to be garbage collected before all generated nodes are expanded.
/// </summary>
public void ClearExpansionData()
{
this.singleAgentDeltaConflictCounts = null;
this.conflictCountLookup = null;
this.currentMoves = null;
}
/// <summary>
/// Counts the number of times this node collides with each agent move in the conflict avoidance table.
/// </summary>
/// <param name="CAT"></param>
/// <returns></returns>
public virtual void IncrementConflictCounts(ConflictAvoidanceTable CAT)
{
foreach (var mddNode in this.allSteps)
{
mddNode.move.IncrementConflictCounts(CAT, this.conflictCounts, this.conflictTimes);
}
}
/// <summary>
/// Returns whether all possible f values were generated from this node already
/// </summary>
/// <returns></returns>
public bool hasMoreChildren()
{
return this.targetDeltaConflictCount <= this.maxDeltaConflictCount;
}
public bool IsAlreadyExpanded()
{
return alreadyExpanded;
}
public bool hasChildrenForCurrentDeltaConflictCount(int agentNum = 0)
{
return existsChildForConflictCount(agentNum, this.remainingDeltaConflictCount);
}
/// <summary>
/// An MDD child node was chosen between calculating the singleAgentDeltaConflictCounts and this call.
/// Using the data that describes its delta conflict count potential before the move.
/// </summary>
/// <param name="mddIndex">which mdd's node was expanded to create this A*_MDDs node</param>
/// <param name="childIndex">which of the mdd node's children was just chosen</param>
public void UpdateRemainingDeltaConflictCount(int mddIndex, int childIndex)
{
if (this.remainingDeltaConflictCount == ushort.MaxValue)
Trace.Assert(false,
$"Remaining deltaConflictCount is ushort.MaxValue, a reserved value with special meaning. agentIndex={mddIndex}");
byte lastMoveDeltaConflictCount = this.singleAgentDeltaConflictCounts[mddIndex][childIndex];
if (lastMoveDeltaConflictCount != byte.MaxValue && this.remainingDeltaConflictCount >= lastMoveDeltaConflictCount)
this.remainingDeltaConflictCount -= lastMoveDeltaConflictCount;
else
this.remainingDeltaConflictCount = ushort.MaxValue; // Either because last move was illegal or because the delta F from the last move was more than the entire remaining delta F budget
}
/// <summary>
/// Recursive func. Kind of dynamic programming as it updates the lookup table as it goes to refrain from computing answers twice.
/// </summary>
/// <param name="mddIndex"></param>
/// <param name="remainingTargetDeltaF"></param>
/// <returns></returns>
protected bool existsChildForConflictCount(int mddIndex, ushort remainingTargetDeltaConflictCount)
{
// Stopping conditions:
if (mddIndex == this.allSteps.Length)
{
if (remainingTargetDeltaConflictCount == 0)
return true;
return false;
}
if (conflictCountLookup[mddIndex][remainingTargetDeltaConflictCount] != 0) // Answer known (arrays are initialized to zero). TODO: Replace the magic.
{
return conflictCountLookup[mddIndex][remainingTargetDeltaConflictCount] == 1; // Return known answer. TODO: Replace the magic
}
// Recursive actions:
for (int i = 0; i < this.allSteps[mddIndex].children.Count; i++)
{
if (singleAgentDeltaConflictCounts[mddIndex][i] > remainingTargetDeltaConflictCount) // Small optimization - no need to make the recursive
// call just to request a negative target from it and
// get false (because we assume the heuristic function
// is consistent)
continue;
if (existsChildForConflictCount(mddIndex + 1,
(byte)(remainingTargetDeltaConflictCount - singleAgentDeltaConflictCounts[mddIndex][i])))
{
conflictCountLookup[mddIndex][remainingTargetDeltaConflictCount] = 1;
return true;
}
}
conflictCountLookup[mddIndex][remainingTargetDeltaConflictCount] = -1;
return false;
}
/// <summary>
/// Calculates for each MDD node and each of its children, the effect of that move on the conflict count.
/// Also calcs maxDeltaConflictCount.
/// Note: Currently avoids the CAT's avoidanceGoal. To compute each individual agent's effect on the count of groups we conflict with would require
/// tracking conflicts as a set of groups we conflict with instead of as a sum of conflicts, and would require 2^(num agents) cells in each singleAgentDeltaConflictCounts[i].
/// </summary>
/// <param name="CAT"></param>
/// <returns></returns>
public void calcSingleAgentDeltaConflictCounts(ConflictAvoidanceTable CAT)
{
// Init
this.singleAgentDeltaConflictCounts = new byte[this.allSteps.Length][];
for (int i = 0; i < this.allSteps.Length; i++)
{
this.singleAgentDeltaConflictCounts[i] = new byte[this.allSteps[i].children.Count];
}
int conflictCountAfter;
this.maxDeltaConflictCount = 0;
// Set values
for (int i = 0; i < this.allSteps.Length; i++)
{
int singleAgentMaxLegalDeltaConflictCount = -1;
foreach ((int childIndex, MDDNode child) in this.allSteps[i].children.Enumerate())
{
if (CAT != null)
{
conflictCountAfter = CAT[child.move].Count;
}
else
conflictCountAfter = 0;
singleAgentDeltaConflictCounts[i][childIndex] = (byte)conflictCountAfter;
singleAgentMaxLegalDeltaConflictCount = Math.Max(singleAgentMaxLegalDeltaConflictCount, singleAgentDeltaConflictCounts[i][childIndex]);
}
if (singleAgentMaxLegalDeltaConflictCount == -1) // No legal action for this agent, so no legal children exist for this node
{
this.maxDeltaConflictCount = 0; // Can't make it negative without widening the field.
break;
}
this.maxDeltaConflictCount += (byte)singleAgentMaxLegalDeltaConflictCount;
}
conflictCountLookup = new sbyte[this.allSteps.Length][];
for (int i = 0; i < conflictCountLookup.Length; i++)
{
conflictCountLookup[i] = new sbyte[this.maxDeltaConflictCount + 1]; // Towards the last agents most of the row will be wasted (the last one can do delta F of 0 or 1),
// but it's easier than fiddling with array sizes
}
}
public A_Star_MDDs_Node(MDDNode[] allSteps, A_Star_MDDs_Node prevStep)
{
this.allSteps = allSteps;
this.prev = prevStep;
this.currentMoves = null; // All non-intermediate nodes have currentMoves == null
this.conflictCount = 0;
// Initialize conflict tracking data structures
this.conflictCounts = new Dictionary<int, int>();
this.conflictTimes = new Dictionary<int, List<int>>();
}
/// <summary>
/// Copy constructor
/// </summary>
public A_Star_MDDs_Node(A_Star_MDDs_Node cpy, bool createIntermediate)
{
this.allSteps = new MDDNode[cpy.allSteps.Length];
for (int i = 0; i < allSteps.Length; i++)
{
this.allSteps[i] = cpy.allSteps[i];
}
this.prev = cpy.prev;
if (cpy.currentMoves != null)
{
// cpy is an intermediate node
if (createIntermediate)
this.currentMoves = new HashSet<TimedMove>(cpy.currentMoves);
else
this.currentMoves = cpy.currentMoves; // We're not going to add anything currentMoves
}
else
// cpy is a concrete node
this.currentMoves = new HashSet<TimedMove>(capacity: cpy.allSteps.Length);
// The conflictTimes and conflictCounts are only copied later if necessary.
alreadyExpanded = false; // Creating a new unexpanded node from cpy
// For intermediate nodes created during expansion (fully expanded nodes have these fields recalculated when they're expanded)
targetDeltaConflictCount = cpy.targetDeltaConflictCount; // Just to ease debugging
remainingDeltaConflictCount = cpy.remainingDeltaConflictCount;
singleAgentDeltaConflictCounts = cpy.singleAgentDeltaConflictCounts; // For the UpdateRemainingDeltaConflictCount call on temporary nodes.
// Notice that after an agent is moved its row won't be up-to-date.
conflictCountLookup = cpy.conflictCountLookup; // For the hasChildrenForCurrentDeltaConflictCount call on temporary nodes.
// Notice that after an agent is moved, all rows up to and including the one of the agent that moved
// won't be up-to-date.
maxDeltaConflictCount = cpy.maxDeltaConflictCount; // Not necessarily achievable after some of the agents moved.
// The above is OK because we won't be using data for agents that already moved.
}
/// <summary>
/// Only compares the steps.
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public override bool Equals(object obj)
{
if (obj == null)
return false;
A_Star_MDDs_Node comp = (A_Star_MDDs_Node)obj;
return this.allSteps.SequenceEqual<MDDNode>(comp.allSteps);
}
/// <summary>
/// Only uses the steps
/// </summary>
/// <returns></returns>
public override int GetHashCode()
{
unchecked
{
int code = 0;
for (int i = 0; i < allSteps.Length; i++)
{
code += allSteps[i].GetHashCode() * Constants.PRIMES_FOR_HASHING[i % Constants.PRIMES_FOR_HASHING.Length];
}
return code;
}
}
public int GetDepth() { return allSteps[0].move.time; }
/// <summary>
/// Updates the conflictCount member according to given CATs. Table may be null.
/// </summary>
/// <param name="CAT"></param>
public void UpdateConflicts(ConflictAvoidanceTable CAT)
{
if (this.prev == null)
return;
if (CAT != null)
{
for (int i = 0; i < allSteps.Length; i++)
{
if (CAT.ContainsKey(allSteps[i].move))
conflictCount += CAT[allSteps[i].move].Count;
}
}
}
/// <summary>
/// BH_Item implementation
/// </summary>
/// <returns></returns>
public int GetIndexInHeap() { return binaryHeapIndex; }
/// <summary>
/// BH_Item implementation
/// </summary>
/// <returns></returns>
public void SetIndexInHeap(int index) { binaryHeapIndex = index; }
/// <summary>
/// Prefers fewer conflicts. If the number of conflicts is the same, prefers more depth.
/// </summary>
/// <param name="other"></param>
/// <returns></returns>
public int CompareTo(IBinaryHeapItem other)
{
A_Star_MDDs_Node that = (A_Star_MDDs_Node)other;
if (this.conflictCount + this.targetDeltaConflictCount < that.conflictCount + that.targetDeltaConflictCount)
return -1;
if (this.conflictCount + this.targetDeltaConflictCount > that.conflictCount + that.targetDeltaConflictCount)
return 1;
if (this.GetDepth() > that.GetDepth())
return -1;
if (this.GetDepth() < that.GetDepth())
return 1;
return 0;
}
}
class A_Star_MDDs_Expander
{
private A_Star_MDDs_Node a_star_mdd_node;
/// <summary>
/// For each MDD, the index of the next child in <children> to choose
/// </summary>
int[] chosenChild;
public A_Star_MDDs_Expander() { }
public A_Star_MDDs_Expander(A_Star_MDDs_Node a_star_mdd_node)
{
this.a_star_mdd_node = a_star_mdd_node;
this.chosenChild = new int[a_star_mdd_node.allSteps.Length];
foreach (MDDNode mddNode in a_star_mdd_node.allSteps)
{
if (mddNode.children.Count == 0)
{
chosenChild[0] = -1;
break;
}
}
}
public void Setup(A_Star_MDDs_Node a_star_mdd_node)
{
this.a_star_mdd_node = a_star_mdd_node;
this.chosenChild = new int[a_star_mdd_node.allSteps.Length];
foreach (MDDNode mddNode in a_star_mdd_node.allSteps)
{
if (mddNode.children.Count == 0)
{
this.chosenChild[0] = -1;
break;
}
}
}
/// <summary>
///
/// </summary>
/// <returns>The next child, or null if there aren't any more</returns>
public A_Star_MDDs_Node GetNextChild()
{
if (this.chosenChild[0] == -1)
return null;
var mddNodes = new MDDNode[a_star_mdd_node.allSteps.Length];
for (int i = 0; i < mddNodes.Length; i++)
{
mddNodes[i] = a_star_mdd_node.allSteps[i].children.ElementAt(this.chosenChild[i]);
}
SetNextChildIndices();
return new A_Star_MDDs_Node(mddNodes, a_star_mdd_node);
}
/// <summary>
/// Increments the chosenChild indices array by 1
/// </summary>
private void SetNextChildIndices()
{
SetNextChildIndices(this.chosenChild.Length - 1);
}
private void SetNextChildIndices(int agentNum)
{
if (agentNum == -1)
this.chosenChild[0] = -1;
else if (this.chosenChild[agentNum] < a_star_mdd_node.allSteps[agentNum].children.Count - 1)
this.chosenChild[agentNum]++;
else
{
this.chosenChild[agentNum] = 0;
SetNextChildIndices(agentNum - 1);
}
}
}
}