Free cookie consent management tool by TermsFeed Policy Generator

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

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

#3026:

  • added an extra loop to add empty default results in Runner (to prevent not existing results)
  • fixed a bug in JsonItemValidValuesControl, now the dropdown should not reset the default value
File size: 2.8 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        File.WriteAllText(outputFile, e.Message + "\n\n\n\n" + e.StackTrace);
36      }
37    }
38
39    private static void WriteResultsToFile(string file, IOptimizer optimizer, IEnumerable<IResultJsonItem> configuredResultItem) =>
40      File.WriteAllText(file, FetchResults(optimizer, configuredResultItem));
41
42    private static string FetchResults(IOptimizer optimizer, IEnumerable<IResultJsonItem> configuredResultItem) {
43      JArray arr = new JArray();
44      IEnumerable<string> configuredResults = configuredResultItem.Select(x => x.Name);
45
46      foreach (var run in optimizer.Runs) {
47        JObject obj = new JObject();
48        arr.Add(obj);
49        obj.Add("Run", JToken.FromObject(run.ToString()));
50
51        // add empty values for configured results
52        var emptyToken = JToken.FromObject("");
53        foreach (var cr in configuredResults) {
54          obj.Add(cr, emptyToken);
55        }
56
57        // change empty values with calculated values
58        var formatter = new SymbolicDataAnalysisExpressionMATLABFormatter();
59        foreach (var res in run.Results) {
60          if(obj.ContainsKey(res.Key)) {
61            if (res.Value is ISymbolicRegressionSolution solution) {
62              var formattedModel = formatter.Format(solution.Model.SymbolicExpressionTree);
63              obj[res.Key] = JToken.FromObject(formattedModel);
64            } else {
65              obj[res.Key] = JToken.FromObject(res.Value.ToString());
66            }
67          }
68        }
69      }
70      return SingleLineArrayJsonWriter.Serialize(arr);
71    }
72  }
73}
Note: See TracBrowser for help on using the repository browser.