Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionSingleObjectiveEvaluator.cs @ 8639

Last change on this file since 8639 was 8639, checked in by mkommend, 12 years ago

#1951: Changed symbolic regression evaluator and scale method in the symbolic regression model to ignore invalid (NaN & infinite) values during calculation of the scaling parameters.

File size: 5.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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 HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
31  [StorableClass]
32  public abstract class SymbolicRegressionSingleObjectiveEvaluator : SymbolicDataAnalysisSingleObjectiveEvaluator<IRegressionProblemData>, ISymbolicRegressionSingleObjectiveEvaluator {
33    private const string ApplyLinearScalingParameterName = "ApplyLinearScaling";
34    public IFixedValueParameter<BoolValue> ApplyLinearScalingParameter {
35      get { return (IFixedValueParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; }
36    }
37    public bool ApplyLinearScaling {
38      get { return ApplyLinearScalingParameter.Value.Value; }
39      set { ApplyLinearScalingParameter.Value.Value = value; }
40    }
41
42    [StorableConstructor]
43    protected SymbolicRegressionSingleObjectiveEvaluator(bool deserializing) : base(deserializing) { }
44    protected SymbolicRegressionSingleObjectiveEvaluator(SymbolicRegressionSingleObjectiveEvaluator original, Cloner cloner) : base(original, cloner) { }
45    protected SymbolicRegressionSingleObjectiveEvaluator()
46      : base() {
47      Parameters.Add(new FixedValueParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the individual should be linearly scaled before evaluating.", new BoolValue(true)));
48      ApplyLinearScalingParameter.Hidden = true;
49    }
50
51    [StorableHook(HookType.AfterDeserialization)]
52    private void AfterDeserialization() {
53      if (!Parameters.ContainsKey(ApplyLinearScalingParameterName)) {
54        Parameters.Add(new FixedValueParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the individual should be linearly scaled before evaluating.", new BoolValue(false)));
55        ApplyLinearScalingParameter.Hidden = true;
56      }
57    }
58
59    [ThreadStatic]
60    private static double[] cache;
61
62    protected static void CalculateWithScaling(IEnumerable<double> targetValues, IEnumerable<double> estimatedValues,
63      double lowerEstimationLimit, double upperEstimationLimit,
64      IOnlineCalculator calculator, int maxRows) {
65      if (cache == null || cache.GetLength(0) < maxRows) {
66        cache = new double[maxRows];
67      }
68
69      //calculate linear scaling
70      //the static methods of the calculator could not be used as it performs a check if the enumerators have an equal amount of elements
71      //this is not true if the cache is used
72      int i = 0;
73      var linearScalingCalculator = new OnlineLinearScalingParameterCalculator();
74      var targetValuesEnumerator = targetValues.GetEnumerator();
75      var estimatedValuesEnumerator = estimatedValues.GetEnumerator();
76      while (targetValuesEnumerator.MoveNext() & estimatedValuesEnumerator.MoveNext()) {
77        double target = targetValuesEnumerator.Current;
78        double estimated = estimatedValuesEnumerator.Current;
79        cache[i] = estimated;
80        if (!double.IsNaN(estimated) && !double.IsInfinity(estimated))
81          linearScalingCalculator.Add(estimated, target);
82        i++;
83      }
84      if (linearScalingCalculator.ErrorState == OnlineCalculatorError.None && (targetValuesEnumerator.MoveNext() || estimatedValuesEnumerator.MoveNext()))
85        throw new ArgumentException("Number of elements in target and estimated values enumeration do not match.");
86
87      double alpha = linearScalingCalculator.Alpha;
88      double beta = linearScalingCalculator.Beta;
89      if (linearScalingCalculator.ErrorState != OnlineCalculatorError.None) {
90        alpha = 0.0;
91        beta = 1.0;
92      }
93
94      //calculate the quality by using the passed online calculator
95      targetValuesEnumerator = targetValues.GetEnumerator();
96      var scaledBoundedEstimatedValuesEnumerator = Enumerable.Range(0, i).Select(x => cache[x] * beta + alpha)
97        .LimitToRange(lowerEstimationLimit, upperEstimationLimit).GetEnumerator();
98
99      while (targetValuesEnumerator.MoveNext() & scaledBoundedEstimatedValuesEnumerator.MoveNext()) {
100        calculator.Add(targetValuesEnumerator.Current, scaledBoundedEstimatedValuesEnumerator.Current);
101      }
102    }
103  }
104}
Note: See TracBrowser for help on using the repository browser.