Ignore:
Timestamp:
04/17/19 14:37:55 (12 months ago)
Author:
chaider
Message:

#2971 Several chanages regarding review comments

File:
1 edited

Legend:

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

    r16780 r16800  
    3737  public class SymbolicRegressionConstraintAnalyzer : SymbolicDataAnalysisAnalyzer, ISymbolicDataAnalysisInterpreterOperator, ISymbolicExpressionTreeAnalyzer {
    3838    private const string ConstraintViolationsResultName = "Constraint Violations";
    39 
    4039    private const string ProblemDataParameterName = "ProblemData";
    4140    private const string SymbolicDataAnalysisTreeInterpreterParameterName = "SymbolicDataAnalysisTreeInterpreter";
    4241
    4342    #region parameter properties
     43
    4444    public ILookupParameter<RegressionProblemData> RegressionProblemDataParameter {
    45       get { return (ILookupParameter<RegressionProblemData>)Parameters[ProblemDataParameterName]; }
     45      get { return (ILookupParameter<RegressionProblemData>) Parameters[ProblemDataParameterName]; }
    4646    }
     47
    4748    public ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> SymbolicDataAnalysisTreeInterpreterParameter {
    48       get { return (ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[SymbolicDataAnalysisTreeInterpreterParameterName]; }
     49      get { return (ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>) Parameters[SymbolicDataAnalysisTreeInterpreterParameterName]; }
    4950    }
     51
    5052    #endregion
     53
    5154    #region properties
    5255
     
    5861      get { return SymbolicDataAnalysisTreeInterpreterParameter.ActualValue; }
    5962    }
     63
    6064    #endregion
    6165
    62     public virtual bool EnabledByDefault {
    63       get { return false; }
    64     }
     66    public override bool EnabledByDefault => false;
    6567
    6668    [StorableConstructor]
     
    6870    }
    6971
    70     protected SymbolicRegressionConstraintAnalyzer(SymbolicRegressionConstraintAnalyzer original, Cloner cloner)
    71       : base(original, cloner) {
     72    protected SymbolicRegressionConstraintAnalyzer(SymbolicRegressionConstraintAnalyzer original, Cloner cloner) : base(original, cloner) {
    7273    }
    7374
     
    8586    }
    8687
    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);
    9090      var constraintViolations = new Dictionary<IntervalConstraint, bool>();
    91       var variableRanges = ((RegressionProblemData)problemData).VariableRanges.VariableIntervals;
    92 
     91      var variableRanges = problemData.VariableRanges.VariableIntervals;
    9392      foreach (var constraint in constraints) {
    9493        if (!constraintViolations.ContainsKey(constraint)) {
     
    10099    }
    101100
    102     public static Dictionary<IntervalConstraint, bool> CheckConstraints(ISymbolicRegressionModel model,
    103       IRegressionProblemData problemData) {
     101    public static Dictionary<IntervalConstraint, bool> CheckConstraints(ISymbolicRegressionModel model, IRegressionProblemData problemData) {
    104102      return CheckConstraints(model.SymbolicExpressionTree, problemData);
    105103    }
    106104
    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) {
    109106      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;
    129121        }
    130122      }
     123
    131124      return true;
    132125    }
    133126
    134     static bool CheckConstraint(ISymbolicRegressionModel model, Dictionary<string, Interval> variableRanges, IntervalConstraint constraint) {
     127    static bool CheckConstraint(ISymbolicRegressionModel model, IDictionary<string, Interval> variableRanges, IntervalConstraint constraint) {
    135128      return CheckConstraint(model.SymbolicExpressionTree, variableRanges, constraint);
    136129    }
     
    140133      if (!results.ContainsKey(ConstraintViolationsResultName)) {
    141134        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));
    144136      }
    145       var dataTable = (DataTable)results[ConstraintViolationsResultName].Value;
     137
     138      var dataTable = (DataTable) results[ConstraintViolationsResultName].Value;
    146139      var problemData = RegressionProblemData;
    147140      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);
    151142      if (dataTable.Rows.Count == 0) {
    152143        foreach (var constraint in constraints) {
     
    163154      foreach (var tree in this.SymbolicExpressionTree) {
    164155        var checkedConstraints = CheckConstraints(tree, problemData);
    165 
    166156        foreach (var kvp in checkedConstraints) {
    167           if (!kvp.Value)
    168             constraintViolations[kvp.Key.Expression]++;
     157          if (!kvp.Value) constraintViolations[kvp.Key.Expression]++;
    169158        }
    170159      }
     
    173162        dataTable.Rows[kvp.Key].Values.Add(kvp.Value);
    174163      }
     164
    175165      return base.Apply();
    176166    }
Note: See TracChangeset for help on using the changeset viewer.