Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Operator Architecture Refactoring/HeuristicLab.GP.StructureIdentification/3.4/HL2TreeEvaluator.cs @ 2553

Last change on this file since 2553 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 with HL2 semantics.
34  /// Not thread-safe!
35  /// </summary>
36  public class HL2TreeEvaluator : TreeEvaluatorBase {
37
38    protected override double EvaluateBakedCode() {
39      Instr currInstr = codeArr[PC++];
40      switch (currInstr.symbol) {
41        case EvaluatorSymbolTable.VARIABLE: {
42            int row = sampleIndex + currInstr.i_arg1;
43            if (row < 0 || row >= dataset.Rows) return double.NaN;
44            else return currInstr.d_arg0 * dataset.GetValue(row, currInstr.i_arg0);
45          }
46        case EvaluatorSymbolTable.CONSTANT: {
47            return currInstr.d_arg0;
48          }
49        case EvaluatorSymbolTable.DIFFERENTIAL: {
50            int row = sampleIndex + currInstr.i_arg1;
51            if (row < 1 || row >= dataset.Rows) return double.NaN;
52            else {
53              double prevValue = dataset.GetValue(row - 1, currInstr.i_arg0);
54              return currInstr.d_arg0 * (dataset.GetValue(row, currInstr.i_arg0) - prevValue);
55            }
56          }
57        case EvaluatorSymbolTable.MULTIPLICATION: {
58            double result = EvaluateBakedCode();
59            for (int i = 1; i < currInstr.arity; i++) {
60              result *= EvaluateBakedCode();
61            }
62            return result;
63          }
64        case EvaluatorSymbolTable.ADDITION: {
65            double sum = EvaluateBakedCode();
66            for (int i = 1; i < currInstr.arity; i++) {
67              sum += EvaluateBakedCode();
68            }
69            return sum;
70          }
71        case EvaluatorSymbolTable.SUBTRACTION: {
72            return EvaluateBakedCode() - EvaluateBakedCode();
73          }
74        case EvaluatorSymbolTable.DIVISION: {
75            double arg0 = EvaluateBakedCode();
76            double arg1 = EvaluateBakedCode();
77            if (double.IsNaN(arg0) || double.IsNaN(arg1)) return double.NaN;
78            if (Math.Abs(arg1) < (10e-20)) return 0.0; else return (arg0 / arg1);
79          }
80        case EvaluatorSymbolTable.COSINUS: {
81            return Math.Cos(EvaluateBakedCode());
82          }
83        case EvaluatorSymbolTable.SINUS: {
84            return Math.Sin(EvaluateBakedCode());
85          }
86        case EvaluatorSymbolTable.EXP: {
87            return Math.Exp(EvaluateBakedCode());
88          }
89        case EvaluatorSymbolTable.LOG: {
90            return Math.Log(EvaluateBakedCode());
91          }
92        case EvaluatorSymbolTable.POWER: {
93            double x = EvaluateBakedCode();
94            double p = EvaluateBakedCode();
95            return Math.Pow(x, p);
96          }
97        case EvaluatorSymbolTable.SIGNUM: {
98            double value = EvaluateBakedCode();
99            if (double.IsNaN(value)) return double.NaN;
100            if (value < 0.0) return -1.0;
101            if (value > 0.0) return 1.0;
102            return 0.0;
103          }
104        case EvaluatorSymbolTable.SQRT: {
105            return Math.Sqrt(EvaluateBakedCode());
106          }
107        case EvaluatorSymbolTable.TANGENS: {
108            return Math.Tan(EvaluateBakedCode());
109          }
110        case EvaluatorSymbolTable.AND: { // only defined for inputs 1 and 0
111            double result = EvaluateBakedCode();
112            bool hasNaNBranch = false;
113            for (int i = 1; i < currInstr.arity; i++) {
114              if (result < 0.5 || double.IsNaN(result)) hasNaNBranch |= double.IsNaN(EvaluateBakedCode());
115              else {
116                result = EvaluateBakedCode();
117              }
118            }
119            if (hasNaNBranch || double.IsNaN(result)) return double.NaN;
120            if (result < 0.5) return 0.0;
121            return 1.0;
122          }
123        case EvaluatorSymbolTable.EQU: {
124            double x = EvaluateBakedCode();
125            double y = EvaluateBakedCode();
126            if (double.IsNaN(x) || double.IsNaN(y)) return double.NaN;
127            // direct comparison of double values is most likely incorrect but
128            // that's the way how it is implemented in the standard HL2 function library
129            if (x == y) return 1.0; else return 0.0;
130          }
131        case EvaluatorSymbolTable.GT: {
132            double x = EvaluateBakedCode();
133            double y = EvaluateBakedCode();
134            if (double.IsNaN(x) || double.IsNaN(y)) return double.NaN;
135            if (x > y) return 1.0;
136            return 0.0;
137          }
138        case EvaluatorSymbolTable.IFTE: { // only defined for condition 0 or 1
139            double condition = EvaluateBakedCode();
140            double result;
141            bool hasNaNBranch = false;
142            if (double.IsNaN(condition)) return double.NaN;
143            if (condition > 0.5) {
144              result = EvaluateBakedCode(); hasNaNBranch = double.IsNaN(EvaluateBakedCode());
145            } else {
146              hasNaNBranch = double.IsNaN(EvaluateBakedCode()); result = EvaluateBakedCode();
147            }
148            if (hasNaNBranch) return double.NaN;
149            return result;
150          }
151        case EvaluatorSymbolTable.LT: {
152            double x = EvaluateBakedCode();
153            double y = EvaluateBakedCode();
154            if (double.IsNaN(x) || double.IsNaN(y)) return double.NaN;
155            if (x < y) return 1.0;
156            return 0.0;
157          }
158        case EvaluatorSymbolTable.NOT: { // only defined for inputs 0 or 1
159            double result = EvaluateBakedCode();
160            if (double.IsNaN(result)) return double.NaN;
161            if (result < 0.5) return 1.0;
162            return 0.0;
163          }
164        case EvaluatorSymbolTable.OR: { // only defined for inputs 0 or 1
165            double result = EvaluateBakedCode();
166            bool hasNaNBranch = false;
167            for (int i = 1; i < currInstr.arity; i++) {
168              if (double.IsNaN(result) || result > 0.5) hasNaNBranch |= double.IsNaN(EvaluateBakedCode());
169              else
170                result = EvaluateBakedCode();
171            }
172            if (hasNaNBranch || double.IsNaN(result)) return double.NaN;
173            if (result > 0.5) return 1.0;
174            return 0.0;
175          }
176        default: {
177            throw new NotImplementedException();
178          }
179      }
180    }
181  }
182}
Note: See TracBrowser for help on using the repository browser.