Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/12/21 15:24:18 (3 years ago)
Author:
dpiringe
Message:

#3026

  • added ResultFormatter to add an extra layer of result transformation logic (converting a result value to a string with a defined logic, e.g. MatlabResultFormatter for ISymbolicRegressionSolution)
  • extended the IResultJsonItem with two properties for result formatting
  • added a new control to selected a result formatter for a result value
  • refactored the Runner for the new result formatting process
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface.App/Runner.cs

    r17828 r17834  
    33using System.IO;
    44using System.Linq;
    5 using System.Text;
    65using System.Threading;
    76using System.Threading.Tasks;
    87using HeuristicLab.Optimization;
    9 using HeuristicLab.ParallelEngine;
    10 using HeuristicLab.Problems.DataAnalysis.Symbolic;
    11 using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
    12 using HeuristicLab.SequentialEngine;
    138using Newtonsoft.Json.Linq;
    149
     
    4237      File.WriteAllText(file, FetchResults(optimizer, configuredResultItem));
    4338
    44     private static string FetchResults(IOptimizer optimizer, IEnumerable<IResultJsonItem> configuredResultItem) {
     39    private static IEnumerable<IResultFormatter> ResultFormatter { get; } =
     40      PluginInfrastructure.ApplicationManager.Manager.GetInstances<IResultFormatter>();
     41
     42    private static IResultFormatter GetResultFormatter(string fullName) =>
     43      ResultFormatter?.Where(x => x.GetType().FullName == fullName).Last();
     44
     45    private static string FetchResults(IOptimizer optimizer, IEnumerable<IResultJsonItem> configuredResultItems) {
    4546      JArray arr = new JArray();
    46       IEnumerable<string> configuredResults = configuredResultItem.Select(x => x.Name);
     47      IEnumerable<string> configuredResults = configuredResultItems.Select(x => x.Name);
    4748
    4849      foreach (var run in optimizer.Runs) {
     
    5152        obj.Add("Run", JToken.FromObject(run.ToString()));
    5253
    53         // add empty values for configured results
    54         var emptyToken = JToken.FromObject("");
    55         foreach (var cr in configuredResults) {
    56           obj.Add(cr, emptyToken);
    57         }
     54        // zip and filter the results with the ResultJsonItems
     55        var filteredResults = configuredResultItems.Zip(
     56          run.Results.Where(x => configuredResultItems.Any(y => y.Name == x.Key)),
     57          (x, y) => new { Item = x, Value = y.Value });
    5858
    59         // change empty values with calculated values
    60         var formatter = new SymbolicDataAnalysisExpressionMATLABFormatter();
    61         foreach (var res in run.Results) {
    62           if(obj.ContainsKey(res.Key)) {
    63             if (res.Value is ISymbolicRegressionSolution solution) {
    64               var formattedModel = formatter.Format(solution.Model.SymbolicExpressionTree);
    65               obj[res.Key] = JToken.FromObject(formattedModel);
    66             } else {
    67               obj[res.Key] = JToken.FromObject(res.Value.ToString());
    68             }
    69           }
     59        // add results to the JObject
     60        foreach(var result in filteredResults) {
     61          var formatter = GetResultFormatter(result.Item.ResultFormatterType);
     62          obj.Add(result.Item.Name, formatter.Format(result.Value));
    7063        }
    7164      }
Note: See TracChangeset for help on using the changeset viewer.