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

Last change on this file since 17370 was 17370, checked in by chaider, 4 years ago

#2971 Removed the possibility of declaring open and closed intervals. All intervals are closed intervals now.

File size: 5.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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
61      var constraints = problemData.IntervalConstraints.Constraints.Where(x => x.Enabled);
62      var variableRanges = problemData.VariableRanges.GetIntervals();
63
64      var r2 = SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(interpreter, tree, problemData, problemData.TrainingIndices, applyLinearScaling, 10);
65      if (SymbolicRegressionConstraintAnalyzer.ConstraintsSatisfied(constraints, variableRanges, tree)) {
66        r2 = 0;
67      }
68      return r2;
69    }
70
71
72    public override double Evaluate(IExecutionContext context, ISymbolicExpressionTree tree,
73      IRegressionProblemData problemData, IEnumerable<int> rows) {
74      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
75      EstimationLimitsParameter.ExecutionContext = context;
76      ApplyLinearScalingParameter.ExecutionContext = context;
77
78      double r2 = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree,
79        EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows,
80        ApplyLinearScalingParameter.ActualValue.Value);
81
82      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
83      EstimationLimitsParameter.ExecutionContext = null;
84      ApplyLinearScalingParameter.ExecutionContext = null;
85
86      return r2;
87    }
88
89    private static bool HasConstraintVioluations(IEnumerable<IntervalConstraint> constraints, IntervalInterpreter intervalInterpreter,
90      IDictionary<string, Interval> variableRanges, ISymbolicExpressionTree solution) {
91      foreach (var constraint in constraints) {
92        if (constraint.Variable != null && !variableRanges.ContainsKey(constraint.Variable))
93          throw new ArgumentException($"The given variable {constraint.Variable} in the constraint does not exists in the model.", nameof(IntervalConstraintsParser));
94        if (!constraint.IsDerivation) {
95          var res = intervalInterpreter.GetSymbolicExpressionTreeInterval(solution, variableRanges);
96          if (!constraint.Interval.Contains(res)) {
97            return true;
98          }
99        } else {
100          var tree = solution;
101          for (var i = 0; i < constraint.NumberOfDerivation; ++i) {
102            tree = DerivativeCalculator.Derive(tree, constraint.Variable);
103          }
104          var res = intervalInterpreter.GetSymbolicExpressionTreeInterval(tree, variableRanges);
105          if (!constraint.Interval.Contains(res)) {
106            return true;
107          }
108        }
109      }
110      return false;
111    }
112  }
113}
Note: See TracBrowser for help on using the repository browser.