-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathPEA_Star.cs
149 lines (123 loc) · 5.49 KB
/
PEA_Star.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
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System;
namespace mapf;
/// <summary>
/// Only keeps generated nodes that have the same f as the parent.
/// The parent is then re-inserted with the lowest f from its discarded children
/// (Yoshizumi, Miura, and Ishida 2000)
/// </summary>
class PEA_Star : A_Star
{
protected int generatedAndDiscarded;
protected int expandedFullStates;
protected int accGeneratedAndDiscarded;
protected int accExpandedFullStates;
bool hasMoreSuccessors;
/// <summary>
/// The lowest discarded F
/// </summary>
public int nextFvalue;
public int currentFTarget;
public PEA_Star(IHeuristicCalculator<WorldState> heuristic = null)
: base(heuristic) { }
override protected WorldState CreateSearchRoot(int minDepth = -1, int minCost = -1, MDDNode mddNode = null)
{
return new WorldStateForPartialExpansion(this.instance.agents, minDepth, minCost, mddNode); // Consider using a WorldStateForBasicPartialExpansion that only has the IsAlreadyExpanded stuff
}
protected override WorldState CreateSearchNode(WorldState from)
{
return new WorldStateForPartialExpansion((WorldStateForPartialExpansion)from);
}
public override void Setup(ProblemInstance problemInstance, int minTimeStep, Run runner,
ConflictAvoidanceTable CAT, ISet<CbsConstraint> constraints, ISet<CbsConstraint> positiveConstraints,
int minCost, int maxCost, MDD mdd)
{
base.Setup(problemInstance, minTimeStep, runner, CAT, constraints, positiveConstraints, minCost, maxCost, mdd);
this.generatedAndDiscarded = 0;
this.expandedFullStates = 0;
}
override public string GetName() { return "(B)PE" + base.GetName(); }
override public void Expand(WorldState simpleLookingNode)
{
var node = (WorldStateForPartialExpansion)simpleLookingNode;
//Debug.Print("Expanding node " + node);
if (!node.IsAlreadyExpanded())
{
node.alreadyExpanded = true;
this.expandedFullStates++;
}
hasMoreSuccessors = false;
this.nextFvalue = int.MaxValue;
this.currentFTarget = node.g + node.h;
base.Expand(node);
if (hasMoreSuccessors && this.nextFvalue <= this.maxSolutionCost)
{
node.h = this.nextFvalue - node.g; // Just to update this node's f value to the desired value.
// Although you could say that since we exhausted the current F value, if we get to this node again it means the heuristic was off by at least 1
this.openList.Add(node); // Re-insert to open list with updated F
}
}
/// <summary>
/// Adds nodes of target F only
/// </summary>
/// <param name="currentNode"></param>
/// <returns></returns>
protected override bool ProcessGeneratedNode(WorldState currentNode)
{
if (currentNode.h + currentNode.g == this.currentFTarget)
return base.ProcessGeneratedNode(currentNode);
else generatedAndDiscarded++; // Notice we don't count the discarded nodes in the genereted count, only here
if (currentNode.h + currentNode.g > this.currentFTarget)
{
this.hasMoreSuccessors = true;
this.nextFvalue = (byte)Math.Min(this.nextFvalue, currentNode.h + currentNode.g);
}
return false;
}
public override void OutputStatisticsHeader(TextWriter output)
{
base.OutputStatisticsHeader(output);
output.Write(this.ToString() + " Generated And Discarded");
output.Write(Run.RESULTS_DELIMITER);
output.Write(this.ToString() + " Expanded Full States");
output.Write(Run.RESULTS_DELIMITER);
}
public override void OutputStatistics(TextWriter output)
{
base.OutputStatistics(output);
Console.WriteLine("Generated And Discarded: {0}", this.generatedAndDiscarded);
Console.WriteLine("Expanded Full States: {0}", this.expandedFullStates);
output.Write(this.generatedAndDiscarded + Run.RESULTS_DELIMITER);
output.Write(this.expandedFullStates + Run.RESULTS_DELIMITER);
// Isn't there a CSV module in C# instead of fussing with the delimeter everywhere?
}
public override int NumStatsColumns
{
get
{
return 2 + base.NumStatsColumns;
}
}
public override void ClearAccumulatedStatistics()
{
base.ClearAccumulatedStatistics();
this.accGeneratedAndDiscarded = 0;
this.accExpandedFullStates = 0;
}
public override void AccumulateStatistics()
{
base.AccumulateStatistics();
this.accGeneratedAndDiscarded += this.generatedAndDiscarded;
this.accExpandedFullStates += this.expandedFullStates;
}
public override void OutputAccumulatedStatistics(TextWriter output)
{
base.OutputAccumulatedStatistics(output);
Console.WriteLine("{0} Accumulated Generated And Discarded (Low-Level): {1}", this, this.accGeneratedAndDiscarded);
Console.WriteLine("{0} Accumulated Expanded Full States (Low-Level): {1}", this, this.accExpandedFullStates);
output.Write(this.accGeneratedAndDiscarded + Run.RESULTS_DELIMITER);
output.Write(this.accExpandedFullStates + Run.RESULTS_DELIMITER);
}
}