Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive_Milestone3/sources/HeuristicLab.GP.StructureIdentification/3.3/TreeEvaluatorBase.cs @ 6479

Last change on this file since 6479 was 2034, checked in by gkronber, 16 years ago

Implemented a first version of an operator to calculate variable impacts of models (generated by GP or SVM). #644 (Variable impact of CEDMA models should be calculated and stored in the result DB)

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