[17958] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using HEAL.Attic;
|
---|
| 26 | using HeuristicLab.Common;
|
---|
| 27 | using HeuristicLab.Core;
|
---|
| 28 | using HeuristicLab.Data;
|
---|
| 29 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 30 | using HeuristicLab.Parameters;
|
---|
| 31 |
|
---|
| 32 | namespace 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 |
|
---|
| 44 |
|
---|
| 45 | public IFixedValueParameter<BoolValue> OptimizerParametersParameter =>
|
---|
| 46 | (IFixedValueParameter<BoolValue>)Parameters[OptimizeParametersParameterName];
|
---|
| 47 |
|
---|
| 48 | public IFixedValueParameter<IntValue> ConstantOptimizationIterationsParameter =>
|
---|
| 49 | (IFixedValueParameter<IntValue>)Parameters[ParameterOptimizationIterationsParameterName];
|
---|
| 50 |
|
---|
| 51 | public IFixedValueParameter<BoolValue> UseSoftConstraintsParameter =>
|
---|
| 52 | (IFixedValueParameter<BoolValue>)Parameters[UseSoftConstraintsParameterName];
|
---|
| 53 |
|
---|
| 54 | public IValueParameter<IBoundsEstimator> BoundsEstimatorParameter =>
|
---|
| 55 | (IValueParameter<IBoundsEstimator>)Parameters[BoundsEstimatorParameterName];
|
---|
| 56 | public IFixedValueParameter<DoubleValue> PenaltyFactorParameter =>
|
---|
| 57 | (IFixedValueParameter<DoubleValue>)Parameters[PenaltyFactorParameterName];
|
---|
| 58 |
|
---|
| 59 | public bool OptimizeParameters {
|
---|
| 60 | get => OptimizerParametersParameter.Value.Value;
|
---|
| 61 | set => OptimizerParametersParameter.Value.Value = value;
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | public int ConstantOptimizationIterations {
|
---|
| 65 | get => ConstantOptimizationIterationsParameter.Value.Value;
|
---|
| 66 | set => ConstantOptimizationIterationsParameter.Value.Value = value;
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | public bool UseSoftConstraints {
|
---|
| 70 | get => UseSoftConstraintsParameter.Value.Value;
|
---|
| 71 | set => UseSoftConstraintsParameter.Value.Value = value;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | public IBoundsEstimator BoundsEstimator {
|
---|
| 75 | get => BoundsEstimatorParameter.Value;
|
---|
| 76 | set => BoundsEstimatorParameter.Value = value;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | public double PenalityFactor {
|
---|
| 80 | get => PenaltyFactorParameter.Value.Value;
|
---|
| 81 | set => PenaltyFactorParameter.Value.Value = value;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 |
|
---|
| 85 | public override bool Maximization => false; // NMSE is minimized
|
---|
| 86 |
|
---|
| 87 | #endregion
|
---|
| 88 |
|
---|
| 89 | #region Constructors/Cloning
|
---|
| 90 |
|
---|
| 91 | [StorableConstructor]
|
---|
| 92 | protected NMSESingleObjectiveConstraintsEvaluator(StorableConstructorFlag _) : base(_) { }
|
---|
| 93 |
|
---|
| 94 | protected NMSESingleObjectiveConstraintsEvaluator(
|
---|
| 95 | NMSESingleObjectiveConstraintsEvaluator original, Cloner cloner) : base(original, cloner) { }
|
---|
| 96 |
|
---|
| 97 | public NMSESingleObjectiveConstraintsEvaluator() {
|
---|
| 98 | Parameters.Add(new FixedValueParameter<BoolValue>(OptimizeParametersParameterName,
|
---|
| 99 | "Define whether optimization of numeric parameters is active or not (default: false).", new BoolValue(false)));
|
---|
| 100 | Parameters.Add(new FixedValueParameter<IntValue>(ParameterOptimizationIterationsParameterName,
|
---|
| 101 | "Define how many parameter optimization steps should be performed (default: 10).", new IntValue(10)));
|
---|
| 102 | Parameters.Add(new FixedValueParameter<BoolValue>(UseSoftConstraintsParameterName,
|
---|
| 103 | "Define whether the constraints are penalized by soft or hard constraints (default: false).", new BoolValue(false)));
|
---|
| 104 | Parameters.Add(new ValueParameter<IBoundsEstimator>(BoundsEstimatorParameterName,
|
---|
| 105 | "The estimator which is used to estimate output ranges of models (default: interval arithmetic).", new IntervalArithBoundsEstimator()));
|
---|
| 106 | Parameters.Add(new FixedValueParameter<DoubleValue>(PenaltyFactorParameterName,
|
---|
| 107 | "Punishment factor for constraint violations for soft constraint handling (fitness = NMSE + penaltyFactor * avg(violations)) (default: 1.0)", new DoubleValue(1.0)));
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 111 | private void AfterDeserialization() { }
|
---|
| 112 |
|
---|
| 113 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 114 | return new NMSESingleObjectiveConstraintsEvaluator(this, cloner);
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | #endregion
|
---|
| 118 |
|
---|
| 119 | public override IOperation InstrumentedApply() {
|
---|
| 120 | var rows = GenerateRowsToEvaluate();
|
---|
| 121 | var tree = SymbolicExpressionTreeParameter.ActualValue;
|
---|
| 122 | var problemData = ProblemDataParameter.ActualValue;
|
---|
| 123 | var interpreter = SymbolicDataAnalysisTreeInterpreterParameter.ActualValue;
|
---|
| 124 | var estimationLimits = EstimationLimitsParameter.ActualValue;
|
---|
| 125 | var applyLinearScaling = ApplyLinearScalingParameter.ActualValue.Value;
|
---|
| 126 |
|
---|
[18103] | 127 | var quality = Evaluate(tree, problemData, rows, interpreter, applyLinearScaling, estimationLimits.Lower, estimationLimits.Upper);
|
---|
| 128 | QualityParameter.ActualValue = new DoubleValue(quality);
|
---|
[17958] | 129 |
|
---|
[18103] | 130 | return base.InstrumentedApply();
|
---|
| 131 | }
|
---|
[17958] | 132 |
|
---|
[18103] | 133 | private static void CalcLinearScalingTerms(
|
---|
| 134 | ISymbolicExpressionTree tree,
|
---|
| 135 | IRegressionProblemData problemData,
|
---|
| 136 | IEnumerable<int> rows,
|
---|
| 137 | ISymbolicDataAnalysisExpressionTreeInterpreter interpreter) {
|
---|
| 138 | var rootNode = new ProgramRootSymbol().CreateTreeNode();
|
---|
| 139 | var startNode = new StartSymbol().CreateTreeNode();
|
---|
| 140 | var offset = tree.Root.GetSubtree(0) //Start
|
---|
| 141 | .GetSubtree(0); //Offset
|
---|
| 142 | var scaling = offset.GetSubtree(0);
|
---|
[17958] | 143 |
|
---|
[18103] | 144 | //Check if tree contains offset and scaling nodes
|
---|
| 145 | if (!(offset.Symbol is Addition) || !(scaling.Symbol is Multiplication))
|
---|
| 146 | throw new ArgumentException($"Shape Constraints Evaluation can only be used with LinearScalingGrammar.");
|
---|
[17958] | 147 |
|
---|
[18103] | 148 | var t = (ISymbolicExpressionTreeNode)scaling.GetSubtree(0).Clone();
|
---|
| 149 | rootNode.AddSubtree(startNode);
|
---|
| 150 | startNode.AddSubtree(t);
|
---|
| 151 | var newTree = new SymbolicExpressionTree(rootNode);
|
---|
[17958] | 152 |
|
---|
[18103] | 153 | //calculate alpha and beta for scaling
|
---|
| 154 | var estimatedValues = interpreter.GetSymbolicExpressionTreeValues(newTree, problemData.Dataset, rows);
|
---|
| 155 |
|
---|
| 156 | var targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
|
---|
| 157 | OnlineLinearScalingParameterCalculator.Calculate(estimatedValues, targetValues, out var alpha, out var beta,
|
---|
| 158 | out var errorState);
|
---|
| 159 | if (errorState == OnlineCalculatorError.None) {
|
---|
| 160 | //Set alpha and beta to the scaling nodes from ia grammar
|
---|
| 161 | var offsetParameter = offset.GetSubtree(1) as ConstantTreeNode;
|
---|
| 162 | offsetParameter.Value = alpha;
|
---|
| 163 | var scalingParameter = scaling.GetSubtree(1) as ConstantTreeNode;
|
---|
| 164 | scalingParameter.Value = beta;
|
---|
[17958] | 165 | }
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | public static double Calculate(
|
---|
[18103] | 169 | ISymbolicExpressionTree tree,
|
---|
| 170 | IRegressionProblemData problemData, IEnumerable<int> rows,
|
---|
[17958] | 171 | ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
|
---|
| 172 | double lowerEstimationLimit, double upperEstimationLimit,
|
---|
| 173 | IBoundsEstimator estimator,
|
---|
| 174 | bool useSoftConstraints = false, double penaltyFactor = 1.0) {
|
---|
| 175 |
|
---|
| 176 | var constraints = Enumerable.Empty<ShapeConstraint>();
|
---|
[18103] | 177 | if (problemData is ShapeConstrainedRegressionProblemData scProbData)
|
---|
[17958] | 178 | constraints = scProbData.ShapeConstraints.EnabledConstraints;
|
---|
| 179 |
|
---|
[18103] | 180 | var estimatedValues = interpreter.GetSymbolicExpressionTreeValues(tree, problemData.Dataset, rows);
|
---|
[17958] | 181 | var boundedEstimatedValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit);
|
---|
[18103] | 182 | var targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
|
---|
| 183 | var nmse = OnlineNormalizedMeanSquaredErrorCalculator.Calculate(targetValues, boundedEstimatedValues, out var errorState);
|
---|
| 184 | if (errorState != OnlineCalculatorError.None)
|
---|
[17958] | 185 | return 1.0;
|
---|
| 186 |
|
---|
[18103] | 187 | if (!constraints.Any())
|
---|
| 188 | return nmse;
|
---|
| 189 |
|
---|
| 190 | var intervalCollection = problemData.VariableRanges;
|
---|
[17958] | 191 | var constraintViolations = IntervalUtil.GetConstraintViolations(constraints, estimator, intervalCollection, tree);
|
---|
| 192 |
|
---|
[18103] | 193 | // infinite/NaN constraints
|
---|
| 194 | if (constraintViolations.Any(x => double.IsNaN(x) || double.IsInfinity(x)))
|
---|
[17958] | 195 | return 1.0;
|
---|
[18103] | 196 |
|
---|
| 197 | // hard constraints
|
---|
| 198 | if (!useSoftConstraints) {
|
---|
| 199 | if (constraintViolations.Any(x => x > 0.0))
|
---|
| 200 | return 1.0;
|
---|
| 201 | return nmse;
|
---|
[17958] | 202 | }
|
---|
| 203 |
|
---|
[18103] | 204 | // soft constraints
|
---|
| 205 | if (penaltyFactor < 0.0)
|
---|
| 206 | throw new ArgumentException("The parameter has to be >= 0.0.", nameof(penaltyFactor));
|
---|
[17958] | 207 |
|
---|
[18103] | 208 | var weightedViolationsAvg = constraints
|
---|
| 209 | .Zip(constraintViolations, (c, v) => c.Weight * v)
|
---|
| 210 | .Average();
|
---|
[17958] | 211 |
|
---|
[18103] | 212 | return Math.Min(nmse, 1.0) + penaltyFactor * weightedViolationsAvg;
|
---|
[17958] | 213 | }
|
---|
| 214 |
|
---|
| 215 | public override double Evaluate(
|
---|
| 216 | IExecutionContext context, ISymbolicExpressionTree tree, IRegressionProblemData problemData,
|
---|
| 217 | IEnumerable<int> rows) {
|
---|
| 218 | SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
|
---|
| 219 | EstimationLimitsParameter.ExecutionContext = context;
|
---|
| 220 | ApplyLinearScalingParameter.ExecutionContext = context;
|
---|
| 221 |
|
---|
[18103] | 222 | var nmse = Calculate(
|
---|
| 223 | tree, problemData, rows,
|
---|
| 224 | SymbolicDataAnalysisTreeInterpreterParameter.ActualValue,
|
---|
| 225 | EstimationLimitsParameter.ActualValue.Lower,
|
---|
| 226 | EstimationLimitsParameter.ActualValue.Upper,
|
---|
| 227 | BoundsEstimator,
|
---|
| 228 | UseSoftConstraints,
|
---|
| 229 | PenalityFactor);
|
---|
[17958] | 230 |
|
---|
| 231 | SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
|
---|
| 232 | EstimationLimitsParameter.ExecutionContext = null;
|
---|
| 233 | ApplyLinearScalingParameter.ExecutionContext = null;
|
---|
| 234 |
|
---|
| 235 | return nmse;
|
---|
| 236 | }
|
---|
[18095] | 237 |
|
---|
[18103] | 238 | public override double Evaluate(
|
---|
| 239 | ISymbolicExpressionTree tree,
|
---|
| 240 | IRegressionProblemData problemData,
|
---|
| 241 | IEnumerable<int> rows,
|
---|
[18095] | 242 | ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
|
---|
| 243 | bool applyLinearScaling = true,
|
---|
| 244 | double lowerEstimationLimit = double.MinValue,
|
---|
| 245 | double upperEstimationLimit = double.MaxValue) {
|
---|
| 246 |
|
---|
[18103] | 247 | if (OptimizeParameters)
|
---|
[18095] | 248 | SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(
|
---|
[18103] | 249 | interpreter, tree,
|
---|
| 250 | problemData, rows,
|
---|
| 251 | applyLinearScaling: false, // OptimizeConstants deletes the scaling terms -> wrong estimations
|
---|
[18095] | 252 | ConstantOptimizationIterations,
|
---|
[18103] | 253 | updateVariableWeights: true,
|
---|
[18095] | 254 | lowerEstimationLimit,
|
---|
| 255 | upperEstimationLimit);
|
---|
[18103] | 256 |
|
---|
| 257 | if (applyLinearScaling) // extra scaling terms, which are included in tree
|
---|
| 258 | CalcLinearScalingTerms(tree, problemData, rows, interpreter);
|
---|
[18095] | 259 |
|
---|
| 260 | return Calculate(
|
---|
[18103] | 261 | tree, problemData,
|
---|
| 262 | rows, interpreter,
|
---|
[18095] | 263 | lowerEstimationLimit,
|
---|
| 264 | upperEstimationLimit,
|
---|
| 265 | BoundsEstimator,
|
---|
| 266 | UseSoftConstraints,
|
---|
| 267 | PenalityFactor);
|
---|
| 268 | }
|
---|
[17958] | 269 | }
|
---|
| 270 | } |
---|