Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP-Refactoring-713/sources/HeuristicLab.GP.StructureIdentification/3.3/TreeEvaluatorBase.cs @ 2202

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

Created a branch for #713

File size: 4.2 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.Data;
31
32namespace HeuristicLab.GP.StructureIdentification {
33  /// <summary>
34  /// Base class for tree evaluators
35  /// </summary>
36  public abstract class TreeEvaluatorBase : ItemBase, ITreeEvaluator {
37    protected const double EPSILON = 1.0e-7;
38    protected double maxValue;
39    protected double minValue;
40
41    protected class Instr {
42      public double d_arg0;
43      public short i_arg0;
44      public short i_arg1;
45      public byte arity;
46      public byte symbol;
47      public IFunction function;
48    }
49
50    protected Instr[] codeArr;
51    protected int PC;
52    protected Dataset dataset;
53    protected int sampleIndex;
54
55    public void PrepareForEvaluation(Dataset dataset, int targetVariable, int start, int end, double punishmentFactor, IFunctionTree functionTree) {
56      this.dataset = dataset;
57      // calculate upper and lower bounds for the estimated value (mean +/- punishmentFactor * range)
58      double mean = dataset.GetMean(targetVariable, start, end);
59      double range = dataset.GetRange(targetVariable, start, end);
60      maxValue = mean + punishmentFactor * range;
61      minValue = mean - punishmentFactor * range;
62
63      BakedFunctionTree bakedTree = functionTree as BakedFunctionTree;
64      if (bakedTree == null) throw new ArgumentException("TreeEvaluators can only evaluate BakedFunctionTrees");
65
66      List<LightWeightFunction> linearRepresentation = bakedTree.LinearRepresentation;
67      codeArr = new Instr[linearRepresentation.Count];
68      int i = 0;
69      foreach (LightWeightFunction f in linearRepresentation) {
70        codeArr[i++] = TranslateToInstr(f);
71      }
72    }
73
74    private Instr TranslateToInstr(LightWeightFunction f) {
75      Instr instr = new Instr();
76      instr.arity = f.arity;
77      instr.symbol = EvaluatorSymbolTable.MapFunction(f.functionType);
78      switch (instr.symbol) {
79        case EvaluatorSymbolTable.DIFFERENTIAL:
80        case EvaluatorSymbolTable.VARIABLE: {
81            instr.i_arg0 = (short)dataset.GetVariableIndex((((StringData)f.localData[1].Value).Data)); // var
82            instr.d_arg0 = ((DoubleData)f.localData[0].Value).Data; // weight
83            instr.i_arg1 = (short)((ConstrainedIntData)f.localData[2].Value).Data; // sample-offset
84            break;
85          }
86        case EvaluatorSymbolTable.CONSTANT: {
87            instr.d_arg0 = ((DoubleData)f.localData[0].Value).Data; // value
88            break;
89          }
90        case EvaluatorSymbolTable.UNKNOWN: {
91            instr.function = f.functionType;
92            break;
93          }
94      }
95      return instr;
96    }
97
98    public double Evaluate(int sampleIndex) {
99      PC = 0;
100      this.sampleIndex = sampleIndex;
101
102      double estimated = EvaluateBakedCode();
103      if (double.IsNaN(estimated) || double.IsInfinity(estimated)) estimated = maxValue;
104      else if (estimated < minValue) estimated = minValue;
105      else if (estimated > maxValue) estimated = maxValue;
106      return estimated;
107    }
108
109    // skips a whole branch
110    protected void SkipBakedCode() {
111      int i = 1;
112      while (i > 0) {
113        i += codeArr[PC++].arity;
114        i--;
115      }
116    }
117
118    protected abstract double EvaluateBakedCode();
119  }
120}
Note: See TracBrowser for help on using the repository browser.