-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathIndependenceDetection.cs
More file actions
1080 lines (963 loc) · 49.6 KB
/
IndependenceDetection.cs
File metadata and controls
1080 lines (963 loc) · 49.6 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
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
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Linq;
using System.Collections.Generic;
using System.IO;
using System.Diagnostics;
using ExtensionMethods;
namespace mapf;
class IndependenceDetection : ISolver
{
protected LinkedList<IndependenceDetectionAgentsGroup> allGroups;
/// <summary>
/// For each group in the problem instance, maps group nums it conflicts with to the number of conflicts betweem them.
/// (Indices that aren't group nums are null)
/// </summary>
public Dictionary<int, int>[] conflictCountsPerGroup;
/// <summary>
/// For each group in the problem instance, maps group nums of groups it collides with to the times they conflict.
/// (Indices that aren't group nums are null)
/// </summary>
public Dictionary<int, List<int>>[] conflictTimesPerGroup;
/// <summary>
/// For each group in the problem instance, saves the number of agents from the problem instance that it conflicts with.
/// Used for choosing the next conflict to resolve.
/// </summary>
public int[] countsOfGroupsThatConflict;
public int totalConflictCount = 0;
protected ProblemInstance instance;
protected int expanded; // TODO: remove and just rely on the subsolvers to accumulate
protected int generated; // TODO: remove and just rely on the subsolvers to accumulate
protected int resolutionAttempts;
protected int resolutionSuccesses;
protected int merges;
protected int accExpanded;
protected int accGenerated;
protected int accResolutionAttempts;
protected int accResolutionSuccesses;
protected int accMerges;
public bool provideGroupCostsToSolver;
public int totalCost;
protected Run runner;
/// <summary>
/// The complete plan for all the agents that was found.
/// </summary>
public Plan plan;
protected int maxGroupSize;
protected int minGroupSize;
protected int accMaxGroupSize;
protected int accMinGroupSize;
public IIndependenceDetectionSolver groupSolver;
public IIndependenceDetectionSolver singleAgentSolver;
private ISet<IndependenceDetectionConflict> resolutionAttemptedFirstGroup;
private ISet<IndependenceDetectionConflict> resolutionAttemptedSecondGroup;
private int solutionDepth;
private ConflictAvoidanceTable conflictAvoidanceTable;
private int maxSolutionCostFound; // FIXME: Maintained but not used
private ConflictAvoidanceTable.AvoidanceGoal avoidanceGoal;
private bool simple;
public IndependenceDetection(IIndependenceDetectionSolver singleAgentSolver, IIndependenceDetectionSolver groupSolver,
ConflictChoice conflictChoice = ConflictChoice.MOST_CONFLICTING_SMALLEST_AGENTS,
bool provideGroupCostsToSolver = true,
ConflictAvoidanceTable.AvoidanceGoal avoidanceGoal = ConflictAvoidanceTable.AvoidanceGoal.MINIMIZE_CONFLICTING_GROUPS, // The effect of a conflict between two groups is total in ID - they're either fully merged or try to fully avoid each other's plan
bool simple = false
)
{
this.singleAgentSolver = singleAgentSolver;
this.groupSolver = groupSolver;
this.conflictChoice = conflictChoice;
this.provideGroupCostsToSolver = provideGroupCostsToSolver;
this.avoidanceGoal = avoidanceGoal;
this.simple = simple;
}
public void Clear()
{
this.allGroups.Clear();
this.singleAgentSolver.Clear();
this.groupSolver.Clear();
this.resolutionAttemptedFirstGroup.Clear();
this.resolutionAttemptedSecondGroup.Clear();
this.conflictAvoidanceTable.Clear();
this.solutionDepth = -1;
this.maxSolutionCostFound = -1;
}
public void Setup(ProblemInstance instance, Run runner)
{
this.instance = instance;
this.runner = runner;
this.totalCost = 0;
this.ClearStatistics();
this.conflictAvoidanceTable = new ConflictAvoidanceTable();
this.conflictAvoidanceTable.avoidanceGoal = this.avoidanceGoal;
this.resolutionAttemptedFirstGroup = new HashSet<IndependenceDetectionConflict>();
this.resolutionAttemptedSecondGroup = new HashSet<IndependenceDetectionConflict>();
this.allGroups = new LinkedList<IndependenceDetectionAgentsGroup>();
// Initialize the agent group collection with a group for every agent
foreach (AgentState agentStartState in instance.agents)
{
this.allGroups.AddLast(new IndependenceDetectionAgentsGroup(
this.instance, new AgentState[1] { agentStartState },
this.singleAgentSolver, this.groupSolver, this)
);
this.conflictAvoidanceTable.agentSizes[agentStartState.agent.agentNum] = 1;
this.conflictAvoidanceTable.agentConflictCounts[agentStartState.agent.agentNum] = 0;
}
conflictCountsPerGroup = new Dictionary<int, int>[instance.GetNumOfAgents()];
conflictTimesPerGroup = new Dictionary<int, List<int>>[instance.GetNumOfAgents()];
for (int i = 0; i < instance.GetNumOfAgents(); i++)
{
conflictCountsPerGroup[i] = new Dictionary<int, int>();
conflictTimesPerGroup[i] = new Dictionary<int, List<int>>();
}
countsOfGroupsThatConflict = new int[instance.GetNumOfAgents()];
}
public virtual String GetName() { return $"{groupSolver.GetName()}+ID({conflictChoice} ProvideInfoToSubsolver={provideGroupCostsToSolver})"; }
/// <summary>
/// Calculate the full plan for all the agents that has been found by the algorithm
/// </summary>
public Plan CalculateJointPlan()
{
var singlePlans = new SinglePlan[this.instance.GetNumOfAgents()];
foreach (var group in this.allGroups)
{
var groupPlan = group.GetPlan();
int i = 0;
foreach (var agentState in group.allAgentsState)
{
singlePlans[agentState.agent.agentNum] = new SinglePlan(groupPlan, i, agentState.agent.agentNum);
i++;
}
}
return new Plan(singlePlans);
}
public Plan GetPlan()
{
return this.plan;
}
public int GetSolutionCost() { return this.totalCost; }
public virtual void OutputStatisticsHeader(TextWriter output)
{
// TODO: Use the solver's statistics, as done in CBS.
output.Write(this.ToString() + " Expanded");
output.Write(Run.RESULTS_DELIMITER);
output.Write(this.ToString() + " Generated");
output.Write(Run.RESULTS_DELIMITER);
output.Write(this.ToString() + " Max Group Size");
output.Write(Run.RESULTS_DELIMITER);
output.Write(this.ToString() + " Min Group Size");
output.Write(Run.RESULTS_DELIMITER);
output.Write(this.ToString() + " Resolution Attempts");
output.Write(Run.RESULTS_DELIMITER);
output.Write(this.ToString() + " Resolution Successes");
output.Write(Run.RESULTS_DELIMITER);
output.Write(this.ToString() + " Merges");
output.Write(Run.RESULTS_DELIMITER);
}
/// <summary>
/// Prints statistics of a single run to the given output.
/// </summary>
public void OutputStatistics(TextWriter output)//, BsonDocument row)
{
Console.WriteLine($"Total Expanded Nodes: {this.expanded}");
Console.WriteLine($"Total Generated Nodes: {this.generated}");
output.Write(this.expanded + Run.RESULTS_DELIMITER);
output.Write(this.generated + Run.RESULTS_DELIMITER);
this.minGroupSize = this.allGroups.Min(group => group.Size()); // MaxGroupSize is computed every time we merge
Console.WriteLine($"Max Group: {this.maxGroupSize}");
Console.WriteLine($"Min Group: {this.minGroupSize}");
output.Write(this.maxGroupSize + Run.RESULTS_DELIMITER);
output.Write(this.minGroupSize + Run.RESULTS_DELIMITER);
Console.WriteLine($"Resolution Attempts: {this.resolutionAttempts}");
Console.WriteLine($"Resolution Successes: {this.resolutionSuccesses}");
Console.WriteLine($"Merges: {this.merges}");
output.Write(this.resolutionAttempts + Run.RESULTS_DELIMITER);
output.Write(this.resolutionSuccesses + Run.RESULTS_DELIMITER);
output.Write(this.merges + Run.RESULTS_DELIMITER);
}
public int NumStatsColumns
{
get
{
return 7;
}
}
public void ClearStatistics()
{
this.expanded = 0;
this.generated = 0;
this.maxGroupSize = 1;
this.minGroupSize = instance.agents.Length;
this.resolutionAttempts = 0;
this.resolutionSuccesses = 0;
this.merges = 0;
}
public void ClearAccumulatedStatistics()
{
this.accExpanded = 0;
this.accGenerated = 0;
this.accMaxGroupSize = 1;
this.accMinGroupSize = this.instance.agents.Length;
this.accResolutionAttempts = 0;
this.accResolutionSuccesses = 0;
this.accMerges = 0;
}
public void AccumulateStatistics()
{
this.accExpanded += this.expanded;
this.accGenerated += this.generated;
this.accMaxGroupSize = Math.Max(this.accMaxGroupSize, this.maxGroupSize);
this.accMinGroupSize = Math.Min(this.accMinGroupSize, this.minGroupSize);
this.accResolutionAttempts += this.resolutionAttempts;
this.accResolutionSuccesses += this.resolutionSuccesses;
this.accMerges += this.merges;
}
public void OutputAccumulatedStatistics(TextWriter output)
{
Console.WriteLine($"{this} Accumulated Expanded Nodes (Low-Level): {this.accExpanded}");
Console.WriteLine($"{this} Accumulated Generated Nodes (Low-Level): {this.accGenerated}");
output.Write(this.accExpanded + Run.RESULTS_DELIMITER);
output.Write(this.accGenerated + Run.RESULTS_DELIMITER);
Console.WriteLine($"{this} Accumulated Max Group (Low-Level): {this.accMaxGroupSize}");
Console.WriteLine($"{this} Accumulated Min Group (Low-Level): {this.accMinGroupSize}");
output.Write(this.accMaxGroupSize + Run.RESULTS_DELIMITER);
output.Write(this.accMinGroupSize + Run.RESULTS_DELIMITER);
Console.WriteLine($"{this} Accumulated resolution attempts (Low-Level): {this.accResolutionAttempts}");
Console.WriteLine($"{this} Accumulated resolution successes (Low-Level): {this.accResolutionSuccesses}");
Console.WriteLine($"{this} Accumulated merges (Low-Level): {this.accMerges}");
output.Write(this.accResolutionAttempts + Run.RESULTS_DELIMITER);
output.Write(this.accResolutionSuccesses + Run.RESULTS_DELIMITER);
output.Write(this.accMerges + Run.RESULTS_DELIMITER);
}
/// <summary>
/// Also calculates min group size along the way.
/// FIXME: Code dup!
/// </summary>
/// <returns></returns>
public int GetMaxGroupSize()
{
this.maxGroupSize = this.allGroups.Max(group => group.allAgentsState.Length);
this.minGroupSize = this.allGroups.Min(group => group.allAgentsState.Length);
return this.maxGroupSize;
}
public enum ConflictChoice : byte
{
FIRST = 0,
MOST_CONFLICTING_SMALLEST_RESULTING_GROUP,
MOST_CONFLICTING_AND_SMALLEST_GROUP,
LEAST_CONFLICTING_LARGEST_RESULTING_GROUP,
MOST_CONFLICTING_SMALLEST_AGENTS
}
public ConflictChoice conflictChoice;
/// <summary>
/// Simulates the execution of the plans found for the different groups.
/// If there are conflicting plans - return the conflicting groups.
/// </summary>
/// <returns>A conflict object with data about the found conflict, or null if no conflict exists</returns>
public IndependenceDetectionConflict ChooseConflict()
{
if (this.allGroups.Count == 1) // A single group can't conflict with itself
return null;
if (totalConflictCount == 0)
return null;
if (this.conflictChoice == IndependenceDetection.ConflictChoice.FIRST)
{
return this.ChooseFirstConflict();
}
else if (this.conflictChoice == IndependenceDetection.ConflictChoice.MOST_CONFLICTING_SMALLEST_RESULTING_GROUP)
{
return this.ChooseConflictOfMostConflictingSmallestResultingGroup();
}
else if (this.conflictChoice == IndependenceDetection.ConflictChoice.LEAST_CONFLICTING_LARGEST_RESULTING_GROUP)
{
return this.ChooseConflictOfLeastConflictingLargestResultingGroup();
}
else if (this.conflictChoice == IndependenceDetection.ConflictChoice.MOST_CONFLICTING_SMALLEST_AGENTS)
{
return this.ChooseConflictOfSmallestAgentsThatConflictWithMostOtherAgents();
}
else if (this.conflictChoice == IndependenceDetection.ConflictChoice.MOST_CONFLICTING_AND_SMALLEST_GROUP)
{
return this.ChooseConflictOfMostConflictingAndSmallestResultingGroup();
}
else
throw new Exception("Unknown conflict choosing method");
}
private int GetGroupSize(int groupNum)
{
foreach (var group in this.allGroups)
{
if (group.groupNum == groupNum)
return group.Size();
}
return -1;
}
/// <summary>
/// Populates the countsOfInternalGroupsThatConflict counters
/// from the conflictCountsPerGroup values that are created while solving or replanning.
/// Those counters are used for tie-breaking.
/// </summary>
protected void CountConflicts()
{
totalConflictCount = 0;
for (int i = 0; i < this.conflictCountsPerGroup.Length; i++)
{
this.countsOfGroupsThatConflict[i] = 0;
if (this.conflictCountsPerGroup[i] == null)
continue;
foreach (KeyValuePair<int, int> conflictingGroupNumAndCount in conflictCountsPerGroup[i])
{
this.countsOfGroupsThatConflict[i]++; // Counts one conflict for each agent the i'th agent conflicts with
totalConflictCount += conflictingGroupNumAndCount.Value;
}
}
totalConflictCount /= 2; // Each conflict was counted twice
}
/// <summary>
/// Chooses the first agent to be the one that maximizes the number of agents it conflicts with internally divided by 2^(group_size-1).
/// Then chooses an agent among the agents it conflicts with using the same formula.
/// Then chooses their first conflict.
///
/// Choosing the agent that conflicts the most is a greedy strategy.
/// Had replanning promised to resolve all conflicts, it would've been better to choose according to the minimum vertex cover.
///
/// Assumes all agents are initially on the same timestep (no OD).
///
/// TODO: Prefer conflicts where one of the conflicting agents is at their goal, to reduce the danger of task blow-up
/// by enabling partial expansion. On the other hand, partial expansion is only possible in basic CBS.
/// </summary>
private IndependenceDetectionConflict ChooseConflictOfSmallestAgentsThatConflictWithMostOtherAgents()
{
int groupRepA = -1; // To quiet the compiler
int groupRepB = -1; // To quiet the compiler
int time = int.MaxValue;
Func<int, double> formula = i => this.conflictCountsPerGroup[i] != null ?
this.countsOfGroupsThatConflict[i] / ((double)(1 << (this.GetGroupSize(i) - 1)))
: -1;
int chosenGroupNum = Enumerable.Range(0, this.instance.agents.Length).MaxByKeyFunc(formula);
// We could just look for any of this agent's conflicts,
// but the best choice among the agents it conflicts with is the one which maximizes the formula itself.
IEnumerable<int> conflictsWithGroupNums = this.conflictCountsPerGroup[chosenGroupNum].Keys;
int chosenConflictingGroupNum = conflictsWithGroupNums.MaxByKeyFunc(formula);
groupRepA = chosenGroupNum;
groupRepB = chosenConflictingGroupNum;
time = this.conflictTimesPerGroup[chosenGroupNum][chosenConflictingGroupNum][0]; // Choosing the earliest conflict between them - the choice doesn't matter for ID, but this is consistent with CBS' strategy
IndependenceDetectionAgentsGroup groupA = null, groupB = null;
foreach (var group in this.allGroups)
{
if (group.groupNum == groupRepA)
groupA = group;
else if (group.groupNum == groupRepB)
groupB = group;
}
if (groupA.Size() <= groupB.Size())
return new IndependenceDetectionConflict(groupA, groupB, time);
else
return new IndependenceDetectionConflict(groupB, groupA, time); // This way when attempting to resolve the conflict, the smaller group would be tried first
}
private IndependenceDetectionConflict ChooseConflictOfMostConflictingAndSmallestResultingGroup()
{
Dictionary<int, int> groupSizes = this.allGroups.ToDictionary(group => group.groupNum, group => group.Size());
double maxScore = -1;
int minTime = int.MaxValue;
IndependenceDetectionAgentsGroup groupA = null; // The must be at least one conflict
int groupRepB = -1; // The must be at least one conflict
foreach (var group in this.allGroups)
{
int size = group.Size();
foreach (var key in this.conflictCountsPerGroup[group.groupNum].Keys)
{
if (key < group.groupNum)
continue; // Already checked in the reverse order
int resultingGroupSize = groupSizes[key] + size;
int numGroupsTheGroupsWereInConflictWith = this.countsOfGroupsThatConflict[group.groupNum] /*- 1*/ + this.countsOfGroupsThatConflict[key] /*- 1*/;
double score = numGroupsTheGroupsWereInConflictWith / ((double)(1 << (resultingGroupSize - 1)));
if (score > maxScore ||
((score == maxScore) && (this.conflictTimesPerGroup[group.groupNum][key][0] < minTime))) // Just to be closer to the earliest conflict strategy
{
maxScore = score;
minTime = this.conflictTimesPerGroup[group.groupNum][key][0];
groupA = group;
groupRepB = key;
}
}
}
int time = this.conflictTimesPerGroup[groupA.groupNum][groupRepB][0]; // Choosing the earliest conflict between them - the choice doesn't matter for ID, but this is consistent with CBS' strategy
IndependenceDetectionAgentsGroup groupB = null;
foreach (var group in this.allGroups)
{
if (group.groupNum == groupRepB)
{
groupB = group;
break;
}
}
if (groupA.Size() <= groupB.Size())
return new IndependenceDetectionConflict(groupA, groupB, time);
else
return new IndependenceDetectionConflict(groupB, groupA, time); // This way when attempting to resolve the conflict, the smaller group would be tried first
}
/// <summary>
/// Also prefers earliest time, all other things being equal, to be closer to the original strategy when possible
/// </summary>
/// <returns></returns>
private IndependenceDetectionConflict ChooseConflictOfLeastConflictingLargestResultingGroup()
{
Dictionary<int, int> groupSizes = this.allGroups.ToDictionary(group => group.groupNum, group => group.Size());
int maxResultingGroupSize = -1;
int minGroupsTheyWereInConflictWith = int.MaxValue;
int minTime = int.MaxValue;
IndependenceDetectionAgentsGroup groupA = null; // The must be at least one conflict
int groupRepB = -1; // There must be at least one conflict
foreach (var group in this.allGroups)
{
int size = group.Size();
foreach (var key in this.conflictCountsPerGroup[group.groupNum].Keys)
{
if (key < group.groupNum)
continue; // Already checked in the reverse order
int resultingGroupSize = groupSizes[key] + size;
int numGroupsTheGroupsWereInConflictWith = this.countsOfGroupsThatConflict[group.groupNum] /*- 1*/ + this.countsOfGroupsThatConflict[key] /*- 1*/;
if (resultingGroupSize > maxResultingGroupSize ||
((resultingGroupSize == maxResultingGroupSize) && (numGroupsTheGroupsWereInConflictWith < minGroupsTheyWereInConflictWith)) ||
((resultingGroupSize == maxResultingGroupSize) && (numGroupsTheGroupsWereInConflictWith == minGroupsTheyWereInConflictWith) && (this.conflictTimesPerGroup[group.groupNum][key][0] < minTime))) // Just to be closer to the earliest conflict strategy
{
maxResultingGroupSize = resultingGroupSize;
minGroupsTheyWereInConflictWith = numGroupsTheGroupsWereInConflictWith;
minTime = this.conflictTimesPerGroup[group.groupNum][key][0];
groupA = group;
groupRepB = key;
}
}
}
int time = this.conflictTimesPerGroup[groupA.groupNum][groupRepB][0]; // Choosing the earliest conflict between them - the choice doesn't matter for ID, but this is consistent with CBS' strategy
IndependenceDetectionAgentsGroup groupB = null;
foreach (var group in this.allGroups)
{
if (group.groupNum == groupRepB)
{
groupB = group;
break;
}
}
if (groupA.Size() <= groupB.Size())
return new IndependenceDetectionConflict(groupA, groupB, time);
else
return new IndependenceDetectionConflict(groupB, groupA, time); // This way when attempting to resolve the conflict, the smaller group would be tried first
}
/// <summary>
/// Also prefers earliest time, all other things being equal, to be closer to the original strategy when possible
/// </summary>
/// <returns></returns>
private IndependenceDetectionConflict ChooseConflictOfMostConflictingSmallestResultingGroup()
{
Dictionary<int, int> groupSizes = this.allGroups.ToDictionary(group => group.groupNum, group => group.Size());
int minResultingGroupSize = this.instance.agents.Length + 1;
int maxGroupsTheyWereInConflictWith = -1;
int minTime = int.MaxValue;
IndependenceDetectionAgentsGroup groupA = null; // The must be at least one conflict
int groupRepB = -1; // The must be at least one conflict
foreach (var group in this.allGroups)
{
int size = group.Size();
foreach (var key in this.conflictCountsPerGroup[group.groupNum].Keys)
{
if (key < group.groupNum)
continue; // Already checked in the reverse order
int resultingGroupSize = groupSizes[key] + size;
int numGroupsTheGroupsWereInConflictWith = this.countsOfGroupsThatConflict[group.groupNum] /*- 1*/ + this.countsOfGroupsThatConflict[key] /*- 1*/;
if (resultingGroupSize < minResultingGroupSize ||
((resultingGroupSize == minResultingGroupSize) && (numGroupsTheGroupsWereInConflictWith > maxGroupsTheyWereInConflictWith)) ||
((resultingGroupSize == minResultingGroupSize) && (numGroupsTheGroupsWereInConflictWith == maxGroupsTheyWereInConflictWith) && (this.conflictTimesPerGroup[group.groupNum][key][0] < minTime))) // Just to be closer to the earliest conflict strategy
{
minResultingGroupSize = resultingGroupSize;
maxGroupsTheyWereInConflictWith = numGroupsTheGroupsWereInConflictWith;
minTime = this.conflictTimesPerGroup[group.groupNum][key][0];
groupA = group;
groupRepB = key;
}
}
}
int time = this.conflictTimesPerGroup[groupA.groupNum][groupRepB][0]; // Choosing the earliest conflict between them - the choice doesn't matter for ID, but this is consistent with CBS' strategy
IndependenceDetectionAgentsGroup groupB = null;
foreach (var group in this.allGroups)
{
if (group.groupNum == groupRepB)
{
groupB = group;
break;
}
}
if (groupA.Size() <= groupB.Size())
return new IndependenceDetectionConflict(groupA, groupB, time);
else
return new IndependenceDetectionConflict(groupB, groupA, time); // This way when attempting to resolve the conflict, the smaller group would be tried first
}
/// <summary>
/// Returns the earliest conflict between the groups
/// </summary>
/// <returns></returns>
private IndependenceDetectionConflict ChooseFirstConflict()
{
int groupRepA = -1; // To quiet the compiler
int groupRepB = -1; // To quiet the compiler
int time = int.MaxValue;
for (int i = 0; i < this.conflictTimesPerGroup.Length; i++)
{
if (conflictCountsPerGroup[i] == null)
continue;
foreach (var otherGroupNumAndConflictTimes in this.conflictTimesPerGroup[i])
{
if (otherGroupNumAndConflictTimes.Value[0] < time) // Conflict times are sorted, so only the first one needs to be checked
{
time = otherGroupNumAndConflictTimes.Value[0];
groupRepA = i;
groupRepB = otherGroupNumAndConflictTimes.Key;
}
}
}
IndependenceDetectionAgentsGroup groupA = null, groupB = null;
foreach (var group in this.allGroups)
{
if (group.groupNum == groupRepA)
groupA = group;
else if (group.groupNum == groupRepB)
groupB = group;
}
if (groupA.Size() <= groupB.Size())
return new IndependenceDetectionConflict(groupA, groupB, time);
else
return new IndependenceDetectionConflict(groupB, groupA, time); // This way when attempting to resolve the conflict, the smaller group would be tried first
}
/// <summary>
/// For SimpleID. Doesn't use the conflictCount and conflictTimes variables.
/// TODO: Merge duplication with FindFirstConflict.
/// </summary>
/// <returns></returns>
public IndependenceDetectionConflict FindFirstConflict()
{
// Find the longest plan among all the groups
int maxPlanSize = this.allGroups.Max(group => group.GetPlan().GetSize());
Plan[] plans = this.allGroups.Select(group => group.GetPlan()).ToArray();
if (this.debug)
{
var globalPlan = new Plan(plans);
Debug.WriteLine($"{globalPlan}");
}
// Check in every time step that the plans do not collide
for (int time = 1 ; time < maxPlanSize ; time++) // Assuming no conflicts exist in time zero.
{
// Check all pairs of groups for a conflict at the given time step
foreach ((int i1, var group1) in this.allGroups.Enumerate())
{
Plan group1Plan = plans[i1];
foreach ((int i2, var group2) in this.allGroups.Enumerate())
{
Plan group2Plan = plans[i2];
if (i1 < i2 && group1Plan.IsColliding(time, group2Plan))
return new IndependenceDetectionConflict(group1, group2, time);
}
}
}
return null;
}
/// <summary>
/// Search for an optimal solution using the Simple Independence Detection algorithm from Trevor Standley's paper.
/// </summary>
/// <param name="runner"></param>
/// <returns></returns>
public bool SimpleID(Run runner)
{
while (true)
{
IndependenceDetectionConflict conflict = FindFirstConflict();
// If there are no conflicts - can finish the run
if (conflict == null)
break;
allGroups.Remove(conflict.group1);
allGroups.Remove(conflict.group2);
IndependenceDetectionAgentsGroup compositeGroup = this.JoinGroups(conflict);
++merges;
// Solve composite group with the subsolver
bool solved = compositeGroup.Solve(runner, conflictAvoidanceTable);
if (solved == false)
{
this.totalCost = compositeGroup.solutionCost;
return false;
}
allGroups.AddFirst(compositeGroup);
}
return true;
}
public bool debug = false;
/// <summary>
/// Search for an optimal solution using the Independence Detection algorithm in Standley's paper,
/// which utilises a CAT.
/// </summary>
/// <param name="runner"></param>
/// <returns></returns>
public bool ImprovedID(Run runner)
{
while (true)
{
if (this.debug)
{
Debug.WriteLine($"{this.totalConflictCount} conflicts");
Debug.WriteLine($"Total cost: {this.allGroups.Sum(group => group.solutionCost)}");
Debug.WriteLine($"{this.allGroups.Count} groups: {String.Join(", ", this.allGroups)}");
Debug.Write("Group single agent costs: ");
foreach (var group in this.allGroups)
{
Debug.Write($"{{{String.Join(" ", group.GetCosts())}}}, ");
}
Debug.WriteLine("");
}
IndependenceDetectionConflict conflict = ChooseConflict();
// If there are no conflicts - can return the current plan
if (conflict == null)
break;
if (this.debug)
{
for (int j = 0; j < this.conflictTimesPerGroup.Length; j++)
{
if (this.conflictTimesPerGroup[j] != null && this.conflictTimesPerGroup[j].Count != 0)
{
Debug.Write($"Group {j} conflict times: ");
foreach (var pair in this.conflictTimesPerGroup[j])
{
Debug.Write($"{pair.Key}:[{String.Join(",", pair.Value)}], ");
}
Debug.WriteLine("");
}
}
var plan = this.CalculateJointPlan();
plan.PrintPlanIfShort();
Debug.WriteLine($"Chose {conflict}");
}
// Try to resolve the current conflict by re-planning one of the groups' path
if (this.resolutionAttemptedFirstGroup.Contains(conflict) == false) // We haven't already tried to resolve this conflict
// without merging the groups by replanning the first group's path
{
// Prevent trying to resolve this conflict this way again
this.resolutionAttemptedFirstGroup.Add(conflict);
// Add the plan of group2 to the illegal moves table and re-plan group1 with equal cost
if ((conflict.time < conflict.group1.GetPlan().GetSize() - 1) ||
(conflict.group1.Size() > 1)) // Otherwise the conflict is while a single agent
// is at its goal, no chance of an alternate path
// with the same cost that avoids the conflict - TODO: If it's an edge conflict while entering the goal it may be resolvable
{
if (this.debug)
{
Debug.WriteLine($"Trying to find an alternative path that avoids the conflict for {conflict.group1}.");
//Debug.WriteLine($"Old plan:\n{conflict.group1.GetPlan()}");
}
conflict.group1.removeGroupFromCAT(conflictAvoidanceTable);
bool resolved = conflict.group1.ReplanUnderConstraints(conflict.group2.GetPlan(), runner, this.conflictAvoidanceTable);
++resolutionAttempts;
this.expanded += conflict.group1.expanded;
this.generated += conflict.group1.generated;
if (resolved == true)
{
if (this.debug)
{
//Debug.WriteLine($"Found an alternative path that avoids the conflict for {conflict.group1}: {conflict.group1.GetPlan()}");
Debug.WriteLine($"Found an alternative path that avoids the conflict for {conflict.group1}");
}
UpdateConflictCounts(conflict.group1);
conflict.group1.addGroupToCAT(conflictAvoidanceTable);
conflictAvoidanceTable.agentConflictCounts[conflict.group1.groupNum] = conflictCountsPerGroup[conflict.group1.groupNum].Count;
++resolutionSuccesses;
continue;
}
else
{
if (conflict.group1.solutionCost < 0) // ReplanUnderConstraints reverts to the old cost if there was simply no solution
{
totalCost = conflict.group1.solutionCost;
return false;
}
conflict.group1.addGroupToCAT(conflictAvoidanceTable);
if (this.debug)
{
Debug.WriteLine($"Couldn't find an alternative path that avoids the conflict for {conflict.group1}");
}
}
}
else
{
if (this.debug)
{
Debug.WriteLine($"Not trying to find an alternative path that avoids the conflict for {conflict.group1} because " +
"the group contains a single agent and the conflict happens after it reaches its goal.");
}
}
}
else
{
if (this.debug)
{
Debug.WriteLine($"Not trying to find an alternative path that avoids the conflict for {conflict.group1} - " +
"we've already tried to in the past.");
}
}
if (this.resolutionAttemptedSecondGroup.Contains(conflict) == false) // We haven't already tried to resolve this conflict
// without merging the groups by replanning the second group's path
{
// Prevent trying to resolve this conflict this way again
this.resolutionAttemptedSecondGroup.Add(conflict);
// Add the plan of group1 to the illegal moves table and re-plan group2 with equal cost
if ((conflict.time < conflict.group2.GetPlan().GetSize() - 1) ||
(conflict.group2.Size() > 1))
{
if (this.debug)
{
Debug.WriteLine($"Trying to find an alternative path that avoids the conflict for {conflict.group2}");
//Debug.WriteLine($"Old plan: {conflict.group2.GetPlan()}");
}
conflict.group2.removeGroupFromCAT(conflictAvoidanceTable);
bool resolved = conflict.group2.ReplanUnderConstraints(conflict.group1.GetPlan(), runner, this.conflictAvoidanceTable);
++resolutionAttempts;
this.expanded += conflict.group2.expanded;
this.generated += conflict.group2.generated;
if (resolved == true)
{
if (this.debug)
{
//Debug.WriteLine($"Found an alternative path that avoids the conflict for group 2: {conflict.group2.GetPlan()}");
Debug.WriteLine($"Found an alternative path that avoids the conflict for {conflict.group2}");
}
UpdateConflictCounts(conflict.group2);
conflict.group2.addGroupToCAT(conflictAvoidanceTable);
conflictAvoidanceTable.agentConflictCounts[conflict.group2.groupNum] = conflictCountsPerGroup[conflict.group2.groupNum].Count;
++resolutionSuccesses;
continue;
}
else
{
if (conflict.group2.solutionCost < 0) // ReplanUnderConstraints reverts to the old cost if there was simply no solution
{
totalCost = conflict.group2.solutionCost; // To propagate special costs
return false;
}
conflict.group2.addGroupToCAT(conflictAvoidanceTable);
if (this.debug)
{
Debug.WriteLine($"Couldn't find an alternative path that avoids the conflict for {conflict.group2}");
}
}
}
else {
if (this.debug)
{
Debug.WriteLine($"Not trying to find an alternative path that avoids the conflict for {conflict.group2} because " +
"the group contains a single agent and the conflict happens after it reaches its goal.");
}
}
}
else
{
if (this.debug)
{
Debug.WriteLine($"Not trying to find an alternative path that avoids the conflict for {conflict.group2} - " +
"we've already tried to in the past.");
}
}
int group1Size = conflict.group1.Size();
int group1Cost = conflict.group1.solutionCost;
int group2Cost = conflict.group2.solutionCost;
// Groups are conflicting - need to join them to a single group
allGroups.Remove(conflict.group1);
allGroups.Remove(conflict.group2);
// Remove both groups from avoidance table
conflict.group1.removeGroupFromCAT(conflictAvoidanceTable);
conflict.group2.removeGroupFromCAT(conflictAvoidanceTable);
conflictAvoidanceTable.agentSizes.Remove(conflict.group1.groupNum);
conflictAvoidanceTable.agentSizes.Remove(conflict.group2.groupNum);
conflictAvoidanceTable.agentConflictCounts.Remove(conflict.group1.groupNum);
conflictAvoidanceTable.agentConflictCounts.Remove(conflict.group2.groupNum);
conflictCountsPerGroup[conflict.group1.groupNum] = null;
conflictTimesPerGroup[conflict.group1.groupNum] = null;
conflictCountsPerGroup[conflict.group2.groupNum] = null;
conflictTimesPerGroup[conflict.group2.groupNum] = null;
// Remove the old groups from the conflict counts - new counts will be put there after replanning
for (int i = 0; i < this.conflictCountsPerGroup.Length; i++)
{
this.conflictCountsPerGroup[i]?.Remove(conflict.group1.groupNum);
this.conflictCountsPerGroup[i]?.Remove(conflict.group2.groupNum);
this.conflictTimesPerGroup[i]?.Remove(conflict.group1.groupNum);
this.conflictTimesPerGroup[i]?.Remove(conflict.group2.groupNum);
}
if (this.debug)
{
Debug.WriteLine($"Merging the agent groups that participate in {conflict}.");
//Debug.WriteLine($"Group1 plan before the merge: {conflict.group1.GetPlan()}");
//Debug.WriteLine($"Group2 plan before the merge: {conflict.group2.GetPlan()}");
}
IndependenceDetectionAgentsGroup compositeGroup = this.JoinGroups(conflict);
++merges;
// Solve composite group with the underlying group solver
bool solved = compositeGroup.Solve(runner, conflictAvoidanceTable,
group1Cost, group2Cost, group1Size);
if (compositeGroup.solutionCost > maxSolutionCostFound)
maxSolutionCostFound = compositeGroup.solutionCost;
this.expanded += compositeGroup.expanded;
this.generated += compositeGroup.generated;
if (compositeGroup.allAgentsState.Length > this.maxGroupSize)
this.maxGroupSize = compositeGroup.allAgentsState.Length;
if (solved == false)
{
this.totalCost = compositeGroup.solutionCost; // To propagate special costs
allGroups.AddFirst(compositeGroup); // Important for printing the statistics
return false;
}
UpdateConflictCounts(compositeGroup);
// Add the new group to conflict avoidance table
compositeGroup.addGroupToCAT(conflictAvoidanceTable);
conflictAvoidanceTable.agentSizes[compositeGroup.groupNum] = compositeGroup.Size();
conflictAvoidanceTable.agentConflictCounts[compositeGroup.groupNum] = this.conflictCountsPerGroup[compositeGroup.groupNum].Count;
allGroups.AddFirst(compositeGroup);
}
return true;
}
void UpdateConflictCounts(IndependenceDetectionAgentsGroup group)
{
conflictCountsPerGroup[group.groupNum] = group.conflictCounts;
conflictTimesPerGroup[group.groupNum] = group.conflictTimes;
// Update conflict counts with what happens after the plan finishes
this.IncrementConflictCountsAtGoal(group, conflictAvoidanceTable);
// Update conflictCountsPerGroup and conflictTimesPerGroup for all other groups
for (int i = 0; i < this.conflictCountsPerGroup.Length; ++i)
{
if (this.conflictCountsPerGroup[i] == null || i == group.groupNum)
continue;
if (group.conflictCounts.ContainsKey(i))
{
this.conflictCountsPerGroup[i][group.groupNum] = group.conflictCounts[i];
this.conflictTimesPerGroup[i][group.groupNum] = group.conflictTimes[i];
}
else
{
this.conflictCountsPerGroup[i].Remove(group.groupNum);
this.conflictTimesPerGroup[i].Remove(group.groupNum);
}
}
CountConflicts();
}
/// <summary>
/// Update conflict counts according to what happens after the plan finishes -
/// needed if the plan is shorter than one of the previous plans and collides
/// with it while at the goal.
/// It's cheaper to do it this way than to force the solver the go more deeply.
/// The conflict counts are saved at the group's representative.
/// </summary>
protected void IncrementConflictCountsAtGoal(IndependenceDetectionAgentsGroup group, ConflictAvoidanceTable CAT)
{
for (int i = 0; i < group.allAgentsState.Length; ++i)
{
var afterGoal = new TimedMove(group.allAgentsState[i].agent.Goal.x, group.allAgentsState[i].agent.Goal.y, Move.Direction.Wait, time: 0);
for (int time = group.GetPlan().GetSize(); time < CAT.GetMaxPlanSize(); time++)
{
afterGoal.time = time;
afterGoal.IncrementConflictCounts(CAT,
this.conflictCountsPerGroup[group.groupNum],
this.conflictTimesPerGroup[group.groupNum]);
}
}
}
/// <summary>
/// Join the conflicting groups into a single group
/// </summary>
/// <param name="conflict">An object that describes the conflict</param>
/// <returns>The composite group of agents</returns>
protected virtual IndependenceDetectionAgentsGroup JoinGroups(IndependenceDetectionConflict conflict)
{
return conflict.group1.Join(conflict.group2);
}
/// <summary>
/// Run Standley's ID framework with the given subsolver
/// </summary>
/// <returns>true if optimal solution has been found</returns>
public bool Solve()
{
// TODO: Add a SolveGiven method that takes the state just before the "if (this.simple == false)" line in this method and solves from it.
// Add a PreSolve method that prepares that state and also gives for each agent the accummulated time it took to presolve up to it.
bool solved;
// Solve the single agent problems independently
this.maxSolutionCostFound = 0;
foreach (var group in this.allGroups)
{
solved = group.Solve(runner, this.conflictAvoidanceTable);
// Check if max time has been exceeded or search failed for another reason
if (solved == false)
{
this.totalCost = group.solutionCost; // Should be some error code from Constants.
this.Clear();
return false;
}