Free cookie consent management tool by TermsFeed Policy Generator

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

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

#3026

  • removed the option to set the value for JsonItems via exporter
    • reworked some base controls
    • added new controls for JsonItem specific properties (e.g. ArrayResizable)
    • deleted a lot of obsolet controls
  • removed the Enable checkbox in the detail view of JsonItems
  • exporter now clones the IOptimizer object
  • added a check + message for unsupported exports
  • list of JsonItems now includes unsupported JsonItems (disabled and marked with 'unsupported')
  • refactored the converter type check
    • now every converter has to specify its supported type(s)
File size: 2.9 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.ParallelEngine;
10using HeuristicLab.Problems.DataAnalysis.Symbolic;
11using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
12using HeuristicLab.SequentialEngine;
13using Newtonsoft.Json.Linq;
14
15namespace HeuristicLab.JsonInterface.App {
16  internal static class Runner {
17    internal static void Run(string template, string config, string outputFile) {
18      try {
19        InstantiatorResult instantiatorResult = JsonTemplateInstantiator.Instantiate(template, config);
20        IOptimizer optimizer = instantiatorResult.Optimizer;
21        IEnumerable<IResultJsonItem> configuredResultItem = instantiatorResult.ConfiguredResultItems;
22
23        optimizer.Runs.Clear();
24        if (optimizer is EngineAlgorithm e)
25          e.Engine = new ParallelEngine.ParallelEngine();
26
27        Task task = optimizer.StartAsync();
28        while (!task.IsCompleted) {
29          WriteResultsToFile(outputFile, optimizer, configuredResultItem);
30          Thread.Sleep(100);
31        }
32
33        WriteResultsToFile(outputFile, optimizer, configuredResultItem);
34      } catch (Exception e) {
35        Console.Error.WriteLine($"{e.Message} \n\n\n\n {e.StackTrace}");
36        File.WriteAllText(outputFile, e.Message + "\n\n\n\n" + e.StackTrace);
37        Environment.Exit(-1);
38      }
39    }
40
41    private static void WriteResultsToFile(string file, IOptimizer optimizer, IEnumerable<IResultJsonItem> configuredResultItem) =>
42      File.WriteAllText(file, FetchResults(optimizer, configuredResultItem));
43
44    private static string FetchResults(IOptimizer optimizer, IEnumerable<IResultJsonItem> configuredResultItem) {
45      JArray arr = new JArray();
46      IEnumerable<string> configuredResults = configuredResultItem.Select(x => x.Name);
47
48      foreach (var run in optimizer.Runs) {
49        JObject obj = new JObject();
50        arr.Add(obj);
51        obj.Add("Run", JToken.FromObject(run.ToString()));
52
53        // add empty values for configured results
54        var emptyToken = JToken.FromObject("");
55        foreach (var cr in configuredResults) {
56          obj.Add(cr, emptyToken);
57        }
58
59        // change empty values with calculated values
60        var formatter = new SymbolicDataAnalysisExpressionMATLABFormatter();
61        foreach (var res in run.Results) {
62          if(obj.ContainsKey(res.Key)) {
63            if (res.Value is ISymbolicRegressionSolution solution) {
64              var formattedModel = formatter.Format(solution.Model.SymbolicExpressionTree);
65              obj[res.Key] = JToken.FromObject(formattedModel);
66            } else {
67              obj[res.Key] = JToken.FromObject(res.Value.ToString());
68            }
69          }
70        }
71      }
72      return SingleLineArrayJsonWriter.Serialize(arr);
73    }
74  }
75}
Note: See TracBrowser for help on using the repository browser.