-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathMvcHeuristicForCbs.cs
200 lines (176 loc) · 8.01 KB
/
MvcHeuristicForCbs.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Diagnostics;
namespace mapf;
class MvcHeuristicForCbs : ILazyHeuristic<CbsNode>
{
protected int targetClearlyTooHigh;
protected int targetReached;
protected int targetNotReached;
protected int accTargetClearlyTooHigh;
protected int accTargetReached;
protected int accTargetNotReached;
public int NumStatsColumns
{
get
{
return 3;
}
}
public string GetName()
{
return "MVC of Cardinal Conflict Graph Heuristic";
}
public void AccumulateStatistics()
{
this.accTargetClearlyTooHigh += this.targetClearlyTooHigh;
this.accTargetReached += this.targetReached;
this.accTargetNotReached += this.targetNotReached;
}
public void ClearAccumulatedStatistics()
{
this.accTargetClearlyTooHigh = 0;
this.accTargetReached = 0;
this.accTargetNotReached = 0;
}
public void ClearStatistics()
{
this.targetClearlyTooHigh = 0;
this.targetReached = 0;
this.targetNotReached = 0;
}
/// <summary>
/// Compute h value with a minimum vertex cover solver.
/// Assumes the s.cbs.mergeThreshold is -1.
/// </summary>
/// <returns></returns>
public uint h(CbsNode s)
{
return this.h(s, int.MaxValue);
}
/// <summary>
/// Lazy version
/// </summary>
/// <param name="s"></param>
/// <param name="target"></param>
/// <returns>The heuristic estimate</returns>
public uint h(CbsNode s, int target)
{
Debug.WriteLine($"Computing heuristic estimate for node hash {s.GetHashCode()}");
if (target != int.MaxValue &&
(
(s.prev != null && s.prev.minimumVertexCover != (int) ConflictGraph.MinVertexCover.NOT_SET &&
target > s.prev.minimumVertexCover + 1) ||
(target > s.totalInternalAgentsThatConflict))
)
{
Debug.WriteLine($"Target estimate {target} was too high!");
this.targetClearlyTooHigh++;
return 0; // We can't give a higher estimate than the size of the parent's MVC + 1,
// or the max number of vertices in this agent's conflict graph
// see comments in ConflictGraph.MinimumVertexCover for details.
// 0 just signals we couldn't raise the h enough.
}
ConflictGraph CardinallyConflictingAgents = new ConflictGraph(s.singleAgentPlans.Length);
ISet<int>[] groups = s.GetGroups();
// Populate the cardinal conflict graph
foreach (var agentIndex in Enumerable.Range(0, s.singleAgentPlans.Length))
{
if (s.conflictTimesPerAgent[agentIndex].Count == 0)
continue; // Agent has no conflicts
bool hasMdd = s.mddNarrownessValues[agentIndex] != null;
bool canBuildMDD = groups[agentIndex].Count == 1;
if (canBuildMDD == false)
continue; // We aren't going to lookahead just for the heuristic
foreach (int conflictingAgentNum in s.conflictTimesPerAgent[agentIndex].Keys)
{
int conflictingAgentIndex = s.agentNumToIndex[conflictingAgentNum];
if (conflictingAgentIndex < agentIndex) // check later
continue;
bool otherHasMdd = s.mddNarrownessValues[conflictingAgentIndex] != null;
bool otherCanBuildMdd = groups[conflictingAgentIndex].Count == 1;
if (otherCanBuildMdd == false)
continue; // We won't lookahead just for the heuristic
foreach (int conflictTime in s.conflictTimesPerAgent[agentIndex][conflictingAgentNum])
{
bool otherNarrow;
if (otherHasMdd)
{
otherNarrow = s.DoesAgentHaveNoOtherOption(conflictingAgentIndex, conflictTime, agentIndex, groups);
if (otherNarrow == false)
continue;
}
else
otherNarrow = false; // Temporarily. Will be computed soon.
s.buildMddForAgentWithItsCurrentCost(agentIndex); // Does nothing if its MDD is already built.
hasMdd = true;
bool iNarrow = s.DoesAgentHaveNoOtherOption(agentIndex, conflictTime, conflictingAgentIndex, groups);
if (iNarrow == false)
continue;
if (otherHasMdd == false)
{
s.buildMddForAgentWithItsCurrentCost(conflictingAgentIndex);
otherHasMdd = true;
otherNarrow = s.DoesAgentHaveNoOtherOption(conflictingAgentIndex, conflictTime, agentIndex, groups);
}
if (otherNarrow) // Cardinal conflict - both agent's MDDs are narrow at this timestep.
CardinallyConflictingAgents.Add(agentIndex, conflictingAgentIndex);
}
}
}
if (s.prev == null || s.prev.minimumVertexCover == (int) ConflictGraph.MinVertexCover.NOT_SET)
s.minimumVertexCover = CardinallyConflictingAgents.MinimumVertexCover();
else
s.minimumVertexCover = CardinallyConflictingAgents.MinimumVertexCover(s.prev.minimumVertexCover);
// FIXME: The value might be incorrect after a merge operation which wasn't followed by a restart
if (target != int.MaxValue)
{
if (s.minimumVertexCover >= target)
{
Debug.WriteLine($"Target estimate {target} reached");
this.targetReached++;
}
else
{
Debug.WriteLine($"Target estimate {target} not reached");
this.targetNotReached++;
}
}
return (uint)s.minimumVertexCover;
}
public void Init(ProblemInstance pi, List<uint> agentsToConsider)
{
}
public void OutputAccumulatedStatistics(TextWriter output)
{
string name = this.GetName();
Console.WriteLine($"{name} Accumulated Times Target Estimate Was Clearly Too High: {this.accTargetClearlyTooHigh}");
Console.WriteLine($"{name} Accumulated Times Target Estimate Was Reached: {this.accTargetReached}");
Console.WriteLine($"{name} Accumulated Times Target Estimate Was Not Reached: {this.accTargetNotReached}");
output.Write(this.accTargetClearlyTooHigh + Run.RESULTS_DELIMITER);
output.Write(this.accTargetReached + Run.RESULTS_DELIMITER);
output.Write(this.accTargetNotReached + Run.RESULTS_DELIMITER);
}
public void OutputStatistics(TextWriter output)
{
string name = this.GetName();
Console.WriteLine($"{name} times target estimate was clearly too high: {this.targetClearlyTooHigh}");
Console.WriteLine($"{name} times target estimate was reached: {this.targetReached}");
Console.WriteLine($"{name} times target estimate was not reached: {this.targetNotReached}");
output.Write(this.targetClearlyTooHigh + Run.RESULTS_DELIMITER);
output.Write(this.targetReached + Run.RESULTS_DELIMITER);
output.Write(this.targetNotReached + Run.RESULTS_DELIMITER);
}
public void OutputStatisticsHeader(TextWriter output)
{
string name = this.GetName();
output.Write($"{name} Times Target Estimate Was Too High");
output.Write(Run.RESULTS_DELIMITER);
output.Write($"{name} Times Target Estimate Was Reached");
output.Write(Run.RESULTS_DELIMITER);
output.Write($"{name} Times Target Estimate Was Not Reached");
output.Write(Run.RESULTS_DELIMITER);
}
}