Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3076_IA_evaluators_analyzers/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/MultiObjective/SymbolicRegressionMultiObjectiveMultiSoftConstraintEvaluator.cs @ 17722

Last change on this file since 17722 was 17722, checked in by dpiringe, 4 years ago

#3076

  • added parameter support for dimensions
  • added region calculation in SymbolicRegressionConstraintAnalyzer
File size: 10.8 KB
Line 
1#region License Information
2
3/* HeuristicLab
4 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
5 *
6 * This file is part of HeuristicLab.
7 *
8 * HeuristicLab is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * HeuristicLab is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#endregion
23
24using System;
25using System.Collections.Generic;
26using System.Linq;
27using HEAL.Attic;
28using HeuristicLab.Common;
29using HeuristicLab.Core;
30using HeuristicLab.Data;
31using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
32using HeuristicLab.Parameters;
33
34namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression.MultiObjective {
35  [Item("Multi Soft Constraints Evaluator",
36    "Calculates the Person R² and the constraints violations of a symbolic regression solution.")]
37  [StorableType("8E9D76B7-ED9C-43E7-9898-01FBD3633880")]
38  public class
39    SymbolicRegressionMultiObjectiveMultiSoftConstraintEvaluator : SymbolicRegressionMultiObjectiveSplittingEvaluator {
40    public const string DimensionsParameterName = "Dimensions";
41
42    public IFixedValueParameter<IntValue> DimensionsParameter => (IFixedValueParameter<IntValue>)Parameters[DimensionsParameterName];
43
44    #region Constructors
45    public SymbolicRegressionMultiObjectiveMultiSoftConstraintEvaluator() {
46      Parameters.Add(new FixedValueParameter<IntValue>(DimensionsParameterName, new IntValue(2)));
47    }
48
49    [StorableConstructor]
50    protected SymbolicRegressionMultiObjectiveMultiSoftConstraintEvaluator(StorableConstructorFlag _) : base(_) { }
51
52    protected SymbolicRegressionMultiObjectiveMultiSoftConstraintEvaluator(
53      SymbolicRegressionMultiObjectiveMultiSoftConstraintEvaluator original, Cloner cloner) : base(original, cloner) { }
54
55    #endregion
56
57    [StorableHook(HookType.AfterDeserialization)]
58    private void AfterDeserialization() {
59      if(!Parameters.ContainsKey(DimensionsParameterName))
60        Parameters.Add(new FixedValueParameter<IntValue>(DimensionsParameterName, new IntValue(2)));
61    }
62
63    public override IDeepCloneable Clone(Cloner cloner) {
64      return new SymbolicRegressionMultiObjectiveMultiSoftConstraintEvaluator(this, cloner);
65    }
66
67
68    public override IOperation InstrumentedApply() {
69      var rows                  = GenerateRowsToEvaluate();
70      var solution              = SymbolicExpressionTreeParameter.ActualValue;
71      var problemData           = ProblemDataParameter.ActualValue;
72      var interpreter           = SymbolicDataAnalysisTreeInterpreterParameter.ActualValue;
73      var estimationLimits      = EstimationLimitsParameter.ActualValue;
74      var minIntervalWidth      = MinSplittingWidth;
75      var maxIntervalSplitDepth = MaxSplittingDepth;
76      var applyLinearScaling  = ApplyLinearScalingParameter.ActualValue.Value;
77
78      if (applyLinearScaling) {
79        //Check for interval arithmetic grammar
80        //remove scaling nodes for linear scaling evaluation
81        var                    rootNode  = new ProgramRootSymbol().CreateTreeNode();
82        var                    startNode = new StartSymbol().CreateTreeNode();
83        SymbolicExpressionTree newTree   = null;
84        foreach (var node in solution.IterateNodesPrefix())
85          if (node.Symbol.Name == "Scaling") {
86            for (var i = 0; i < node.SubtreeCount; ++i) startNode.AddSubtree(node.GetSubtree(i));
87            rootNode.AddSubtree(startNode);
88            newTree = new SymbolicExpressionTree(rootNode);
89            break;
90          }
91
92        //calculate alpha and beta for scaling
93        var estimatedValues = interpreter.GetSymbolicExpressionTreeValues(newTree, problemData.Dataset, rows);
94        var targetValues    = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
95        OnlineLinearScalingParameterCalculator.Calculate(estimatedValues, targetValues, out var alpha, out var beta,
96          out var errorState);
97        //Set alpha and beta to the scaling nodes from ia grammar
98        foreach (var node in solution.IterateNodesPrefix())
99          if (node.Symbol.Name == "Offset") {
100            node.RemoveSubtree(1);
101            var alphaNode = new ConstantTreeNode(new Constant()) {Value = alpha};
102            node.AddSubtree(alphaNode);
103          } else if (node.Symbol.Name == "Scaling") {
104            node.RemoveSubtree(1);
105            var betaNode = new ConstantTreeNode(new Constant()) {Value = beta};
106            node.AddSubtree(betaNode);
107          }
108      }
109
110      if (UseConstantOptimization)
111        SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(interpreter, solution, problemData, rows,
112          false,
113          ConstantOptimizationIterations,
114          ConstantOptimizationUpdateVariableWeights,
115          estimationLimits.Lower,
116          estimationLimits.Upper);
117
118      var qualities = Calculate(interpreter, solution, estimationLimits.Lower, estimationLimits.Upper, problemData,
119        rows, DecimalPlaces, minIntervalWidth, maxIntervalSplitDepth);
120      QualitiesParameter.ActualValue = new DoubleArray(qualities);
121      return base.InstrumentedApply();
122    }
123
124    public override double[] Evaluate(IExecutionContext context, ISymbolicExpressionTree tree,
125                                      IRegressionProblemData problemData,
126                                      IEnumerable<int> rows) {
127      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
128      EstimationLimitsParameter.ExecutionContext                    = context;
129      ApplyLinearScalingParameter.ExecutionContext                  = context;
130
131      var quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree,
132        EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper,
133        problemData, rows, DecimalPlaces, MinSplittingWidth,
134        MaxSplittingDepth);
135
136      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
137      EstimationLimitsParameter.ExecutionContext                    = null;
138      ApplyLinearScalingParameter.ExecutionContext                  = null;
139
140      return quality;
141    }
142
143
144    public static double[] Calculate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
145                                     ISymbolicExpressionTree solution, double lowerEstimationLimit,
146                                     double upperEstimationLimit,
147                                     IRegressionProblemData problemData, IEnumerable<int> rows,
148                                     int decimalPlaces, double minIntervalSplitWidth, int maxIntervalSplitDepth) {
149      OnlineCalculatorError errorState;
150      var estimatedValues =
151        interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, rows);
152      var targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
153
154      double nmse;
155
156      var boundedEstimatedValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit);
157      nmse = OnlineNormalizedMeanSquaredErrorCalculator.Calculate(targetValues, boundedEstimatedValues, out errorState);
158      if (errorState != OnlineCalculatorError.None) nmse = 1.0;
159
160      if (nmse > 1)
161        nmse = 1.0;
162
163      var constraints    = problemData.IntervalConstraints.Constraints.Where(c => c.Enabled);
164      var variableRanges = problemData.VariableRanges.GetReadonlyDictionary();
165
166      var objectives          = new List<double> {nmse};
167      var intervalInterpreter = new IntervalInterpreter();
168      /*{MinIntervalSplitWidth = minIntervalSplitWidth, MaxIntervalSplitDepth = maxIntervalSplitDetph};*/
169
170      var constraintObjectives = new List<double>();
171      foreach (var c in constraints) {
172        var penalty = ConstraintExceeded(c, intervalInterpreter, variableRanges,
173          solution /*, problemData.IntervalSplitting*/);
174        var maxP = 0.1;
175
176        if (double.IsNaN(penalty) || double.IsInfinity(penalty) || penalty > maxP)
177          penalty = maxP;
178
179        constraintObjectives.Add(penalty);
180      }
181
182      objectives.AddRange(constraintObjectives);
183
184      return objectives.ToArray();
185    }
186
187    public static double ConstraintExceeded(IntervalConstraint constraint, IntervalInterpreter intervalInterpreter,
188                                            IReadOnlyDictionary<string, Interval> variableRanges,
189                                            ISymbolicExpressionTree solution /*, bool splitting*/) {
190      if (constraint.Variable != null && !variableRanges.ContainsKey(constraint.Variable))
191        throw new ArgumentException(
192          $"The given variable {constraint.Variable} in the constraint does not exists in the model.",
193          nameof(IntervalConstraintsParser));
194      Interval resultInterval;
195      if (!constraint.IsDerivative) {
196        resultInterval =
197          intervalInterpreter.GetSymbolicExpressionTreeInterval(solution, variableRanges /*, splitting:splitting*/);
198      }
199      else {
200        var tree = solution;
201        for (var i = 0; i < constraint.NumberOfDerivations; ++i)
202          tree = DerivativeCalculator.Derive(tree, constraint.Variable);
203
204        resultInterval =
205          intervalInterpreter.GetSymbolicExpressionTreeInterval(tree, variableRanges /*, splitting: splitting*/);
206      }
207     
208      //Calculate soft-constraints for intervals
209      if (constraint.Interval.Contains(resultInterval)) return 0;
210      var pLower = 0.0;
211      var pUpper = 0.0;
212      if (constraint.Interval.Contains(resultInterval.LowerBound))
213        pLower = 0;
214      else
215        pLower = constraint.Interval.LowerBound - resultInterval.LowerBound;
216
217      if (constraint.Interval.Contains(resultInterval.UpperBound))
218        pUpper = 0;
219      else
220        pUpper = resultInterval.UpperBound - constraint.Interval.UpperBound;
221      var penalty = Math.Abs(pLower) + Math.Abs(pUpper);
222
223      return penalty;
224    }
225
226    /*
227     * First objective is to maximize the Pearson R² value
228     * All following objectives have to be minimized ==> Constraints
229     */
230    public override IEnumerable<bool> Maximization {
231      get {
232        var objectives = new List<bool> {false};          //First NMSE ==> min
233        objectives.AddRange(Enumerable.Repeat(false, DimensionsParameter.Value.Value)); //Constraints ==> min
234       
235        return objectives;
236      }
237    }
238  }
239}
Note: See TracBrowser for help on using the repository browser.