Changeset 16596 for branches/2971_named_intervals
- Timestamp:
- 02/08/19 14:11:07 (6 years ago)
- 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 57 57 IEnumerable<double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, rows); 58 58 IEnumerable<double> targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows); 59 OnlineCalculatorError errorState ;59 OnlineCalculatorError errorState = OnlineCalculatorError.None; 60 60 61 61 IntervalConstraintsParser parser = new IntervalConstraintsParser(); … … 65 65 66 66 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 //} 88 88 // TODO 89 89 // m = new SymbolicRegressionModel(...) … … 97 97 // constraints mit scaledTree berechnen (auch die Ableitungen) 98 98 99 100 101 99 double r; 102 100 if (applyLinearScaling) { 103 101 var rCalculator = new OnlinePearsonsRCalculator(); 104 102 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; 105 107 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 } 107 113 } else { 108 114 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 } 110 120 } 111 121 if (errorState != OnlineCalculatorError.None) return double.NaN; 112 122 return r*r; 113 123 } 124 114 125 115 126 public override double Evaluate(IExecutionContext context, ISymbolicExpressionTree tree, … … 129 140 return r2; 130 141 } 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 } 131 171 } 132 172 } -
branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Parser/IntervalConstraint.cs
r16592 r16596 15 15 public string Variable { get; set; } 16 16 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 } 17 30 } 18 31 }
Note: See TracChangeset
for help on using the changeset viewer.