-
Notifications
You must be signed in to change notification settings - Fork 3
/
AdditivePDBs.cs
153 lines (128 loc) · 5.37 KB
/
AdditivePDBs.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
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
namespace mapf
{
public class AdditivePDBs : IHeuristicCalculator<WorldState>
{
List<PDB> PDBs;
/// <summary>
/// The set of agents that are not covered by this set of pattern
/// databases. This information is not used by this class or any of its
/// related classes, but is simply precomputed as useful information for
/// the caller (for example, if the caller wanted to add to our
/// heuristic estimate).
/// </summary>
public SortedSet<uint> excludedAgents;
/// <summary>
/// Determines how many additive pattern databases to build and divides
/// the agents among them, possibly leaving some agents out.
/// </summary>
/// <param name="pi">the problem instance to use</param>
/// <param name="s">The root of the search tree. This is also expected
/// to have context parameters such as agents' goal states.</param>
public void build(ProblemInstance pi, WorldState s)
{
Debug.Write("Building database...");
// As a simple rule, we'll simply take pairs of agents starting
// with the first two, then the second two, etc.
PDBs = new List<PDB>();
if (s.allAgentsState.Length > 1)
{
for (uint i = 0; i < s.allAgentsState.Length - 1; i += 2)
{
// Make a list of agents we want to include together in the
// next additive pattern database. We specify agents by
// their index into the WorldState.allAgentsState
// array.
List<uint> agentsToConsider = new List<uint>();
agentsToConsider.Add(i);
agentsToConsider.Add(i + 1);
// Create a new root search node where the state only
// includes a subset of the agents of the original search
// node. This is done by passing into the state copy
// constructor our list of important agents.
WorldState tws = new WorldState(s.allAgentsState, agentsToConsider);
// Initialize, build, and save the new pattern database.
EnumeratedPDB pdb = new EnumeratedPDB();
pdb.Init(pi, agentsToConsider);
pdb.build();
Debug.Write(".");
PDBs.Add(pdb);
}
}
// Create single shortest path pattern database heuristics for the
// remaining agents if we have any left over.
if (s.allAgentsState.Length % 2 == 1)
{
SumIndividualCosts pdb = new SumIndividualCosts();
List<uint> agentsToConsider = new List<uint>(1);
agentsToConsider.Add((uint) s.allAgentsState.Length - 1);
pdb.Init(pi, agentsToConsider);
pdb.build();
PDBs.Add(pdb);
}
// For informational purposes, we will set the number of agents
// that aren't included in this set of pattern databases.
excludedAgents = new SortedSet<uint>();
Debug.WriteLine("done.");
}
/// <summary>
/// Initializes the pattern database by storing references to the
/// problem instance and also the subset of agents that the pattern
/// database pertains to.
/// </summary>
/// <param name="pi">The problem instance.</param>
/// <param name="vAgents">The agents that the pattern database should keep track of.</param>
public virtual void Init(ProblemInstance pi, List<uint> vAgents) {}
/// <summary>
/// Simply returns the sum of each of the additive pattern database
/// heuristic estimates on the given state.
/// </summary>
/// <param name="s">The state.</param>
/// <returns>The admissible heuristic value for the additive pattern
/// databases.</returns>
public uint h(WorldState s)
{
uint nHeuristicValue = 0;
foreach (PDB p in PDBs)
{
nHeuristicValue += p.h(s);
}
return nHeuristicValue;
}
public bool empty()
{
return PDBs.Count == 0;
}
/// <summary>
/// Prints header of statistics of a single run to the given output.
/// </summary>
public void OutputStatisticsHeader(TextWriter output) { }
/// <summary>
/// Prints statistics of a single run to the given output.
/// </summary>
public void OutputStatistics(TextWriter output) { }
public int NumStatsColumns {
get
{
return 0;
}
}
/// <summary>
/// Clears statistics.
/// </summary>
public void ClearStatistics() { }
public void ClearAccumulatedStatistics() { }
public void AccumulateStatistics() { }
public void OutputAccumulatedStatistics(TextWriter output) { }
public override string ToString()
{
return this.GetName();
}
public string GetName()
{
return "Additive PDB";
}
}
}