-
Notifications
You must be signed in to change notification settings - Fork 3
/
SAT_Solver.cs
242 lines (205 loc) · 8.12 KB
/
SAT_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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
using System;
using System.Linq;
using System.Collections.Generic;
using System.IO;
using System.Diagnostics;
namespace mapf
{
/// <summary>
/// This is an implementation of the A* algorithm for the MAPF problem.
/// It r
/// </summary>
public class SATSolver : 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 SATSolver()
{
}
/// <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 "SAT";
}
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();
String sat_problem_file = this.instance.parameters[ProblemInstance.SAT_FILE_NAME].ToString();
String map_file = this.instance.parameters[ProblemInstance.MAP_FILE_PATH].ToString();
String scen_file = this.instance.parameters[ProblemInstance.SCEN_FILE].ToString();
int n_agents = (int)this.instance.parameters[ProblemInstance.N_AGENTS];
generateMPFFile(sat_problem_file, map_file, scen_file, n_agents);
Console.WriteLine("Starting to solve SAT problem {0}", sat_problem_file);
if (!File.Exists((String)sat_problem_file))
{
Console.WriteLine("SAT Problem file not found at {0}",sat_problem_file);
Console.WriteLine("!!!!!!!!!!!!!!!!!!!");
return false;
}
process.StartInfo.FileName = "mapf_solver_boOX";
process.StartInfo.Arguments = String.Format("--algorithm=smtcbs++" +
" --output-file=solution.txt --input-file={0} --cost-limit=99999999", sat_problem_file);
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.NO_SOLUTION_COST;
Console.WriteLine("Out of time");
return false;
}
totalCost = 0;
return true;
}
private void generateMPFFile(string sat_problem_file, string map_file, string scen_file, int n_agents)
{
if (!File.Exists(sat_problem_file))
{
System.Console.WriteLine("Creating sat file at {0}", sat_problem_file);
var process = new Process();
//Console.WriteLine("Converting {0} to SAT file", fileName);
String commandArgs = String.Format("--input-movi-map-file={0}" +
" --input-movi-scen-file={1}" +
" --output-mpf-file={2}" +
" --N-agents={3}", map_file, scen_file, sat_problem_file, n_agents);
//Console.WriteLine(commandArgs);
process.StartInfo.FileName = "moviscen_convert_boOX";
process.StartInfo.Arguments = commandArgs;
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);
process.Close();
}
}
/// <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
{
get
{
return 0;
}
}
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()
{
}
}
}