Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2900 was 2722, checked in by gkronber, 14 years ago

Added a special predictor builder for linear scaling tree evaluation and changed default GP engines to use linear scaling evaluators. #823.

File size: 4.9 KB
Line 
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;
29using System.Xml;
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
38    public ScalingTreeEvaluator(double minValue, double maxValue)
39      : base(minValue, maxValue) {
40    }
41
42    private string targetVariable;
43    public string TargetVariable {
44      get { return targetVariable; }
45      set { targetVariable = value; }
46    }
47
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
59    public override IEnumerable<double> Evaluate(Dataset dataset, IFunctionTree tree, IEnumerable<int> rows) {
60      int targetVariableIndex = dataset.GetVariableIndex(targetVariable);
61      // evaluate for all rows
62      PrepareForEvaluation(dataset, tree);
63      var result = (from row in rows
64                    let y = Evaluate(row)
65                    let y_ = dataset.GetValue(row, targetVariableIndex)
66                    select new { Row = row, Estimation = y, Target = y_ }).ToArray();
67
68      // calculate alpha and beta on the subset of rows with valid values
69      var filteredResult = result.Where(x => IsValidValue(x.Target) && IsValidValue(x.Estimation));
70      var target = filteredResult.Select(x => x.Target);
71      var estimation = filteredResult.Select(x => x.Estimation);
72      double a, b;
73      if (filteredResult.Count() > 2) {
74        double tMean = target.Sum() / target.Count();
75        double xMean = estimation.Sum() / estimation.Count();
76        double sumXT = 0;
77        double sumXX = 0;
78        foreach (var r in result) {
79          double x = r.Estimation;
80          double t = r.Target;
81          sumXT += (x - xMean) * (t - tMean);
82          sumXX += (x - xMean) * (x - xMean);
83        }
84        b = sumXT / sumXX;
85        a = tMean - b * xMean;
86      } else {
87        b = 1.0;
88        a = 0.0;
89      }
90      // return scaled results
91      return from r in result
92             let scaledR = Math.Min(Math.Max(r.Estimation * b + a, LowerEvaluationLimit), UpperEvaluationLimit)
93             select double.IsNaN(scaledR) ? UpperEvaluationLimit : scaledR;
94    }
95
96    private bool IsValidValue(double d) {
97      return !double.IsInfinity(d) && !double.IsNaN(d);
98    }
99
100    public override object Clone(IDictionary<Guid, object> clonedObjects) {
101      ScalingTreeEvaluator clone = (ScalingTreeEvaluator)base.Clone(clonedObjects);
102      clone.targetVariable = targetVariable;
103      return clone;
104    }
105
106    public override System.Xml.XmlNode GetXmlNode(string name, System.Xml.XmlDocument document, IDictionary<Guid, HeuristicLab.Core.IStorable> persistedObjects) {
107      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
108      XmlAttribute targetVariableAttribute = document.CreateAttribute("TargetVariable");
109      targetVariableAttribute.Value = targetVariable;
110      node.Attributes.Append(targetVariableAttribute);
111      return node;
112    }
113
114    public override void Populate(XmlNode node, IDictionary<Guid, HeuristicLab.Core.IStorable> restoredObjects) {
115      base.Populate(node, restoredObjects);
116      targetVariable = node.Attributes["TargetVariable"].Value;
117    }
118  }
119}
Note: See TracBrowser for help on using the repository browser.