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 |
|
---|
22 | using System;
|
---|
23 | using System.Diagnostics;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.DataAnalysis;
|
---|
26 | using HeuristicLab.GP.Interfaces;
|
---|
27 | using System.Collections.Generic; // double.IsAlmost extension
|
---|
28 | using System.Linq;
|
---|
29 | using System.Xml;
|
---|
30 | namespace 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 | double[] result = base.Evaluate(dataset, tree, rows).ToArray();
|
---|
61 | int targetVariableIndex = dataset.GetVariableIndex(targetVariable);
|
---|
62 | double tMean = dataset.GetMean(targetVariableIndex);
|
---|
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];
|
---|
68 | double t = dataset.GetValue(rows.ElementAt(i), targetVariableIndex);
|
---|
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 | }
|
---|
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 | }
|
---|
101 | }
|
---|
102 | }
|
---|