Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/02/19 15:08:43 (6 years ago)
Author:
chaider
Message:

#2971 Added static CheckConstrain and CheckConstraints methods to SymbolicRegressionConstraintAnalyzer. Changed Apply method to use new CheckConstraints method

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/SymbolicRegressionConstraintAnalyzer.cs

    r16713 r16747  
    2424using System;
    2525using System.Collections.Generic;
     26using System.Linq;
    2627using HEAL.Attic;
    2728using HeuristicLab.Analysis;
     
    8485    }
    8586
     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
    86138    public override IOperation Apply() {
    87139      var results = ResultCollectionParameter.ActualValue;
    88       var intervalInterpreter = new IntervalInterpreter();
    89140      if (!results.ContainsKey(ConstraintViolationsResultName)) {
    90141        var newDataTable = new DataTable(ConstraintViolationsResultName);
     
    92143          newDataTable));
    93144      }
    94 
    95145      var dataTable = (DataTable)results[ConstraintViolationsResultName].Value;
    96146      var problemData = RegressionProblemData;
    97 
    98147      var constraintViolations = new Dictionary<string, int>();
    99148
    100149      var constraints = IntervalConstraintsParser.Parse(problemData.IntervalConstraints.Value);
    101       var variableRanges = problemData.VariableRanges.VariableIntervals;
    102150
    103151      if (dataTable.Rows.Count == 0) {
     
    114162
    115163      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);
    133165
    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]++;
    141169        }
    142170      }
Note: See TracChangeset for help on using the changeset viewer.