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

Last change on this file since 17540 was 17540, checked in by dpiringe, 9 months ago

#3026:

  • added new converter: BatchRunConverter, ExperimentConverter
  • fixed a bug with the path of the .hl file in JCGenerator
  • fixed a bug with a redundant search for a suitable converter in JsonItemConverter
  • added a restriction for TargetVariable and AllowedInputVariables in RegressionProblemDataConverter
  • removed unnecessary code in Runner
File size: 2.6 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        foreach (var res in run.Results) {
51          if (configuredResults.Contains(res.Key)) {
52            if (res.Value is ISymbolicRegressionSolution solution) {
53              var formatter = new SymbolicDataAnalysisExpressionMATLABFormatter();
54              var x = formatter.Format(solution.Model.SymbolicExpressionTree);
55              obj.Add(res.Key, JToken.FromObject(x));
56            } else
57              obj.Add(res.Key, JToken.FromObject(res.Value.ToString()));
58          }
59        }
60      }
61      return SingleLineArrayJsonWriter.Serialize(arr);
62    }
63  }
64}
Note: See TracBrowser for help on using the repository browser.