Free cookie consent management tool by TermsFeed Policy Generator

Changeset 16589


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
Location:
branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4
Files:
2 edited
1 copied

Legend:

Unmodified
Added
Removed
  • branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression-3.4.csproj

    r16556 r16589  
    183183    <Compile Include="SingleObjective\ConstantOptimizationAnalyzer.cs" />
    184184    <Compile Include="SingleObjective\Evaluators\SymbolicRegressionMeanRelativeErrorEvaluator.cs" />
     185    <Compile Include="SingleObjective\Evaluators\SymbolicRegressionSingleObjectiveConstraintPearsonRSquaredEvaluator.cs" />
    185186    <Compile Include="SingleObjective\SymbolicRegressionSolutionsAnalyzer.cs" />
    186187    <Compile Include="SymbolicRegressionPhenotypicDiversityAnalyzer.cs" />
  • 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}
  • branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SymbolicRegressionSolution.cs

    r16556 r16589  
    4949    private const string TestNaNEvaluationsResultName = "Test NaN Evaluations";
    5050
    51     private const string EstimatedDerivatesResultName = "Derivates of the Model";
     51    private const string IntervalEvaluationResultName = "Interval Evaluation";
    5252    private const string EstimatedDerivationInterval = "Interval";
    5353
     
    101101    }
    102102
    103     private ResultCollection EstimatedDerivateResultCollection =>
    104       (ResultCollection) this[EstimatedDerivatesResultName].Value;
    105 
    106     public NamedIntervals EstimationInterval =>
    107       (NamedIntervals) EstimatedDerivateResultCollection[EstimatedDerivationInterval].Value;
     103    private NamedIntervals IntervalEvaluaitonCollection =>
     104      (NamedIntervals) this[IntervalEvaluationResultName].Value;
     105
    108106
    109107
     
    132130      RecalculateResults();
    133131
    134       var estimationDerivatesResult = new ResultCollection();
    135       Add(new Result(EstimatedDerivatesResultName, "Results concerning the derivation of symbolic regression solution", estimationDerivatesResult));
    136       CalculateDerivates(estimationDerivatesResult);
     132      var namedIntervalCollection = new NamedIntervals();
     133      Add(new Result(IntervalEvaluationResultName, "Results concerning the derivation of symbolic regression solution", namedIntervalCollection));
     134      GetIntervalEvaulations(namedIntervalCollection);
    137135    }
    138136
     
    155153        CalculateResults();
    156154
    157         var estimationDerivatesResult = new ResultCollection();
    158         Add(new Result(EstimatedDerivatesResultName, "Results concerning the derivation of symbolic regression solution", estimationDerivatesResult));
    159         CalculateDerivates(estimationDerivatesResult);
     155        var namedIntervalCollection = new NamedIntervals();
     156        Add(new Result(IntervalEvaluationResultName, "Results concerning the derivation of symbolic regression solution", namedIntervalCollection));
     157        GetIntervalEvaulations(namedIntervalCollection);
    160158      }
    161159    }
     
    166164    }
    167165
    168     private void CalculateDerivates(ResultCollection estimationDerivatesResults) {
     166    private void GetIntervalEvaulations(NamedIntervals intervalEvaluation) {
    169167      var interpreter = new IntervalInterpreter();
    170       var variableRanges = (ProblemData as RegressionProblemData).VariableRangesParameter.Value.VariableIntervals;
    171       var customIntervals = new Dictionary<string, Interval>();
    172       var intervals = new NamedIntervals();
    173 
    174       foreach (var variable in variableRanges) {
    175         customIntervals.Add(variable.Key, new Interval(variable.Value.LowerBound, variable.Value.UpperBound));
    176       }
    177 
    178       foreach (var derivate in customIntervals) {
    179         if (derivate.Key != ProblemData.TargetVariable) {
    180           var derived = DerivativeCalculator.Derive(Model.SymbolicExpressionTree, derivate.Key);
    181           var derivedResultInterval = interpreter.GetSymbolicExressionTreeInterval(derived, customIntervals);
    182           intervals.Add(" ∂f/∂" + derivate.Key,
    183             new Interval(derivedResultInterval.LowerBound, derivedResultInterval.UpperBound));
     168      var variableRanges = (ProblemData as RegressionProblemData)?.VariableRangesParameter.Value.VariableIntervals;
     169
     170      if (variableRanges != null) {
     171        intervalEvaluation.Add($"Target {ProblemData.TargetVariable}", new Interval(variableRanges[ProblemData.TargetVariable].LowerBound, variableRanges[ProblemData.TargetVariable].UpperBound));
     172        intervalEvaluation.Add("Modell Interval", interpreter.GetSymbolicExressionTreeInterval(Model.SymbolicExpressionTree, variableRanges));
     173
     174        foreach (var derivate in variableRanges) {
     175          if (derivate.Key != ProblemData.TargetVariable) {
     176            var derived = DerivativeCalculator.Derive(Model.SymbolicExpressionTree, derivate.Key);
     177            var derivedResultInterval = interpreter.GetSymbolicExressionTreeInterval(derived, variableRanges);
     178            intervalEvaluation.Add(" ∂f/∂" + derivate.Key,
     179              new Interval(derivedResultInterval.LowerBound, derivedResultInterval.UpperBound));
     180          }
    184181        }
    185182      }
    186       estimationDerivatesResults.AddOrUpdateResult("Derived Intervals", intervals);
    187 
    188183    }
    189184   
Note: See TracChangeset for help on using the changeset viewer.