-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMddPruningHeuristicForCbs.cs
208 lines (187 loc) · 8.33 KB
/
MddPruningHeuristicForCbs.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace mapf
{
class MddPruningHeuristicForCbs : ILazyHeuristic<CbsNode>
{
public MddPruningHeuristicForCbs(bool ignoreConstraints = false)
{
this.ignoreConstraints = ignoreConstraints;
}
protected int pruningSuccesses;
protected int pruningFailures;
protected int cacheHits;
protected int targetTooHigh;
protected int accPruningSuccesses;
protected int accPruningFailures;
protected int accCacheHits;
protected int accTargetTooHigh;
protected bool ignoreConstraints;
protected ProblemInstance instance;
/// <summary>
/// Maps a pair of agents and their costs to whether there's a solution with those costs
/// </summary>
public Dictionary<(int agentAIndex, int agentBIndex, int agentACost, int agentBCost), ushort> cache;
public int NumStatsColumns
{
get
{
return 4;
}
}
public string GetName()
{
return "MDD Pruning Heuristic";
}
public void AccumulateStatistics()
{
this.accPruningSuccesses += this.pruningSuccesses;
this.accPruningFailures += this.pruningFailures;
this.accCacheHits += this.cacheHits;
this.accTargetTooHigh += this.targetTooHigh;
}
public void ClearAccumulatedStatistics()
{
this.accPruningSuccesses = 0;
this.accPruningFailures = 0;
this.accCacheHits = 0;
this.accTargetTooHigh = 0;
}
public void ClearStatistics()
{
this.pruningSuccesses = 0;
this.pruningFailures = 0;
this.cacheHits = 0;
this.targetTooHigh = 0;
}
/// <summary>
/// Returns 0 if the conflict is solvable with the current costs, 1 otherwise. If unsure, returns 0;
/// Currently avoiding building a k-agent MDD.
/// TODO: This became largely irrelevant after we've discovered cardinal conflicts, but
/// now that we're always building all MDDs, it might be worth it to try to sync the MDDs
/// for nodes with no cardinal conflicts. We might find they're still unsolvable
/// with the current costs.
/// </summary>
/// <param name="s"></param>
/// <param name="ignoreConstraints">
/// Ignore constraints. This allows the results for the given costs to be used elsewhere in
/// the CBS tree, but can cause under-estimates and make building the MDD a little slower.
/// </param>
/// <returns>
/// If after syncing and pruning the MDDs at the current costs for the two conflicting agents
/// the whole MDD was pruned, indicating these agents can't be solved at the current cost,
/// returns 1. Else returns 0
/// </returns>
public uint h(CbsNode s)
{
var agentIndicesAndCosts = (s.conflict.agentAIndex, s.conflict.agentBIndex,
s.allSingleAgentCosts[s.conflict.agentAIndex], s.allSingleAgentCosts[s.conflict.agentBIndex]);
if (this.ignoreConstraints)
{
if (this.cache.ContainsKey(agentIndicesAndCosts))
{
this.cacheHits++;
return this.cache[agentIndicesAndCosts];
}
}
// TODO
if (s.GoalTest())
{
return 0;
}
if (s.h > 1)
{
return 1; // We can't raise the heuristic more than that
}
if (s.GetGroupSize(s.conflict.agentAIndex) > 1 || s.GetGroupSize(s.conflict.agentBIndex) > 1)
{
return 0; // Without saving the result, as it's just a cop-out
}
int maxCost = Math.Max(s.allSingleAgentCosts[s.conflict.agentAIndex],
s.allSingleAgentCosts[s.conflict.agentBIndex]);
// Building MDDs for the conflicting agents. We can't keep them because we're
// destructively syncing them later (the first one, at least).
var mddA = new MDD(s.conflict.agentAIndex, this.instance.agents[s.conflict.agentAIndex].agent.agentNum,
this.instance.agents[s.conflict.agentAIndex].lastMove,
s.allSingleAgentCosts[s.conflict.agentAIndex], maxCost,
this.instance.GetNumOfAgents(), this.instance, this.ignoreConstraints);
var mddB = new MDD(s.conflict.agentBIndex, this.instance.agents[s.conflict.agentBIndex].agent.agentNum,
this.instance.agents[s.conflict.agentBIndex].lastMove,
s.allSingleAgentCosts[s.conflict.agentBIndex], maxCost,
this.instance.GetNumOfAgents(), this.instance, this.ignoreConstraints);
s.cbs.mddsBuilt += 2;
(MDD.PruningDone ans, int stat) = mddA.SyncMDDs(mddB, checkTriples: false);
if (ans == MDD.PruningDone.EVERYTHING)
{
if (this.ignoreConstraints)
this.cache.Add(agentIndicesAndCosts, 1);
this.pruningSuccesses++;
return 1;
}
else
{
if (this.ignoreConstraints)
this.cache.Add(agentIndicesAndCosts, 0);
this.pruningFailures++;
return 0;
}
}
/// <summary>
/// Lazy version
/// </summary>
/// <param name="s"></param>
/// <param name="target"></param>
/// <returns></returns>
public uint h(CbsNode s, int target)
{
if (s.g + 1 < target)
{
this.targetTooHigh++;
return 0; // Currently we can only give an estimate of 1
}
return this.h(s);
}
public void Init(ProblemInstance pi, List<uint> agentsToConsider)
{
this.instance = pi;
}
public void OutputAccumulatedStatistics(TextWriter output)
{
string name = this.GetName();
Console.WriteLine($"{name} Accumulated Pruning Successes (High-Level): {this.accPruningSuccesses}");
Console.WriteLine($"{name} Accumulated Pruning Failures (High-Level): {this.accPruningFailures}");
Console.WriteLine($"{name} Accumulated Cache Hits (High-Level): {this.accCacheHits}");
Console.WriteLine($"{name} Accumulated Times Target Estimate Was Too High (High-Level): {this.accTargetTooHigh}");
output.Write(this.accPruningSuccesses + Run.RESULTS_DELIMITER);
output.Write(this.accPruningFailures + Run.RESULTS_DELIMITER);
output.Write(this.accCacheHits + Run.RESULTS_DELIMITER);
output.Write(this.accTargetTooHigh + Run.RESULTS_DELIMITER);
}
public void OutputStatistics(TextWriter output)
{
string name = this.GetName();
Console.WriteLine($"{name} Pruning successes (High-Level): {this.pruningSuccesses}");
Console.WriteLine($"{name} Pruning failures (High-Level): {this.pruningFailures}");
Console.WriteLine($"{name} Cache hits (High-Level): {this.cacheHits}");
Console.WriteLine($"{name} Times Target Estimate was Too High (High-Level): {this.targetTooHigh}");
output.Write(this.pruningSuccesses + Run.RESULTS_DELIMITER);
output.Write(this.pruningFailures + Run.RESULTS_DELIMITER);
output.Write(this.cacheHits + Run.RESULTS_DELIMITER);
output.Write(this.targetTooHigh + Run.RESULTS_DELIMITER);
}
public void OutputStatisticsHeader(TextWriter output)
{
string name = this.GetName();
output.Write($"{name} Pruning Successes (HL)");
output.Write(Run.RESULTS_DELIMITER);
output.Write($"{name} Pruning Failures (HL)");
output.Write(Run.RESULTS_DELIMITER);
output.Write($"{name} Cache Hits (HL)");
output.Write(Run.RESULTS_DELIMITER);
output.Write($"{name} Times Target Estimate Was Too High");
output.Write(Run.RESULTS_DELIMITER);
}
}
}