Changeset 17834


Ignore:
Timestamp:
02/12/21 15:24:18 (7 months 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
Location:
branches/3026_IntegrationIntoSymSpace
Files:
9 added
14 edited

Legend:

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

    r17599 r17834  
    102102      <Private>False</Private>
    103103    </ProjectReference>
    104     <ProjectReference Include="..\HeuristicLab.Encodings.SymbolicExpressionTreeEncoding\3.4\HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4.csproj">
    105       <Project>{06D4A186-9319-48A0-BADE-A2058D462EEA}</Project>
    106       <Name>HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4</Name>
    107       <Private>False</Private>
    108     </ProjectReference>
    109104    <ProjectReference Include="..\HeuristicLab.JsonInterface\HeuristicLab.JsonInterface.csproj">
    110105      <Project>{0e3aab5e-f152-44e0-a054-4d9a83ecee08}</Project>
     
    125120      <Project>{94186a6a-5176-4402-ae83-886557b53cca}</Project>
    126121      <Name>HeuristicLab.PluginInfrastructure-3.3</Name>
    127       <Private>False</Private>
    128     </ProjectReference>
    129     <ProjectReference Include="..\HeuristicLab.Problems.DataAnalysis.Symbolic.Regression\3.4\HeuristicLab.Problems.DataAnalysis.Symbolic.Regression-3.4.csproj">
    130       <Project>{5AC82412-911B-4FA2-A013-EDC5E3F3FCC2}</Project>
    131       <Name>HeuristicLab.Problems.DataAnalysis.Symbolic.Regression-3.4</Name>
    132       <Private>False</Private>
    133     </ProjectReference>
    134     <ProjectReference Include="..\HeuristicLab.Problems.DataAnalysis.Symbolic\3.4\HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.csproj">
    135       <Project>{3d28463f-ec96-4d82-afee-38be91a0ca00}</Project>
    136       <Name>HeuristicLab.Problems.DataAnalysis.Symbolic-3.4</Name>
    137       <Private>False</Private>
    138     </ProjectReference>
    139     <ProjectReference Include="..\HeuristicLab.Problems.DataAnalysis\3.4\HeuristicLab.Problems.DataAnalysis-3.4.csproj">
    140       <Project>{DF87C13E-A889-46FF-8153-66DCAA8C5674}</Project>
    141       <Name>HeuristicLab.Problems.DataAnalysis-3.4</Name>
    142122      <Private>False</Private>
    143123    </ProjectReference>
  • 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      }
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface.OptimizerIntegration/HeuristicLab.JsonInterface.OptimizerIntegration.csproj

    r17829 r17834  
    155155    <Compile Include="Plugin.cs" />
    156156    <Compile Include="Properties\AssemblyInfo.cs" />
     157    <Compile Include="Views\ResultJsonItemControl.cs">
     158      <SubType>UserControl</SubType>
     159    </Compile>
     160    <Compile Include="Views\ResultJsonItemControl.Designer.cs">
     161      <DependentUpon>ResultJsonItemControl.cs</DependentUpon>
     162    </Compile>
    157163    <Compile Include="Views\ValueLookupJsonItemControl.cs">
    158164      <SubType>UserControl</SubType>
     
    229235    <EmbeddedResource Include="Views\ExportJsonDialog.resx">
    230236      <DependentUpon>ExportJsonDialog.cs</DependentUpon>
     237    </EmbeddedResource>
     238    <EmbeddedResource Include="Views\ResultJsonItemControl.resx">
     239      <DependentUpon>ResultJsonItemControl.cs</DependentUpon>
    231240    </EmbeddedResource>
    232241    <EmbeddedResource Include="Views\ValueLookupJsonItemControl.resx">
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface.OptimizerIntegration/ViewModels/ResultItemVM.cs

    r17473 r17834  
    99  public class ResultItemVM : JsonItemVMBase<ResultJsonItem> {
    1010    public override Type TargetedJsonItemType => typeof(ResultJsonItem);
    11     public override UserControl Control =>
    12       new JsonItemBaseControl(this);
     11    public override UserControl Control => ResultJsonItemControl.Create(this);
     12
     13    public string ResultFormatterType {
     14      get => Item.ResultFormatterType;
     15      set {
     16        Item.ResultFormatterType = value;
     17        OnPropertyChange(this, nameof(ResultFormatterType));
     18      }
     19    }
    1320  }
    1421}
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/AlgorithmConverter.cs

    r17828 r17834  
    3030      IAlgorithm algorithm = value as IAlgorithm;
    3131      foreach (var res in algorithm.Results) {
     32        item.AddChildren(root.Extract(res, root));
     33        /*
    3234        item.AddChildren(new ResultJsonItem() {
    3335          Name = res.Name,
    3436          Description = res.Description
    35         });
     37        });*/
    3638      }
    3739      item.AddChildren(root.Extract(algorithm.Problem, root));
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/BaseConverter.cs

    r17828 r17834  
    2020    public abstract IJsonItem Extract(IItem value, IJsonItemConverter root);
    2121
     22
    2223    #region Helper
    23 
    2424    protected IItem Instantiate(Type type, params object[] args) =>
    2525      (IItem)Activator.CreateInstance(type,args);
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/RegressionProblemDataConverter.cs

    r17828 r17834  
    6666
    6767      // check data
    68       if(!dataset.RowNames.Any(x => x == targetVariable.Value)) {
     68      if(!dataset.ColumnNames.Any(x => x == targetVariable.Value)) {
    6969        throw new Exception($"The value of the target variable ('{targetVariable.Value}') has no matching row name value of the dataset.");
    7070      }
    7171
    7272      foreach(var v in allowedInputVariables.Value) {
    73         if(!dataset.RowNames.Any(x => x == v))
     73        if(!dataset.ColumnNames.Any(x => x == v))
    7474          throw new Exception($"The value of the input variable ('{v}') has no matching row name value of the dataset.");
    7575      }
     
    106106        var dictTmp = new Dictionary<string, IList>();
    107107        int c = 0;
    108         foreach (var col in item.RowNames) {
     108        foreach (var col in item.ColumnNames) {
    109109          dictTmp.Add(col, new List<double>(item.Value[c]));
    110110          ++c;
     
    116116
    117117        var variableNames = dataset.GetType().GetField(VariableNames, flags);
    118         variableNames.SetValue(dataset, item.RowNames);
     118        variableNames.SetValue(dataset, item.ColumnNames);
    119119
    120120        var dataInfo = dataset.GetType().GetField(VariableValues, flags);
     
    162162
    163163        // add list items and set their check state (based on allowed input variables)
    164         foreach(var i in matrix.RowNames) {
     164        foreach(var i in matrix.ColumnNames) {
    165165          bool isChecked = false;
    166166          foreach(var x in item.Value)
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/ResultParameterConverter.cs

    r17828 r17834  
    11using System;
    2 using System.Collections.Generic;
    32using System.Linq;
    4 using System.Text;
    5 using System.Threading.Tasks;
    63using HeuristicLab.Core;
    74using HeuristicLab.Optimization;
     
    1411
    1512    public override bool CanConvertType(Type t) =>
    16       t.GetInterfaces().Any(x => x == typeof(IResultParameter));
     13      t.GetInterfaces().Any(x => x == ConvertableType);
    1714
    1815    public override IJsonItem Extract(IItem value, IJsonItemConverter root) {
    1916      IResultParameter res = value as IResultParameter;
     17      var formatter = ResultFormatter.ForType(res.DataType).Last();
    2018      return new ResultJsonItem() {
    2119        Name = res.ActualName,
    22         Description = res.Description
     20        Description = res.Description,
     21        ResultFormatterType = formatter.GetType().FullName,
     22        ValueType = res.DataType
    2323      };
    2424    }
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/HeuristicLab.JsonInterface.csproj

    r17829 r17834  
    6666    <Compile Include="Converters\ExperimentConverter.cs" />
    6767    <Compile Include="Converters\RegressionProblemDataConverter.cs" />
     68    <Compile Include="Converters\ResultConverter.cs" />
    6869    <Compile Include="Converters\ResultParameterConverter.cs" />
     70    <Compile Include="Converters\SymbolicRegressionSolutionConverter.cs" />
    6971    <Compile Include="Converters\ValueLookupParameterConverter.cs" />
    7072    <Compile Include="Converters\ValueRangeConverter.cs" />
     
    7779    <Compile Include="Interfaces\IMatrixJsonItem.cs" />
    7880    <Compile Include="Interfaces\IRangedJsonItem.cs" />
     81    <Compile Include="Interfaces\IResultFormatter.cs" />
    7982    <Compile Include="Interfaces\IResultJsonItem.cs" />
    8083    <Compile Include="Interfaces\IValueJsonItem.cs" />
     
    100103    <Compile Include="JsonItems\ValueJsonItem.cs" />
    101104    <Compile Include="JsonItems\ValueLookupJsonItem.cs" />
     105    <Compile Include="ResultFormatter\MatlabResultFormatter.cs" />
     106    <Compile Include="ResultFormatter\ResultFormatter.cs" />
     107    <Compile Include="ResultFormatter\StringResultFormatter.cs" />
    102108    <Compile Include="SingleLineArrayJsonWriter.cs" />
    103109    <Compile Include="JsonTemplateGenerator.cs" />
     
    142148      <Name>HeuristicLab.Data-3.3</Name>
    143149    </ProjectReference>
     150    <ProjectReference Include="..\HeuristicLab.Encodings.SymbolicExpressionTreeEncoding\3.4\HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4.csproj">
     151      <Project>{06d4a186-9319-48a0-bade-a2058d462eea}</Project>
     152      <Name>HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4</Name>
     153    </ProjectReference>
    144154    <ProjectReference Include="..\HeuristicLab.Optimization\3.3\HeuristicLab.Optimization-3.3.csproj">
    145155      <Project>{14ab8d24-25bc-400c-a846-4627aa945192}</Project>
     
    153163      <Project>{94186a6a-5176-4402-ae83-886557b53cca}</Project>
    154164      <Name>HeuristicLab.PluginInfrastructure-3.3</Name>
     165    </ProjectReference>
     166    <ProjectReference Include="..\HeuristicLab.Problems.DataAnalysis.Symbolic.Regression\3.4\HeuristicLab.Problems.DataAnalysis.Symbolic.Regression-3.4.csproj">
     167      <Project>{5ac82412-911b-4fa2-a013-edc5e3f3fcc2}</Project>
     168      <Name>HeuristicLab.Problems.DataAnalysis.Symbolic.Regression-3.4</Name>
     169    </ProjectReference>
     170    <ProjectReference Include="..\HeuristicLab.Problems.DataAnalysis.Symbolic\3.4\HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.csproj">
     171      <Project>{3d28463f-ec96-4d82-afee-38be91a0ca00}</Project>
     172      <Name>HeuristicLab.Problems.DataAnalysis.Symbolic-3.4</Name>
    155173    </ProjectReference>
    156174    <ProjectReference Include="..\HeuristicLab.Problems.DataAnalysis\3.4\HeuristicLab.Problems.DataAnalysis-3.4.csproj">
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Interfaces/IResultJsonItem.cs

    r17519 r17834  
    1 namespace HeuristicLab.JsonInterface {
     1using System;
     2using Newtonsoft.Json;
     3
     4namespace HeuristicLab.JsonInterface {
    25  /// <summary>
    3   /// Empty JsonItem, which indicates a result. For example 'BestQuality'.
     6  /// JsonItem, which indicates a result. For example 'BestQuality'.
    47  /// Types of this JsonItems are stored in the result section of the template.
    58  /// </summary>
    6   public interface IResultJsonItem : IJsonItem { }
     9  public interface IResultJsonItem : IJsonItem {
     10    /// <summary>
     11    /// the result formatter type's fullname
     12    /// </summary>
     13    string ResultFormatterType { get; set; }
     14
     15    /// <summary>
     16    /// the type of the result value
     17    /// </summary>
     18    [JsonIgnore]
     19    Type ValueType { get; set; }
     20  }
    721}
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/JsonItems/JsonItem.cs

    r17828 r17834  
    125125      });
    126126
    127     public virtual void SetJObject(JObject jObject) { }
     127    public virtual void SetJObject(JObject jObject) {
     128      Name = (jObject[nameof(IJsonItem.Name)]?.ToObject<string>());
     129      Description = (jObject[nameof(IJsonItem.Description)]?.ToObject<string>());
     130    }
    128131    #endregion
    129132
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/JsonItems/ResultJsonItem.cs

    r17828 r17834  
    1 namespace HeuristicLab.JsonInterface {
     1using System;
     2using System.Collections.Generic;
     3using System.Linq;
     4using Newtonsoft.Json.Linq;
     5
     6namespace HeuristicLab.JsonInterface {
    27  public class ResultJsonItem : JsonItem, IResultJsonItem {
     8    public string ResultFormatterType { get; set; }
     9
     10    public Type ValueType { get; set; }
     11
    312    protected override ValidationResult Validate() => ValidationResult.Successful();
    413
    5     public string Converter { get; set; }
     14    public override void SetJObject(JObject jObject) {
     15      base.SetJObject(jObject);
     16      ResultFormatterType = (jObject[nameof(IResultJsonItem.ResultFormatterType)]?.ToObject<string>());
     17    }
     18
    619  }
    720}
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/JsonTemplateInstantiator.cs

    r17828 r17834  
    8181      IList<IResultJsonItem> res = new List<IResultJsonItem>();
    8282      foreach(JObject obj in Template[Constants.Results]) {
    83         string name = obj.Property("Name").Value.ToString();
    84         res.Add(new ResultJsonItem() { Name = name });
     83        //string name = obj.Property("Name").Value.ToString();
     84        var resultItem = new ResultJsonItem();
     85        resultItem.SetJObject(obj);
     86        res.Add(resultItem);
     87        //res.Add(new ResultJsonItem() { Name = name });
    8588      }
    8689      return res;
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Plugin.cs.frame

    r17330 r17834  
    3434  [PluginDependency("HeuristicLab.Parameters", "3.3")]
    3535  [PluginDependency("HeuristicLab.SequentialEngine", "3.3")]
     36  [PluginDependency("HeuristicLab.Encodings.SymbolicExpressionTreeEncoding", "3.4")]
     37  [PluginDependency("HeuristicLab.Problems.DataAnalysis", "3.4")]
     38  [PluginDependency("HeuristicLab.Problems.DataAnalysis.Symbolic", "3.4")]
     39  [PluginDependency("HeuristicLab.Problems.DataAnalysis.Symbolic.Regression", "3.4")]
     40  [PluginDependency("HeuristicLab.Problems.Instances.DataAnalysis", "3.3")]
    3641  public class HeuristicLabJsonInterfacePlugin : PluginBase {
    3742  }
Note: See TracChangeset for help on using the changeset viewer.