Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17869 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
Line 
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Linq;
5using System.Threading;
6using System.Threading.Tasks;
7using HeuristicLab.Optimization;
8using Newtonsoft.Json.Linq;
9
10namespace HeuristicLab.JsonInterface.App {
11  internal static class Runner {
12    internal static void Run(string template, string config, string outputFile) {
13      try {
14        InstantiatorResult instantiatorResult = JsonTemplateInstantiator.Instantiate(template, config);
15        IOptimizer optimizer = instantiatorResult.Optimizer;
16        IEnumerable<IResultJsonItem> configuredResultItem = instantiatorResult.ConfiguredResultItems;
17
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
28        WriteResultsToFile(outputFile, optimizer, configuredResultItem);
29      } catch (Exception e) {
30        Console.Error.WriteLine($"{e.Message} \n\n\n\n {e.StackTrace}");
31        File.WriteAllText(outputFile, e.Message + "\n\n\n\n" + e.StackTrace);
32        Environment.Exit(-1);
33      }
34    }
35
36    private static void WriteResultsToFile(string file, IOptimizer optimizer, IEnumerable<IResultJsonItem> configuredResultItem) =>
37      File.WriteAllText(file, FetchResults(optimizer, configuredResultItem));
38
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) {
46      JArray arr = new JArray();
47      IEnumerable<string> configuredResults = configuredResultItems.Select(x => x.Name);
48
49      foreach (var run in optimizer.Runs) {
50        JObject obj = new JObject();
51        arr.Add(obj);
52        obj.Add("Run", JToken.FromObject(run.ToString()));
53
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 });
58
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));
63        }
64      }
65      return SingleLineArrayJsonWriter.Serialize(arr);
66    }
67  }
68}
Note: See TracBrowser for help on using the repository browser.