Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/05/19 14:50:21 (6 years ago)
Author:
chaider
Message:

#2971

  • Added new Evaluator to evaulate Pearson RSquared with repsect to given constraints
  • changes in SymbolicRegressionSolution
File:
1 copied

Legend:

Unmodified
Added
Removed
  • branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionSingleObjectiveConstraintPearsonRSquaredEvaluator.cs

    r16582 r16589  
    2020#endregion
    2121
     22using System;
    2223using System.Collections.Generic;
    2324using HeuristicLab.Common;
     
    2829
    2930namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
    30   [Item("Pearson R² Evaluator", "Calculates the square of the pearson correlation coefficient (also known as coefficient of determination) of a symbolic regression solution.")]
     31  [Item("Pearson R² Constraint Evaluator", "Calculates the square of the pearson correlation coefficient (also known as coefficient of determination) of a symbolic regression solution.")]
    3132  [StorableClass]
    32   public class SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator : SymbolicRegressionSingleObjectiveEvaluator {
     33  public class SymbolicRegressionSingleObjectiveConstraintPearsonRSquaredEvaluator : SymbolicRegressionSingleObjectiveEvaluator {
    3334    [StorableConstructor]
    34     protected SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator(bool deserializing) : base(deserializing) { }
    35     protected SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator(SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator original, Cloner cloner)
     35    protected SymbolicRegressionSingleObjectiveConstraintPearsonRSquaredEvaluator(bool deserializing) : base(deserializing) { }
     36    protected SymbolicRegressionSingleObjectiveConstraintPearsonRSquaredEvaluator(SymbolicRegressionSingleObjectiveConstraintPearsonRSquaredEvaluator original, Cloner cloner)
    3637      : base(original, cloner) {
    3738    }
    3839    public override IDeepCloneable Clone(Cloner cloner) {
    39       return new SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator(this, cloner);
     40      return new SymbolicRegressionSingleObjectiveConstraintPearsonRSquaredEvaluator(this, cloner);
    4041    }
    4142
    42     public SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator() : base() { }
     43    public SymbolicRegressionSingleObjectiveConstraintPearsonRSquaredEvaluator() : base() { }
    4344
    4445    public override bool Maximization { get { return true; } }
     
    5859      IEnumerable<double> targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
    5960      OnlineCalculatorError errorState;
     61
     62      IntervalConstraintsParser parser = new IntervalConstraintsParser();
     63      var constraints = parser.Parse(((RegressionProblemData) problemData).IntervalConstraintsParameter.Value.Value);
     64      var intervalInterpreter = new IntervalInterpreter();
     65      var variableRanges = ((RegressionProblemData) problemData).VariableRangesParameter.Value.VariableIntervals;
     66
     67
     68      foreach (var constraint in constraints) {
     69        if (!variableRanges.ContainsKey(constraint.Variable))
     70          throw new ArgumentException($"The given variable {constraint.Variable} in the constraint does not exists in the model.", nameof(IntervalConstraintsParser));
     71        if (!constraint.IsDerivation) {
     72          var res = intervalInterpreter.GetSymbolicExressionTreeInterval(solution, variableRanges);
     73          if (!IntervalInBoundaries(constraint.Interval, res, constraint.InclusiveLowerBound,
     74            constraint.InclusiveUpperBound)) {
     75            return 0;
     76          }
     77        } else {
     78          var tree = solution;
     79          for (var i = 0; i < constraint.NumberOfDerivation; ++i) {
     80            tree = DerivativeCalculator.Derive(tree, constraint.Variable);
     81          }
     82          var res = intervalInterpreter.GetSymbolicExressionTreeInterval(tree, variableRanges);
     83          if (!IntervalInBoundaries(constraint.Interval, res, constraint.InclusiveLowerBound,
     84            constraint.InclusiveUpperBound)) {
     85            return 0;
     86          }
     87        }
     88      }
     89
    6090
    6191      double r;
     
    86116      return r2;
    87117    }
     118
     119    private static bool IntervalInBoundaries(Interval i1, Interval i2, bool inclusiveLower, bool inclusiveUpper) {
     120      if (inclusiveLower) {
     121        if (i2.LowerBound < i1.LowerBound)
     122          return false;
     123      } else {
     124        if (i2.LowerBound <= i1.LowerBound)
     125          return false;
     126      }
     127
     128      if (inclusiveUpper) {
     129        if (i2.UpperBound > i1.UpperBound)
     130          return false;
     131      } else {
     132        if (i2.UpperBound >= i1.UpperBound)
     133          return false;
     134      }
     135
     136      return true;
     137    }
    88138  }
    89139}
Note: See TracChangeset for help on using the changeset viewer.