Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17511 was 17477, checked in by dpiringe, 5 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
RevLine 
[17330]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;
[17453]9using HeuristicLab.ParallelEngine;
[17464]10using HeuristicLab.Problems.DataAnalysis.Symbolic;
11using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
[17439]12using HeuristicLab.SequentialEngine;
[17442]13using Newtonsoft.Json.Linq;
[17330]14
15namespace HeuristicLab.JsonInterface.App {
16  internal static class Runner {
[17442]17    internal static void Run(string template, string config, string outputFile) {
[17477]18      InstantiatorResult instantiatorResult = JsonTemplateInstantiator.Instantiate(template, config);
19      IOptimizer optimizer = instantiatorResult.Optimizer;
20      IEnumerable<IResultJsonItem> configuredResultItem = instantiatorResult.ConfiguredResultItems;
21
[17453]22      optimizer.Runs.Clear();
[17439]23      if(optimizer is EngineAlgorithm e)
[17453]24        e.Engine = new ParallelEngine.ParallelEngine();
[17439]25     
[17395]26      Task task = optimizer.StartAsync();
[17330]27      while(!task.IsCompleted) {
[17477]28        WriteResultsToFile(outputFile, optimizer, configuredResultItem);
[17339]29        Thread.Sleep(100);
[17330]30      }
[17451]31     
[17477]32      WriteResultsToFile(outputFile, optimizer, configuredResultItem);
[17330]33    }
34
[17477]35    private static void WriteResultsToFile(string file, IOptimizer optimizer, IEnumerable<IResultJsonItem> configuredResultItem) =>
36      File.WriteAllText(file, FetchResults(optimizer, configuredResultItem));
[17330]37
[17477]38    private static string FetchResults(IOptimizer optimizer, IEnumerable<IResultJsonItem> configuredResultItem) {
[17442]39      JArray arr = new JArray();
[17477]40      IEnumerable<string> configuredResults = configuredResultItem.Select(x => x.Name);
41
[17395]42      foreach (var run in optimizer.Runs) {
[17442]43        JObject obj = new JObject();
44        arr.Add(obj);
45        obj.Add("Run", JToken.FromObject(run.ToString()));
[17395]46        foreach (var res in run.Results) {
[17477]47          if (configuredResults.Contains(res.Key)) {
[17464]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          }
[17330]55        }
[17395]56      }
[17442]57      return SingleLineArrayJsonWriter.Serialize(arr);
[17330]58    }
59  }
60}
Note: See TracBrowser for help on using the repository browser.