Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionSingleObjectiveConstraintConstOptEvaluator.cs @ 16781

Last change on this file since 16781 was 16781, checked in by chaider, 5 years ago

#2971

  • Added isChecked validation for SymbolicRegressionSingleObjectiveConstraintConstOptEvaluator
File size: 6.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HEAL.Attic;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
30
31namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
32  [Item("Constraint ConstOpt Evaluator", "")]
33  [StorableType("4170FD8B-DDD9-43B7-8BF8-F9C7290D4D1C")]
34  public class SymbolicRegressionSingleObjectiveConstraintConstOptEvaluator : SymbolicRegressionSingleObjectiveEvaluator {
35    [StorableConstructor]
36    protected SymbolicRegressionSingleObjectiveConstraintConstOptEvaluator(StorableConstructorFlag _) : base(_) { }
37    protected SymbolicRegressionSingleObjectiveConstraintConstOptEvaluator(SymbolicRegressionSingleObjectiveConstraintConstOptEvaluator original, Cloner cloner)
38      : base(original, cloner) {
39    }
40    public override IDeepCloneable Clone(Cloner cloner) {
41      return new SymbolicRegressionSingleObjectiveConstraintConstOptEvaluator(this, cloner);
42    }
43
44    public SymbolicRegressionSingleObjectiveConstraintConstOptEvaluator() : base() { }
45
46    public override bool Maximization { get { return true; } }
47
48    public override IOperation InstrumentedApply() {
49      var solution = SymbolicExpressionTreeParameter.ActualValue;
50      IEnumerable<int> rows = GenerateRowsToEvaluate();
51
52      double quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, rows, ApplyLinearScalingParameter.ActualValue.Value);
53      QualityParameter.ActualValue = new DoubleValue(quality);
54      return base.InstrumentedApply();
55    }
56
57    public static double Calculate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree tree, double lowerEstimationLimit, double upperEstimationLimit, IRegressionProblemData problemData, IEnumerable<int> rows, bool applyLinearScaling) {
58      IEnumerable<double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(tree, problemData.Dataset, rows);
59      IEnumerable<double> targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
60      OnlineCalculatorError errorState = OnlineCalculatorError.None;
61
62      //var constraints = IntervalConstraintsParser.Parse(((RegressionProblemData)problemData).IntervalConstraintsParameter.Value.Value);
63      var constraints = ((RegressionProblemData)problemData).IntervalConstraintsParameter.Value.Constraints.Where(x => x.IsChecked);
64      var intervalInterpreter = new IntervalInterpreter();
65      var variableRanges = ((RegressionProblemData)problemData).VariableRangesParameter.Value.VariableIntervals;
66
67
68      var r2 = SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(interpreter, tree, problemData, problemData.TrainingIndices, applyLinearScaling, 10);
69      if (HasConstraintVioluations(constraints, intervalInterpreter, variableRanges, tree)) {
70        r2 = 0;
71      }
72      return r2;
73    }
74
75
76    public override double Evaluate(IExecutionContext context, ISymbolicExpressionTree tree,
77      IRegressionProblemData problemData, IEnumerable<int> rows) {
78      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
79      EstimationLimitsParameter.ExecutionContext = context;
80      ApplyLinearScalingParameter.ExecutionContext = context;
81
82      double r2 = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree,
83        EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows,
84        ApplyLinearScalingParameter.ActualValue.Value);
85
86      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
87      EstimationLimitsParameter.ExecutionContext = null;
88      ApplyLinearScalingParameter.ExecutionContext = null;
89
90      return r2;
91    }
92
93    private static bool HasConstraintVioluations(IEnumerable<IntervalConstraint> constraints, IntervalInterpreter intervalInterpreter,
94      Dictionary<string, Interval> variableRanges, ISymbolicExpressionTree solution) {
95      foreach (var constraint in constraints) {
96        if (constraint.Variable != null && !variableRanges.ContainsKey(constraint.Variable))
97          throw new ArgumentException($"The given variable {constraint.Variable} in the constraint does not exists in the model.", nameof(IntervalConstraintsParser));
98        if (!constraint.IsDerivation) {
99          var res = intervalInterpreter.GetSymbolicExpressionTreeInterval(solution, variableRanges);
100          if (!constraint.Interval.Contains(res, constraint.InclusiveLowerBound,
101            constraint.InclusiveUpperBound)) {
102            return true;
103          }
104        } else {
105          var tree = solution;
106          for (var i = 0; i < constraint.NumberOfDerivation; ++i) {
107            tree = DerivativeCalculator.Derive(tree, constraint.Variable);
108          }
109          var res = intervalInterpreter.GetSymbolicExpressionTreeInterval(tree, variableRanges);
110          if (!constraint.Interval.Contains(res, constraint.InclusiveLowerBound,
111            constraint.InclusiveUpperBound)) {
112            return true;
113          }
114        }
115      }
116      return false;
117    }
118  }
119}
Note: See TracBrowser for help on using the repository browser.