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 @ 16756

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

#2971 Changed Interval Constraints View
-Added View for successfully parsed Constraints
-Save Constraints as IntervalConstraints

File size: 6.0 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 HEAL.Attic;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
29
30namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
31  [Item("Constraint ConstOpt Evaluator", "")]
32  [StorableType("4170FD8B-DDD9-43B7-8BF8-F9C7290D4D1C")]
33  public class SymbolicRegressionSingleObjectiveConstraintConstOptEvaluator : SymbolicRegressionSingleObjectiveEvaluator {
34    [StorableConstructor]
35    protected SymbolicRegressionSingleObjectiveConstraintConstOptEvaluator(StorableConstructorFlag _) : base(_) { }
36    protected SymbolicRegressionSingleObjectiveConstraintConstOptEvaluator(SymbolicRegressionSingleObjectiveConstraintConstOptEvaluator original, Cloner cloner)
37      : base(original, cloner) {
38    }
39    public override IDeepCloneable Clone(Cloner cloner) {
40      return new SymbolicRegressionSingleObjectiveConstraintConstOptEvaluator(this, cloner);
41    }
42
43    public SymbolicRegressionSingleObjectiveConstraintConstOptEvaluator() : base() { }
44
45    public override bool Maximization { get { return true; } }
46
47    public override IOperation InstrumentedApply() {
48      var solution = SymbolicExpressionTreeParameter.ActualValue;
49      IEnumerable<int> rows = GenerateRowsToEvaluate();
50
51      double quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, rows, ApplyLinearScalingParameter.ActualValue.Value);
52      QualityParameter.ActualValue = new DoubleValue(quality);
53      return base.InstrumentedApply();
54    }
55
56    public static double Calculate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree tree, double lowerEstimationLimit, double upperEstimationLimit, IRegressionProblemData problemData, IEnumerable<int> rows, bool applyLinearScaling) {
57      IEnumerable<double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(tree, problemData.Dataset, rows);
58      IEnumerable<double> targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
59      OnlineCalculatorError errorState = OnlineCalculatorError.None;
60
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;
65
66
67      var r2 = SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(interpreter, tree, problemData, problemData.TrainingIndices, applyLinearScaling, 10);
68      if (HasConstraintVioluations(constraints, intervalInterpreter, variableRanges, tree)) {
69        r2 = 0;
70      }
71      return r2;
72    }
73
74
75    public override double Evaluate(IExecutionContext context, ISymbolicExpressionTree tree,
76      IRegressionProblemData problemData, IEnumerable<int> rows) {
77      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
78      EstimationLimitsParameter.ExecutionContext = context;
79      ApplyLinearScalingParameter.ExecutionContext = context;
80
81      double r2 = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree,
82        EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows,
83        ApplyLinearScalingParameter.ActualValue.Value);
84
85      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
86      EstimationLimitsParameter.ExecutionContext = null;
87      ApplyLinearScalingParameter.ExecutionContext = null;
88
89      return r2;
90    }
91
92    private static bool HasConstraintVioluations(IEnumerable<IntervalConstraint> constraints, IntervalInterpreter intervalInterpreter,
93      Dictionary<string, Interval> variableRanges, ISymbolicExpressionTree solution) {
94      foreach (var constraint in constraints) {
95        if (constraint.Variable != null && !variableRanges.ContainsKey(constraint.Variable))
96          throw new ArgumentException($"The given variable {constraint.Variable} in the constraint does not exists in the model.", nameof(IntervalConstraintsParser));
97        if (!constraint.IsDerivation) {
98          var res = intervalInterpreter.GetSymbolicExpressionTreeInterval(solution, variableRanges);
99          if (!constraint.Interval.Contains(res, constraint.InclusiveLowerBound,
100            constraint.InclusiveUpperBound)) {
101            return true;
102          }
103        } else {
104          var tree = solution;
105          for (var i = 0; i < constraint.NumberOfDerivation; ++i) {
106            tree = DerivativeCalculator.Derive(tree, constraint.Variable);
107          }
108          var res = intervalInterpreter.GetSymbolicExpressionTreeInterval(tree, variableRanges);
109          if (!constraint.Interval.Contains(res, constraint.InclusiveLowerBound,
110            constraint.InclusiveUpperBound)) {
111            return true;
112          }
113        }
114      }
115      return false;
116    }
117  }
118}
Note: See TracBrowser for help on using the repository browser.