-
Notifications
You must be signed in to change notification settings - Fork 3
/
BCP_Solver.cs
186 lines (156 loc) · 5.89 KB
/
BCP_Solver.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
using System;
using System.Linq;
using System.Collections.Generic;
using System.IO;
using System.Diagnostics;
namespace mapf
{
public class BCPSolver : ISolver
{
protected ProblemInstance instance;
/// <summary>
/// Holds the cost of the solution when a solution found
/// </summary>
public int totalCost;
public int numOfAgents;
protected int maxSolutionCost;
protected HashSet<TimedMove> illegalMoves;
protected HashSet_U<CbsConstraint> constraints;
protected Run runner;
protected Plan solution;
/// <summary>
/// Default constructor.
/// </summary>
public BCPSolver()
{
}
/// <summary>
/// Setup the relevant data structures for a run under CBS.
/// </summary>
public virtual void Setup(ProblemInstance problemInstance, Run runner)
{
this.instance = problemInstance;
this.runner = runner;
this.numOfAgents = problemInstance.agents.Length;
// Store parameters used by the Independence Detection algorithm
if (problemInstance.parameters.ContainsKey(IndependenceDetection.MAX_COST_KEY))
this.maxSolutionCost = (int)problemInstance.parameters[IndependenceDetection.MAX_COST_KEY];
else
this.maxSolutionCost = int.MaxValue;
if (problemInstance.parameters.ContainsKey(IndependenceDetection.ILLEGAL_MOVES_KEY) &&
((HashSet<TimedMove>)problemInstance.parameters[IndependenceDetection.ILLEGAL_MOVES_KEY]).Count != 0)
this.illegalMoves = (HashSet<TimedMove>)(problemInstance.parameters[IndependenceDetection.ILLEGAL_MOVES_KEY]);
else
this.illegalMoves = null;
}
/// <summary>
/// Factory method. Creates the initial state from which the search will start.
/// This will be the first state to be inserted to OPEN.
/// </summary>
/// <returns>The root of the search tree</returns>
protected virtual WorldState CreateSearchRoot(int minDepth = -1, int minCost = -1, MDDNode mddNode = null)
{
return new WorldState(this.instance.agents, minDepth, minCost, mddNode);
}
/// <summary>
/// Clears the relevant data structures and variables to free memory.
/// </summary>
public void Clear()
{
this.illegalMoves = null;
}
public virtual String GetName()
{
return "BCP";
}
public override string ToString()
{
return this.GetName();
}
public int GetSolutionCost() { return this.totalCost; }
public virtual void OutputStatisticsHeader(TextWriter output)
{
}
/// <summary>
/// Prints statistics of a single run to the given output.
/// </summary>
public virtual void OutputStatistics(TextWriter output)
{
}
public bool debug = false;
/// <summary>
/// Runs the algorithm until the problem is solved or memory/time is exhausted
/// </summary>
/// <returns>True if solved</returns>
public virtual bool Solve()
{
var process = new Process();
var bcp_problem_file = this.instance.parameters[ProblemInstance.BCP_FILE_NAME];
Console.WriteLine("Starting to solve BCP problem {0}", bcp_problem_file);
if (!File.Exists((String)bcp_problem_file))
{
Console.WriteLine("BCP Problem file not found at {0}", bcp_problem_file);
Console.WriteLine("!!!!!!!!!!!!!!!!!!!");
return false;
}
process.StartInfo.FileName = "bcp-mapf";
process.StartInfo.Arguments = String.Format("--file={0} --agents-limit={1}", bcp_problem_file, this.instance.agents.Length);
process.StartInfo.CreateNoWindow = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.OutputDataReceived += (sender, data) => {
//Console.WriteLine(data.Data);
};
process.StartInfo.RedirectStandardError = true;
process.ErrorDataReceived += (sender, data) => {
//Console.WriteLine(data.Data);
};
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
bool successInTime = process.WaitForExit(Constants.MAX_TIME);
if (!successInTime) //Timeout
{
process.Kill();
totalCost = Constants.TIMEOUT_COST;
Console.WriteLine("Out of time");
return false;
}
return true;
}
/// <summary>
/// Returns the found plan, or null if no plan was found.
/// </summary>
/// <returns></returns>
public virtual Plan GetPlan()
{
return this.solution;
}
protected SinglePlan[] singlePlans;
public virtual SinglePlan[] GetSinglePlans()
{
return this.singlePlans;
}
protected int[] singleCosts;
public int NumStatsColumns => throw new NotImplementedException();
public virtual int[] GetSingleCosts()
{
return this.singleCosts;
}
public int GetExpanded() { return -1; }
public int GetGenerated() { return -1; }
public int GetMaxGroupSize() { return numOfAgents; }
public int GetSolutionDepth()
{
return 1;
//throw new NotImplementedException();
}
public long GetMemoryUsed()
{
throw new NotImplementedException();
}
public void ClearStatistics()
{
}
}
}