Changeset 16747
 Timestamp:
 04/02/19 15:08:43 (5 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/SymbolicRegressionConstraintAnalyzer.cs
r16713 r16747 24 24 using System; 25 25 using System.Collections.Generic; 26 using System.Linq; 26 27 using HEAL.Attic; 27 28 using HeuristicLab.Analysis; … … 84 85 } 85 86 87 static Dictionary<IntervalConstraint, bool> CheckConstraints(ISymbolicExpressionTree model, 88 IRegressionProblemData problemData) { 89 var constraints = IntervalConstraintsParser.Parse(((RegressionProblemData)problemData).IntervalConstraints.Value); 90 var constraintViolations = new Dictionary<IntervalConstraint, bool>(); 91 var variableRanges = ((RegressionProblemData)problemData).VariableRanges.VariableIntervals; 92 93 foreach (var constraint in constraints) { 94 if (!constraintViolations.ContainsKey(constraint)) { 95 constraintViolations.Add(constraint, CheckConstraint(model, variableRanges, constraint)); 96 } 97 } 98 99 return constraintViolations; 100 } 101 102 static Dictionary<IntervalConstraint, bool> CheckConstraints(ISymbolicRegressionModel model, 103 IRegressionProblemData problemData) { 104 return CheckConstraints(model.SymbolicExpressionTree, problemData); 105 } 106 107 static bool CheckConstraint(ISymbolicExpressionTree model, Dictionary<string, Interval> variableRanges, 108 IntervalConstraint constraint) { 109 var intervalInterpreter = new IntervalInterpreter(); 110 if (constraint.Variable != null) { 111 if (!constraint.IsDerivation) { 112 var res = intervalInterpreter.GetSymbolicExpressionTreeInterval(model, 113 variableRanges); 114 if (!constraint.Interval.Contains(res, constraint.InclusiveLowerBound, 115 constraint.InclusiveUpperBound)) { 116 return false; 117 } 118 } else { 119 var dTree = model; 120 for (var i = 0; i < constraint.NumberOfDerivation; ++i) { 121 dTree = DerivativeCalculator.Derive(dTree, constraint.Variable); 122 } 123 var res = intervalInterpreter.GetSymbolicExpressionTreeInterval(dTree, 124 variableRanges); 125 if (!constraint.Interval.Contains(res, constraint.InclusiveLowerBound, 126 constraint.InclusiveUpperBound)) { 127 return false; 128 } 129 } 130 } 131 return true; 132 } 133 134 static bool CheckConstraint(ISymbolicRegressionModel model, Dictionary<string, Interval> variableRanges, IntervalConstraint constraint) { 135 return CheckConstraint(model.SymbolicExpressionTree, variableRanges, constraint); 136 } 137 86 138 public override IOperation Apply() { 87 139 var results = ResultCollectionParameter.ActualValue; 88 var intervalInterpreter = new IntervalInterpreter();89 140 if (!results.ContainsKey(ConstraintViolationsResultName)) { 90 141 var newDataTable = new DataTable(ConstraintViolationsResultName); … … 92 143 newDataTable)); 93 144 } 94 95 145 var dataTable = (DataTable)results[ConstraintViolationsResultName].Value; 96 146 var problemData = RegressionProblemData; 97 98 147 var constraintViolations = new Dictionary<string, int>(); 99 148 100 149 var constraints = IntervalConstraintsParser.Parse(problemData.IntervalConstraints.Value); 101 var variableRanges = problemData.VariableRanges.VariableIntervals;102 150 103 151 if (dataTable.Rows.Count == 0) { … … 114 162 115 163 foreach (var tree in this.SymbolicExpressionTree) { 116 foreach (var constraint in constraints) { 117 if (constraint.Variable != null && !variableRanges.ContainsKey(constraint.Variable)) 118 throw new ArgumentException( 119 $"The given variable {constraint.Variable} in the constraint does not exists in the model.", 120 nameof(IntervalConstraintsParser)); 121 if (!constraint.IsDerivation) { 122 var res = intervalInterpreter.GetSymbolicExpressionTreeInterval(tree, 123 variableRanges); 124 if (!constraint.Interval.Contains(res, constraint.InclusiveLowerBound, 125 constraint.InclusiveUpperBound)) { 126 constraintViolations[constraint.Expression]++; 127 } 128 } else { 129 var dTree = tree; 130 for (var i = 0; i < constraint.NumberOfDerivation; ++i) { 131 dTree = DerivativeCalculator.Derive(dTree, constraint.Variable); 132 } 164 var checkedConstraints = CheckConstraints(tree, problemData); 133 165 134 var res = intervalInterpreter.GetSymbolicExpressionTreeInterval(dTree, 135 variableRanges); 136 if (!constraint.Interval.Contains(res, constraint.InclusiveLowerBound, 137 constraint.InclusiveUpperBound)) { 138 constraintViolations[constraint.Expression]++; 139 } 140 } 166 foreach (var kvp in checkedConstraints) { 167 if (!kvp.Value) 168 constraintViolations[kvp.Key.Expression]++; 141 169 } 142 170 }
Note: See TracChangeset
for help on using the changeset viewer.