Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17442 was 17442, checked in by dpiringe, 4 years ago

#3026:

  • added new CLI argument StringArgument
  • removed unnecessary check in StartArgument
  • changed output format from simple .txt to .json in HeuristicLab.JsonInterface.App.Runner
  • user has to enter output path now when executing JsonInterface on console
File size: 1.7 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Linq;
5using System.Text;
6using System.Threading;
7using System.Threading.Tasks;
8using HeuristicLab.Optimization;
9using HeuristicLab.SequentialEngine;
10using Newtonsoft.Json.Linq;
11
12namespace HeuristicLab.JsonInterface.App {
13  internal static class Runner {
14    internal static void Run(string template, string config, string outputFile) {
15      IOptimizer optimizer = JsonTemplateInstantiator.Instantiate(template, config, out IEnumerable<string> allowedResultNames);
16      if(optimizer is EngineAlgorithm e)
17        e.Engine = new SequentialEngine.SequentialEngine();
18     
19      Task task = optimizer.StartAsync();
20      while(!task.IsCompleted) {
21        WriteResultsToFile(outputFile, optimizer, allowedResultNames);
22        Thread.Sleep(100);
23      }
24      WriteResultsToFile(outputFile, optimizer, allowedResultNames);
25    }
26
27    private static void WriteResultsToFile(string file, IOptimizer optimizer, IEnumerable<string> allowedResultNames) =>
28      File.WriteAllText(file, FetchResults(optimizer, allowedResultNames));
29
30    private static string FetchResults(IOptimizer optimizer, IEnumerable<string> allowedResultNames) {
31      JArray arr = new JArray();
32     
33      foreach (var run in optimizer.Runs) {
34        JObject obj = new JObject();
35        arr.Add(obj);
36        obj.Add("Run", JToken.FromObject(run.ToString()));
37        foreach (var res in run.Results) {
38          if (allowedResultNames.Contains(res.Key))
39            obj.Add(res.Key, JToken.FromObject(res.Value.ToString()));
40        }
41      }
42      return SingleLineArrayJsonWriter.Serialize(arr);
43    }
44  }
45}
Note: See TracBrowser for help on using the repository browser.