Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17536 was 17519, checked in by dpiringe, 5 years ago

#3026:

  • added error output for failed runner initialization
  • reorganised some final view models
  • TargetedJsonItemType (in JsonItemVMBase) now automatically returns the type of the defined JsonItem
  • code cleanup
  • refactored RegressionProblemDataConverter
  • added lots of comments
  • added new view for StringArrayJsonItem
  • added new UI component for concrete restricted items and used it in JsonItemConcreteItemArrayControl and JsonItemValidValuesControl
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        foreach (var res in run.Results) {
51          if (configuredResults.Contains(res.Key)) {
52            if (res.Value is ISymbolicRegressionSolution solution) {
53              /* TEST */
54              var csFormatter = new CSharpSymbolicExpressionTreeStringFormatter();
55              File.WriteAllText(@"C:\Workspace\output\csFormatted.cs", csFormatter.Format(solution.Model.SymbolicExpressionTree));
56              /* END TEST */
57              var formatter = new SymbolicDataAnalysisExpressionMATLABFormatter();
58              var x = formatter.Format(solution.Model.SymbolicExpressionTree);
59              obj.Add(res.Key, JToken.FromObject(x));
60            } else
61              obj.Add(res.Key, JToken.FromObject(res.Value.ToString()));
62          }
63        }
64      }
65      return SingleLineArrayJsonWriter.Serialize(arr);
66    }
67  }
68}
Note: See TracBrowser for help on using the repository browser.