source: branches/3138_Shape_Constraints_Transformations/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/NMSESingleObjectiveConstraintsEvaluator.cs @ 18181

Last change on this file since 18181 was 18181, checked in by dpiringe, 5 months ago

#3138

  • added the class ExtendedConstraint which represents an expression (the transformation of the found model) and the corresponding shape constraints
  • added a list of ExtendedConstraint in NMSESingleObjectiveConstraintsEvaluator to make future tests/experiments (exists only temporarily in NMSESingleObjectiveConstraintsEvaluator, will be moved later)
File size: 11.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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;
30using HeuristicLab.Parameters;
31
32namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
33  [Item("NMSE Evaluator with shape-constraints (single-objective)", "Calculates NMSE of a symbolic regression solution and checks constraints. The fitness is a combination of NMSE and constraint violations.")]
34  [StorableType("27473973-DD8D-4375-997D-942E2280AE8E")]
35  public class NMSESingleObjectiveConstraintsEvaluator : SymbolicRegressionSingleObjectiveEvaluator {
36    #region Parameter/Properties
37
38    private const string OptimizeParametersParameterName = "OptimizeParameters";
39    private const string ParameterOptimizationIterationsParameterName = "ParameterOptimizationIterations";
40    private const string UseSoftConstraintsParameterName = "UseSoftConstraintsEvaluation";
41    private const string BoundsEstimatorParameterName = "BoundsEstimator";
42    private const string PenaltyFactorParameterName = "PenaltyFactor";
43    private const string ExtendedConstraintsParameterName = "ExtendedConstraints";
44
45
46    public IFixedValueParameter<BoolValue> OptimizerParametersParameter =>
47      (IFixedValueParameter<BoolValue>)Parameters[OptimizeParametersParameterName];
48
49    public IFixedValueParameter<IntValue> ParameterOptimizationIterationsParameter =>
50      (IFixedValueParameter<IntValue>)Parameters[ParameterOptimizationIterationsParameterName];
51
52    public IFixedValueParameter<BoolValue> UseSoftConstraintsParameter =>
53      (IFixedValueParameter<BoolValue>)Parameters[UseSoftConstraintsParameterName];
54
55    public IValueParameter<IBoundsEstimator> BoundsEstimatorParameter =>
56      (IValueParameter<IBoundsEstimator>)Parameters[BoundsEstimatorParameterName];
57    public IFixedValueParameter<DoubleValue> PenaltyFactorParameter =>
58      (IFixedValueParameter<DoubleValue>)Parameters[PenaltyFactorParameterName];
59
60    public IFixedValueParameter<IItemList<ExtendedConstraint>> ExtendedConstraintsParameter =>
61      (IFixedValueParameter<IItemList<ExtendedConstraint>>)Parameters[ExtendedConstraintsParameterName];
62
63
64    public bool OptimizeParameters {
65      get => OptimizerParametersParameter.Value.Value;
66      set => OptimizerParametersParameter.Value.Value = value;
67    }
68
69    public int ParameterOptimizationIterations {
70      get => ParameterOptimizationIterationsParameter.Value.Value;
71      set => ParameterOptimizationIterationsParameter.Value.Value = value;
72    }
73
74    public bool UseSoftConstraints {
75      get => UseSoftConstraintsParameter.Value.Value;
76      set => UseSoftConstraintsParameter.Value.Value = value;
77    }
78
79    public IBoundsEstimator BoundsEstimator {
80      get => BoundsEstimatorParameter.Value;
81      set => BoundsEstimatorParameter.Value = value;
82    }
83
84    public double PenalityFactor {
85      get => PenaltyFactorParameter.Value.Value;
86      set => PenaltyFactorParameter.Value.Value = value;
87    }
88
89    public IEnumerable<ExtendedConstraint> ExtendedConstraints {
90      get => ExtendedConstraintsParameter.Value;
91    }
92     
93
94
95    public override bool Maximization => false; // NMSE is minimized
96
97    #endregion
98
99    #region Constructors/Cloning
100
101    [StorableConstructor]
102    protected NMSESingleObjectiveConstraintsEvaluator(StorableConstructorFlag _) : base(_) { }
103
104    protected NMSESingleObjectiveConstraintsEvaluator(
105      NMSESingleObjectiveConstraintsEvaluator original, Cloner cloner) : base(original, cloner) { }
106
107    public NMSESingleObjectiveConstraintsEvaluator() {
108      Parameters.Add(new FixedValueParameter<BoolValue>(OptimizeParametersParameterName,
109        "Define whether optimization of parameters is active or not (default: false).", new BoolValue(false)));
110      Parameters.Add(new FixedValueParameter<IntValue>(ParameterOptimizationIterationsParameterName,
111        "Define how many parameter optimization steps should be performed (default: 10).", new IntValue(10)));
112      Parameters.Add(new FixedValueParameter<BoolValue>(UseSoftConstraintsParameterName,
113        "Define whether the constraints are penalized by soft or hard constraints (default: false).", new BoolValue(false)));
114      Parameters.Add(new ValueParameter<IBoundsEstimator>(BoundsEstimatorParameterName,
115        "The estimator which is used to estimate output ranges of models (default: interval arithmetic).", new IntervalArithBoundsEstimator()));
116      Parameters.Add(new FixedValueParameter<DoubleValue>(PenaltyFactorParameterName,
117        "Punishment factor for constraint violations for soft constraint handling (fitness = NMSE + penaltyFactor * avg(violations)) (default: 1.0)", new DoubleValue(1.0)));
118      Parameters.Add(new FixedValueParameter<ItemList<ExtendedConstraint>>(ExtendedConstraintsParameterName, "", new ItemList<ExtendedConstraint>()));
119    }
120
121    [StorableHook(HookType.AfterDeserialization)]
122    private void AfterDeserialization() { }
123
124    public override IDeepCloneable Clone(Cloner cloner) {
125      return new NMSESingleObjectiveConstraintsEvaluator(this, cloner);
126    }
127
128    #endregion
129
130    public override IOperation InstrumentedApply() {
131      var rows = GenerateRowsToEvaluate();
132      var tree = SymbolicExpressionTreeParameter.ActualValue;
133      var problemData = ProblemDataParameter.ActualValue;
134      var interpreter = SymbolicDataAnalysisTreeInterpreterParameter.ActualValue;
135      var estimationLimits = EstimationLimitsParameter.ActualValue;
136      var applyLinearScaling = ApplyLinearScalingParameter.ActualValue.Value;
137
138      if (OptimizeParameters) {
139        SymbolicRegressionParameterOptimizationEvaluator.OptimizeParameters(interpreter, tree, problemData, rows,
140          false, ParameterOptimizationIterations, true,
141          estimationLimits.Lower, estimationLimits.Upper);
142      } else {
143        if (applyLinearScaling) {
144          var rootNode = new ProgramRootSymbol().CreateTreeNode();
145          var startNode = new StartSymbol().CreateTreeNode();
146          var offset = tree.Root.GetSubtree(0) //Start
147                                .GetSubtree(0); //Offset
148          var scaling = offset.GetSubtree(0);
149
150          //Check if tree contains offset and scaling nodes
151          if (!(offset.Symbol is Addition) || !(scaling.Symbol is Multiplication))
152            throw new ArgumentException($"{ItemName} can only be used with LinearScalingGrammar.");
153
154          var t = (ISymbolicExpressionTreeNode)scaling.GetSubtree(0).Clone();
155          rootNode.AddSubtree(startNode);
156          startNode.AddSubtree(t);
157          var newTree = new SymbolicExpressionTree(rootNode);
158
159          //calculate alpha and beta for scaling
160          var estimatedValues = interpreter.GetSymbolicExpressionTreeValues(newTree, problemData.Dataset, rows);
161
162          var targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
163          OnlineLinearScalingParameterCalculator.Calculate(estimatedValues, targetValues, out var alpha, out var beta,
164            out var errorState);
165
166          if (errorState == OnlineCalculatorError.None) {
167            //Set alpha and beta to the scaling nodes from ia grammar
168            var offsetParameter = offset.GetSubtree(1) as NumberTreeNode;
169            offsetParameter.Value = alpha;
170            var scalingParameter = scaling.GetSubtree(1) as NumberTreeNode;
171            scalingParameter.Value = beta;
172          }
173        } // else: alpha and beta are evolved
174      }
175
176      var quality = Calculate(interpreter, tree, estimationLimits.Lower, estimationLimits.Upper, problemData, rows,
177        BoundsEstimator, UseSoftConstraints, PenalityFactor, ExtendedConstraints);
178      QualityParameter.ActualValue = new DoubleValue(quality);
179
180      return base.InstrumentedApply();
181    }
182
183    public static double Calculate(
184      ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
185      ISymbolicExpressionTree tree,
186      double lowerEstimationLimit, double upperEstimationLimit,
187      IRegressionProblemData problemData, IEnumerable<int> rows,
188      IBoundsEstimator estimator,
189      bool useSoftConstraints = false, double penaltyFactor = 1.0,
190      IEnumerable<ExtendedConstraint> extendedConstraints = null) {
191
192      var estimatedValues = interpreter.GetSymbolicExpressionTreeValues(tree, problemData.Dataset, rows);
193      var targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
194      var constraints = Enumerable.Empty<ShapeConstraint>();
195      if (problemData is ShapeConstrainedRegressionProblemData scProbData) {
196        constraints = scProbData.ShapeConstraints.EnabledConstraints;
197      }
198      var intervalCollection = problemData.VariableRanges;
199
200      var boundedEstimatedValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit);
201      var nmse = OnlineNormalizedMeanSquaredErrorCalculator.Calculate(targetValues, boundedEstimatedValues,
202        out var errorState);
203
204      if (errorState != OnlineCalculatorError.None) {
205        return 1.0;
206      }
207
208      var constraintViolations = IntervalUtil.GetConstraintViolations(constraints, estimator, intervalCollection, tree);
209
210      if (constraintViolations.Any(x => double.IsNaN(x) || double.IsInfinity(x))) {
211        return 1.0;
212      }
213
214      if (useSoftConstraints) {
215        if (penaltyFactor < 0.0)
216          throw new ArgumentException("The parameter has to be >= 0.0.", nameof(penaltyFactor));
217
218        var weightedViolationsAvg = constraints
219          .Zip(constraintViolations, (c, v) => c.Weight * v)
220          .Average();
221
222        return Math.Min(nmse, 1.0) + penaltyFactor * weightedViolationsAvg;
223      } else if (constraintViolations.Any(x => x > 0.0)) {
224        return 1.0;
225      }
226
227      return nmse;
228    }
229
230    public override double Evaluate(
231      IExecutionContext context, ISymbolicExpressionTree tree, IRegressionProblemData problemData,
232      IEnumerable<int> rows) {
233      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
234      EstimationLimitsParameter.ExecutionContext = context;
235      ApplyLinearScalingParameter.ExecutionContext = context;
236
237      var nmse = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree,
238        EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper,
239        problemData, rows, BoundsEstimator, UseSoftConstraints, PenalityFactor, ExtendedConstraints);
240
241      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
242      EstimationLimitsParameter.ExecutionContext = null;
243      ApplyLinearScalingParameter.ExecutionContext = null;
244
245      return nmse;
246    }
247  }
248}
Note: See TracBrowser for help on using the repository browser.