Changeset 16592 for branches/2971_named_intervals
- Timestamp:
- 02/06/19 16:16:50 (6 years ago)
- Location:
- branches/2971_named_intervals
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionSingleObjectiveConstraintPearsonRSquaredEvaluator.cs
r16591 r16592 70 70 if (!constraint.IsDerivation) { 71 71 var res = intervalInterpreter.GetSymbolicExressionTreeInterval(solution, variableRanges); 72 if (! IntervalInBoundaries(constraint.Interval,res, constraint.InclusiveLowerBound,72 if (!constraint.Interval.Contains(res, constraint.InclusiveLowerBound, 73 73 constraint.InclusiveUpperBound)) { 74 74 return 0; … … 80 80 } 81 81 var res = intervalInterpreter.GetSymbolicExressionTreeInterval(tree, variableRanges); 82 if (! IntervalInBoundaries(constraint.Interval,res, constraint.InclusiveLowerBound,82 if (!constraint.Interval.Contains(res, constraint.InclusiveLowerBound, 83 83 constraint.InclusiveUpperBound)) { 84 84 return 0; … … 86 86 } 87 87 } 88 // TODO 89 // m = new SymbolicRegressionModel(...) 90 // m.Scale(); 91 92 // var e = m.GetEstimatedValues (TRAINING) 93 // OnlinePearsonCalc.Calculate(e, TARGET_TRAIING) 94 95 // scaledTree = model.Tree; 96 97 // constraints mit scaledTree berechnen (auch die Ableitungen) 98 88 99 89 100 … … 102 113 } 103 114 104 public override double Evaluate(IExecutionContext context, ISymbolicExpressionTree tree, IRegressionProblemData problemData, IEnumerable<int> rows) { 115 public override double Evaluate(IExecutionContext context, ISymbolicExpressionTree tree, 116 IRegressionProblemData problemData, IEnumerable<int> rows) { 105 117 SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context; 106 118 EstimationLimitsParameter.ExecutionContext = context; 107 119 ApplyLinearScalingParameter.ExecutionContext = context; 108 120 109 double r2 = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows, ApplyLinearScalingParameter.ActualValue.Value); 121 double r2 = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, 122 EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows, 123 ApplyLinearScalingParameter.ActualValue.Value); 110 124 111 125 SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null; … … 115 129 return r2; 116 130 } 117 118 private static bool IntervalInBoundaries(Interval i1, Interval i2, bool inclusiveLower, bool inclusiveUpper) {119 if (double.IsNegativeInfinity(i1.LowerBound) && double.IsPositiveInfinity(i1.UpperBound))120 return true;121 //Left-unbounded and right-bounded:122 if (double.IsNegativeInfinity(i1.LowerBound)) {123 if (inclusiveUpper)124 return i2.LowerBound <= i1.UpperBound && i2.UpperBound <= i1.UpperBound;125 return i2.LowerBound < i1.UpperBound && i2.UpperBound < i1.UpperBound;126 }127 128 //Left-bounded and right-unbounded:129 if (double.IsPositiveInfinity(i1.UpperBound)) {130 if (inclusiveLower)131 return i2.LowerBound >= i1.LowerBound && i2.UpperBound >= i1.LowerBound;132 return i2.LowerBound > i1.LowerBound && i2.UpperBound > i1.LowerBound;133 }134 135 //Proper and bounded:136 //Closed:137 if (inclusiveLower && inclusiveUpper) {138 return i1.LowerBound <= i2.LowerBound && i2.UpperBound <= i1.UpperBound;139 }140 //Open:141 if (!inclusiveLower && !inclusiveUpper) {142 return i1.LowerBound < i2.LowerBound && i2.UpperBound < i1.UpperBound;143 }144 //Left-closed, right-open:145 if (inclusiveLower) {146 return i1.LowerBound <= i2.LowerBound && i2.UpperBound < i1.UpperBound;147 }148 //Left-open, right-closed:149 return i1.LowerBound < i2.LowerBound && i2.UpperBound <= i1.UpperBound;150 }151 131 } 152 132 } -
branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/SymbolicRegressionConstraintAnalyzer.cs
r16590 r16592 23 23 24 24 using System; 25 using System.Collections.Generic; 25 26 using System.Linq; 26 27 using HeuristicLab.Analysis; … … 36 37 namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression { 37 38 [StorableClass] 38 public class SymbolicRegressionConstraintAnalyzer : SingleSuccessorOperator, IAnalyzer { 39 private const string ResultCollectionParameterName = "Results"; 40 private const string RegressionSolutionQualitiesResultName = "Constraint Violations"; 39 public class SymbolicRegressionConstraintAnalyzer : SymbolicDataAnalysisAnalyzer, ISymbolicDataAnalysisInterpreterOperator, ISymbolicExpressionTreeAnalyzer { 40 private const string ConstraintViolationsResultName = "Constraint Violations"; 41 41 42 private const string ProblemDataParameterName = "ProblemData"; 43 private const string SymbolicDataAnalysisTreeInterpreterParameterName = "SymbolicDataAnalysisTreeInterpreter"; 42 44 43 public ILookupParameter<ResultCollection> ResultCollectionParameter { 44 get { return (ILookupParameter<ResultCollection>)Parameters[ResultCollectionParameterName]; } 45 #region parameter properties 46 public ILookupParameter<RegressionProblemData> RegressionProblemDataParameter { 47 get { return (ILookupParameter<RegressionProblemData>)Parameters[ProblemDataParameterName]; } 48 } 49 public ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> SymbolicDataAnalysisTreeInterpreterParameter { 50 get { return (ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[SymbolicDataAnalysisTreeInterpreterParameterName]; } 51 } 52 #endregion 53 #region properties 54 55 public RegressionProblemData RegressionProblemData { 56 get { return RegressionProblemDataParameter.ActualValue; } 45 57 } 46 58 59 public ISymbolicDataAnalysisExpressionTreeInterpreter Interpreter { 60 get { return SymbolicDataAnalysisTreeInterpreterParameter.ActualValue; } 61 } 62 #endregion 47 63 48 64 public virtual bool EnabledByDefault { … … 63 79 64 80 public SymbolicRegressionConstraintAnalyzer() { 65 Parameters.Add(new LookupParameter<ResultCollection>(ResultCollectionParameterName, 66 "The result collection to store the analysis results.")); 67 81 Parameters.Add(new LookupParameter<RegressionProblemData>(ProblemDataParameterName, "The problem data of the symbolic data analysis problem.")); 82 Parameters.Add(new LookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicDataAnalysisTreeInterpreterParameterName, "The interpreter for symbolic data analysis expression trees.")); 68 83 } 69 84 70 85 [StorableHook(HookType.AfterDeserialization)] 71 86 private void AfterDeserialization() { 72 // BackwardsCompatibility3.373 87 } 74 88 … … 77 91 IntervalConstraintsParser parser = new IntervalConstraintsParser(); 78 92 var intervalInterpreter = new IntervalInterpreter(); 79 if (!results.ContainsKey( RegressionSolutionQualitiesResultName)) {80 var newDataTable = new DataTable( RegressionSolutionQualitiesResultName);81 results.Add(new Result( RegressionSolutionQualitiesResultName, "Chart displaying the constraint violatoins.",93 if (!results.ContainsKey(ConstraintViolationsResultName)) { 94 var newDataTable = new DataTable(ConstraintViolationsResultName); 95 results.Add(new Result(ConstraintViolationsResultName, "Chart displaying the constraint violations.", 82 96 newDataTable)); 83 97 } 84 98 85 var dataTable = (DataTable)results[RegressionSolutionQualitiesResultName].Value; 99 var dataTable = (DataTable)results[ConstraintViolationsResultName].Value; 100 var problemData = RegressionProblemData; 86 101 87 foreach (var result in results.Where(r => r.Value is IRegressionSolution)) { 88 var solution = (ISymbolicRegressionSolution)result.Value; 89 var constraints = 90 parser.Parse(((RegressionProblemData)solution.ProblemData).IntervalConstraintsParameter.Value.Value); 91 var variableRanges = ((RegressionProblemData)solution.ProblemData).VariableRangesParameter.Value 92 .VariableIntervals; 102 var constraintViolations = new Dictionary<string, int>(); 93 103 94 if (dataTable.Rows.Count == 0) { 95 foreach (var constraint in constraints) { 96 if (!dataTable.Rows.ContainsKey(constraint.Derivaiton)) { 97 dataTable.Rows.Add(new DataRow(constraint.Derivaiton)); 98 } 104 var constraints = 105 parser.Parse(problemData.IntervalConstraints.Value); 106 var variableRanges = problemData.VariableRanges.VariableIntervals; 107 108 if (dataTable.Rows.Count == 0) { 109 foreach (var constraint in constraints) { 110 if (!dataTable.Rows.ContainsKey(constraint.Expression)) { 111 dataTable.Rows.Add(new DataRow(constraint.Expression)); 99 112 } 100 113 } 114 } 101 115 116 foreach (var constraint in constraints) { 117 constraintViolations.Add(constraint.Expression, 0); 118 } 119 120 foreach (var tree in this.SymbolicExpressionTree) { 102 121 foreach (var constraint in constraints) { 103 122 if (constraint.Variable != null && !variableRanges.ContainsKey(constraint.Variable)) … … 105 124 $"The given variable {constraint.Variable} in the constraint does not exists in the model.", 106 125 nameof(IntervalConstraintsParser)); 107 var numberOfViolations = dataTable.Rows[constraint.Derivaiton].Values.Count > 0108 ? dataTable.Rows[constraint.Derivaiton].Values.Last()109 : 0;110 126 if (!constraint.IsDerivation) { 111 var res = intervalInterpreter.GetSymbolicExressionTreeInterval( solution.Model.SymbolicExpressionTree,127 var res = intervalInterpreter.GetSymbolicExressionTreeInterval(tree, 112 128 variableRanges); 113 if (! IntervalInBoundaries(constraint.Interval,res, constraint.InclusiveLowerBound,129 if (!constraint.Interval.Contains(res, constraint.InclusiveLowerBound, 114 130 constraint.InclusiveUpperBound)) { 115 dataTable.Rows[constraint.Derivaiton].Values.Add(numberOfViolations + 1); 116 } else { 117 dataTable.Rows[constraint.Derivaiton].Values.Add(numberOfViolations); 131 constraintViolations[constraint.Expression]++; 118 132 } 119 133 } else { 120 var tree = solution.Model.SymbolicExpressionTree;134 var dTree = tree; 121 135 for (var i = 0; i < constraint.NumberOfDerivation; ++i) { 122 tree = DerivativeCalculator.Derive(tree, constraint.Variable);136 dTree = DerivativeCalculator.Derive(dTree, constraint.Variable); 123 137 } 124 138 125 var res = intervalInterpreter.GetSymbolicExressionTreeInterval( solution.Model.SymbolicExpressionTree,139 var res = intervalInterpreter.GetSymbolicExressionTreeInterval(dTree, 126 140 variableRanges); 127 if (! IntervalInBoundaries(constraint.Interval,res, constraint.InclusiveLowerBound,141 if (!constraint.Interval.Contains(res, constraint.InclusiveLowerBound, 128 142 constraint.InclusiveUpperBound)) { 129 dataTable.Rows[constraint.Derivaiton].Values.Add(numberOfViolations + 1); 130 } else { 131 dataTable.Rows[constraint.Derivaiton].Values.Add(numberOfViolations); 143 constraintViolations[constraint.Expression]++; 132 144 } 133 145 } 134 146 } 147 } 135 148 149 foreach (var kvp in constraintViolations) { 150 dataTable.Rows[kvp.Key].Values.Add(kvp.Value); 136 151 } 137 152 return base.Apply(); 138 153 } 139 140 private static bool IntervalInBoundaries(Interval i1, Interval i2, bool inclusiveLower, bool inclusiveUpper) {141 if (double.IsNegativeInfinity(i1.LowerBound) && double.IsPositiveInfinity(i1.UpperBound))142 return true;143 //Left-unbounded and right-bounded:144 if (double.IsNegativeInfinity(i1.LowerBound)) {145 if (inclusiveUpper)146 return i2.LowerBound <= i1.UpperBound && i2.UpperBound <= i1.UpperBound;147 return i2.LowerBound < i1.UpperBound && i2.UpperBound < i1.UpperBound;148 }149 150 //Left-bounded and right-unbounded:151 if (double.IsPositiveInfinity(i1.UpperBound)) {152 if (inclusiveLower)153 return i2.LowerBound >= i1.LowerBound && i2.UpperBound >= i1.LowerBound;154 return i2.LowerBound > i1.LowerBound && i2.UpperBound > i1.LowerBound;155 }156 157 //Proper and bounded:158 //Closed:159 if (inclusiveLower && inclusiveUpper) {160 return i1.LowerBound <= i2.LowerBound && i2.UpperBound <= i1.UpperBound;161 }162 163 //Open:164 if (!inclusiveLower && !inclusiveUpper) {165 return i1.LowerBound < i2.LowerBound && i2.UpperBound < i1.UpperBound;166 }167 168 //Left-closed, right-open:169 if (inclusiveLower) {170 return i1.LowerBound <= i2.LowerBound && i2.UpperBound < i1.UpperBound;171 }172 173 //Left-open, right-closed:174 return i1.LowerBound < i2.LowerBound && i2.UpperBound <= i1.UpperBound;175 }176 154 } 177 155 } -
branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/SymbolicRegressionSingleObjectiveProblem.cs
r15583 r16592 113 113 Operators.Add(new SymbolicRegressionSolutionsAnalyzer()); 114 114 Operators.Add(new SymbolicExpressionTreePhenotypicSimilarityCalculator()); 115 Operators.Add(new SymbolicRegressionConstraintAnalyzer()); 115 116 Operators.Add(new SymbolicRegressionPhenotypicDiversityAnalyzer(Operators.OfType<SymbolicExpressionTreePhenotypicSimilarityCalculator>()) { DiversityResultName = "Phenotypic Diversity" }); 116 117 ParameterizeOperators(); -
branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval.cs
r16548 r16592 43 43 public bool Contains(double value) { 44 44 return LowerBound <= value && value <= UpperBound; 45 } 46 47 public bool Contains(Interval other, bool lowerBoundInclusive = true, bool upperBoundInclusive = false) { 48 if (double.IsNegativeInfinity(this.LowerBound) && double.IsPositiveInfinity(this.UpperBound)) 49 return true; 50 //Left-unbounded and right-bounded: 51 if (double.IsNegativeInfinity(this.LowerBound)) { 52 if (upperBoundInclusive) 53 return other.LowerBound <= this.UpperBound && other.UpperBound <= this.UpperBound; 54 return other.LowerBound < this.UpperBound && other.UpperBound < this.UpperBound; 55 } 56 57 //Left-bounded and right-unbounded: 58 if (double.IsPositiveInfinity(this.UpperBound)) { 59 if (lowerBoundInclusive) 60 return other.LowerBound >= this.LowerBound && other.UpperBound >= this.LowerBound; 61 return other.LowerBound > this.LowerBound && other.UpperBound > this.LowerBound; 62 } 63 64 //Proper and bounded: 65 //Closed: 66 if (lowerBoundInclusive && upperBoundInclusive) { 67 return this.LowerBound <= other.LowerBound && other.UpperBound <= this.UpperBound; 68 } 69 70 //Open: 71 if (!lowerBoundInclusive && !upperBoundInclusive) { 72 return this.LowerBound < other.LowerBound && other.UpperBound < this.UpperBound; 73 } 74 75 //Left-closed, right-open: 76 if (lowerBoundInclusive) { 77 return this.LowerBound <= other.LowerBound && other.UpperBound < this.UpperBound; 78 } 79 80 //Left-open, right-closed: 81 return this.LowerBound < other.LowerBound && other.UpperBound <= this.UpperBound; 45 82 } 46 83 -
branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Parser/IntervalConstraint.cs
r16590 r16592 7 7 namespace HeuristicLab.Problems.DataAnalysis { 8 8 public class IntervalConstraint { 9 public string Derivaiton { get; set; }9 public string Expression { get; set; } 10 10 public string Definition { get; set; } 11 11 public Interval Interval { get; set; } -
branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Parser/IntervalConstraintsParser.cs
r16590 r16592 40 40 intervalConstraint.IsDerivation = false; 41 41 } 42 intervalConstraint. Derivaiton = matches[i].Groups[0].Value;42 intervalConstraint.Expression = matches[i].Groups[0].Value; 43 43 intervalConstraint.InclusiveLowerBound = (matches[i].Groups[2].Value == "["); 44 44 intervalConstraint.InclusiveUpperBound = (matches[i].Groups[6].Value == "]"); -
branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Regression/RegressionProblemData.cs
r16586 r16592 105 105 } 106 106 107 public IFixedValueParameter<NamedIntervals> VariableRangesParameter {108 get { return (IFixedValueParameter<NamedIntervals>)Parameters[VariableRangesParameterName]; } 109 }107 public IFixedValueParameter<NamedIntervals> VariableRangesParameter => (IFixedValueParameter<NamedIntervals>)Parameters[VariableRangesParameterName]; 108 109 public NamedIntervals VariableRanges => VariableRangesParameter.Value; 110 110 111 111 public IFixedValueParameter<TextValue> IntervalConstraintsParameter => (IFixedValueParameter<TextValue>) Parameters[IntervalConstraintsParameterName]; 112 public TextValue IntervalConstraints => IntervalConstraintsParameter.Value; 112 113 113 114 public string TargetVariable {
Note: See TracChangeset
for help on using the changeset viewer.