-
Notifications
You must be signed in to change notification settings - Fork 3
/
Program.cs
490 lines (453 loc) · 23.9 KB
/
Program.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
using System;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
using System.IO;
using System.Text;
namespace mapf
{
/// <summary>
/// This is the entry point of the application.
/// </summary>
class Program
{
public static string RESULTS_FILE_NAME = "Results.csv"; // Overridden by Main
public static string METAIDRESULTS_FILE_NAME = "ResultsMETAID.csv"; // Overridden by Main
private static bool onlyReadInstances = false;
/// <summary>
/// Simplest run possible with a randomly generated problem instance.
/// </summary>
public void SimpleRun()
{
using (Run runner = new Run())
{
runner.OpenResultsFile(RESULTS_FILE_NAME);
runner.PrintResultsFileHeader();
ProblemInstance instance = runner.GenerateProblemInstance(10, 3, 10);
instance.Export("Test.instance");
runner.SolveGivenProblem(instance);
}
}
/// <summary>
/// Runs a single instance, imported from a given filename.
/// </summary>
/// <param name="fileName"></param>
public void RunInstance(string fileName)
{
ProblemInstance instance;
try
{
instance = ProblemInstance.Import(Path.Combine(Directory.GetCurrentDirectory(), "..", "..", "Instances", fileName));
}
catch (Exception e)
{
Console.WriteLine($"Skipping bad problem instance {fileName}. Error: {e.Message}");
return;
}
Run runner = new Run();
using (runner)
{
bool resultsFileExisted = File.Exists(RESULTS_FILE_NAME);
runner.OpenResultsFile(RESULTS_FILE_NAME);
if (resultsFileExisted == false)
runner.PrintResultsFileHeader();
runner.SolveGivenProblem(instance);
}
}
/// <summary>
/// Runs a set of experiments.
/// This function will generate a random instance (or load it from a file if it was already generated)
/// </summary>
public void RunExperimentSet(int[] gridSizes, int[] agentListSizes, int[] obstaclesProbs, int instances)
{
ProblemInstance instance;
string instanceName;
using (Run runner = new Run())
{
bool resultsFileExisted = File.Exists(RESULTS_FILE_NAME);
runner.OpenResultsFile(RESULTS_FILE_NAME);
if (resultsFileExisted == false)
runner.PrintResultsFileHeader();
bool continueFromLastRun = false;
string[] LastProblemDetails = null;
string currentProblemFileName = Path.Combine(
Directory.GetCurrentDirectory(), $"current-problem-{Process.GetCurrentProcess().ProcessName}");
if (File.Exists(currentProblemFileName)) //if we're continuing running from last time
{
var lastProblemFile = new StreamReader(currentProblemFileName);
LastProblemDetails = lastProblemFile.ReadLine().Split(','); //get the last problem
lastProblemFile.Close();
continueFromLastRun = true;
}
string allocation = Process.GetCurrentProcess().ProcessName.Substring(1); // When executable names are of the form "g###"
for (int gridSizeIndex = 0; gridSizeIndex < gridSizes.Length; gridSizeIndex++)
{
for (int obstaclePercentageIndex = 0; obstaclePercentageIndex < obstaclesProbs.Length; obstaclePercentageIndex++)
{
runner.ResetOutOfTimeCounters();
for (int numOfAgentsIndex = 0; numOfAgentsIndex < agentListSizes.Length; numOfAgentsIndex++)
{
if (gridSizes[gridSizeIndex] * gridSizes[gridSizeIndex] * (1 - obstaclesProbs[obstaclePercentageIndex] / 100) < agentListSizes[numOfAgentsIndex]) // Probably not enough room for all agents
continue;
for (int i = 0; i < instances; i++)
{
//if (i % 33 != Convert.ToInt32(allocation)) // grids!
// continue;
//if (i % 5 != 0) // grids!
// continue;
if (continueFromLastRun) //set the latest problem
{
gridSizeIndex = int.Parse(LastProblemDetails[0]);
obstaclePercentageIndex = int.Parse(LastProblemDetails[1]);
numOfAgentsIndex = int.Parse(LastProblemDetails[2]);
i = int.Parse(LastProblemDetails[3]);
for (int j = 4; j < LastProblemDetails.Length; j++)
{
runner.outOfTimeCounters[j - 4] = int.Parse(LastProblemDetails[j]);
}
continueFromLastRun = false;
continue; // "current problem" file describes last solved problem, no need to solve it again
}
if (runner.outOfTimeCounters.Length != 0 &&
runner.outOfTimeCounters.Sum() == runner.outOfTimeCounters.Length * Constants.MAX_FAIL_COUNT) // All algs should be skipped
break;
instanceName = $"Instance-{gridSizes[gridSizeIndex]}-{obstaclesProbs[obstaclePercentageIndex]}-{agentListSizes[numOfAgentsIndex]}-{i}";
try
{
instance = ProblemInstance.Import(Path.Combine(Directory.GetCurrentDirectory(), "..", "..", "Instances", instanceName));
instance.instanceId = i;
}
catch (Exception importException)
{
if (onlyReadInstances)
{
Console.WriteLine($"File {instanceName} dosen't exist");
return;
}
instance = runner.GenerateProblemInstance(gridSizes[gridSizeIndex], agentListSizes[numOfAgentsIndex], obstaclesProbs[obstaclePercentageIndex] * gridSizes[gridSizeIndex] * gridSizes[gridSizeIndex] / 100);
instance.ComputeSingleAgentShortestPaths(); // REMOVE FOR GENERATOR
instance.instanceId = i;
instance.Export(instanceName);
}
runner.SolveGivenProblem(instance);
// Save the latest problem
StreamWriter lastProblemFile;
try
{
lastProblemFile = new StreamWriter(currentProblemFileName);
}
catch (Exception)
{
System.Threading.Thread.Sleep(1000);
lastProblemFile = new StreamWriter(currentProblemFileName);
}
lastProblemFile.Write("{0},{1},{2},{3}", gridSizeIndex, obstaclePercentageIndex, numOfAgentsIndex, i);
for (int j = 0; j < runner.outOfTimeCounters.Length; j++)
{
lastProblemFile.Write($",{runner.outOfTimeCounters[j]}");
}
lastProblemFile.Close();
lastProblemFile = null;
}
}
}
}
}
}
protected static readonly string[] daoMapPaths = {
Path.Combine("..", "..", "maps", "den520d.map"),
Path.Combine(@"..\", @"..\", "maps", "ost003d.map"),
Path.Combine(@"..\", @"..\", "maps", "brc202d.map")
};
protected static readonly string[] mazeMapPaths = {
Path.Combine(@"..\", @"..\", "maps", "maze512-1-6.map"),
Path.Combine(@"..\", @"..\", "maps", "maze512-1-2.map"),
Path.Combine(@"..\", @"..\", "maps", "maze512-1-9.map")
};
protected static readonly string[] scenDirs = {
//Path.Combine("scen", "scen1"),
//Path.Combine("..", "..", "scen", "scen-random")
Path.Combine("..", "..", "scen", "scen-custom")
};
/// <summary>
/// Dragon Age experiment
/// </summary>
/// <param name="numInstances"></param>
/// <param name="mapFilePaths"></param>
public void RunDragonAgeExperimentSet(int numInstances, string[] mapFilePaths)
{
ProblemInstance instance;
string instanceName;
using (Run runner = new Run())
{
bool resultsFileExisted = File.Exists(RESULTS_FILE_NAME);
runner.OpenResultsFile(RESULTS_FILE_NAME);
if (resultsFileExisted == false)
runner.PrintResultsFileHeader();
// FIXME: Code dup with RunExperimentSet
TextWriter output;
int[] agentListSizes = { 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100 };
//int[] agentListSizes = { 60, 65, 70, 75, 80, 85, 90, 95, 100 };
//int[] agentListSizes = { 100 };
bool continueFromLastRun = false;
string[] lineParts = null;
string currentProblemFileName = Path.Combine(
Directory.GetCurrentDirectory(), $"current-problem-{Process.GetCurrentProcess().ProcessName}");
if (File.Exists(currentProblemFileName)) //if we're continuing running from last time
{
TextReader input = new StreamReader(currentProblemFileName);
lineParts = input.ReadLine().Split(','); //get the last problem
input.Close();
continueFromLastRun = true;
}
for (int ag = 0; ag < agentListSizes.Length; ag++)
{
for (int i = 0; i < numInstances; i++)
{
//string name = Process.GetCurrentProcess().ProcessName.Substring(1);
//if (i % 33 != Convert.ToInt32(name)) // DAO!
// continue;
for (int map = 0; map < mapFilePaths.Length; map++)
{
if (continueFromLastRun) // Set the latest problem
{
ag = int.Parse(lineParts[0]);
i = int.Parse(lineParts[1]);
map = int.Parse(lineParts[2]);
for (int j = 3; j < lineParts.Length && j - 3 < runner.outOfTimeCounters.Length; j++)
{
runner.outOfTimeCounters[j - 3] = int.Parse(lineParts[j]);
}
continueFromLastRun = false;
continue; // We write the details of the last problem that was solved, no need to solve it again
}
if (runner.outOfTimeCounters.Sum() == runner.outOfTimeCounters.Length * 20) // All algs should be skipped
break;
string mapFilePath = mapFilePaths[map];
instanceName = $"{Path.GetFileNameWithoutExtension(mapFilePath)}-{agentListSizes[ag]}-{i}";
try
{
instance = ProblemInstance.Import(Path.Combine(Directory.GetCurrentDirectory(), "..", "..", "Instances", instanceName));
}
catch (Exception importException)
{
if (onlyReadInstances)
{
Console.WriteLine($"File {instanceName} dosen't exist");
return;
}
instance = runner.GenerateDragonAgeProblemInstance(mapFilePath, agentListSizes[ag]);
instance.ComputeSingleAgentShortestPaths(); // Consider just importing the generated problem after exporting it to remove the duplication of this line from Import()
instance.instanceId = i;
instance.Export(instanceName);
}
runner.SolveGivenProblem(instance);
//save the latest problem
try
{
output = new StreamWriter(currentProblemFileName);
}
catch (Exception e)
{
System.Threading.Thread.Sleep(1000);
output = new StreamWriter(currentProblemFileName);
}
output.Write("{0},{1},{2}", ag, i, map);
for (int j = 0; j < runner.outOfTimeCounters.Length; j++)
{
output.Write($",{runner.outOfTimeCounters[j]}");
}
output.Close();
}
}
}
}
}
/// <summary>
/// This is the starting point of the program.
/// </summary>
static void Main(string[] args)
{
Program me = new Program();
var resultsFileName = System.Guid.NewGuid();
//Program.RESULTS_FILE_NAME = Process.GetCurrentProcess().ProcessName + ".csv";
Program.RESULTS_FILE_NAME = resultsFileName + ".csv";
Program.METAIDRESULTS_FILE_NAME = resultsFileName + "METAID.csv";
if (System.Diagnostics.Debugger.IsAttached)
{
Constants.MAX_TIME = 300000;
Debug.WriteLine("Debugger attached - running without a timeout!!");
}
if (Directory.Exists(Path.Combine(Directory.GetCurrentDirectory(), "..", "..", "Instances")) == false)
{
Directory.CreateDirectory(Path.Combine(Directory.GetCurrentDirectory(), "..", "..", "Instances"));
}
Program.onlyReadInstances = false;
int instances = 100;
bool runGrids = false;
bool runDragonAge = false;
bool runMazesWidth1 = false;
bool runSpecific = false;
bool runPaperProblems = false;
bool runPaperProblemsIncrementally = false; // Turn on for CA*
bool runNathan = true;
if (runGrids == true)
{
//int[] gridSizes = new int[] { 8, };
//int[] agentListSizes = new int[] { 2, 3, 4 };
//int[] gridSizes = new int[] { 6, };
//int[] agentListSizes = new int[] { /*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 };
// Note that success rate drops almost to zero for EPEA* and A*+OD/SIC on 40 agents.
int[] gridSizes = new int[] { 20, };
//int[] agentListSizes = new int[] { 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, /*60, 65, 70, 75, 80, 85, 90, 95, 100, 105, 110, 115, 120, 125, 130, 135, 140, 145, 150*/ };
int[] agentListSizes = new int[] { 40, 60, 80, 100 };
//int[] obstaclesPercents = new int[] { 20, };
//int[] obstaclesPercents = new int[] { /*0, 5, 10, 15, 20, 25, 30, 35, */20, 30, 40};
int[] obstaclesPercents = new int[] { /*0, 5, 10,*/ 15, /*20, 25, 30, 35, 20, 30, 40*/ };
me.RunExperimentSet(gridSizes, agentListSizes, obstaclesPercents, instances);
}
else if (runDragonAge == true)
me.RunDragonAgeExperimentSet(instances, Program.daoMapPaths); // Obstacle percents and grid sizes built-in to the maps.
else if (runMazesWidth1 == true)
me.RunDragonAgeExperimentSet(instances, Program.mazeMapPaths); // Obstacle percents and grid sizes built-in to the maps.
else if (runSpecific == true)
{
ProblemInstance instance;
try
{
if (args[0].EndsWith(".dll"))
instance = ProblemInstance.Import(args[2], args[1]);
else
instance = ProblemInstance.Import(args[1], args[0]);
}
catch (Exception e)
{
Console.WriteLine($"Bad problem instance {args[1]}. Error: {e.Message}");
return;
}
Run runner = new Run(); // instantiates stuff unnecessarily
runner.startTime = runner.ElapsedMillisecondsTotal();
IHeuristicCalculator<WorldState> lowLevelHeuristic = new SumIndividualCosts();
List<uint> agentList = Enumerable.Range(0, instance.agents.Length).Select(x=> (uint)x).ToList(); // FIXME: Must the heuristics really receive a list of uints?
lowLevelHeuristic.Init(instance, agentList);
IIndependenceDetectionSolver lowLevel = new A_Star(lowLevelHeuristic);
ILazyHeuristic<CbsNode> highLevelHeuristic = new MvcHeuristicForCbs();
highLevelHeuristic.Init(instance, agentList);
// ISolver solver = new CBS(lowLevel, lowLevel,
// bypassStrategy: CBS.BypassStrategy.FIRST_FIT_LOOKAHEAD,
// conflictChoice: CBS.ConflictChoice.CARDINAL_MDD,
// heuristic: highLevelHeuristic,
// cacheMdds: true,
// useOldCost: true,
// replanSameCostWithMdd: true
// );
//ISolver solver = new IndependenceDetection(lowLevel, new EPEA_Star(lowLevelHeuristic));
//ISolver solver = new IndependenceDetection(lowLevel, new CostTreeSearchSolverOldMatching(3));
ISolver solver = new IndependenceDetection(lowLevel, new A_Star_WithOD(lowLevelHeuristic));
solver.Setup(instance, runner);
bool solved = solver.Solve();
if (solved == false)
{
Console.WriteLine("Failed to solve");
return;
}
Plan plan = solver.GetPlan();
plan.PrintPlan();
//me.RunInstance("Instance-5-15-3-792");
//me.RunInstance("Instance-5-15-3-792-4rows");
//me.RunInstance("Instance-5-15-3-792-3rows");
//me.RunInstance("Instance-5-15-3-792-2rows");
//me.RunInstance("corridor1");
//me.RunInstance("corridor2");
//me.RunInstance("corridor3");
//me.RunInstance("corridor4");
return;
}
else if (runPaperProblems)
{
foreach (var dirName in scenDirs)
{
foreach (var scenPath in Directory.GetFiles(dirName))
{
Console.WriteLine("Computing single agent shortest path for all agents...");
var problem = ProblemInstance.Import(scenPath);
foreach (var numAgents in Enumerable.Range(1, problem.agents.Length))
{
var subProblem = problem.Subproblem(problem.agents.Take(numAgents).ToArray());
// don't create a subproblem, just feed time adding one agent and report the sum of times so far
Run runner = new Run();
using (runner)
{
bool resultsFileExisted = File.Exists(RESULTS_FILE_NAME);
runner.OpenResultsFile(RESULTS_FILE_NAME);
if (resultsFileExisted == false)
runner.PrintResultsFileHeader();
bool success = runner.SolveGivenProblem(subProblem);
if (success == false)
break;
}
}
}
}
}
else if (runPaperProblemsIncrementally)
{
foreach (var dirName in scenDirs)
{
foreach (var scenPath in Directory.GetFiles(dirName))
{
var problem = ProblemInstance.Import(scenPath);
CooperativeAStar castar = new CooperativeAStar();
Run runner = new Run();
using (runner)
{
bool resultsFileExisted = File.Exists(RESULTS_FILE_NAME);
runner.OpenResultsFile(RESULTS_FILE_NAME);
if (resultsFileExisted == false)
runner.PrintResultsFileHeader();
runner.SolveGivenProblemIncrementally(problem);
}
}
}
}
else if (runNathan == true)
{
//string[] scenDirs = { Path.Combine(@"..","scen", "scen-random") };
string[] scenDirs = { Path.Combine(@"..", "scen", "scen-custom") };
foreach (var dirName in scenDirs)
{
foreach ( var scenPath in Directory.GetFiles(dirName))
{
if (!scenPath.EndsWith(".scen"))
{
continue;
}
Console.WriteLine(scenPath);
me.RunNathanExperimentSet(scenPath);
}
}
}
// A function to be used by Eric's PDB code
//me.runForPdb();
Console.WriteLine("*********************THE END**************************");
}
public void RunNathanExperimentSet(String scenMapFileName)
{
try
{
ProblemInstance.Import(Path.Combine(Directory.GetCurrentDirectory(), scenMapFileName));
}
catch (Exception e)
{
StringBuilder sb = new StringBuilder();
sb.Append("ERROR! occured at: "+ DateTime.Now.ToString());
sb.Append(e);
// flush every 20 seconds as you do it
File.AppendAllText("log.txt", sb.ToString());
sb.Clear();
Console.WriteLine(String.Format("Skipping bad problem instance {0}. Error: {1}", scenMapFileName, e.Message));
return;
}
}
}
}