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.Analysis;
|
---|
27 | using HeuristicLab.Common;
|
---|
28 | using HeuristicLab.Core;
|
---|
29 | using HeuristicLab.Data;
|
---|
30 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
31 | using HeuristicLab.Optimization;
|
---|
32 | using HeuristicLab.Optimization.Operators;
|
---|
33 | using HeuristicLab.Parameters;
|
---|
34 | using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
|
---|
35 |
|
---|
36 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
37 | [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.")]
|
---|
38 | [StorableType("27473973-DD8D-4375-997D-942E2280AE8E")]
|
---|
39 | public class NMSEConstraintsEvaluator : SymbolicRegressionSingleObjectiveEvaluator {
|
---|
40 | #region Parameter/Properties
|
---|
41 |
|
---|
42 | private const string OptimizeParametersParameterName = "OptimizeParameters";
|
---|
43 | private const string ParameterOptimizationIterationsParameterName = "ParameterOptimizationIterations";
|
---|
44 | private const string UseConstraintsParameterName = "UseConstraintsEvaluation";
|
---|
45 | private const string UseSoftConstraintsParameterName = "UseSoftConstraintsEvaluation";
|
---|
46 | private const string BoundsEstimatorParameterName = "BoundsEstimator";
|
---|
47 | private const string PenaltyFactorParameterName = "PenaltyFactor";
|
---|
48 | private const string GenerationOfConvergenceParameterName = "Generation of Convergence";
|
---|
49 | private const string AlphaParameterName = "Alpha";
|
---|
50 | private const string ResultCollectionParameterName = "Results";
|
---|
51 | private const string GenerationsEntry = "Generations";
|
---|
52 | private const string LPValueParameterName = "Low Pass Value";
|
---|
53 | private const string UseDynamicPenaltyImpl1ParameterName = "UseDynamicPenaltyImpl1";
|
---|
54 | private const string UseDynamicPenaltyImpl2ParameterName = "UseDynamicPenaltyImpl2";
|
---|
55 | private const string UseDynamicPenaltyImpl3ParameterName = "UseDynamicPenaltyImpl3";
|
---|
56 | private const string UseAdditivePenaltyParameterName = "UseAdditivePenalty";
|
---|
57 | private const string RisingPenaltyParameterName = "RisingPenalty";
|
---|
58 | private const string StepSizeParameterName = "Step Size";
|
---|
59 | private const string MaximumStepsParameterName = "Maximum Steps";
|
---|
60 | private const string StartpenaltyParameterName = "Start penalty";
|
---|
61 |
|
---|
62 |
|
---|
63 | public IFixedValueParameter<BoolValue> OptimizerParametersParameter =>
|
---|
64 | (IFixedValueParameter<BoolValue>)Parameters[OptimizeParametersParameterName];
|
---|
65 |
|
---|
66 | public IFixedValueParameter<IntValue> ConstantOptimizationIterationsParameter =>
|
---|
67 | (IFixedValueParameter<IntValue>)Parameters[ParameterOptimizationIterationsParameterName];
|
---|
68 |
|
---|
69 | public IFixedValueParameter<BoolValue> UseConstraintsParameter =>
|
---|
70 | (IFixedValueParameter<BoolValue>)Parameters[UseConstraintsParameterName];
|
---|
71 |
|
---|
72 | public IFixedValueParameter<BoolValue> UseSoftConstraintsParameter =>
|
---|
73 | (IFixedValueParameter<BoolValue>)Parameters[UseSoftConstraintsParameterName];
|
---|
74 |
|
---|
75 | public IValueParameter<IBoundsEstimator> BoundsEstimatorParameter =>
|
---|
76 | (IValueParameter<IBoundsEstimator>)Parameters[BoundsEstimatorParameterName];
|
---|
77 |
|
---|
78 | public IFixedValueParameter<DoubleValue> PenaltyFactorParameter =>
|
---|
79 | (IFixedValueParameter<DoubleValue>)Parameters[PenaltyFactorParameterName];
|
---|
80 |
|
---|
81 | public IFixedValueParameter<IntValue> GenerationOfConvergenceParameter =>
|
---|
82 | (IFixedValueParameter<IntValue>)Parameters[GenerationOfConvergenceParameterName];
|
---|
83 |
|
---|
84 | public IFixedValueParameter<DoubleValue> AlphaParameter =>
|
---|
85 | (IFixedValueParameter<DoubleValue>)Parameters[AlphaParameterName];
|
---|
86 |
|
---|
87 | public ILookupParameter<ResultCollection> ResultCollectionParameter =>
|
---|
88 | (ILookupParameter<ResultCollection>)Parameters[ResultCollectionParameterName];
|
---|
89 |
|
---|
90 | public IResultParameter<DataTable> LPValueParameter {
|
---|
91 | get {
|
---|
92 | if (Parameters.TryGetValue(LPValueParameterName, out IParameter p))
|
---|
93 | return (IResultParameter<DataTable>)p;
|
---|
94 | return null;
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | public IFixedValueParameter<BoolValue> UseDynamicPenaltyImpl1Parameter =>
|
---|
99 | (IFixedValueParameter<BoolValue>)Parameters[UseDynamicPenaltyImpl1ParameterName];
|
---|
100 |
|
---|
101 | public IFixedValueParameter<BoolValue> UseDynamicPenaltyImpl2Parameter =>
|
---|
102 | (IFixedValueParameter<BoolValue>)Parameters[UseDynamicPenaltyImpl2ParameterName];
|
---|
103 |
|
---|
104 | public IFixedValueParameter<BoolValue> UseDynamicPenaltyImpl3Parameter =>
|
---|
105 | (IFixedValueParameter<BoolValue>)Parameters[UseDynamicPenaltyImpl3ParameterName];
|
---|
106 |
|
---|
107 | public IFixedValueParameter<BoolValue> UseAdditivePenaltyParameter =>
|
---|
108 | (IFixedValueParameter<BoolValue>)Parameters[UseAdditivePenaltyParameterName];
|
---|
109 |
|
---|
110 | public IFixedValueParameter<IntValue> StepSizeParameter =>
|
---|
111 | (IFixedValueParameter<IntValue>)Parameters[StepSizeParameterName];
|
---|
112 |
|
---|
113 | public IFixedValueParameter<IntValue> MaximumStepsParameter =>
|
---|
114 | (IFixedValueParameter<IntValue>)Parameters[MaximumStepsParameterName];
|
---|
115 |
|
---|
116 | public IResultParameter<DataTable> RisingPenaltyParameter {
|
---|
117 | get {
|
---|
118 | if (Parameters.TryGetValue(RisingPenaltyParameterName, out IParameter p))
|
---|
119 | return (IResultParameter<DataTable>)p;
|
---|
120 | return null;
|
---|
121 | }
|
---|
122 | }
|
---|
123 |
|
---|
124 | public IFixedValueParameter<DoubleValue> StartpenaltyParameter =>
|
---|
125 | (IFixedValueParameter<DoubleValue>)Parameters[StartpenaltyParameterName];
|
---|
126 |
|
---|
127 | public bool OptimizeParameters {
|
---|
128 | get => OptimizerParametersParameter.Value.Value;
|
---|
129 | set => OptimizerParametersParameter.Value.Value = value;
|
---|
130 | }
|
---|
131 |
|
---|
132 | public int ConstantOptimizationIterations {
|
---|
133 | get => ConstantOptimizationIterationsParameter.Value.Value;
|
---|
134 | set => ConstantOptimizationIterationsParameter.Value.Value = value;
|
---|
135 | }
|
---|
136 |
|
---|
137 | public bool UseConstraints {
|
---|
138 | get => UseConstraintsParameter.Value.Value;
|
---|
139 | set => UseConstraintsParameter.Value.Value = value;
|
---|
140 | }
|
---|
141 |
|
---|
142 | public bool UseSoftConstraints {
|
---|
143 | get => UseSoftConstraintsParameter.Value.Value;
|
---|
144 | set => UseSoftConstraintsParameter.Value.Value = value;
|
---|
145 | }
|
---|
146 |
|
---|
147 | public IBoundsEstimator BoundsEstimator {
|
---|
148 | get => BoundsEstimatorParameter.Value;
|
---|
149 | set => BoundsEstimatorParameter.Value = value;
|
---|
150 | }
|
---|
151 |
|
---|
152 | public double PenaltyFactor {
|
---|
153 | get => PenaltyFactorParameter.Value.Value;
|
---|
154 | set => PenaltyFactorParameter.Value.Value = value;
|
---|
155 | }
|
---|
156 |
|
---|
157 | public int GenerationOfConvergence {
|
---|
158 | get => GenerationOfConvergenceParameter.Value.Value;
|
---|
159 | set => GenerationOfConvergenceParameter.Value.Value = value;
|
---|
160 | }
|
---|
161 |
|
---|
162 | public double Alpha {
|
---|
163 | get => AlphaParameter.Value.Value;
|
---|
164 | set => AlphaParameter.Value.Value = value;
|
---|
165 | }
|
---|
166 |
|
---|
167 | public ResultCollection ResultCollection =>
|
---|
168 | ResultCollectionParameter.ActualValue;
|
---|
169 |
|
---|
170 | private IntValue Generations {
|
---|
171 | get {
|
---|
172 | IResult result;
|
---|
173 | ResultCollection.TryGetValue(GenerationsEntry, out result);
|
---|
174 | if (result == null) return new IntValue(0);
|
---|
175 | return result.Value == null ? new IntValue(0) : (IntValue)result.Value;
|
---|
176 | }
|
---|
177 | }
|
---|
178 |
|
---|
179 | public override bool Maximization => false; // NMSE is minimized
|
---|
180 |
|
---|
181 | #endregion
|
---|
182 |
|
---|
183 | #region Constructors/Cloning
|
---|
184 |
|
---|
185 | [StorableConstructor]
|
---|
186 | protected NMSEConstraintsEvaluator(StorableConstructorFlag _) : base(_) { }
|
---|
187 |
|
---|
188 | protected NMSEConstraintsEvaluator(
|
---|
189 | NMSEConstraintsEvaluator original, Cloner cloner) : base(original, cloner) { }
|
---|
190 |
|
---|
191 | public NMSEConstraintsEvaluator() {
|
---|
192 | Parameters.Add(new FixedValueParameter<BoolValue>(OptimizeParametersParameterName,
|
---|
193 | "Define whether optimization of numeric parameters is active or not (default: false).", new BoolValue(false)));
|
---|
194 | Parameters.Add(new FixedValueParameter<BoolValue>(UseConstraintsParameterName,
|
---|
195 | "Define whether evaluation of constraints is active or not (default: true).", new BoolValue(true)));
|
---|
196 | Parameters.Add(new FixedValueParameter<IntValue>(ParameterOptimizationIterationsParameterName,
|
---|
197 | "Define how many parameter optimization steps should be performed (default: 10).", new IntValue(10)));
|
---|
198 | Parameters.Add(new FixedValueParameter<BoolValue>(UseSoftConstraintsParameterName,
|
---|
199 | "Define whether the constraints are penalized by soft or hard constraints (default: false).", new BoolValue(false)));
|
---|
200 | Parameters.Add(new ValueParameter<IBoundsEstimator>(BoundsEstimatorParameterName,
|
---|
201 | "The estimator which is used to estimate output ranges of models (default: interval arithmetic).", new IntervalArithBoundsEstimator()));
|
---|
202 | Parameters.Add(new FixedValueParameter<DoubleValue>(PenaltyFactorParameterName,
|
---|
203 | "Punishment factor for constraint violations for soft constraint handling (fitness = NMSE + penaltyFactor * avg(violations)) (default: 1.0)", new DoubleValue(1.0)));
|
---|
204 | Parameters.Add(new FixedValueParameter<IntValue>(GenerationOfConvergenceParameterName, "", new IntValue(100)));
|
---|
205 | Parameters.Add(new FixedValueParameter<DoubleValue>(AlphaParameterName, "", new DoubleValue(0.9)));
|
---|
206 | Parameters.Add(new LookupParameter<ResultCollection>(ResultCollectionParameterName, "The result collection to store the analysis results."));
|
---|
207 |
|
---|
208 | Parameters.Add(new FixedValueParameter<BoolValue>(UseDynamicPenaltyImpl1ParameterName, "", new BoolValue(false)));
|
---|
209 | Parameters.Add(new FixedValueParameter<BoolValue>(UseDynamicPenaltyImpl2ParameterName, "", new BoolValue(false)));
|
---|
210 | Parameters.Add(new FixedValueParameter<BoolValue>(UseDynamicPenaltyImpl3ParameterName, "", new BoolValue(false)));
|
---|
211 | Parameters.Add(new FixedValueParameter<BoolValue>(UseAdditivePenaltyParameterName, "", new BoolValue(false)));
|
---|
212 |
|
---|
213 |
|
---|
214 | Parameters.Add(new FixedValueParameter<IntValue>(StepSizeParameterName,
|
---|
215 | "Defines the step size for the increasing penalty multiplier.", new IntValue(1)));
|
---|
216 | Parameters.Add(new FixedValueParameter<IntValue>(MaximumStepsParameterName,
|
---|
217 | "Defines maximum steps for the increasing penalty multiplier.", new IntValue(1000)));
|
---|
218 | Parameters.Add(new FixedValueParameter<DoubleValue>(StartpenaltyParameterName,
|
---|
219 | "The start value for the penalty multiplier.", new DoubleValue(0.5)));
|
---|
220 |
|
---|
221 | /*
|
---|
222 | Parameters.Add(new ResultParameter<DataTable>(RisingPenaltyParameterName,
|
---|
223 | "Shows the behavior of the penalty multiplier."));
|
---|
224 | RisingPenaltyParameter.DefaultValue = new DataTable(RisingPenaltyParameterName) {
|
---|
225 | VisualProperties = {
|
---|
226 | XAxisTitle = "Generations",
|
---|
227 | YAxisTitle = "penalty Multiplier"
|
---|
228 | }
|
---|
229 | };
|
---|
230 |
|
---|
231 |
|
---|
232 | Parameters.Add(new ResultParameter<DataTable>(LPValueParameterName,
|
---|
233 | "Low Pass Value"));
|
---|
234 | LPValueParameter.DefaultValue = new DataTable(LPValueParameterName) {
|
---|
235 | VisualProperties = {
|
---|
236 | XAxisTitle = "Generations",
|
---|
237 | YAxisTitle = "Value"
|
---|
238 | }
|
---|
239 | };*/
|
---|
240 | }
|
---|
241 |
|
---|
242 | [StorableHook(HookType.AfterDeserialization)]
|
---|
243 | private void AfterDeserialization() { }
|
---|
244 |
|
---|
245 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
246 | return new NMSEConstraintsEvaluator(this, cloner);
|
---|
247 | }
|
---|
248 |
|
---|
249 | #endregion
|
---|
250 |
|
---|
251 | public override IOperation InstrumentedApply() {
|
---|
252 | var rows = GenerateRowsToEvaluate();
|
---|
253 | var tree = SymbolicExpressionTreeParameter.ActualValue;
|
---|
254 | var problemData = ProblemDataParameter.ActualValue;
|
---|
255 | var interpreter = SymbolicDataAnalysisTreeInterpreterParameter.ActualValue;
|
---|
256 | var estimationLimits = EstimationLimitsParameter.ActualValue;
|
---|
257 | var applyLinearScaling = ApplyLinearScalingParameter.ActualValue.Value;
|
---|
258 |
|
---|
259 | if (OptimizeParameters) {
|
---|
260 | SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(interpreter, tree, problemData, rows,
|
---|
261 | false, ConstantOptimizationIterations, true,
|
---|
262 | estimationLimits.Lower, estimationLimits.Upper);
|
---|
263 | } else {
|
---|
264 | if (applyLinearScaling) {
|
---|
265 | var rootNode = new ProgramRootSymbol().CreateTreeNode();
|
---|
266 | var startNode = new StartSymbol().CreateTreeNode();
|
---|
267 | var offset = tree.Root.GetSubtree(0) //Start
|
---|
268 | .GetSubtree(0); //Offset
|
---|
269 | var scaling = offset.GetSubtree(0);
|
---|
270 |
|
---|
271 | //Check if tree contains offset and scaling nodes
|
---|
272 | if (!(offset.Symbol is Addition) || !(scaling.Symbol is Multiplication))
|
---|
273 | throw new ArgumentException($"{ItemName} can only be used with IntervalArithmeticGrammar.");
|
---|
274 |
|
---|
275 | var t = (ISymbolicExpressionTreeNode)scaling.GetSubtree(0).Clone();
|
---|
276 | rootNode.AddSubtree(startNode);
|
---|
277 | startNode.AddSubtree(t);
|
---|
278 | var newTree = new SymbolicExpressionTree(rootNode);
|
---|
279 |
|
---|
280 | //calculate alpha and beta for scaling
|
---|
281 | var estimatedValues = interpreter.GetSymbolicExpressionTreeValues(newTree, problemData.Dataset, rows);
|
---|
282 |
|
---|
283 | var targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
|
---|
284 | OnlineLinearScalingParameterCalculator.Calculate(estimatedValues, targetValues, out var alpha, out var beta,
|
---|
285 | out var errorState);
|
---|
286 |
|
---|
287 | if (errorState == OnlineCalculatorError.None) {
|
---|
288 | //Set alpha and beta to the scaling nodes from ia grammar
|
---|
289 | var offsetParameter = offset.GetSubtree(1) as ConstantTreeNode;
|
---|
290 | offsetParameter.Value = alpha;
|
---|
291 | var scalingParameter = scaling.GetSubtree(1) as ConstantTreeNode;
|
---|
292 | scalingParameter.Value = beta;
|
---|
293 | }
|
---|
294 | } // else: alpha and beta are evolved
|
---|
295 | }
|
---|
296 |
|
---|
297 | var quality = Calculate(interpreter, tree, estimationLimits.Lower, estimationLimits.Upper, problemData, rows,
|
---|
298 | BoundsEstimator, UseConstraints, UseSoftConstraints,
|
---|
299 | UseDynamicPenaltyImpl1Parameter.Value.Value, UseDynamicPenaltyImpl2Parameter.Value.Value,
|
---|
300 | UseDynamicPenaltyImpl3Parameter.Value.Value, UseAdditivePenaltyParameter.Value.Value,
|
---|
301 | PenaltyFactor,
|
---|
302 | StepSizeParameter.Value.Value, StartpenaltyParameter.Value.Value, MaximumStepsParameter.Value.Value,
|
---|
303 | RisingPenaltyParameter?.ActualValue,
|
---|
304 | Generations.Value, GenerationOfConvergence, Alpha,
|
---|
305 | LPValueParameter);
|
---|
306 | QualityParameter.ActualValue = new DoubleValue(quality);
|
---|
307 |
|
---|
308 | return base.InstrumentedApply();
|
---|
309 | }
|
---|
310 |
|
---|
311 | public override void InitializeState() {
|
---|
312 | oldValue = 0.0;
|
---|
313 | actualValue = 0.0;
|
---|
314 | oldGeneration = 0;
|
---|
315 | base.InitializeState();
|
---|
316 | }
|
---|
317 |
|
---|
318 | // bei mehrmaligen ausführungen bleibt der state!
|
---|
319 | private static int oldGeneration = 0;
|
---|
320 | private static double oldValue = 0.0;
|
---|
321 | private static double actualValue = 0.0;
|
---|
322 |
|
---|
323 | public static double Calculate(
|
---|
324 | ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
|
---|
325 | ISymbolicExpressionTree tree,
|
---|
326 | double lowerEstimationLimit, double upperEstimationLimit,
|
---|
327 | IRegressionProblemData problemData, IEnumerable<int> rows,
|
---|
328 | IBoundsEstimator estimator,
|
---|
329 | bool useConstraints, bool useSoftConstraints = false,
|
---|
330 | bool useDynamicConstraints1 = false, bool useDynamicConstraints2 = false, bool useDynamicConstraints3 = false,
|
---|
331 | bool useAdditivePenalty = false,
|
---|
332 | double penaltyFactor = 1.0,
|
---|
333 | int stepSize = 1, double startpenalty = 0.5, int maximumSteps = 1000, DataTable penaltyDataTable = null,
|
---|
334 | int generation = 0, int generationOfConvergence = 100, double alpha = 0.9,
|
---|
335 | IResultParameter<DataTable> lpValueParameter = null) {
|
---|
336 |
|
---|
337 | double risingPenalty = 1.0;
|
---|
338 | if (useDynamicConstraints1) {
|
---|
339 | risingPenalty = LinearDiscreteDoubleValueModifier.Apply(0, startpenalty, 1.0,
|
---|
340 | (int)(generation / stepSize) * stepSize, 0, maximumSteps);
|
---|
341 | }
|
---|
342 |
|
---|
343 | if (oldGeneration != generation) {
|
---|
344 | oldGeneration = generation;
|
---|
345 |
|
---|
346 | if(lpValueParameter != null) {
|
---|
347 | var LPValueParameterDataTable = lpValueParameter.ActualValue;
|
---|
348 | if (LPValueParameterDataTable.Rows.Count == 0)
|
---|
349 | LPValueParameterDataTable.Rows.Add(new DataRow(LPValueParameterName));
|
---|
350 |
|
---|
351 | LPValueParameterDataTable.Rows[LPValueParameterName]
|
---|
352 | .Values
|
---|
353 | .Add(oldValue);
|
---|
354 | }
|
---|
355 |
|
---|
356 | if (penaltyDataTable != null && useDynamicConstraints1) {
|
---|
357 | if (penaltyDataTable.Rows.Count == 0)
|
---|
358 | penaltyDataTable.Rows.Add(new DataRow("LinearDiscreteDoubleValueModifier"));
|
---|
359 | penaltyDataTable.Rows["LinearDiscreteDoubleValueModifier"].Values.Add(risingPenalty);
|
---|
360 | }
|
---|
361 |
|
---|
362 | oldValue = actualValue;
|
---|
363 | }
|
---|
364 |
|
---|
365 | var estimatedValues = interpreter.GetSymbolicExpressionTreeValues(tree, problemData.Dataset, rows);
|
---|
366 | var targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
|
---|
367 | var constraints = problemData.ShapeConstraints.EnabledConstraints;
|
---|
368 | var intervalCollection = problemData.VariableRanges;
|
---|
369 |
|
---|
370 | var boundedEstimatedValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit);
|
---|
371 | var nmse = OnlineNormalizedMeanSquaredErrorCalculator.Calculate(targetValues, boundedEstimatedValues,
|
---|
372 | out var errorState);
|
---|
373 |
|
---|
374 | if (errorState != OnlineCalculatorError.None) {
|
---|
375 | actualValue = alpha * 1.0 + (1.0 - alpha) * actualValue;
|
---|
376 | return 10000.0;
|
---|
377 | }
|
---|
378 |
|
---|
379 | if (!useConstraints)
|
---|
380 | return nmse;
|
---|
381 |
|
---|
382 |
|
---|
383 | if(useDynamicConstraints2) {
|
---|
384 | foreach(var c in constraints) {
|
---|
385 | if(!double.IsNegativeInfinity(c.DynInterval.LowerBound) && !double.IsPositiveInfinity(c.DynInterval.UpperBound)) {
|
---|
386 | int step = (int)(generation / stepSize) * stepSize;
|
---|
387 | var lb = LinearDiscreteDoubleValueModifier.Apply(0, c.DynInterval.LowerBound, c.TargetInterval.LowerBound, step, 0, maximumSteps);
|
---|
388 | var ub = LinearDiscreteDoubleValueModifier.Apply(0, c.DynInterval.UpperBound, c.TargetInterval.UpperBound, step, 0, maximumSteps);
|
---|
389 | c.Interval = new Interval(lb, ub);
|
---|
390 | }
|
---|
391 | }
|
---|
392 | } else {
|
---|
393 | foreach (var c in constraints) {
|
---|
394 | c.Interval = new Interval(c.TargetInterval.LowerBound, c.TargetInterval.UpperBound);
|
---|
395 | }
|
---|
396 | }
|
---|
397 |
|
---|
398 | var constraintViolations = IntervalUtil.GetConstraintViolations(constraints, estimator, intervalCollection, tree);
|
---|
399 | var constraintBounds = IntervalUtil.GetModelBounds(constraints, estimator, intervalCollection, tree);
|
---|
400 |
|
---|
401 | if (constraintViolations.Any(x => double.IsNaN(x) || double.IsInfinity(x))) {
|
---|
402 | actualValue = alpha * 1.0 + (1.0 - alpha) * actualValue;
|
---|
403 | return 10000.0;
|
---|
404 | }
|
---|
405 |
|
---|
406 |
|
---|
407 | /*
|
---|
408 | if(constraintViolations.Any(x => x > 0.0)) {
|
---|
409 | actualValue = alpha * 1.0 + (1.0 - alpha) * actualValue;
|
---|
410 | } else {
|
---|
411 | actualValue *= (1.0 - alpha);
|
---|
412 | }*/
|
---|
413 |
|
---|
414 | if (useSoftConstraints) {
|
---|
415 | if (penaltyFactor < 0.0)
|
---|
416 | throw new ArgumentException("The parameter has to be greater or equal 0.0!", nameof(penaltyFactor));
|
---|
417 |
|
---|
418 |
|
---|
419 | var errors = constraints
|
---|
420 | .Zip(constraintBounds, CalcSoftConstraintError);
|
---|
421 |
|
---|
422 | var weightedViolationSum = constraints
|
---|
423 | .Zip(errors, (c, e) => c.Weight * e)
|
---|
424 | .Average();
|
---|
425 |
|
---|
426 | /*
|
---|
427 | var weightedViolationSum = constraints
|
---|
428 | .Zip(constraintViolations, (c, v) => c.Weight * v)
|
---|
429 | .Average();
|
---|
430 | */
|
---|
431 |
|
---|
432 | actualValue = alpha * errors.Average() + (1.0 - alpha) * actualValue;
|
---|
433 |
|
---|
434 | var violation = (weightedViolationSum * penaltyFactor);
|
---|
435 | if (useDynamicConstraints1)
|
---|
436 | violation *= risingPenalty;
|
---|
437 |
|
---|
438 |
|
---|
439 | if (useDynamicConstraints3)
|
---|
440 | violation *= oldValue;
|
---|
441 |
|
---|
442 | if (useAdditivePenalty)
|
---|
443 | nmse += violation;
|
---|
444 | else
|
---|
445 | nmse += nmse * violation;
|
---|
446 |
|
---|
447 |
|
---|
448 | return nmse;
|
---|
449 | //Math.Min(nmse, 1.0) +
|
---|
450 | //(Math.Min(nmse, 1.0) *
|
---|
451 | //(//(penaltyFactor * oldValue) *
|
---|
452 | /*Math.Min(penaltyFactor, penaltyFactor * ((generation + 1) / generationOfConvergence)) * */ /* penaltyFactor rises over time */
|
---|
453 | //penaltyFactor * weightedViolationSum);
|
---|
454 | } else if (constraintViolations.Any(x => x > 0.0)) {
|
---|
455 | return 1.0;
|
---|
456 | } // globale constraints -> wenn diese verletzt werden -> nmse = 1.0 ????
|
---|
457 | // analyzer -> avg. quality von lösung die nix verletzen
|
---|
458 |
|
---|
459 |
|
---|
460 | return nmse;
|
---|
461 | }
|
---|
462 |
|
---|
463 | private static double CalcSoftConstraintError(ShapeConstraint constraint, Interval bounds) {
|
---|
464 | if (!constraint.Interval.Contains(bounds)) {
|
---|
465 |
|
---|
466 | // get the absolute threshold bounds
|
---|
467 | var thresholdLb = Math.Abs(constraint.Threshold.LowerBound);
|
---|
468 | var thresholdUb = Math.Abs(constraint.Threshold.UpperBound);
|
---|
469 |
|
---|
470 | // calc the absolute bound errors
|
---|
471 | var errorLb = 0.0;//Math.Abs(Math.Abs(b.LowerBound) - Math.Abs(c.Interval.LowerBound));
|
---|
472 | var errorUb = 0.0;//Math.Abs(Math.Abs(b.UpperBound) - Math.Abs(c.Interval.UpperBound));
|
---|
473 |
|
---|
474 | if (!constraint.Interval.Contains(bounds.LowerBound)) {
|
---|
475 | errorLb = Math.Abs(bounds.LowerBound - constraint.Interval.LowerBound); // immer einfach 0 als "Mitte"?
|
---|
476 | }
|
---|
477 |
|
---|
478 | if (!constraint.Interval.Contains(bounds.UpperBound)) {
|
---|
479 | errorUb = Math.Abs(bounds.UpperBound - constraint.Interval.UpperBound);
|
---|
480 | }
|
---|
481 |
|
---|
482 | double relativeLb;
|
---|
483 | if (double.IsInfinity(thresholdLb))
|
---|
484 | relativeLb = 0.0;
|
---|
485 | if (thresholdLb > 0.0) {
|
---|
486 | relativeLb = errorLb / thresholdLb;
|
---|
487 | relativeLb = double.IsNaN(relativeLb) ? 1.0 : Math.Min(relativeLb, 1.0);
|
---|
488 | } else
|
---|
489 | relativeLb = 1.0;
|
---|
490 |
|
---|
491 | double relativeUb;
|
---|
492 | if (double.IsInfinity(thresholdUb))
|
---|
493 | relativeUb = 0.0;
|
---|
494 | else if (thresholdUb > 0.0) {
|
---|
495 | relativeUb = errorUb / thresholdUb;
|
---|
496 | relativeUb = double.IsNaN(relativeUb) ? 1.0 : Math.Min(relativeUb, 1.0);
|
---|
497 | } else
|
---|
498 | relativeUb = 1.0;
|
---|
499 |
|
---|
500 | var error = (relativeLb + relativeUb) / 2.0;
|
---|
501 | //actualValue = alpha * error + (1.0 - alpha) * actualValue;
|
---|
502 | return error; //* constraint.Weight;
|
---|
503 | }
|
---|
504 | //actualValue *= (1.0 - alpha);
|
---|
505 | return 0.0;
|
---|
506 | }
|
---|
507 |
|
---|
508 | public override double Evaluate(
|
---|
509 | IExecutionContext context, ISymbolicExpressionTree tree, IRegressionProblemData problemData,
|
---|
510 | IEnumerable<int> rows) {
|
---|
511 | SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
|
---|
512 | EstimationLimitsParameter.ExecutionContext = context;
|
---|
513 | ApplyLinearScalingParameter.ExecutionContext = context;
|
---|
514 |
|
---|
515 | var nmse = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree,
|
---|
516 | EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper,
|
---|
517 | problemData, rows, BoundsEstimator, UseConstraints, UseSoftConstraints,
|
---|
518 | UseDynamicPenaltyImpl1Parameter.Value.Value, UseDynamicPenaltyImpl2Parameter.Value.Value,
|
---|
519 | UseDynamicPenaltyImpl3Parameter.Value.Value, UseAdditivePenaltyParameter.Value.Value,
|
---|
520 | PenaltyFactor,
|
---|
521 | StepSizeParameter.Value.Value, StartpenaltyParameter.Value.Value, MaximumStepsParameter.Value.Value);
|
---|
522 |
|
---|
523 | SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
|
---|
524 | EstimationLimitsParameter.ExecutionContext = null;
|
---|
525 | ApplyLinearScalingParameter.ExecutionContext = null;
|
---|
526 |
|
---|
527 | return nmse;
|
---|
528 | }
|
---|
529 | }
|
---|
530 | } |
---|