Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CEDMA-Exporter-715/sources/HeuristicLab.GP.StructureIdentification/3.4/TreeEvaluatorBase.cs @ 3921

Last change on this file since 3921 was 1914, checked in by epitzer, 16 years ago

Migration of DataAnalysis, GP, GP.StructureIdentification and Modeling to new Persistence-3.3 (#603)

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.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Core;
27using System.Xml;
28using System.Diagnostics;
29using HeuristicLab.DataAnalysis;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.GP.StructureIdentification {
33  /// <summary>
34  /// Base class for tree evaluators
35  /// </summary>
36  public abstract class TreeEvaluatorBase : ItemBase, ITreeEvaluator {
37
38    protected const double EPSILON = 1.0e-7;
39
40    [Storable]
41    protected double estimatedValueMax;
42
43    [Storable]
44    protected double estimatedValueMin;
45
46    [Storable]
47    protected Dataset dataset;
48
49    protected class Instr {
50      public double d_arg0;
51      public short i_arg0;
52      public short i_arg1;
53      public byte arity;
54      public byte symbol;
55      public IFunction function;
56    }
57
58    protected Instr[] codeArr;
59    protected int PC;
60    protected int sampleIndex;
61
62    public void ResetEvaluator(Dataset dataset, int targetVariable, int start, int end, double punishmentFactor) {
63      this.dataset = dataset;
64      double maximumPunishment = punishmentFactor * dataset.GetRange(targetVariable, start, end);
65
66      // get the mean of the values of the target variable to determine the max and min bounds of the estimated value
67      double targetMean = dataset.GetMean(targetVariable, start, end);
68      estimatedValueMin = targetMean - maximumPunishment;
69      estimatedValueMax = targetMean + maximumPunishment;
70    }
71
72    public void PrepareForEvaluation(IFunctionTree functionTree) {
73      BakedFunctionTree bakedTree = functionTree as BakedFunctionTree;
74      if (bakedTree == null) throw new ArgumentException("TreeEvaluators can only evaluate BakedFunctionTrees");
75
76      List<LightWeightFunction> linearRepresentation = bakedTree.LinearRepresentation;
77      codeArr = new Instr[linearRepresentation.Count];
78      int i = 0;
79      foreach (LightWeightFunction f in linearRepresentation) {
80        codeArr[i++] = TranslateToInstr(f);
81      }
82    }
83
84    private Instr TranslateToInstr(LightWeightFunction f) {
85      Instr instr = new Instr();
86      instr.arity = f.arity;
87      instr.symbol = EvaluatorSymbolTable.MapFunction(f.functionType);
88      switch (instr.symbol) {
89        case EvaluatorSymbolTable.DIFFERENTIAL:
90        case EvaluatorSymbolTable.VARIABLE: {
91            instr.i_arg0 = (short)f.data[0]; // var
92            instr.d_arg0 = f.data[1]; // weight
93            instr.i_arg1 = (short)f.data[2]; // sample-offset
94            break;
95          }
96        case EvaluatorSymbolTable.CONSTANT: {
97            instr.d_arg0 = f.data[0]; // value
98            break;
99          }
100        case EvaluatorSymbolTable.UNKNOWN: {
101            instr.function = f.functionType;
102            break;
103          }
104      }
105      return instr;
106    }
107
108    public double Evaluate(int sampleIndex) {
109      PC = 0;
110      this.sampleIndex = sampleIndex;
111
112      double estimated = EvaluateBakedCode();
113      if (double.IsNaN(estimated) || double.IsInfinity(estimated)) {
114        estimated = estimatedValueMax;
115      } else if (estimated > estimatedValueMax) {
116        estimated = estimatedValueMax;
117      } else if (estimated < estimatedValueMin) {
118        estimated = estimatedValueMin;
119      }
120      return estimated;
121    }
122
123    // skips a whole branch
124    protected void SkipBakedCode() {
125      int i = 1;
126      while (i > 0) {
127        i += codeArr[PC++].arity;
128        i--;
129      }
130    }
131
132    protected abstract double EvaluateBakedCode();
133
134    public override object Clone(IDictionary<Guid, object> clonedObjects) {
135      TreeEvaluatorBase clone = (TreeEvaluatorBase)base.Clone(clonedObjects);
136      if (!clonedObjects.ContainsKey(dataset.Guid)) {
137        clone.dataset = (Dataset)dataset.Clone(clonedObjects);
138      } else {
139        clone.dataset = (Dataset)clonedObjects[dataset.Guid];
140      }
141      clone.estimatedValueMax = estimatedValueMax;
142      clone.estimatedValueMin = estimatedValueMin;
143      return clone;
144    }
145  }
146}
Note: See TracBrowser for help on using the repository browser.