Free cookie consent management tool by TermsFeed Policy Generator

source: branches/plugins/HeuristicLab.Functions/3.2/BakedTreeEvaluator.cs @ 1543

Last change on this file since 1543 was 642, checked in by gkronber, 16 years ago

fixed #169 (Possibility to extend the set of available functions for GP via plugins)

File size: 9.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.DataAnalysis;
27using HeuristicLab.Core;
28using System.Xml;
29using System.Diagnostics;
30
31namespace HeuristicLab.Functions {
32  internal class BakedTreeEvaluator : IEvaluator {
33    private const int MAX_TREE_SIZE = 4096;
34    private const double EPSILON = 1.0e-7;
35
36    private class Instr {
37      public double d_arg0;
38      public int i_arg0;
39      public int i_arg1;
40      public int arity;
41      public int symbol;
42      public IFunction function;
43    }
44
45    private Instr[] codeArr;
46    private int PC;
47    private Dataset dataset;
48    private int sampleIndex;
49
50
51    public BakedTreeEvaluator() {
52      codeArr = new Instr[MAX_TREE_SIZE];
53      for(int i = 0; i < MAX_TREE_SIZE; i++) {
54        codeArr[i] = new Instr();
55      }
56    }
57
58    public void ResetEvaluator(IFunctionTree functionTree, Dataset dataset) {
59      this.dataset = dataset;
60      List<LightWeightFunction> linearRepresentation = ((BakedFunctionTree)functionTree).LinearRepresentation;
61      int i = 0;
62      foreach(LightWeightFunction f in linearRepresentation) {
63        TranslateToInstr(f, codeArr[i++]);
64      }
65    }
66
67    private Instr TranslateToInstr(LightWeightFunction f, Instr instr) {
68      instr.arity = f.arity;
69      instr.symbol = EvaluatorSymbolTable.MapFunction(f.functionType);
70      switch(instr.symbol) {
71        case EvaluatorSymbolTable.DIFFERENTIAL:
72        case EvaluatorSymbolTable.VARIABLE: {
73            instr.i_arg0 = (int)f.data[0]; // var
74            instr.d_arg0 = f.data[1]; // weight
75            instr.i_arg1 = (int)f.data[2]; // sample-offset
76            break;
77          }
78        case EvaluatorSymbolTable.CONSTANT: {
79            instr.d_arg0 = f.data[0]; // value
80            break;
81          }
82        case EvaluatorSymbolTable.UNKNOWN: {
83            instr.function = f.functionType;
84            break;
85          }
86      }
87      return instr;
88    }
89
90    public double Evaluate(int sampleIndex) {
91      PC = 0;
92      this.sampleIndex = sampleIndex;
93      return EvaluateBakedCode();
94    }
95
96    // skips a whole branch
97    private void SkipBakedCode() {
98      int i = 1;
99      while(i > 0) {
100        i += codeArr[PC++].arity;
101        i--;
102      }
103    }
104
105    private double EvaluateBakedCode() {
106      Instr currInstr = codeArr[PC++];
107      switch(currInstr.symbol) {
108        case EvaluatorSymbolTable.VARIABLE: {
109            int row = sampleIndex + currInstr.i_arg1;
110            if(row < 0 || row >= dataset.Rows) return double.NaN;
111            else return currInstr.d_arg0 * dataset.GetValue(row, currInstr.i_arg0);
112          }
113        case EvaluatorSymbolTable.CONSTANT: {
114            return currInstr.d_arg0;
115          }
116        case EvaluatorSymbolTable.DIFFERENTIAL: {
117            int row = sampleIndex + currInstr.i_arg1;
118            if(row < 1 || row >= dataset.Rows) return double.NaN;
119            else return currInstr.d_arg0 * (dataset.GetValue(row, currInstr.i_arg0) - dataset.GetValue(row - 1, currInstr.i_arg0));
120          }
121        case EvaluatorSymbolTable.MULTIPLICATION: {
122            double result = EvaluateBakedCode();
123            for(int i = 1; i < currInstr.arity; i++) {
124              result *= EvaluateBakedCode();
125            }
126            return result;
127          }
128        case EvaluatorSymbolTable.ADDITION: {
129            double sum = EvaluateBakedCode();
130            for(int i = 1; i < currInstr.arity; i++) {
131              sum += EvaluateBakedCode();
132            }
133            return sum;
134          }
135        case EvaluatorSymbolTable.SUBTRACTION: {
136            if(currInstr.arity == 1) {
137              return -EvaluateBakedCode();
138            } else {
139              double result = EvaluateBakedCode();
140              for(int i = 1; i < currInstr.arity; i++) {
141                result -= EvaluateBakedCode();
142              }
143              return result;
144            }
145          }
146        case EvaluatorSymbolTable.DIVISION: {
147            double result;
148            if(currInstr.arity == 1) {
149              result = 1.0 / EvaluateBakedCode();
150            } else {
151              result = EvaluateBakedCode();
152              for(int i = 1; i < currInstr.arity; i++) {
153                result /= EvaluateBakedCode();
154              }
155            }
156            if(double.IsInfinity(result)) return 0.0;
157            else return result;
158          }
159        case EvaluatorSymbolTable.AVERAGE: {
160            double sum = EvaluateBakedCode();
161            for(int i = 1; i < currInstr.arity; i++) {
162              sum += EvaluateBakedCode();
163            }
164            return sum / currInstr.arity;
165          }
166        case EvaluatorSymbolTable.COSINUS: {
167            return Math.Cos(EvaluateBakedCode());
168          }
169        case EvaluatorSymbolTable.SINUS: {
170            return Math.Sin(EvaluateBakedCode());
171          }
172        case EvaluatorSymbolTable.EXP: {
173            return Math.Exp(EvaluateBakedCode());
174          }
175        case EvaluatorSymbolTable.LOG: {
176            return Math.Log(EvaluateBakedCode());
177          }
178        case EvaluatorSymbolTable.POWER: {
179            double x = EvaluateBakedCode();
180            double p = EvaluateBakedCode();
181            return Math.Pow(x, p);
182          }
183        case EvaluatorSymbolTable.SIGNUM: {
184            double value = EvaluateBakedCode();
185            if(double.IsNaN(value)) return double.NaN;
186            else return Math.Sign(value);
187          }
188        case EvaluatorSymbolTable.SQRT: {
189            return Math.Sqrt(EvaluateBakedCode());
190          }
191        case EvaluatorSymbolTable.TANGENS: {
192            return Math.Tan(EvaluateBakedCode());
193          }
194        case EvaluatorSymbolTable.AND: { // only defined for inputs 1 and 0
195            double result = EvaluateBakedCode();
196            for(int i = 1; i < currInstr.arity; i++) {
197              if(result == 0.0) SkipBakedCode();
198              else {
199                result = EvaluateBakedCode();
200              }
201              Debug.Assert(result == 0.0 || result == 1.0);
202            }
203            return result;
204          }
205        case EvaluatorSymbolTable.EQU: {
206            double x = EvaluateBakedCode();
207            double y = EvaluateBakedCode();
208            if(Math.Abs(x - y) < EPSILON) return 1.0; else return 0.0;
209          }
210        case EvaluatorSymbolTable.GT: {
211            double x = EvaluateBakedCode();
212            double y = EvaluateBakedCode();
213            if(x > y) return 1.0;
214            else return 0.0;
215          }
216        case EvaluatorSymbolTable.IFTE: { // only defined for condition 0 or 1
217            double condition = EvaluateBakedCode();
218            Debug.Assert(condition == 0.0 || condition == 1.0);
219            double result;
220            if(condition == 0.0) {
221              result = EvaluateBakedCode(); SkipBakedCode();
222            } else {
223              SkipBakedCode(); result = EvaluateBakedCode();
224            }
225            return result;
226          }
227        case EvaluatorSymbolTable.LT: {
228            double x = EvaluateBakedCode();
229            double y = EvaluateBakedCode();
230            if(x < y) return 1.0;
231            else return 0.0;
232          }
233        case EvaluatorSymbolTable.NOT: { // only defined for inputs 0 or 1
234            double result = EvaluateBakedCode();
235            Debug.Assert(result == 0.0 || result == 1.0);
236            return Math.Abs(result - 1.0);
237          }
238        case EvaluatorSymbolTable.OR: { // only defined for inputs 0 or 1
239            double result = EvaluateBakedCode();
240            for(int i = 1; i < currInstr.arity; i++) {
241              if(result > 0.0) SkipBakedCode();
242              else {
243                result = EvaluateBakedCode();
244                Debug.Assert(result == 0.0 || result == 1.0);
245              }
246            }
247            return result;
248          }
249        case EvaluatorSymbolTable.XOR: { // only defined for inputs 0 or 1
250            double x = EvaluateBakedCode();
251            double y = EvaluateBakedCode();
252            return Math.Abs(x - y);
253          }
254        case EvaluatorSymbolTable.UNKNOWN: { // evaluate functions which are not statically defined directly
255            return currInstr.function.Apply();
256          }
257        default: {
258            throw new NotImplementedException();
259          }
260      }
261    }
262  }
263}
Note: See TracBrowser for help on using the repository browser.