Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP-MoveOperators/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionSingleObjectiveEvaluator.cs @ 12747

Last change on this file since 12747 was 8660, checked in by gkronber, 12 years ago

#1847 merged r8205:8635 from trunk into branch

File size: 5.0 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        linearScalingCalculator.Add(estimated, target);
81        i++;
82      }
83      if (linearScalingCalculator.ErrorState == OnlineCalculatorError.None && (targetValuesEnumerator.MoveNext() || estimatedValuesEnumerator.MoveNext()))
84        throw new ArgumentException("Number of elements in target and estimated values enumeration do not match.");
85
86      double alpha = linearScalingCalculator.Alpha;
87      double beta = linearScalingCalculator.Beta;
88      if (linearScalingCalculator.ErrorState != OnlineCalculatorError.None) {
89        alpha = 0.0;
90        beta = 1.0;
91      }
92
93      //calculate the quality by using the passed online calculator
94      targetValuesEnumerator = targetValues.GetEnumerator();
95      var scaledBoundedEstimatedValuesEnumerator = Enumerable.Range(0, i).Select(x => cache[x] * beta + alpha)
96        .LimitToRange(lowerEstimationLimit, upperEstimationLimit).GetEnumerator();
97
98      while (targetValuesEnumerator.MoveNext() & scaledBoundedEstimatedValuesEnumerator.MoveNext()) {
99        calculator.Add(targetValuesEnumerator.Current, scaledBoundedEstimatedValuesEnumerator.Current);
100      }
101    }
102  }
103}
Note: See TracBrowser for help on using the repository browser.