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