Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP.StructureIdentification/3.4/BakedTreeEvaluator.cs @ 2325

Last change on this file since 2325 was 1836, checked in by gkronber, 16 years ago

implemented persistence of tree evaluators in a common base class for tree evaluators. #615 (Evaluation of HL3 function trees should be equivalent to evaluation in HL2)

File size: 7.3 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  /// Evaluates FunctionTrees recursively by interpretation of the function symbols in each node.
34  /// Not thread-safe!
35  /// </summary>
36  public class BakedTreeEvaluator : TreeEvaluatorBase {
37
38
39    protected override double EvaluateBakedCode() {
40      Instr currInstr = codeArr[PC++];
41      switch (currInstr.symbol) {
42        case EvaluatorSymbolTable.VARIABLE: {
43            int row = sampleIndex + currInstr.i_arg1;
44            if (row < 0 || row >= dataset.Rows) return double.NaN;
45            else return currInstr.d_arg0 * dataset.GetValue(row, currInstr.i_arg0);
46          }
47        case EvaluatorSymbolTable.CONSTANT: {
48            return currInstr.d_arg0;
49          }
50        case EvaluatorSymbolTable.DIFFERENTIAL: {
51            int row = sampleIndex + currInstr.i_arg1;
52            if (row < 0 || row >= dataset.Rows) return double.NaN;
53            else if (row < 1) return 0.0;
54            else {
55              double prevValue = dataset.GetValue(row - 1, currInstr.i_arg0);
56              if (double.IsNaN(prevValue) || double.IsInfinity(prevValue)) return 0.0;
57              else return currInstr.d_arg0 * (dataset.GetValue(row, currInstr.i_arg0) - prevValue);
58            }
59          }
60        case EvaluatorSymbolTable.MULTIPLICATION: {
61            double result = EvaluateBakedCode();
62            for (int i = 1; i < currInstr.arity; i++) {
63              result *= EvaluateBakedCode();
64            }
65            return result;
66          }
67        case EvaluatorSymbolTable.ADDITION: {
68            double sum = EvaluateBakedCode();
69            for (int i = 1; i < currInstr.arity; i++) {
70              sum += EvaluateBakedCode();
71            }
72            return sum;
73          }
74        case EvaluatorSymbolTable.SUBTRACTION: {
75            double result = EvaluateBakedCode();
76            for (int i = 1; i < currInstr.arity; i++) {
77              result -= EvaluateBakedCode();
78            }
79            return result;
80          }
81        case EvaluatorSymbolTable.DIVISION: {
82            double result;
83            result = EvaluateBakedCode();
84            for (int i = 1; i < currInstr.arity; i++) {
85              result /= EvaluateBakedCode();
86            }
87            if (double.IsInfinity(result)) return 0.0;
88            else return result;
89          }
90        case EvaluatorSymbolTable.AVERAGE: {
91            double sum = EvaluateBakedCode();
92            for (int i = 1; i < currInstr.arity; i++) {
93              sum += EvaluateBakedCode();
94            }
95            return sum / currInstr.arity;
96          }
97        case EvaluatorSymbolTable.COSINUS: {
98            return Math.Cos(EvaluateBakedCode());
99          }
100        case EvaluatorSymbolTable.SINUS: {
101            return Math.Sin(EvaluateBakedCode());
102          }
103        case EvaluatorSymbolTable.EXP: {
104            return Math.Exp(EvaluateBakedCode());
105          }
106        case EvaluatorSymbolTable.LOG: {
107            return Math.Log(EvaluateBakedCode());
108          }
109        case EvaluatorSymbolTable.POWER: {
110            double x = EvaluateBakedCode();
111            double p = EvaluateBakedCode();
112            return Math.Pow(x, p);
113          }
114        case EvaluatorSymbolTable.SIGNUM: {
115            double value = EvaluateBakedCode();
116            if (double.IsNaN(value)) return double.NaN;
117            else return Math.Sign(value);
118          }
119        case EvaluatorSymbolTable.SQRT: {
120            return Math.Sqrt(EvaluateBakedCode());
121          }
122        case EvaluatorSymbolTable.TANGENS: {
123            return Math.Tan(EvaluateBakedCode());
124          }
125        case EvaluatorSymbolTable.AND: { // only defined for inputs 1 and 0
126            double result = EvaluateBakedCode();
127            for (int i = 1; i < currInstr.arity; i++) {
128              if (result == 0.0) SkipBakedCode();
129              else {
130                result = EvaluateBakedCode();
131              }
132              Debug.Assert(result == 0.0 || result == 1.0);
133            }
134            return result;
135          }
136        case EvaluatorSymbolTable.EQU: {
137            double x = EvaluateBakedCode();
138            double y = EvaluateBakedCode();
139            if (Math.Abs(x - y) < EPSILON) return 1.0; else return 0.0;
140          }
141        case EvaluatorSymbolTable.GT: {
142            double x = EvaluateBakedCode();
143            double y = EvaluateBakedCode();
144            if (x > y) return 1.0;
145            else return 0.0;
146          }
147        case EvaluatorSymbolTable.IFTE: { // only defined for condition 0 or 1
148            double condition = EvaluateBakedCode();
149            Debug.Assert(condition == 0.0 || condition == 1.0);
150            double result;
151            if (condition == 0.0) {
152              result = EvaluateBakedCode(); SkipBakedCode();
153            } else {
154              SkipBakedCode(); result = EvaluateBakedCode();
155            }
156            return result;
157          }
158        case EvaluatorSymbolTable.LT: {
159            double x = EvaluateBakedCode();
160            double y = EvaluateBakedCode();
161            if (x < y) return 1.0;
162            else return 0.0;
163          }
164        case EvaluatorSymbolTable.NOT: { // only defined for inputs 0 or 1
165            double result = EvaluateBakedCode();
166            Debug.Assert(result == 0.0 || result == 1.0);
167            return Math.Abs(result - 1.0);
168          }
169        case EvaluatorSymbolTable.OR: { // only defined for inputs 0 or 1
170            double result = EvaluateBakedCode();
171            for (int i = 1; i < currInstr.arity; i++) {
172              if (result > 0.0) SkipBakedCode();
173              else {
174                result = EvaluateBakedCode();
175                Debug.Assert(result == 0.0 || result == 1.0);
176              }
177            }
178            return result;
179          }
180        case EvaluatorSymbolTable.XOR: { // only defined for inputs 0 or 1
181            double x = EvaluateBakedCode();
182            double y = EvaluateBakedCode();
183            return Math.Abs(x - y);
184          }
185        case EvaluatorSymbolTable.UNKNOWN: { // evaluate functions which are not statically defined directly
186            return currInstr.function.Apply();
187          }
188        default: {
189            throw new NotImplementedException();
190          }
191      }
192    }
193  }
194}
Note: See TracBrowser for help on using the repository browser.