Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1847: merged r8084:8205 from trunk into GP move operators branch

File size: 4.6 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      double alpha = linearScalingCalculator.Alpha;
84      double beta = linearScalingCalculator.Beta;
85
86      //calculate the quality by using the passed online calculator
87      targetValuesEnumerator = targetValues.GetEnumerator();
88      var scaledBoundedEstimatedValuesEnumerator = Enumerable.Range(0, i).Select(x => cache[x] * beta + alpha)
89        .LimitToRange(lowerEstimationLimit, upperEstimationLimit).GetEnumerator();
90
91      while (targetValuesEnumerator.MoveNext() & scaledBoundedEstimatedValuesEnumerator.MoveNext()) {
92        calculator.Add(targetValuesEnumerator.Current, scaledBoundedEstimatedValuesEnumerator.Current);
93      }
94    }
95  }
96}
Note: See TracBrowser for help on using the repository browser.