Free cookie consent management tool by TermsFeed Policy Generator

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

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

#3026:

  • refactored JsonTemplateInstantiator -> now returns a InstantiatorResult which contains the optimizer and an IEnumerable of IResultJsonItem
  • code cleanup in JCGenerator
  • relocated the serialization of json items into IJsonItem with method GenerateJObject (virtual base implementation in JsonItem)
    • this allows custom serialization for json items (example: ValueLookupJsonItem)
    • items of interface IIntervalRestrictedJsonItem have a custom implementation of GenerateJObject -> hides Minimum and Maximum if the values are the physically min/max of their type
  • code cleanup in BaseConverter
File size: 2.4 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      InstantiatorResult instantiatorResult = JsonTemplateInstantiator.Instantiate(template, config);
19      IOptimizer optimizer = instantiatorResult.Optimizer;
20      IEnumerable<IResultJsonItem> configuredResultItem = instantiatorResult.ConfiguredResultItems;
21
22      optimizer.Runs.Clear();
23      if(optimizer is EngineAlgorithm e)
24        e.Engine = new ParallelEngine.ParallelEngine();
25     
26      Task task = optimizer.StartAsync();
27      while(!task.IsCompleted) {
28        WriteResultsToFile(outputFile, optimizer, configuredResultItem);
29        Thread.Sleep(100);
30      }
31     
32      WriteResultsToFile(outputFile, optimizer, configuredResultItem);
33    }
34
35    private static void WriteResultsToFile(string file, IOptimizer optimizer, IEnumerable<IResultJsonItem> configuredResultItem) =>
36      File.WriteAllText(file, FetchResults(optimizer, configuredResultItem));
37
38    private static string FetchResults(IOptimizer optimizer, IEnumerable<IResultJsonItem> configuredResultItem) {
39      JArray arr = new JArray();
40      IEnumerable<string> configuredResults = configuredResultItem.Select(x => x.Name);
41
42      foreach (var run in optimizer.Runs) {
43        JObject obj = new JObject();
44        arr.Add(obj);
45        obj.Add("Run", JToken.FromObject(run.ToString()));
46        foreach (var res in run.Results) {
47          if (configuredResults.Contains(res.Key)) {
48            if (res.Value is ISymbolicRegressionSolution solution) {
49              var formatter = new SymbolicDataAnalysisExpressionMATLABFormatter();
50              var x = formatter.Format(solution.Model.SymbolicExpressionTree);
51              obj.Add(res.Key, JToken.FromObject(x));
52            } else
53              obj.Add(res.Key, JToken.FromObject(res.Value.ToString()));
54          }
55        }
56      }
57      return SingleLineArrayJsonWriter.Serialize(arr);
58    }
59  }
60}
Note: See TracBrowser for help on using the repository browser.