Free cookie consent management tool by TermsFeed Policy Generator

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

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

#3026

  • added ResultFormatter to add an extra layer of result transformation logic (converting a result value to a string with a defined logic, e.g. MatlabResultFormatter for ISymbolicRegressionSolution)
  • extended the IResultJsonItem with two properties for result formatting
  • added a new control to selected a result formatter for a result value
  • refactored the Runner for the new result formatting process
File size: 2.8 KB
RevLine 
[17330]1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Linq;
5using System.Threading;
6using System.Threading.Tasks;
7using HeuristicLab.Optimization;
[17442]8using Newtonsoft.Json.Linq;
[17330]9
10namespace HeuristicLab.JsonInterface.App {
11  internal static class Runner {
[17442]12    internal static void Run(string template, string config, string outputFile) {
[17519]13      try {
14        InstantiatorResult instantiatorResult = JsonTemplateInstantiator.Instantiate(template, config);
15        IOptimizer optimizer = instantiatorResult.Optimizer;
16        IEnumerable<IResultJsonItem> configuredResultItem = instantiatorResult.ConfiguredResultItems;
[17477]17
[17519]18        optimizer.Runs.Clear();
19        if (optimizer is EngineAlgorithm e)
20          e.Engine = new ParallelEngine.ParallelEngine();
21
22        Task task = optimizer.StartAsync();
23        while (!task.IsCompleted) {
24          WriteResultsToFile(outputFile, optimizer, configuredResultItem);
25          Thread.Sleep(100);
26        }
27
[17477]28        WriteResultsToFile(outputFile, optimizer, configuredResultItem);
[17519]29      } catch (Exception e) {
[17828]30        Console.Error.WriteLine($"{e.Message} \n\n\n\n {e.StackTrace}");
[17519]31        File.WriteAllText(outputFile, e.Message + "\n\n\n\n" + e.StackTrace);
[17828]32        Environment.Exit(-1);
[17330]33      }
34    }
35
[17477]36    private static void WriteResultsToFile(string file, IOptimizer optimizer, IEnumerable<IResultJsonItem> configuredResultItem) =>
37      File.WriteAllText(file, FetchResults(optimizer, configuredResultItem));
[17330]38
[17834]39    private static IEnumerable<IResultFormatter> ResultFormatter { get; } =
40      PluginInfrastructure.ApplicationManager.Manager.GetInstances<IResultFormatter>();
41
42    private static IResultFormatter GetResultFormatter(string fullName) =>
43      ResultFormatter?.Where(x => x.GetType().FullName == fullName).Last();
44
45    private static string FetchResults(IOptimizer optimizer, IEnumerable<IResultJsonItem> configuredResultItems) {
[17442]46      JArray arr = new JArray();
[17834]47      IEnumerable<string> configuredResults = configuredResultItems.Select(x => x.Name);
[17477]48
[17395]49      foreach (var run in optimizer.Runs) {
[17442]50        JObject obj = new JObject();
51        arr.Add(obj);
52        obj.Add("Run", JToken.FromObject(run.ToString()));
[17560]53
[17834]54        // zip and filter the results with the ResultJsonItems
55        var filteredResults = configuredResultItems.Zip(
56          run.Results.Where(x => configuredResultItems.Any(y => y.Name == x.Key)),
57          (x, y) => new { Item = x, Value = y.Value });
[17560]58
[17834]59        // add results to the JObject
60        foreach(var result in filteredResults) {
61          var formatter = GetResultFormatter(result.Item.ResultFormatterType);
62          obj.Add(result.Item.Name, formatter.Format(result.Value));
[17330]63        }
[17395]64      }
[17442]65      return SingleLineArrayJsonWriter.Serialize(arr);
[17330]66    }
67  }
68}
Note: See TracBrowser for help on using the repository browser.