Changeset 16592


Ignore:
Timestamp:
02/06/19 16:16:50 (2 months ago)
Author:
chaider
Message:

#2971

  • Added ContainsInterval method in Interval
  • Some renaming
  • Added ConstraintAnalyzer to SymbolicRegressionProblem
  • Added Counter for constraint violations in analyzer
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  
    7070        if (!constraint.IsDerivation) {
    7171          var res = intervalInterpreter.GetSymbolicExressionTreeInterval(solution, variableRanges);
    72           if (!IntervalInBoundaries(constraint.Interval, res, constraint.InclusiveLowerBound,
     72          if (!constraint.Interval.Contains(res, constraint.InclusiveLowerBound,
    7373            constraint.InclusiveUpperBound)) {
    7474            return 0;
     
    8080          }
    8181          var res = intervalInterpreter.GetSymbolicExressionTreeInterval(tree, variableRanges);
    82           if (!IntervalInBoundaries(constraint.Interval, res, constraint.InclusiveLowerBound,
     82          if (!constraint.Interval.Contains(res, constraint.InclusiveLowerBound,
    8383            constraint.InclusiveUpperBound)) {
    8484            return 0;
     
    8686        }
    8787      }
     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
    8899
    89100
     
    102113    }
    103114
    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) {
    105117      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
    106118      EstimationLimitsParameter.ExecutionContext = context;
    107119      ApplyLinearScalingParameter.ExecutionContext = context;
    108120
    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);
    110124
    111125      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
     
    115129      return r2;
    116130    }
    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     }
    151131  }
    152132}
  • branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/SymbolicRegressionConstraintAnalyzer.cs

    r16590 r16592  
    2323
    2424using System;
     25using System.Collections.Generic;
    2526using System.Linq;
    2627using HeuristicLab.Analysis;
     
    3637namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
    3738  [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";
    4141
     42    private const string ProblemDataParameterName = "ProblemData";
     43    private const string SymbolicDataAnalysisTreeInterpreterParameterName = "SymbolicDataAnalysisTreeInterpreter";
    4244
    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; }
    4557    }
    4658
     59    public ISymbolicDataAnalysisExpressionTreeInterpreter Interpreter {
     60      get { return SymbolicDataAnalysisTreeInterpreterParameter.ActualValue; }
     61    }
     62    #endregion
    4763
    4864    public virtual bool EnabledByDefault {
     
    6379
    6480    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."));
    6883    }
    6984
    7085    [StorableHook(HookType.AfterDeserialization)]
    7186    private void AfterDeserialization() {
    72       // BackwardsCompatibility3.3
    7387    }
    7488
     
    7791      IntervalConstraintsParser parser = new IntervalConstraintsParser();
    7892      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.",
    8296          newDataTable));
    8397      }
    8498
    85       var dataTable = (DataTable)results[RegressionSolutionQualitiesResultName].Value;
     99      var dataTable = (DataTable)results[ConstraintViolationsResultName].Value;
     100      var problemData = RegressionProblemData;
    86101
    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>();
    93103
    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));
    99112          }
    100113        }
     114      }
    101115
     116      foreach (var constraint in constraints) {
     117        constraintViolations.Add(constraint.Expression, 0);
     118      }
     119
     120      foreach (var tree in this.SymbolicExpressionTree) {
    102121        foreach (var constraint in constraints) {
    103122          if (constraint.Variable != null && !variableRanges.ContainsKey(constraint.Variable))
     
    105124              $"The given variable {constraint.Variable} in the constraint does not exists in the model.",
    106125              nameof(IntervalConstraintsParser));
    107           var numberOfViolations = dataTable.Rows[constraint.Derivaiton].Values.Count > 0
    108             ? dataTable.Rows[constraint.Derivaiton].Values.Last()
    109             : 0;
    110126          if (!constraint.IsDerivation) {
    111             var res = intervalInterpreter.GetSymbolicExressionTreeInterval(solution.Model.SymbolicExpressionTree,
     127            var res = intervalInterpreter.GetSymbolicExressionTreeInterval(tree,
    112128              variableRanges);
    113             if (!IntervalInBoundaries(constraint.Interval, res, constraint.InclusiveLowerBound,
     129            if (!constraint.Interval.Contains(res, constraint.InclusiveLowerBound,
    114130              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]++;
    118132            }
    119133          } else {
    120             var tree = solution.Model.SymbolicExpressionTree;
     134            var dTree = tree;
    121135            for (var i = 0; i < constraint.NumberOfDerivation; ++i) {
    122               tree = DerivativeCalculator.Derive(tree, constraint.Variable);
     136              dTree = DerivativeCalculator.Derive(dTree, constraint.Variable);
    123137            }
    124138
    125             var res = intervalInterpreter.GetSymbolicExressionTreeInterval(solution.Model.SymbolicExpressionTree,
     139            var res = intervalInterpreter.GetSymbolicExressionTreeInterval(dTree,
    126140              variableRanges);
    127             if (!IntervalInBoundaries(constraint.Interval, res, constraint.InclusiveLowerBound,
     141            if (!constraint.Interval.Contains(res, constraint.InclusiveLowerBound,
    128142              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]++;
    132144            }
    133145          }
    134146        }
     147      }
    135148
     149      foreach (var kvp in constraintViolations) {
     150        dataTable.Rows[kvp.Key].Values.Add(kvp.Value);
    136151      }
    137152      return base.Apply();
    138153    }
    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     }
    176154  }
    177155}
  • branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/SymbolicRegressionSingleObjectiveProblem.cs

    r15583 r16592  
    113113      Operators.Add(new SymbolicRegressionSolutionsAnalyzer());
    114114      Operators.Add(new SymbolicExpressionTreePhenotypicSimilarityCalculator());
     115      Operators.Add(new SymbolicRegressionConstraintAnalyzer());
    115116      Operators.Add(new SymbolicRegressionPhenotypicDiversityAnalyzer(Operators.OfType<SymbolicExpressionTreePhenotypicSimilarityCalculator>()) { DiversityResultName = "Phenotypic Diversity" });
    116117      ParameterizeOperators();
  • branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Interval.cs

    r16548 r16592  
    4343    public bool Contains(double value) {
    4444      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;
    4582    }
    4683
  • branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Parser/IntervalConstraint.cs

    r16590 r16592  
    77namespace HeuristicLab.Problems.DataAnalysis {
    88  public class IntervalConstraint {
    9     public string Derivaiton { get; set; }
     9    public string Expression { get; set; }
    1010    public string Definition { get; set; }
    1111    public Interval Interval { get; set; }
  • branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Parser/IntervalConstraintsParser.cs

    r16590 r16592  
    4040            intervalConstraint.IsDerivation = false;
    4141          }
    42           intervalConstraint.Derivaiton = matches[i].Groups[0].Value;
     42          intervalConstraint.Expression = matches[i].Groups[0].Value;
    4343          intervalConstraint.InclusiveLowerBound = (matches[i].Groups[2].Value == "[");
    4444          intervalConstraint.InclusiveUpperBound = (matches[i].Groups[6].Value == "]");
  • branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Regression/RegressionProblemData.cs

    r16586 r16592  
    105105    }
    106106
    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;
    110110
    111111    public IFixedValueParameter<TextValue> IntervalConstraintsParameter => (IFixedValueParameter<TextValue>) Parameters[IntervalConstraintsParameterName];
     112    public TextValue IntervalConstraints => IntervalConstraintsParameter.Value;
    112113
    113114    public string TargetVariable {
Note: See TracChangeset for help on using the changeset viewer.