Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/ResultCollectionProcessors/AbstractProcessors/RunCollectionSRSolutionFormatter.cs @ 18183

Last change on this file since 18183 was 18055, checked in by dpiringe, 3 years ago

#3026

  • added StorableTypeAttribute and StorableConstructorAttribute to all JsonItems
  • added a new JsonItem ListJsonItem + Interfaces IListJsonItem
  • renamed SymRegPythonProcessor to RunCollectionSRSolutionPythonFormatter
  • removed Interface IResultCollectionProcessor -> using the interface IRunCollectionModifier now (has aleady implementations)
  • renamed all related variables/fields/properties with a connection to ResultCollectionProcessor
  • added new implementations for IRunCollectionModifier
File size: 2.6 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using HeuristicLab.Collections;
7using HeuristicLab.Common;
8using HeuristicLab.Core;
9using HeuristicLab.Optimization;
10using HeuristicLab.Problems.DataAnalysis.Symbolic;
11using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
12using HEAL.Attic;
13using HeuristicLab.Data;
14using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
15using HeuristicLab.Parameters;
16
17namespace HeuristicLab.JsonInterface {
18  [StorableType("806BB409-E50C-4827-ABEF-0376D65F7414")]
19  public abstract class RunCollectionSRSolutionFormatter : ParameterizedNamedItem, IRunCollectionModifier {
20
21    #region Constants
22    private const string SuffixParameterName = "Suffix";
23    #endregion
24
25    #region Parameter Properties
26    public IFixedValueParameter<StringValue> SuffixParameter =>
27      (IFixedValueParameter<StringValue>)Parameters[SuffixParameterName];
28    #endregion
29
30    #region Properties
31    public string Suffix {
32      get => SuffixParameter.Value.Value;
33      set => SuffixParameter.Value.Value = value;
34    }
35    protected abstract ISymbolicExpressionTreeStringFormatter Formatter { get; }
36    #endregion
37
38    #region Constructors & Cloning
39    [StorableConstructor]
40    protected RunCollectionSRSolutionFormatter(StorableConstructorFlag _) : base(_) { }
41    public RunCollectionSRSolutionFormatter() {
42      Parameters.Add(new FixedValueParameter<StringValue>(SuffixParameterName, "Appends a suffix to the original name.", new StringValue()));
43    }
44    public RunCollectionSRSolutionFormatter(RunCollectionSRSolutionFormatter original, Cloner cloner) : base(original, cloner) { }
45    #endregion
46
47    public void Apply(IObservableDictionary<string, IItem> results) {
48      var resultCopy = new ObservableDictionary<string, IItem>(results);
49      foreach (var kvp in resultCopy) {
50        if (kvp.Value is ISymbolicRegressionSolution sol) {
51          results.Add(
52            $"{kvp.Key} - {SuffixParameter.Value.Value}",
53            new StringValue(Formatter.Format(sol.Model.SymbolicExpressionTree)));
54        }
55      }
56    }
57
58    public void Modify(List<IRun> runs) {
59      foreach(var run in runs) {
60        var resultCopy = new ObservableDictionary<string, IItem>(run.Results);
61        foreach (var kvp in resultCopy) {
62          if (kvp.Value is ISymbolicRegressionSolution sol) {
63            run.Results.Add(
64              $"{kvp.Key} - {SuffixParameter.Value.Value}",
65              new StringValue(Formatter.Format(sol.Model.SymbolicExpressionTree)));
66          }
67        }
68      }
69    }
70  }
71}
Note: See TracBrowser for help on using the repository browser.