Free cookie consent management tool by TermsFeed Policy Generator

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

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

#3026

  • added a first version of a new feature called ResultCollectionPostProcessors, which should transform/process the results of an optimizer run
    • created a new interface IResultCollectionPostProcessor
    • created a new class SymRegPythonPostProcessor, which formats all ISymbolicRegressionSolution with the usage of the SymbolicDataAnalysisExpressionPythonFormatter
    • changed the generation and instantiation of templates to handle this new feature
    • the template files contains a new area PostProcessors
    • added functionality in Runner to use these new type of result processing
    • added a new tab in ExportJsonDialog to configure PostProcessors
File size: 3.3 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      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, instantiatorResult.PostProcessors);
25        Thread.Sleep(100);
26      }
27
28      WriteResultsToFile(outputFile, optimizer, configuredResultItem, instantiatorResult.PostProcessors);
29    }
30
31    private static void WriteResultsToFile(string file, IOptimizer optimizer, IEnumerable<IResultJsonItem> configuredResultItem, IEnumerable<IResultCollectionPostProcessor> postProcessors) {
32      if (optimizer.Runs.Count > 0)
33        File.WriteAllText(file, FetchResults(optimizer, configuredResultItem, postProcessors));
34    }
35     
36
37    private static IEnumerable<IResultFormatter> ResultFormatter { get; } =
38      PluginInfrastructure.ApplicationManager.Manager.GetInstances<IResultFormatter>();
39
40    private static IResultFormatter GetResultFormatter(string fullName) =>
41      ResultFormatter?.Where(x => x.GetType().FullName == fullName).Last();
42
43    private static string FetchResults(IOptimizer optimizer, IEnumerable<IResultJsonItem> configuredResultItems, IEnumerable<IResultCollectionPostProcessor> postProcessors) {
44      JArray arr = new JArray();
45      IEnumerable<string> configuredResults = configuredResultItems.Select(x => x.Name);
46
47      foreach (var run in optimizer.Runs) {
48        JObject obj = new JObject();
49        arr.Add(obj);
50        obj.Add("Run", JToken.FromObject(run.ToString()));
51
52        // zip and filter the results with the ResultJsonItems
53        var filteredResults = new List<Tuple<IResultJsonItem, IItem>>();
54        foreach(var resultItem in configuredResultItems) {
55          foreach(var result in run.Results) {
56            if(resultItem.Name == result.Key) {
57              filteredResults.Add(Tuple.Create(resultItem, result.Value));
58            }
59          }
60        }
61
62        // add results to the JObject
63        foreach(var result in filteredResults) {
64          var formatter = GetResultFormatter(result.Item1.ResultFormatterType);
65          if(!obj.ContainsKey(result.Item1.Name)) // to prevent duplicates
66            obj.Add(result.Item1.Name, formatter.Format(result.Item2));
67        }
68
69        IDictionary<string, string> resultDict = new Dictionary<string, string>();
70        foreach (var processor in postProcessors) {
71          processor.Apply(run.Results, resultDict);
72        }
73        foreach(var kvp in resultDict) {
74          obj.Add(kvp.Key, kvp.Value);
75        }
76      }
77      return SingleLineArrayJsonWriter.Serialize(arr);
78    }
79  }
80}
Note: See TracBrowser for help on using the repository browser.