Free cookie consent management tool by TermsFeed Policy Generator

Changeset 16851


Ignore:
Timestamp:
04/19/19 14:35:08 (6 years ago)
Author:
chaider
Message:

#2791 Refactored Constraint Evaluator and Analyzer

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.Regression-3.4.csproj

    r16713 r16851  
    204204    <Compile Include="Plugin.cs" />
    205205    <Compile Include="SingleObjective\ConstantOptimizationAnalyzer.cs" />
    206     <Compile Include="SingleObjective\Evaluators\SymbolicRegressionConstraintSatisfactionEvaluator.cs" />
    207206    <Compile Include="SingleObjective\Evaluators\SymbolicRegressionMeanRelativeErrorEvaluator.cs" />
    208207    <Compile Include="SingleObjective\Evaluators\SymbolicRegressionSingleObjectiveConstraintConstOptEvaluator.cs" />
  • branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionSingleObjectiveConstraintConstOptEvaluator.cs

    r16844 r16851  
    5858      IEnumerable<double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(tree, problemData.Dataset, rows);
    5959      IEnumerable<double> targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
    60       OnlineCalculatorError errorState = OnlineCalculatorError.None;
    6160
    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;
    6763
    6864      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)) {
    7066        r2 = 0;
    7167      }
  • branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionSingleObjectiveConstraintPearsonRSquaredEvaluator.cs

    r16800 r16851  
    2222using System;
    2323using System.Collections.Generic;
     24using System.Linq;
    2425using HEAL.Attic;
    2526using HeuristicLab.Common;
     
    5960      OnlineCalculatorError errorState = OnlineCalculatorError.None;
    6061
    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;
    6565
    6666      double r;
     
    7070        var model = new SymbolicRegressionModel(problemData.TargetVariable, solution, interpreter, lowerEstimationLimit, upperEstimationLimit);
    7171        model.Scale(problemData);
    72         var e = model.GetEstimatedValues(problemData.Dataset, problemData.TrainingIndices);
    73         var scaledTree = model.SymbolicExpressionTree;
     72        tree = model.SymbolicExpressionTree;
    7473        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;
    8075      } else {
    8176        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);
    8778      }
     79
     80      if (!SymbolicRegressionConstraintAnalyzer.ConstraintsSatisfied(constraints, variableRanges, tree)) {
     81        return 0;
     82      }
     83
    8884      if (errorState != OnlineCalculatorError.None) return double.NaN;
    8985      return r * r;
     
    107103      return r2;
    108104    }
    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     }
    138105  }
    139106}
  • branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionSingleObjectiveEvaluator.cs

    r16644 r16851  
    2222using HeuristicLab.Common;
    2323using HEAL.Attic;
    24 
    2524namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
    2625  [StorableType("7EB90F03-4385-474F-BDE7-3B133E8FEAAB")]
  • branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/SymbolicRegressionConstraintAnalyzer.cs

    r16844 r16851  
    2222#endregion
    2323
     24using System;
    2425using System.Collections.Generic;
    2526using System.Linq;
     
    3435namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
    3536  [StorableType("4318C6BD-E0A1-45FE-AC30-96E7F73B51FB")]
    36   public class SymbolicRegressionConstraintAnalyzer : SymbolicDataAnalysisAnalyzer, ISymbolicDataAnalysisInterpreterOperator, ISymbolicExpressionTreeAnalyzer {
     37  public class SymbolicRegressionConstraintAnalyzer : SymbolicDataAnalysisAnalyzer, ISymbolicExpressionTreeAnalyzer {
    3738    private const string ConstraintViolationsResultName = "Constraint Violations";
    3839    private const string ProblemDataParameterName = "ProblemData";
    39     private const string SymbolicDataAnalysisTreeInterpreterParameterName = "SymbolicDataAnalysisTreeInterpreter";
    4040
    4141    #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]; }
    4544    }
    46 
    47     public ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> SymbolicDataAnalysisTreeInterpreterParameter {
    48       get { return (ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>) Parameters[SymbolicDataAnalysisTreeInterpreterParameterName]; }
    49     }
    50 
    51     #endregion
    52 
    53     #region properties
    54 
    55     public RegressionProblemData RegressionProblemData {
    56       get { return RegressionProblemDataParameter.ActualValue; }
    57     }
    58 
    59     public ISymbolicDataAnalysisExpressionTreeInterpreter Interpreter {
    60       get { return SymbolicDataAnalysisTreeInterpreterParameter.ActualValue; }
    61     }
    62 
    6345    #endregion
    6446
     
    7658    }
    7759
    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."));
    8162    }
    8263
     
    8566    }
    8667
    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;
    8873      var constraints = problemData.IntervalConstraints.Constraints.Where(x => x.Enabled);
    89       var constraintViolations = new Dictionary<IntervalConstraint, bool>();
    9074      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       }
    9675
    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;
    13276      if (!results.ContainsKey(ConstraintViolationsResultName)) {
    13377        var newDataTable = new DataTable(ConstraintViolationsResultName);
     78          foreach (var constraint in constraints) {
     79              newDataTable.Rows.Add(new DataRow(constraint.Expression));
     80        }
    13481        results.Add(new Result(ConstraintViolationsResultName, "Chart displaying the constraint violations.", newDataTable));
    13582      }
     83      var dataTable = (DataTable)results[ConstraintViolationsResultName].Value;
    13684
    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++;
    14691        }
     92        dataTable.Rows[constraint.Expression].Values.Add(violations);
    14793      }
    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     
    16395
    16496      return base.Apply();
    16597    }
     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    }
    166132  }
    167133}
  • branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SymbolicRegressionSolution.cs

    r16830 r16851  
    9898    }
    9999
    100 
    101100    public NamedIntervals IntervalEvaluationCollection {
    102101      get { return (NamedIntervals)this[IntervalEvaluationResultName].Value; }
     
    128127      estimationLimitResults.Add(new Result(TestNaNEvaluationsResultName, "", new IntValue()));
    129128      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()));
    131130      RecalculateResults();
    132131    }
     
    148147        estimationLimitResults.Add(new Result(TestNaNEvaluationsResultName, "", new IntValue()));
    149148        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()));
    151150        CalculateResults();
    152151      }
     
    156155      base.RecalculateResults();
    157156      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;
    180157    }
    181158
     
    194171      TestNaNEvaluations = Model.Interpreter.GetSymbolicExpressionTreeValues(Model.SymbolicExpressionTree, ProblemData.Dataset, ProblemData.TestIndices).Count(double.IsNaN);
    195172
    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;
    197195    }
    198196  }
  • branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/NamedIntervals.cs

    r16830 r16851  
    2929  [StorableType("230B4E4B-41E5-4D33-9BC3-E2DAADDCA5AE")]
    3030  public class NamedIntervals : Item {
     31    public static new System.Drawing.Image StaticItemImage {
     32      get => HeuristicLab.Common.Resources.VSImageLibrary.Object;
     33    }
    3134    public ObservableDictionary<string, Interval> VariableIntervals { get; } = new ObservableDictionary<string, Interval>();
    3235
Note: See TracChangeset for help on using the changeset viewer.