Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/19/20 19:07:40 (4 years ago)
Author:
fbaching
Message:

#1837: merged changes from trunk

  • apply changes from Attic release to all SlidingWindow specific code files (replace StorableClass with StorableType)
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/1837_Sliding Window GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SymbolicRegressionSolution.cs

    r9456 r17687  
    1 #region License Information
     1#region License Information
    22/* HeuristicLab
    3  * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
    44 *
    55 * This file is part of HeuristicLab.
     
    2121
    2222using System.Linq;
     23using HEAL.Attic;
    2324using HeuristicLab.Common;
    2425using HeuristicLab.Core;
    2526using HeuristicLab.Data;
     27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2628using HeuristicLab.Optimization;
    27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2829
    2930namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
     
    3132  /// Represents a symbolic regression solution (model + data) and attributes of the solution like accuracy and complexity
    3233  /// </summary>
    33   [StorableClass]
     34  [StorableType("88E56AF9-AD72-47E4-A613-8875703BD927")]
    3435  [Item(Name = "SymbolicRegressionSolution", Description = "Represents a symbolic regression solution (model + data) and attributes of the solution like accuracy and complexity.")]
    3536  public sealed class SymbolicRegressionSolution : RegressionSolution, ISymbolicRegressionSolution {
     
    4647    private const string TestNaNEvaluationsResultName = "Test NaN Evaluations";
    4748
     49    private const string ModelBoundsResultName = "Model Bounds";
     50
    4851    public new ISymbolicRegressionModel Model {
    4952      get { return (ISymbolicRegressionModel)base.Model; }
     
    9598    }
    9699
     100    public IntervalCollection ModelBoundsCollection {
     101      get {
     102        if (!ContainsKey(ModelBoundsResultName)) return null;
     103        return (IntervalCollection)this[ModelBoundsResultName].Value;
     104      }
     105      private set {
     106        if (ContainsKey(ModelBoundsResultName)) {
     107          this[ModelBoundsResultName].Value = value;
     108        } else {
     109          Add(new Result(ModelBoundsResultName, "Results concerning the derivation of symbolic regression solution", value));
     110        }
     111
     112      }
     113    }
     114
     115
     116
    97117    [StorableConstructor]
    98     private SymbolicRegressionSolution(bool deserializing) : base(deserializing) { }
     118    private SymbolicRegressionSolution(StorableConstructorFlag _) : base(_) { }
    99119    private SymbolicRegressionSolution(SymbolicRegressionSolution original, Cloner cloner)
    100120      : base(original, cloner) {
     
    102122    public SymbolicRegressionSolution(ISymbolicRegressionModel model, IRegressionProblemData problemData)
    103123      : base(model, problemData) {
     124      foreach (var node in model.SymbolicExpressionTree.Root.IterateNodesPrefix().OfType<SymbolicExpressionTreeTopLevelNode>())
     125        node.SetGrammar(null);
     126
    104127      Add(new Result(ModelLengthResultName, "Length of the symbolic regression model.", new IntValue()));
    105128      Add(new Result(ModelDepthResultName, "Depth of the symbolic regression model.", new IntValue()));
     
    114137      estimationLimitResults.Add(new Result(TestNaNEvaluationsResultName, "", new IntValue()));
    115138      Add(new Result(EstimationLimitsResultsResultName, "Results concerning the estimation limits of symbolic regression solution", estimationLimitResults));
     139
     140      if (IntervalInterpreter.IsCompatible(Model.SymbolicExpressionTree))
     141        Add(new Result(ModelBoundsResultName, "Results concerning the derivation of symbolic regression solution", new IntervalCollection()));
    116142
    117143      RecalculateResults();
     
    136162        CalculateResults();
    137163      }
     164
     165      if (!ContainsKey(ModelBoundsResultName)) {
     166        if (IntervalInterpreter.IsCompatible(Model.SymbolicExpressionTree)) {
     167          Add(new Result(ModelBoundsResultName, "Results concerning the derivation of symbolic regression solution", new IntervalCollection()));
     168          CalculateResults();
     169        }
     170      }
    138171    }
    139172
     
    156189      TrainingNaNEvaluations = Model.Interpreter.GetSymbolicExpressionTreeValues(Model.SymbolicExpressionTree, ProblemData.Dataset, ProblemData.TrainingIndices).Count(double.IsNaN);
    157190      TestNaNEvaluations = Model.Interpreter.GetSymbolicExpressionTreeValues(Model.SymbolicExpressionTree, ProblemData.Dataset, ProblemData.TestIndices).Count(double.IsNaN);
     191
     192      //Check if the tree contains unknown symbols for the interval calculation
     193      if (IntervalInterpreter.IsCompatible(Model.SymbolicExpressionTree))
     194        ModelBoundsCollection = CalculateModelIntervals(this);
     195    }
     196
     197    private static IntervalCollection CalculateModelIntervals(ISymbolicRegressionSolution solution) {
     198      var intervalEvaluation = new IntervalCollection();
     199      var interpreter = new IntervalInterpreter();
     200      var problemData = solution.ProblemData;
     201      var model = solution.Model;
     202      var variableRanges = problemData.VariableRanges.GetReadonlyDictionary();
     203
     204      intervalEvaluation.AddInterval($"Target {problemData.TargetVariable}", new Interval(variableRanges[problemData.TargetVariable].LowerBound, variableRanges[problemData.TargetVariable].UpperBound));
     205      intervalEvaluation.AddInterval("Model", interpreter.GetSymbolicExpressionTreeInterval(model.SymbolicExpressionTree, variableRanges));
     206
     207      if (DerivativeCalculator.IsCompatible(model.SymbolicExpressionTree)) {
     208        foreach (var inputVariable in model.VariablesUsedForPrediction.OrderBy(v => v, new NaturalStringComparer())) {
     209          var derivedModel = DerivativeCalculator.Derive(model.SymbolicExpressionTree, inputVariable);
     210          var derivedResultInterval = interpreter.GetSymbolicExpressionTreeInterval(derivedModel, variableRanges);
     211
     212          intervalEvaluation.AddInterval(" ∂f/∂" + inputVariable, new Interval(derivedResultInterval.LowerBound, derivedResultInterval.UpperBound));
     213        }
     214      }
     215
     216      return intervalEvaluation;
    158217    }
    159218  }
Note: See TracChangeset for help on using the changeset viewer.