Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 7720 was 7677, checked in by mkommend, 13 years ago

#1788: Implemente new symbolic regression evaluators.

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