Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface.App/Runner.cs @ 18183

Last change on this file since 18183 was 18059, checked in by dpiringe, 3 years ago

#3026

  • removed/renamed all interfaces, classes and views related to results (are replaced by RunCollectionModifiers)
  • fixed a bug in JsonTemplateInstantiator to prevent errors for opening templates without a defined property for RunCollectionModifiers
File size: 1.9 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Linq;
5using System.Threading;
6using System.Threading.Tasks;
7using HeuristicLab.Core;
8using HeuristicLab.Optimization;
9using Newtonsoft.Json.Linq;
10
11namespace 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}
Note: See TracBrowser for help on using the repository browser.