1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.IO;
|
---|
4 | using System.Linq;
|
---|
5 | using System.Threading;
|
---|
6 | using System.Threading.Tasks;
|
---|
7 | using HeuristicLab.Core;
|
---|
8 | using HeuristicLab.Optimization;
|
---|
9 | using Newtonsoft.Json.Linq;
|
---|
10 |
|
---|
11 | namespace HeuristicLab.JsonInterface.App {
|
---|
12 | internal static class Runner {
|
---|
13 | internal static void Run(string template, string config, string outputFile) {
|
---|
14 | InstantiatorResult instantiatorResult = JsonTemplateInstantiator.Instantiate(template, config);
|
---|
15 | IOptimizer optimizer = instantiatorResult.Optimizer;
|
---|
16 |
|
---|
17 | optimizer.Runs.Clear();
|
---|
18 | if (optimizer is EngineAlgorithm e)
|
---|
19 | e.Engine = new ParallelEngine.ParallelEngine();
|
---|
20 |
|
---|
21 | Task task = optimizer.StartAsync();
|
---|
22 | while (!task.IsCompleted) {
|
---|
23 | WriteResultsToFile(outputFile, optimizer, instantiatorResult.RunCollectionModifiers);
|
---|
24 | Thread.Sleep(100);
|
---|
25 | }
|
---|
26 |
|
---|
27 | WriteResultsToFile(outputFile, optimizer, instantiatorResult.RunCollectionModifiers);
|
---|
28 | }
|
---|
29 |
|
---|
30 | private static void WriteResultsToFile(string file, IOptimizer optimizer, IEnumerable<IRunCollectionModifier> runCollectionModifiers) {
|
---|
31 | if (optimizer.Runs.Count > 0)
|
---|
32 | File.WriteAllText(file, FetchResults(optimizer, runCollectionModifiers));
|
---|
33 | }
|
---|
34 |
|
---|
35 | private static string FetchResults(IOptimizer optimizer, IEnumerable<IRunCollectionModifier> runCollectionModifiers) {
|
---|
36 | JArray arr = new JArray();
|
---|
37 |
|
---|
38 | foreach (var modifier in runCollectionModifiers)
|
---|
39 | modifier.Modify(new List<IRun>(optimizer.Runs.ToArray()));
|
---|
40 |
|
---|
41 | foreach (var run in optimizer.Runs) {
|
---|
42 | JObject obj = new JObject();
|
---|
43 | arr.Add(obj);
|
---|
44 | obj.Add("Run", JToken.FromObject(run.ToString()));
|
---|
45 | foreach (var result in run.Results)
|
---|
46 | obj.Add(result.Key, result.Value.ToString());
|
---|
47 | }
|
---|
48 | return SingleLineArrayJsonWriter.Serialize(arr);
|
---|
49 | }
|
---|
50 | }
|
---|
51 | }
|
---|