Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/ScalingTreeEvaluator.cs @ 2668

Last change on this file since 2668 was 2640, checked in by gkronber, 15 years ago

Removed the restriction of the estimated value to the range [LowerBound..UpperBound] because the estimation is scaled anyway. However the scaled values are restricted to that range. #823 (Implement tree evaluator with linear scaling to improve convergence in symbolic regression.)

File size: 4.2 KB
RevLine 
[2579]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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.Diagnostics;
24using HeuristicLab.Common;
25using HeuristicLab.DataAnalysis;
26using HeuristicLab.GP.Interfaces;
27using System.Collections.Generic; // double.IsAlmost extension
28using System.Linq;
[2634]29using System.Xml;
[2579]30namespace HeuristicLab.GP.StructureIdentification {
31  /// <summary>
32  /// Evaluates FunctionTrees recursively by interpretation of the function symbols in each node.
33  /// Scales the output of the function-tree to the desired output range of the target variable by linear transformation
34  /// Not thread-safe!
35  /// </summary>
36  public class ScalingTreeEvaluator : HL3TreeEvaluator, ITreeEvaluator {
37    public ScalingTreeEvaluator() : base() { } // for persistence
[2640]38    public ScalingTreeEvaluator(double minValue, double maxValue)
39      : base(minValue, maxValue) {
40    }
[2579]41
[2634]42    private string targetVariable;
43    public string TargetVariable {
44      get { return targetVariable; }
45      set { targetVariable = value; }
46    }
47
[2640]48
49    public override double Evaluate(int sampleIndex) {
50      PC = 0;
51      this.sampleIndex = sampleIndex;
52      double estimation = EvaluateBakedCode();
53      if (double.IsPositiveInfinity(estimation)) estimation = UpperEvaluationLimit;
54      else if (double.IsNegativeInfinity(estimation)) estimation = LowerEvaluationLimit;
55      else if (double.IsNaN(estimation)) estimation = UpperEvaluationLimit;
56      return estimation;
57    }
58
[2579]59    public override IEnumerable<double> Evaluate(Dataset dataset, IFunctionTree tree, IEnumerable<int> rows) {
60      double[] result = base.Evaluate(dataset, tree, rows).ToArray();
[2634]61      int targetVariableIndex = dataset.GetVariableIndex(targetVariable);
62      double tMean = dataset.GetMean(targetVariableIndex);
[2579]63      double xMean = Statistics.Mean(result);
64      double sumXT = 0;
65      double sumXX = 0;
66      for (int i = 0; i < result.Length; i++) {
67        double x = result[i];
[2634]68        double t = dataset.GetValue(rows.ElementAt(i), targetVariableIndex);
[2579]69        sumXT += (x - xMean) * (t - tMean);
70        sumXX += (x - xMean) * (x - xMean);
71      }
72      double b = sumXT / sumXX;
73      double a = tMean - b * xMean;
74      for (int i = 0; i < result.Length; i++) {
75        double scaledResult = result[i] * b + a;
76        scaledResult = Math.Min(Math.Max(scaledResult, LowerEvaluationLimit), UpperEvaluationLimit);
77        if (double.IsNaN(scaledResult)) scaledResult = UpperEvaluationLimit;
78        result[i] = scaledResult;
79      }
80      return result;
81    }
[2634]82
83    public override object Clone(IDictionary<Guid, object> clonedObjects) {
84      ScalingTreeEvaluator clone = (ScalingTreeEvaluator)base.Clone(clonedObjects);
85      clone.targetVariable = targetVariable;
86      return clone;
87    }
88
89    public override System.Xml.XmlNode GetXmlNode(string name, System.Xml.XmlDocument document, IDictionary<Guid, HeuristicLab.Core.IStorable> persistedObjects) {
90      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
91      XmlAttribute targetVariableAttribute = document.CreateAttribute("TargetVariable");
92      targetVariableAttribute.Value = targetVariable;
93      node.Attributes.Append(targetVariableAttribute);
94      return node;
95    }
96
97    public override void Populate(XmlNode node, IDictionary<Guid, HeuristicLab.Core.IStorable> restoredObjects) {
98      base.Populate(node, restoredObjects);
99      targetVariable = node.Attributes["TargetVariable"].Value;
100    }
[2579]101  }
102}
Note: See TracBrowser for help on using the repository browser.