Changeset 16851
 Timestamp:
 04/19/19 14:35:08 (2 months ago)
 Location:
 branches/2971_named_intervals
 Files:

 1 deleted
 7 edited
Legend:
 Unmodified
 Added
 Removed

branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression3.4.csproj
r16713 r16851 204 204 <Compile Include="Plugin.cs" /> 205 205 <Compile Include="SingleObjective\ConstantOptimizationAnalyzer.cs" /> 206 <Compile Include="SingleObjective\Evaluators\SymbolicRegressionConstraintSatisfactionEvaluator.cs" />207 206 <Compile Include="SingleObjective\Evaluators\SymbolicRegressionMeanRelativeErrorEvaluator.cs" /> 208 207 <Compile Include="SingleObjective\Evaluators\SymbolicRegressionSingleObjectiveConstraintConstOptEvaluator.cs" /> 
branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionSingleObjectiveConstraintConstOptEvaluator.cs
r16844 r16851 58 58 IEnumerable<double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(tree, problemData.Dataset, rows); 59 59 IEnumerable<double> targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows); 60 OnlineCalculatorError errorState = OnlineCalculatorError.None;61 60 62 //var constraints = IntervalConstraintsParser.Parse(((RegressionProblemData)problemData).IntervalConstraintsParameter.Value.Value); 63 var constraints = ((RegressionProblemData)problemData).IntervalConstraintsParameter.Value.Constraints.Where(x => x.Enabled); 64 var intervalInterpreter = new IntervalInterpreter(); 65 var variableRanges = ((RegressionProblemData) problemData).VariableRangesParameter.Value.VariableIntervals; 66 61 var constraints = problemData.IntervalConstraints.Constraints.Where(x => x.Enabled); 62 var variableRanges = problemData.VariableRanges.VariableIntervals; 67 63 68 64 var r2 = SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(interpreter, tree, problemData, problemData.TrainingIndices, applyLinearScaling, 10); 69 if ( HasConstraintVioluations(constraints, intervalInterpreter, variableRanges, tree)) {65 if (SymbolicRegressionConstraintAnalyzer.ConstraintsSatisfied(constraints, variableRanges, tree)) { 70 66 r2 = 0; 71 67 } 
branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionSingleObjectiveConstraintPearsonRSquaredEvaluator.cs
r16800 r16851 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.Linq; 24 25 using HEAL.Attic; 25 26 using HeuristicLab.Common; … … 59 60 OnlineCalculatorError errorState = OnlineCalculatorError.None; 60 61 61 //var constraints = IntervalConstraintsParser.Parse(((RegressionProblemData)problemData).IntervalConstraintsParameter.Value.Value); 62 var constraints = ((RegressionProblemData)problemData).IntervalConstraintsParameter.Value.Constraints; 63 var intervalInterpreter = new IntervalInterpreter(); 64 var variableRanges = ((RegressionProblemData)problemData).VariableRangesParameter.Value.VariableIntervals; 62 var constraints = problemData.IntervalConstraints.Constraints.Where(x => x.Enabled); 63 var variableRanges = problemData.VariableRanges.VariableIntervals; 64 var tree = solution; 65 65 66 66 double r; … … 70 70 var model = new SymbolicRegressionModel(problemData.TargetVariable, solution, interpreter, lowerEstimationLimit, upperEstimationLimit); 71 71 model.Scale(problemData); 72 var e = model.GetEstimatedValues(problemData.Dataset, problemData.TrainingIndices); 73 var scaledTree = model.SymbolicExpressionTree; 72 tree = model.SymbolicExpressionTree; 74 73 errorState = rCalculator.ErrorState; 75 if (CheckConstraintsViolations(constraints, intervalInterpreter, variableRanges, scaledTree, out var val)) { 76 r = val; 77 } else { 78 r = rCalculator.R; 79 } 74 r = rCalculator.R; 80 75 } else { 81 76 IEnumerable<double> boundedEstimatedValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit); 82 if (CheckConstraintsViolations(constraints, intervalInterpreter, variableRanges, solution, out var val)) { 83 r = val; 84 } else { 85 r = OnlinePearsonsRCalculator.Calculate(targetValues, boundedEstimatedValues, out errorState); 86 } 77 r = OnlinePearsonsRCalculator.Calculate(targetValues, boundedEstimatedValues, out errorState); 87 78 } 79 80 if (!SymbolicRegressionConstraintAnalyzer.ConstraintsSatisfied(constraints, variableRanges, tree)) { 81 return 0; 82 } 83 88 84 if (errorState != OnlineCalculatorError.None) return double.NaN; 89 85 return r * r; … … 107 103 return r2; 108 104 } 109 110 private static bool CheckConstraintsViolations(IEnumerable<IntervalConstraint> constraints, IntervalInterpreter intervalInterpreter,111 IDictionary<string, Interval> variableRanges, ISymbolicExpressionTree solution, out double r) {112 foreach (var constraint in constraints) {113 if (constraint.Variable != null && !variableRanges.ContainsKey(constraint.Variable))114 throw new ArgumentException($"The given variable {constraint.Variable} in the constraint does not exists in the model.", nameof(IntervalConstraintsParser));115 if (!constraint.IsDerivation) {116 var res = intervalInterpreter.GetSymbolicExpressionTreeInterval(solution, variableRanges);117 if (!constraint.Interval.Contains(res, constraint.InclusiveLowerBound,118 constraint.InclusiveUpperBound)) {119 r = 0;120 return true;121 }122 } else {123 var tree = solution;124 for (var i = 0; i < constraint.NumberOfDerivation; ++i) {125 tree = DerivativeCalculator.Derive(tree, constraint.Variable);126 }127 var res = intervalInterpreter.GetSymbolicExpressionTreeInterval(tree, variableRanges);128 if (!constraint.Interval.Contains(res, constraint.InclusiveLowerBound,129 constraint.InclusiveUpperBound)) {130 r = 0;131 return true;132 }133 }134 }135 r = 1;136 return false;137 }138 105 } 139 106 } 
branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionSingleObjectiveEvaluator.cs
r16644 r16851 22 22 using HeuristicLab.Common; 23 23 using HEAL.Attic; 24 25 24 namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression { 26 25 [StorableType("7EB90F034385474FBDE73B133E8FEAAB")] 
branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/SymbolicRegressionConstraintAnalyzer.cs
r16844 r16851 22 22 #endregion 23 23 24 using System; 24 25 using System.Collections.Generic; 25 26 using System.Linq; … … 34 35 namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression { 35 36 [StorableType("4318C6BDE0A145FEAC3096E7F73B51FB")] 36 public class SymbolicRegressionConstraintAnalyzer : SymbolicDataAnalysisAnalyzer, ISymbolic DataAnalysisInterpreterOperator, ISymbolicExpressionTreeAnalyzer {37 public class SymbolicRegressionConstraintAnalyzer : SymbolicDataAnalysisAnalyzer, ISymbolicExpressionTreeAnalyzer { 37 38 private const string ConstraintViolationsResultName = "Constraint Violations"; 38 39 private const string ProblemDataParameterName = "ProblemData"; 39 private const string SymbolicDataAnalysisTreeInterpreterParameterName = "SymbolicDataAnalysisTreeInterpreter";40 40 41 41 #region parameter properties 42 43 public ILookupParameter<RegressionProblemData> RegressionProblemDataParameter { 44 get { return (ILookupParameter<RegressionProblemData>) Parameters[ProblemDataParameterName]; } 42 public ILookupParameter<IRegressionProblemData> RegressionProblemDataParameter { 43 get { return (ILookupParameter<IRegressionProblemData>) Parameters[ProblemDataParameterName]; } 45 44 } 46 47 public ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> SymbolicDataAnalysisTreeInterpreterParameter {48 get { return (ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>) Parameters[SymbolicDataAnalysisTreeInterpreterParameterName]; }49 }50 51 #endregion52 53 #region properties54 55 public RegressionProblemData RegressionProblemData {56 get { return RegressionProblemDataParameter.ActualValue; }57 }58 59 public ISymbolicDataAnalysisExpressionTreeInterpreter Interpreter {60 get { return SymbolicDataAnalysisTreeInterpreterParameter.ActualValue; }61 }62 63 45 #endregion 64 46 … … 76 58 } 77 59 78 public SymbolicRegressionConstraintAnalyzer() { 79 Parameters.Add(new LookupParameter<RegressionProblemData>(ProblemDataParameterName, "The problem data of the symbolic data analysis problem.")); 80 Parameters.Add(new LookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicDataAnalysisTreeInterpreterParameterName, "The interpreter for symbolic data analysis expression trees.")); 60 public SymbolicRegressionConstraintAnalyzer() : base(){ 61 Parameters.Add(new LookupParameter<IRegressionProblemData>(ProblemDataParameterName, "The problem data of the symbolic data analysis problem.")); 81 62 } 82 63 … … 85 66 } 86 67 87 public static Dictionary<IntervalConstraint, bool> CheckConstraints(ISymbolicExpressionTree model, IRegressionProblemData problemData) { 68 public override IOperation Apply() { 69 var problemData = RegressionProblemDataParameter.ActualValue; 70 var trees = SymbolicExpressionTreeParameter.ActualValue; 71 72 var results = ResultCollectionParameter.ActualValue; 88 73 var constraints = problemData.IntervalConstraints.Constraints.Where(x => x.Enabled); 89 var constraintViolations = new Dictionary<IntervalConstraint, bool>();90 74 var variableRanges = problemData.VariableRanges.VariableIntervals; 91 foreach (var constraint in constraints) {92 if (!constraintViolations.ContainsKey(constraint)) {93 constraintViolations.Add(constraint, CheckConstraint(model, variableRanges, constraint));94 }95 }96 75 97 return constraintViolations;98 }99 100 public static Dictionary<IntervalConstraint, bool> CheckConstraints(ISymbolicRegressionModel model, IRegressionProblemData problemData) {101 return CheckConstraints(model.SymbolicExpressionTree, problemData);102 }103 104 public static bool CheckConstraint(ISymbolicExpressionTree model, IDictionary<string, Interval> variableRanges, IntervalConstraint constraint) {105 var intervalInterpreter = new IntervalInterpreter();106 if (!constraint.IsDerivation) {107 var res = intervalInterpreter.GetSymbolicExpressionTreeInterval(model, variableRanges);108 if (!constraint.Interval.Contains(res, constraint.InclusiveLowerBound, constraint.InclusiveUpperBound)) {109 return false;110 }111 } else {112 var dTree = model;113 for (var i = 0; i < constraint.NumberOfDerivation; ++i) {114 dTree = DerivativeCalculator.Derive(dTree, constraint.Variable);115 }116 117 var res = intervalInterpreter.GetSymbolicExpressionTreeInterval(dTree, variableRanges);118 if (!constraint.Interval.Contains(res, constraint.InclusiveLowerBound, constraint.InclusiveUpperBound)) {119 return false;120 }121 }122 123 return true;124 }125 126 public static bool CheckConstraint(ISymbolicRegressionModel model, IDictionary<string, Interval> variableRanges, IntervalConstraint constraint) {127 return CheckConstraint(model.SymbolicExpressionTree, variableRanges, constraint);128 }129 130 public override IOperation Apply() {131 var results = ResultCollectionParameter.ActualValue;132 76 if (!results.ContainsKey(ConstraintViolationsResultName)) { 133 77 var newDataTable = new DataTable(ConstraintViolationsResultName); 78 foreach (var constraint in constraints) { 79 newDataTable.Rows.Add(new DataRow(constraint.Expression)); 80 } 134 81 results.Add(new Result(ConstraintViolationsResultName, "Chart displaying the constraint violations.", newDataTable)); 135 82 } 83 var dataTable = (DataTable)results[ConstraintViolationsResultName].Value; 136 84 137 var dataTable = (DataTable) results[ConstraintViolationsResultName].Value; 138 var problemData = RegressionProblemData; 139 var constraintViolations = new Dictionary<string, int>(); 140 var constraints = problemData.IntervalConstraintsParameter.Value.Constraints.Where(x => x.Enabled); 141 if (dataTable.Rows.Count == 0) { 142 foreach (var constraint in constraints) { 143 if (!dataTable.Rows.ContainsKey(constraint.Expression)) { 144 dataTable.Rows.Add(new DataRow(constraint.Expression)); 145 } 85 var interpreter = new IntervalInterpreter(); 86 foreach (var constraint in constraints) { 87 int violations = 0; 88 foreach (var tree in trees) { 89 var satisfied = SymbolicRegressionConstraintAnalyzer.ConstraintSatisfied(constraint, interpreter, variableRanges, tree); 90 if (!satisfied) violations++; 146 91 } 92 dataTable.Rows[constraint.Expression].Values.Add(violations); 147 93 } 148 149 foreach (var constraint in constraints) { 150 constraintViolations.Add(constraint.Expression, 0); 151 } 152 153 foreach (var tree in this.SymbolicExpressionTree) { 154 var checkedConstraints = CheckConstraints(tree, problemData); 155 foreach (var kvp in checkedConstraints) { 156 if (!kvp.Value) constraintViolations[kvp.Key.Expression]++; 157 } 158 } 159 160 foreach (var kvp in constraintViolations) { 161 dataTable.Rows[kvp.Key].Values.Add(kvp.Value); 162 } 94 163 95 164 96 return base.Apply(); 165 97 } 98 99 public static bool ConstraintSatisfied(IntervalConstraint constraint, IntervalInterpreter intervalInterpreter, 100 IDictionary<string, Interval> variableRanges, ISymbolicExpressionTree solution) { 101 102 if (constraint.Variable != null && !variableRanges.ContainsKey(constraint.Variable)) 103 throw new ArgumentException($"The given variable {constraint.Variable} in the constraint does not exists in the model.", nameof(IntervalConstraintsParser)); 104 105 Interval resultInterval; 106 107 if (!constraint.IsDerivation) { 108 resultInterval = intervalInterpreter.GetSymbolicExpressionTreeInterval(solution, variableRanges); 109 } else { 110 var tree = solution; 111 for (var i = 0; i < constraint.NumberOfDerivation; ++i) { 112 tree = DerivativeCalculator.Derive(tree, constraint.Variable); 113 } 114 resultInterval = intervalInterpreter.GetSymbolicExpressionTreeInterval(tree, variableRanges); 115 } 116 117 var satisfied = constraint.Interval.Contains(resultInterval, constraint.InclusiveLowerBound, constraint.InclusiveUpperBound); 118 return satisfied; 119 } 120 121 public static bool ConstraintsSatisfied(IEnumerable<IntervalConstraint> constraints, IDictionary<string, Interval> variableRanges, ISymbolicExpressionTree solution) { 122 var intervalInterpreter = new IntervalInterpreter(); 123 foreach (var constraint in constraints) { 124 if (constraint.Variable != null && !variableRanges.ContainsKey(constraint.Variable)) 125 throw new ArgumentException($"The given variable {constraint.Variable} in the constraint does not exists in the model.", nameof(IntervalConstraintsParser)); 126 127 var satisfied = ConstraintSatisfied(constraint, intervalInterpreter, variableRanges, solution); 128 if (!satisfied) return false; 129 } 130 return true; 131 } 166 132 } 167 133 } 
branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SymbolicRegressionSolution.cs
r16830 r16851 98 98 } 99 99 100 101 100 public NamedIntervals IntervalEvaluationCollection { 102 101 get { return (NamedIntervals)this[IntervalEvaluationResultName].Value; } … … 128 127 estimationLimitResults.Add(new Result(TestNaNEvaluationsResultName, "", new IntValue())); 129 128 Add(new Result(EstimationLimitsResultsResultName, "Results concerning the estimation limits of symbolic regression solution", estimationLimitResults)); 130 Add(new Result(IntervalEvaluationResultName, "Results concerning the derivation of symbolic regression solution", GetIntervalEvaluations()));129 Add(new Result(IntervalEvaluationResultName, "Results concerning the derivation of symbolic regression solution", new NamedIntervals())); 131 130 RecalculateResults(); 132 131 } … … 148 147 estimationLimitResults.Add(new Result(TestNaNEvaluationsResultName, "", new IntValue())); 149 148 Add(new Result(EstimationLimitsResultsResultName, "Results concerning the estimation limits of symbolic regression solution", estimationLimitResults)); 150 Add(new Result(IntervalEvaluationResultName, "Results concerning the derivation of symbolic regression solution", GetIntervalEvaluations()));149 Add(new Result(IntervalEvaluationResultName, "Results concerning the derivation of symbolic regression solution", new NamedIntervals())); 151 150 CalculateResults(); 152 151 } … … 156 155 base.RecalculateResults(); 157 156 CalculateResults(); 158 }159 160 private NamedIntervals GetIntervalEvaluations() {161 var intervalEvaluation = new NamedIntervals();162 var interpreter = new IntervalInterpreter();163 var variableRanges = ProblemData.VariableRanges.VariableIntervals;164 165 if (variableRanges != null) {166 intervalEvaluation.VariableIntervals.Add($"Target {ProblemData.TargetVariable}", new Interval(variableRanges[ProblemData.TargetVariable].LowerBound, variableRanges[ProblemData.TargetVariable].UpperBound));167 intervalEvaluation.VariableIntervals.Add("Modell Interval", interpreter.GetSymbolicExpressionTreeInterval(Model.SymbolicExpressionTree, variableRanges));168 169 foreach (var derivate in variableRanges) {170 if (derivate.Key != ProblemData.TargetVariable) {171 var derived = DerivativeCalculator.Derive(Model.SymbolicExpressionTree, derivate.Key);172 var derivedResultInterval = interpreter.GetSymbolicExpressionTreeInterval(derived, variableRanges);173 174 intervalEvaluation.VariableIntervals.Add(" \u2202f/\u2202" + derivate.Key,175 new Interval(derivedResultInterval.LowerBound, derivedResultInterval.UpperBound));176 }177 }178 }179 return intervalEvaluation;180 157 } 181 158 … … 194 171 TestNaNEvaluations = Model.Interpreter.GetSymbolicExpressionTreeValues(Model.SymbolicExpressionTree, ProblemData.Dataset, ProblemData.TestIndices).Count(double.IsNaN); 195 172 196 IntervalEvaluationCollection = GetIntervalEvaluations(); 173 IntervalEvaluationCollection = CalculateModelIntervals(this); 174 } 175 176 private static NamedIntervals CalculateModelIntervals(ISymbolicRegressionSolution solution) { 177 var intervalEvaluation = new NamedIntervals(); 178 var interpreter = new IntervalInterpreter(); 179 var problemData = solution.ProblemData; 180 var model = solution.Model; 181 var variableRanges = problemData.VariableRanges.VariableIntervals; 182 183 intervalEvaluation.VariableIntervals.Add($"Target {problemData.TargetVariable}", new Interval(variableRanges[problemData.TargetVariable].LowerBound, variableRanges[problemData.TargetVariable].UpperBound)); 184 intervalEvaluation.VariableIntervals.Add("Model Interval", interpreter.GetSymbolicExpressionTreeInterval(model.SymbolicExpressionTree, variableRanges)); 185 186 foreach (var inputVariable in model.VariablesUsedForPrediction.OrderBy(v => v, new NaturalStringComparer())) { 187 var derivedModel = DerivativeCalculator.Derive(model.SymbolicExpressionTree, inputVariable); 188 var derivedResultInterval = interpreter.GetSymbolicExpressionTreeInterval(derivedModel, variableRanges); 189 190 intervalEvaluation.VariableIntervals.Add(" \u2202f/\u2202" + inputVariable, 191 new Interval(derivedResultInterval.LowerBound, derivedResultInterval.UpperBound)); 192 } 193 194 return intervalEvaluation; 197 195 } 198 196 } 
branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/NamedIntervals.cs
r16830 r16851 29 29 [StorableType("230B4E4B41E54D339BC3E2DAADDCA5AE")] 30 30 public class NamedIntervals : Item { 31 public static new System.Drawing.Image StaticItemImage { 32 get => HeuristicLab.Common.Resources.VSImageLibrary.Object; 33 } 31 34 public ObservableDictionary<string, Interval> VariableIntervals { get; } = new ObservableDictionary<string, Interval>(); 32 35
Note: See TracChangeset
for help on using the changeset viewer.