Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/TreeEvaluatorBase.cs @ 2222

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

Merged changes from GP-refactoring branch back into the trunk #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 HeuristicLab.Core;
25using HeuristicLab.DataAnalysis;
26using HeuristicLab.GP.Interfaces;
27
28namespace HeuristicLab.GP.StructureIdentification {
29  /// <summary>
30  /// Base class for tree evaluators
31  /// </summary>
32  public abstract class TreeEvaluatorBase : ItemBase, ITreeEvaluator {
33    protected const double EPSILON = 1.0e-7;
34    protected double maxValue;
35    protected double minValue;
36
37    protected class Instr {
38      public double d_arg0;
39      public short i_arg0;
40      public short i_arg1;
41      public byte arity;
42      public byte symbol;
43    }
44
45    protected Instr[] codeArr;
46    protected int PC;
47    protected Dataset dataset;
48    protected int sampleIndex;
49
50    public void PrepareForEvaluation(Dataset dataset, int targetVariable, int start, int end, double punishmentFactor, IFunctionTree functionTree) {
51      this.dataset = dataset;
52      // calculate upper and lower bounds for the estimated value (mean +/- punishmentFactor * range)
53      double mean = dataset.GetMean(targetVariable, start, end);
54      double range = dataset.GetRange(targetVariable, start, end);
55      maxValue = mean + punishmentFactor * range;
56      minValue = mean - punishmentFactor * range;
57
58      codeArr = new Instr[functionTree.GetSize()];
59      int i = 0;
60      foreach (IFunctionTree tree in IteratePrefix(functionTree)) {
61        codeArr[i++] = TranslateToInstr(tree);
62      }
63    }
64
65    private IEnumerable<IFunctionTree> IteratePrefix(IFunctionTree functionTree) {
66      List<IFunctionTree> prefixForm = new List<IFunctionTree>();
67      prefixForm.Add(functionTree);
68      foreach (IFunctionTree subTree in functionTree.SubTrees) {
69        prefixForm.AddRange(IteratePrefix(subTree));
70      }
71      return prefixForm;
72    }
73
74    private Instr TranslateToInstr(IFunctionTree tree) {
75      Instr instr = new Instr();
76      instr.arity = (byte)tree.SubTrees.Count;
77      instr.symbol = EvaluatorSymbolTable.MapFunction(tree.Function);
78      switch (instr.symbol) {
79        case EvaluatorSymbolTable.DIFFERENTIAL:
80        case EvaluatorSymbolTable.VARIABLE: {
81            VariableFunctionTree varTree = (VariableFunctionTree)tree;
82            instr.i_arg0 = (short)dataset.GetVariableIndex(varTree.VariableName);
83            instr.d_arg0 = varTree.Weight;
84            instr.i_arg1 = (short)varTree.SampleOffset;
85            break;
86          }
87        case EvaluatorSymbolTable.CONSTANT: {
88            ConstantFunctionTree constTree = (ConstantFunctionTree)tree;
89            instr.d_arg0 = constTree.Value;
90            break;
91          }
92        case EvaluatorSymbolTable.UNKNOWN: {
93            throw new NotSupportedException("Unknown function symbol: " + instr.symbol);
94          }
95      }
96      return instr;
97    }
98
99    public double Evaluate(int sampleIndex) {
100      PC = 0;
101      this.sampleIndex = sampleIndex;
102
103      double estimated = EvaluateBakedCode();
104      if (double.IsNaN(estimated) || double.IsInfinity(estimated)) estimated = maxValue;
105      else if (estimated < minValue) estimated = minValue;
106      else if (estimated > maxValue) estimated = maxValue;
107      return estimated;
108    }
109
110    // skips a whole branch
111    protected void SkipBakedCode() {
112      int i = 1;
113      while (i > 0) {
114        i += codeArr[PC++].arity;
115        i--;
116      }
117    }
118
119    protected abstract double EvaluateBakedCode();
120  }
121}
Note: See TracBrowser for help on using the repository browser.