-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathOptimizationRunner.cs
774 lines (699 loc) · 30 KB
/
OptimizationRunner.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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
using SC.Heuristics;
using SC.Heuristics.PrimalHeuristic;
using SC.ObjectModel;
using SC.ObjectModel.Additionals;
using SC.ObjectModel.Configuration;
using SC.ObjectModel.Generator;
using SC.ObjectModel.Interfaces;
using SC.Toolbox;
using SC.Linear;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace SC.GUI
{
/// <summary>
/// Used to execute demonstrations
/// </summary>
class OptimizationRunner
{
/// <summary>
/// The instance to solve
/// </summary>
private Instance _instance;
/// <summary>
/// The method to execute
/// </summary>
private IMethod method;
/// <summary>
/// The action used to submit solutions
/// </summary>
private Action<COSolution, bool, string, string> _submitAction;
/// <summary>
/// The action executed when finished
/// </summary>
private Action _finishAction;
/// <summary>
/// The action when starting to solve
/// </summary>
private Action _startSingleSolveAction;
/// <summary>
/// The action when finishing to solve
/// </summary>
private Action _finishSingleSolveAction;
/// <summary>
/// The export dir
/// </summary>
private string _exportDir;
/// <summary>
/// Runs the progress
/// </summary>
/// <param name="context">Dummy / not used</param>
public void Run(object context)
{
// Write instance-solution as xml
_instance.WriteXMLWithoutSolutions(Path.Combine(_exportDir, _instance.Name + ".xml"));
_startSingleSolveAction.Invoke();
PerformanceResult result = method.Run();
_finishSingleSolveAction.Invoke();
string solutionFile = _instance.Name + "_Solution.txt";
using (StreamWriter sw = new StreamWriter(File.Open(Path.Combine(_exportDir, solutionFile), FileMode.Create)))
{
sw.WriteLine("ObjectiveValue: " + result.ObjectiveValue);
sw.WriteLine("Runtime: " + result.SolutionTime.ToString());
result.Instance.OutputInfo(sw);
//result.Solution.out // TODO define output of solution info
}
_submitAction.Invoke(method.Solution, method.HasSolution, ExportationConstants.ExportDir, _instance.GetIdent());
// Write instance-solution as xml
_instance.WriteXML(Path.Combine(_exportDir, _instance.GetIdent() + "_Solution.xml"));
_finishAction();
}
/// <summary>
/// Cancels the ongoing progress
/// </summary>
public void Cancel() => method.Cancel();
/// <summary>
/// Creates a new optimization runner
/// </summary>
/// <param name="method">The method</param>
/// <param name="instance">The instance to solve</param>
/// <param name="exportDir">The export-dir</param>
/// <param name="submitAction">The action used to submit solutions</param>
/// <param name="finishAction">The action to execute when finished</param>
/// <param name="startSingleSolveAction">The action when starting to solve</param>
/// <param name="finishSingleSolveAction">The action when finishing to solve</param>
public OptimizationRunner(
IMethod method,
Instance instance,
string exportDir,
Action<COSolution, bool, string, string> submitAction,
Action finishAction,
Action startSingleSolveAction,
Action finishSingleSolveAction)
{
this.method = method;
_instance = instance;
_submitAction = submitAction;
_finishAction = finishAction;
_exportDir = exportDir;
_startSingleSolveAction = startSingleSolveAction;
_finishSingleSolveAction = finishSingleSolveAction;
}
}
/// <summary>
/// Used to execute demonstrations
/// </summary>
class EvaluationFolderRunner
{
/// <summary>
/// The method to execute
/// </summary>
private readonly IMethod _method;
/// <summary>
/// The method to execute
/// </summary>
private readonly List<Instance> _instances;
/// <summary>
/// instance names
/// </summary>
private readonly Dictionary<Instance, string> _names;
/// <summary>
/// The action executed when finished
/// </summary>
private readonly Action<PerformanceResult> _instanceFinishedAction;
/// <summary>
/// The action when finishing to solve
/// </summary>
private readonly Action _finishAction;
/// <summary>
/// The export dir
/// </summary>
private readonly string _exportDir;
/// <summary>
/// Time limit
/// </summary>
protected TimeSpan TimeLimit;
/// <summary>
/// Log every Improvement
/// </summary>
public bool DetailedLog = false;
/// <summary>
/// Creates a new optimization runner
/// </summary>
/// <param name="method">The method</param>
/// <param name="instances">The instances</param>
/// <param name="exportDir">The export-dir</param>
/// <param name="instanceFinishedAction">The action to execute when finished one instance</param>
/// <param name="finishAction">The action to execute when finished</param>
/// <param name="timeLimit">time limit</param>
public EvaluationFolderRunner(IMethod method, List<Instance> instances, Dictionary<Instance, string> names, string exportDir, Action<PerformanceResult> instanceFinishedAction, Action finishAction, TimeSpan timeLimit)
{
_method = method;
_names = names;
_instances = instances;
_finishAction = finishAction;
_instanceFinishedAction = instanceFinishedAction;
_exportDir = exportDir;
TimeLimit = timeLimit;
}
/// <summary>
/// Runs the progress
/// </summary>
/// <param name="context">Dummy / not used</param>
/// <param name="nameExtension">additional name</param>
public void Run(object context)
{
// Write instance-solution as xml
//_instance.WriteXMLWithoutSolutions(Path.Combine(_exportDir, _instance.GetIdent() + ".xml"));
foreach (var instace in _instances)
{
var output = new Dictionary<string, List<Tuple<double, double>>>();
_method.Instance = instace;
_method.Reset(); //Load the instance correctly etc.
using (var sw = new StreamWriter(File.Open(Path.Combine(_exportDir, @"evaluationResult_" + _names[_method.Instance].Replace(".xinst", "") + @".csv"), FileMode.Create)))
{
// Start statistics file
sw.Write(
"Instance" + ExportationConstants.CSV_DELIMITER +
"Runtime" + ExportationConstants.CSV_DELIMITER +
"Volume of Containters" + ExportationConstants.CSV_DELIMITER
);
//header
foreach (var config in output.Keys)
sw.Write(config + ExportationConstants.CSV_DELIMITER);
//new line
sw.WriteLine();
for (double time = 0; time < TimeLimit.TotalSeconds + 3.0; time = time + 0.1)
{
//default begining
sw.Write(
_method.Instance.GetIdent() + ExportationConstants.CSV_DELIMITER +
time.ToString(CultureInfo.InvariantCulture) + ExportationConstants.CSV_DELIMITER +
_method.Solution.VolumeOfContainers.ToString(CultureInfo.InvariantCulture) + ExportationConstants.CSV_DELIMITER
);
foreach (var config in output.Keys)
{
//get the solution value
int entry;
for (entry = 0; entry < output[config].Count && output[config][entry].Item1 <= time; entry++)
{
}
entry--;
//output to file
sw.Write(output[config][entry].Item2.ToString(CultureInfo.InvariantCulture) + ExportationConstants.CSV_DELIMITER);
}
//new line
sw.WriteLine();
}
sw.Flush();
}
//reset method
_method.Reset();
}
if (_finishAction != null)
_finishAction.Invoke();
}
/// <summary>
/// Cancels the ongoing progress
/// </summary>
public void Cancel()
{
_method.Cancel();
}
}
/// <summary>
/// Used to execute evaluations
/// </summary>
class EvaluationRunner
{
/// <summary>
/// The action when starting to solve
/// </summary>
private Action _startSingleSolveAction;
/// <summary>
/// The action when finishing to solve
/// </summary>
private Action _finishSingleSolveAction;
/// <summary>
/// The action used to submit solutions
/// </summary>
private Action<COSolution, bool, string, string> _submitAction;
/// <summary>
/// The action executed when finishing the progress
/// </summary>
private Action _finishAction;
/// <summary>
/// The action used to log
/// </summary>
private Action<string> _logAction;
/// <summary>
/// The export-dir
/// </summary>
private string _exportDir;
/// <summary>
/// Indicates a cancelation of the progress
/// </summary>
private bool _cancel;
/// <summary>
/// The statistics-file to log to
/// </summary>
string _statisticsFile = "Statistics.csv";
/// <summary>
/// The instance to solve
/// </summary>
private Instance _instance;
/// <summary>
/// The list of all obtained evaluation values
/// </summary>
private List<double> _evaluationValues;
/// <summary>
/// The list of all best-bounds
/// </summary>
private List<double> _bestBounds;
/// <summary>
/// The list of all gaps
/// </summary>
private List<double> _gaps;
/// <summary>
/// The list of the time consumed per run
/// </summary>
private List<TimeSpan> _timeConsumed;
#region Parameter fields
/// <summary>
/// The method to use
/// </summary>
private IMethod _method;
/// <summary>
/// The type of the method to use
/// </summary>
private MethodType _methodToUse;
/// <summary>
/// The config
/// </summary>
public Configuration Config { get; set; }
#endregion
#region Instance generation fields
/// <summary>
/// The config for the instance-generator
/// </summary>
public InstanceGeneratorConfiguration GeneratorConfig { get; set; }
/// <summary>
/// The number of seed-passes to do
/// </summary>
public int SeedPasses { get; set; }
#endregion
/// <summary>
/// Runs the process
/// </summary>
/// <param name="context">Dummy / not in use</param>
public void Run(object context)
{
// Prepare statistics file
using (StreamWriter statisticsWriter = new StreamWriter(File.Open(Path.Combine(_exportDir, _statisticsFile), FileMode.Create)))
{
// Start statistics file
statisticsWriter.WriteLine(
"Instance" + ExportationConstants.CSV_DELIMITER +
"Runtime" + ExportationConstants.CSV_DELIMITER +
"Solution" + ExportationConstants.CSV_DELIMITER +
"BestBound" + ExportationConstants.CSV_DELIMITER +
"Gap");
}
// Init default config
switch (_methodToUse)
{
case MethodType.ExtremePointInsertion:
case MethodType.SpaceDefragmentation:
case MethodType.PushInsertion:
{
if (Config == null)
{
Config = new Configuration(_methodToUse, true) { Log = _logAction };
}
}
break;
case MethodType.FrontLeftBottomStyle:
case MethodType.TetrisStyle:
{
if (Config == null)
{
Config = new Configuration(_methodToUse, true) { Log = _logAction };
}
}
break;
case MethodType.SpaceIndexed:
throw new NotImplementedException("Space indexed model not working for now.");
default:
throw new ArgumentException("Unknown method: " + _methodToUse.ToString());
}
_timeConsumed = _timeConsumed ?? new List<TimeSpan>();
_evaluationValues = _evaluationValues ?? new List<double>();
_bestBounds = _bestBounds ?? new List<double>();
_gaps = _gaps ?? new List<double>();
// Solve incrementally for each seed
for (int k = 0; k < SeedPasses; k++)
{
// Solve incrementally for given container count
for (int j = GeneratorConfig.ContainerMin; j <= GeneratorConfig.ContainerMax; j++)
{
// Solve incrementally for given piece count
for (int i = 1; i <= GeneratorConfig.MaxBoxCount && !_cancel; i++)
{
// Prepare
switch (_methodToUse)
{
case MethodType.ExtremePointInsertion:
case MethodType.TetrisStyle:
{
_instance = new Instance();
_instance.Containers = InstanceGenerator.GenerateContainer(
j,
GeneratorConfig.ContainerSideLengthMin,
GeneratorConfig.ContainerSideLengthMax,
GeneratorConfig.Rounding,
k);
_instance.Pieces = InstanceGenerator.GeneratePerformanceTestTetrisPieces(
i,
GeneratorConfig.PieceMaxSize,
GeneratorConfig.PieceMinSize,
GeneratorConfig.PieceMinEquals,
GeneratorConfig.PieceMaxEquals,
roundedDecimals: GeneratorConfig.Rounding,
seed: k);
}
break;
case MethodType.FrontLeftBottomStyle:
case MethodType.SpaceDefragmentation:
case MethodType.PushInsertion:
{
_instance = new Instance();
_instance.Containers = InstanceGenerator.GenerateContainer(
j,
GeneratorConfig.ContainerSideLengthMin,
GeneratorConfig.ContainerSideLengthMax,
GeneratorConfig.Rounding,
k);
_instance.Pieces = InstanceGenerator.GeneratePieces(
i,
GeneratorConfig.PieceMaxSize,
GeneratorConfig.PieceMinSize,
GeneratorConfig.PieceMinEquals,
GeneratorConfig.PieceMaxEquals,
roundedDecimals: GeneratorConfig.Rounding,
seed: k);
}
break;
case MethodType.SpaceIndexed:
throw new NotImplementedException("Space indexed model not working for now.");
default:
throw new ArgumentException("Unknown method: " + _methodToUse.ToString());
}
// Execute
Execute(ExportationConstants.ExportDir, _instance.GetIdent(), int.MaxValue);
}
}
}
_finishAction();
}
/// <summary>
/// Executes a single method-run
/// </summary>
/// <param name="exportationDir">The exportation dir to export the results to</param>
/// <param name="logFilename">The name of the log-files</param>
/// <param name="timeOut">A timeout in milliseconds</param>
private void Execute(string exportationDir, string logFilename, int timeOut)
{
// Prepare
IMethod method = null;
switch (_methodToUse)
{
case MethodType.FrontLeftBottomStyle:
{
method = new LinearModelFLB(_instance, Config);
}
break;
case MethodType.TetrisStyle:
{
method = new LinearModelTetris(_instance, Config);
}
break;
case MethodType.HybridStyle:
{
method = new LinearModelHybrid(_instance, Config);
}
break;
case MethodType.SpaceIndexed:
throw new NotImplementedException("Space indexed model not working for now.");
case MethodType.ExtremePointInsertion:
{
method = new ExtremePointInsertionHeuristic(_instance, Config);
}
break;
case MethodType.SpaceDefragmentation:
{
method = new SpaceDefragmentationHeuristic(_instance, Config);
}
break;
case MethodType.PushInsertion:
{
method = new PushInsertion(_instance, Config);
}
break;
case MethodType.ALNS:
{
method = new ALNS(_instance, Config);
}
break;
default:
throw new ArgumentException("Unknown method: " + _methodToUse.ToString());
}
// Write instance-solution as xml
string xmlInstanceFile = logFilename + ".xinst";
if (!File.Exists(xmlInstanceFile))
{
_instance.WriteXML(Path.Combine(exportationDir, xmlInstanceFile));
}
_method = method;
// Execute
_startSingleSolveAction.Invoke();
PerformanceResult result = null;
bool executionSuccess = Helper.TryExecute(method.Run, timeOut, out result);
_finishSingleSolveAction.Invoke();
string solutionFile = logFilename + "_Solution.txt";
using (StreamWriter solutionInfoWriter = new StreamWriter(File.Open(Path.Combine(exportationDir, solutionFile), FileMode.Create)))
{
solutionInfoWriter.WriteLine("Runtime: " + ((executionSuccess) ? result.SolutionTime.ToString() : "timeout"));
solutionInfoWriter.WriteLine("ObjectiveValue: " + ((executionSuccess) ? result.ObjectiveValue.ToString(ExportationConstants.FORMATTER) : "timeout"));
solutionInfoWriter.WriteLine("BestBound: " + ((executionSuccess) ? result.BestBound.ToString(ExportationConstants.FORMATTER) : "timeout"));
solutionInfoWriter.WriteLine("RemainingGap: " + ((executionSuccess) ? result.Gap.ToString(ExportationConstants.FORMATTER) : "timeout"));
if (executionSuccess)
{
result.Instance.OutputInfo(solutionInfoWriter);
}
//result.Solution.out // TODO define output of solution info
}
// Log information
if (executionSuccess)
{
_timeConsumed.Add(result.SolutionTime);
_evaluationValues.Add(result.ObjectiveValue);
_bestBounds.Add(result.BestBound);
_gaps.Add(result.Gap);
}
else
{
_timeConsumed.Add(TimeSpan.FromMilliseconds(timeOut));
_evaluationValues.Add(0);
_bestBounds.Add(double.NaN);
_gaps.Add(double.NaN);
}
// Write statistics
using (StreamWriter statisticsWriter = new StreamWriter(File.Open(Path.Combine(exportationDir, _statisticsFile), FileMode.Append)))
{
statisticsWriter.WriteLine(
_instance.GetIdent() +
ExportationConstants.CSV_DELIMITER +
((executionSuccess) ?
result.SolutionTime.TotalSeconds.ToString(ExportationConstants.EXPORT_FORMAT_SHORT, ExportationConstants.FORMATTER) :
TimeSpan.FromMilliseconds(timeOut).TotalSeconds.ToString(ExportationConstants.EXPORT_FORMAT_SHORT, ExportationConstants.FORMATTER)) +
ExportationConstants.CSV_DELIMITER +
((executionSuccess) ?
result.ObjectiveValue.ToString(ExportationConstants.EXPORT_FORMAT_SHORT, ExportationConstants.FORMATTER) :
(0.0).ToString(ExportationConstants.EXPORT_FORMAT_SHORT, ExportationConstants.FORMATTER)) +
ExportationConstants.CSV_DELIMITER +
((executionSuccess) ?
result.BestBound.ToString(ExportationConstants.EXPORT_FORMAT_SHORT, ExportationConstants.FORMATTER) :
double.NaN.ToString(ExportationConstants.EXPORT_FORMAT_SHORT, ExportationConstants.FORMATTER)) +
ExportationConstants.CSV_DELIMITER +
((executionSuccess) ?
result.Gap.ToString(ExportationConstants.EXPORT_FORMAT_SHORT, ExportationConstants.FORMATTER) :
double.NaN.ToString(ExportationConstants.EXPORT_FORMAT_SHORT, ExportationConstants.FORMATTER)));
}
// Write instance-solution as xml
if (_instance.Solutions.Any())
{
_instance.WriteXML(Path.Combine(exportationDir, logFilename + "_Solution.xinst"));
}
// Draw visuals
if (_method.HasSolution)
{
_submitAction(method.Solution, _method.HasSolution, exportationDir, logFilename);
}
// Wait a bit
Thread.Sleep(5000);
}
/// <summary>
/// Cancels any ongoing progress at the next point it is safe
/// </summary>
public void Cancel()
{
if (_method != null)
{
_method.Cancel();
}
_cancel = true;
}
/// <summary>
/// Creates a new evaluation-runner
/// </summary>
/// <param name="methodToUse">Defines the method to execute</param>
/// <param name="exportDir">Defines the export-dir</param>
/// <param name="submitSolutionAction">The action used to submit solutions</param>
/// <param name="logAction">The action used to log</param>
/// <param name="finishAction">The action to execute when finished</param>
/// <param name="startSingleSolveAction">The action executed when starting a single process</param>
/// <param name="finishSingleSolveAction">The action executed when finishing a single process</param>
public EvaluationRunner(
MethodType methodToUse,
string exportDir,
Action<COSolution, bool, string, string> submitSolutionAction,
Action<string> logAction,
Action finishAction,
Action startSingleSolveAction,
Action finishSingleSolveAction)
{
_methodToUse = methodToUse;
_startSingleSolveAction = startSingleSolveAction;
_finishSingleSolveAction = finishSingleSolveAction;
_finishAction = finishAction;
_submitAction = submitSolutionAction;
_logAction = logAction;
_exportDir = exportDir;
}
}
class BatchRunner
{
/// <summary>
/// The method to use.
/// </summary>
public IMethod Method;
/// <summary>
/// Creates all instances to solve.
/// </summary>
public Func<Instance> InstanceCreator;
/// <summary>
/// The timeout to adhere to.
/// </summary>
public double Timeout;
/// <summary>
/// The directory to write all results to.
/// </summary>
public string ResultsDir = Path.Combine(Environment.CurrentDirectory, "Results");
/// <summary>
/// The name of the result file.
/// </summary>
public string StatisticsFile = "results.csv";
/// <summary>
/// The action to invoke, if a solution was found.
/// </summary>
public Action<COSolution, bool, string, string> SubmitSolutionAction;
/// <summary>
/// The action to use for logging a line.
/// </summary>
public Action<string> LogAction;
/// <summary>
/// The action to invoke after finishing all runs.
/// </summary>
public Action FinishAction;
/// <summary>
/// The action to invoke before starting a single run.
/// </summary>
public Action StartSingleSolveAction;
/// <summary>
/// The action to invoke after starting a single run.
/// </summary>
public Action FinishSingleSolveAction;
/// <summary>
/// Indicates that a quit is pending.
/// </summary>
private bool _quit = false;
/// <summary>
/// Cancels the ongoing progress
/// </summary>
public void Cancel()
{
_quit = true;
Method.Cancel();
}
/// <summary>
/// Runs the process.
/// </summary>
/// <param name="context">Dummy / not in use</param>
public void Run(object context)
{
// Prepare directory (clear previous results)
if (Directory.Exists(ResultsDir))
{ Directory.Delete(ResultsDir, true); Thread.Sleep(50); }
Directory.CreateDirectory(ResultsDir);
Thread.Sleep(50);
// Start statistics file
using (StreamWriter sw = new StreamWriter(Path.Combine(ResultsDir, StatisticsFile), false))
sw.WriteLine(
"Instance" + ExportationConstants.CSV_DELIMITER +
"Runtime" + ExportationConstants.CSV_DELIMITER +
"SpaceUtil" + ExportationConstants.CSV_DELIMITER +
"PackedPieces");
// Prepare
Method.Config.TimeLimit = TimeSpan.FromSeconds(Timeout);
Method.Config.Log = LogAction;
// Execute all
Instance instance = InstanceCreator();
while (instance != null)
{
// Quit, if desired
if (_quit)
break;
// Prepare individual run
Method.Instance = instance;
Method.Reset();
instance.WriteXMLWithoutSolutions(Path.Combine(ResultsDir, instance.Name + "_raw.xinst"));
// Execute
StartSingleSolveAction?.Invoke();
PerformanceResult result = Method.Run();
FinishSingleSolveAction?.Invoke();
// Write result
instance.WriteXML(Path.Combine(ResultsDir, instance.Name + "_sol.xinst"));
using (StreamWriter sw = new StreamWriter(Path.Combine(ResultsDir, StatisticsFile), true))
sw.WriteLine(
instance.Name + ExportationConstants.CSV_DELIMITER +
result.SolutionTime.TotalSeconds.ToString(CultureInfo.InvariantCulture) + ExportationConstants.CSV_DELIMITER +
instance.Solutions.Single().VolumeContainedRelative.ToString(CultureInfo.InvariantCulture) + ExportationConstants.CSV_DELIMITER +
instance.Solutions.Single().NumberOfPiecesPacked.ToString(CultureInfo.InvariantCulture));
// Update view
SubmitSolutionAction?.Invoke(instance.Solutions.Single(), Method.HasSolution, ResultsDir, instance.Name);
// Create next instance
instance = InstanceCreator();
}
// Mark finish
FinishAction?.Invoke();
}
}
}