Free cookie consent management tool by TermsFeed Policy Generator

Changeset 16596


Ignore:
Timestamp:
02/08/19 14:11:07 (5 years ago)
Author:
chaider
Message:

#2971

  • Extended evaluator with linear scaling
  • Added constructor to IntervalConstraint
Location:
branches/2971_named_intervals
Files:
2 edited

Legend:

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

    r16592 r16596  
    5757      IEnumerable<double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, rows);
    5858      IEnumerable<double> targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
    59       OnlineCalculatorError errorState;
     59      OnlineCalculatorError errorState = OnlineCalculatorError.None;
    6060
    6161      IntervalConstraintsParser parser = new IntervalConstraintsParser();
     
    6565
    6666
    67       foreach (var constraint in constraints) {
    68         if (constraint.Variable != null && !variableRanges.ContainsKey(constraint.Variable))
    69           throw new ArgumentException($"The given variable {constraint.Variable} in the constraint does not exists in the model.", nameof(IntervalConstraintsParser));
    70         if (!constraint.IsDerivation) {
    71           var res = intervalInterpreter.GetSymbolicExressionTreeInterval(solution, variableRanges);
    72           if (!constraint.Interval.Contains(res, constraint.InclusiveLowerBound,
    73             constraint.InclusiveUpperBound)) {
    74             return 0;
    75           }
    76         } else {
    77           var tree = solution;
    78           for (var i = 0; i < constraint.NumberOfDerivation; ++i) {
    79             tree = DerivativeCalculator.Derive(tree, constraint.Variable);
    80           }
    81           var res = intervalInterpreter.GetSymbolicExressionTreeInterval(tree, variableRanges);
    82           if (!constraint.Interval.Contains(res, constraint.InclusiveLowerBound,
    83             constraint.InclusiveUpperBound)) {
    84             return 0;
    85           }
    86         }
    87       }
     67      //foreach (var constraint in constraints) {
     68      //  if (constraint.Variable != null && !variableRanges.ContainsKey(constraint.Variable))
     69      //    throw new ArgumentException($"The given variable {constraint.Variable} in the constraint does not exists in the model.", nameof(IntervalConstraintsParser));
     70      //  if (!constraint.IsDerivation) {
     71      //    var res = intervalInterpreter.GetSymbolicExressionTreeInterval(solution, variableRanges);
     72      //    if (!constraint.Interval.Contains(res, constraint.InclusiveLowerBound,
     73      //      constraint.InclusiveUpperBound)) {
     74      //      return 0;
     75      //    }
     76      //  } else {
     77      //    var tree = solution;
     78      //    for (var i = 0; i < constraint.NumberOfDerivation; ++i) {
     79      //      tree = DerivativeCalculator.Derive(tree, constraint.Variable);
     80      //    }
     81      //    var res = intervalInterpreter.GetSymbolicExressionTreeInterval(tree, variableRanges);
     82      //    if (!constraint.Interval.Contains(res, constraint.InclusiveLowerBound,
     83      //      constraint.InclusiveUpperBound)) {
     84      //      return 0;
     85      //    }
     86      //  }
     87      //}
    8888      // TODO
    8989      // m = new SymbolicRegressionModel(...)
     
    9797      // constraints mit scaledTree berechnen (auch die Ableitungen)
    9898
    99 
    100 
    10199      double r;
    102100      if (applyLinearScaling) {
    103101        var rCalculator = new OnlinePearsonsRCalculator();
    104102        CalculateWithScaling(targetValues, estimatedValues, lowerEstimationLimit, upperEstimationLimit, rCalculator, problemData.Dataset.Rows);
     103        var model = new SymbolicRegressionModel(problemData.TargetVariable, solution, interpreter, lowerEstimationLimit, upperEstimationLimit);
     104        model.Scale(problemData);
     105        var e = model.GetEstimatedValues(problemData.Dataset, problemData.TrainingIndices);
     106        var scaledTree = model.SymbolicExpressionTree;
    105107        errorState = rCalculator.ErrorState;
    106         r = rCalculator.R;
     108        if (CheckConstraintsViolations(constraints, intervalInterpreter, variableRanges, scaledTree, out var val)) {
     109          r = val;
     110        } else {
     111          r = rCalculator.R;
     112        }
    107113      } else {
    108114        IEnumerable<double> boundedEstimatedValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit);
    109         r = OnlinePearsonsRCalculator.Calculate(targetValues, boundedEstimatedValues, out errorState);
     115        if (CheckConstraintsViolations(constraints, intervalInterpreter, variableRanges, solution, out var val)) {
     116          r = val;
     117        } else {
     118          r = OnlinePearsonsRCalculator.Calculate(targetValues, boundedEstimatedValues, out errorState);
     119        }
    110120      }
    111121      if (errorState != OnlineCalculatorError.None) return double.NaN;
    112122      return r*r;
    113123    }
     124
    114125
    115126    public override double Evaluate(IExecutionContext context, ISymbolicExpressionTree tree,
     
    129140      return r2;
    130141    }
     142
     143    private static bool CheckConstraintsViolations(List<IntervalConstraint> constraints, IntervalInterpreter intervalInterpreter,
     144      Dictionary<string, Interval> variableRanges, ISymbolicExpressionTree solution, out double r) {
     145      foreach (var constraint in constraints) {
     146        if (constraint.Variable != null && !variableRanges.ContainsKey(constraint.Variable))
     147          throw new ArgumentException($"The given variable {constraint.Variable} in the constraint does not exists in the model.", nameof(IntervalConstraintsParser));
     148        if (!constraint.IsDerivation) {
     149          var res = intervalInterpreter.GetSymbolicExressionTreeInterval(solution, variableRanges);
     150          if (!constraint.Interval.Contains(res, constraint.InclusiveLowerBound,
     151            constraint.InclusiveUpperBound)) {
     152            r = 0;
     153            return true;
     154          }
     155        } else {
     156          var tree = solution;
     157          for (var i = 0; i < constraint.NumberOfDerivation; ++i) {
     158            tree = DerivativeCalculator.Derive(tree, constraint.Variable);
     159          }
     160          var res = intervalInterpreter.GetSymbolicExressionTreeInterval(tree, variableRanges);
     161          if (!constraint.Interval.Contains(res, constraint.InclusiveLowerBound,
     162            constraint.InclusiveUpperBound)) {
     163            r = 0;
     164            return true;
     165          }
     166        }
     167      }
     168      r = 1;
     169      return false;
     170    }
    131171  }
    132172}
  • branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Parser/IntervalConstraint.cs

    r16592 r16596  
    1515    public string Variable { get; set; }
    1616    public int NumberOfDerivation { get; set; }
     17
     18    public IntervalConstraint() { }
     19
     20    public IntervalConstraint(string expression, string definition, Interval interval, bool inclusiveLowerBound, bool inclusiveUpperBound, bool isDerivation, string variable, int numberOfDerivation) {
     21      Expression = expression;
     22      Definition = definition;
     23      Interval = interval;
     24      InclusiveLowerBound = inclusiveLowerBound;
     25      InclusiveUpperBound = inclusiveUpperBound;
     26      IsDerivation = isDerivation;
     27      Variable = variable;
     28      NumberOfDerivation = numberOfDerivation;
     29    }
    1730  }
    1831}
Note: See TracChangeset for help on using the changeset viewer.