Changeset 16800 for branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/SymbolicRegressionConstraintAnalyzer.cs
 Timestamp:
 04/17/19 14:37:55 (12 months ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/SymbolicRegressionConstraintAnalyzer.cs
r16780 r16800 37 37 public class SymbolicRegressionConstraintAnalyzer : SymbolicDataAnalysisAnalyzer, ISymbolicDataAnalysisInterpreterOperator, ISymbolicExpressionTreeAnalyzer { 38 38 private const string ConstraintViolationsResultName = "Constraint Violations"; 39 40 39 private const string ProblemDataParameterName = "ProblemData"; 41 40 private const string SymbolicDataAnalysisTreeInterpreterParameterName = "SymbolicDataAnalysisTreeInterpreter"; 42 41 43 42 #region parameter properties 43 44 44 public ILookupParameter<RegressionProblemData> RegressionProblemDataParameter { 45 get { return (ILookupParameter<RegressionProblemData>) Parameters[ProblemDataParameterName]; }45 get { return (ILookupParameter<RegressionProblemData>) Parameters[ProblemDataParameterName]; } 46 46 } 47 47 48 public ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> SymbolicDataAnalysisTreeInterpreterParameter { 48 get { return (ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>) Parameters[SymbolicDataAnalysisTreeInterpreterParameterName]; }49 get { return (ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>) Parameters[SymbolicDataAnalysisTreeInterpreterParameterName]; } 49 50 } 51 50 52 #endregion 53 51 54 #region properties 52 55 … … 58 61 get { return SymbolicDataAnalysisTreeInterpreterParameter.ActualValue; } 59 62 } 63 60 64 #endregion 61 65 62 public virtual bool EnabledByDefault { 63 get { return false; } 64 } 66 public override bool EnabledByDefault => false; 65 67 66 68 [StorableConstructor] … … 68 70 } 69 71 70 protected SymbolicRegressionConstraintAnalyzer(SymbolicRegressionConstraintAnalyzer original, Cloner cloner) 71 : base(original, cloner) { 72 protected SymbolicRegressionConstraintAnalyzer(SymbolicRegressionConstraintAnalyzer original, Cloner cloner) : base(original, cloner) { 72 73 } 73 74 … … 85 86 } 86 87 87 public static Dictionary<IntervalConstraint, bool> CheckConstraints(ISymbolicExpressionTree model, 88 IRegressionProblemData problemData) { 89 var constraints = ((RegressionProblemData)problemData).IntervalConstraintsParameter.Value.Constraints.Where(x => x.IsChecked); 88 public static Dictionary<IntervalConstraint, bool> CheckConstraints(ISymbolicExpressionTree model, IRegressionProblemData problemData) { 89 var constraints = problemData.IntervalConstraints.Constraints.Where(x => x.Enabled); 90 90 var constraintViolations = new Dictionary<IntervalConstraint, bool>(); 91 var variableRanges = ((RegressionProblemData)problemData).VariableRanges.VariableIntervals; 92 91 var variableRanges = problemData.VariableRanges.VariableIntervals; 93 92 foreach (var constraint in constraints) { 94 93 if (!constraintViolations.ContainsKey(constraint)) { … … 100 99 } 101 100 102 public static Dictionary<IntervalConstraint, bool> CheckConstraints(ISymbolicRegressionModel model, 103 IRegressionProblemData problemData) { 101 public static Dictionary<IntervalConstraint, bool> CheckConstraints(ISymbolicRegressionModel model, IRegressionProblemData problemData) { 104 102 return CheckConstraints(model.SymbolicExpressionTree, problemData); 105 103 } 106 104 107 public static bool CheckConstraint(ISymbolicExpressionTree model, Dictionary<string, Interval> variableRanges, 108 IntervalConstraint constraint) { 105 public static bool CheckConstraint(ISymbolicExpressionTree model, IDictionary<string, Interval> variableRanges, IntervalConstraint constraint) { 109 106 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 } 107 if (!constraint.IsDerivation) { 108 var res = intervalInterpreter.GetSymbolicExpressionTreeInterval(model, variableRanges); 109 if (!constraint.Interval.Contains(res, constraint.InclusiveLowerBound, constraint.InclusiveUpperBound)) { 110 return false; 111 } 112 } else { 113 var dTree = model; 114 for (var i = 0; i < constraint.NumberOfDerivation; ++i) { 115 dTree = DerivativeCalculator.Derive(dTree, constraint.Variable); 116 } 117 118 var res = intervalInterpreter.GetSymbolicExpressionTreeInterval(dTree, variableRanges); 119 if (!constraint.Interval.Contains(res, constraint.InclusiveLowerBound, constraint.InclusiveUpperBound)) { 120 return false; 129 121 } 130 122 } 123 131 124 return true; 132 125 } 133 126 134 static bool CheckConstraint(ISymbolicRegressionModel model, Dictionary<string, Interval> variableRanges, IntervalConstraint constraint) {127 static bool CheckConstraint(ISymbolicRegressionModel model, IDictionary<string, Interval> variableRanges, IntervalConstraint constraint) { 135 128 return CheckConstraint(model.SymbolicExpressionTree, variableRanges, constraint); 136 129 } … … 140 133 if (!results.ContainsKey(ConstraintViolationsResultName)) { 141 134 var newDataTable = new DataTable(ConstraintViolationsResultName); 142 results.Add(new Result(ConstraintViolationsResultName, "Chart displaying the constraint violations.", 143 newDataTable)); 135 results.Add(new Result(ConstraintViolationsResultName, "Chart displaying the constraint violations.", newDataTable)); 144 136 } 145 var dataTable = (DataTable)results[ConstraintViolationsResultName].Value; 137 138 var dataTable = (DataTable) results[ConstraintViolationsResultName].Value; 146 139 var problemData = RegressionProblemData; 147 140 var constraintViolations = new Dictionary<string, int>(); 148 149 var constraints = problemData.IntervalConstraintsParameter.Value.Constraints.Where(x => x.IsChecked); 150 141 var constraints = problemData.IntervalConstraintsParameter.Value.Constraints.Where(x => x.Enabled); 151 142 if (dataTable.Rows.Count == 0) { 152 143 foreach (var constraint in constraints) { … … 163 154 foreach (var tree in this.SymbolicExpressionTree) { 164 155 var checkedConstraints = CheckConstraints(tree, problemData); 165 166 156 foreach (var kvp in checkedConstraints) { 167 if (!kvp.Value) 168 constraintViolations[kvp.Key.Expression]++; 157 if (!kvp.Value) constraintViolations[kvp.Key.Expression]++; 169 158 } 170 159 } … … 173 162 dataTable.Rows[kvp.Key].Values.Add(kvp.Value); 174 163 } 164 175 165 return base.Apply(); 176 166 }
Note: See TracChangeset
for help on using the changeset viewer.