Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP-Refactoring-713/sources/HeuristicLab.GP.StructureIdentification/3.3/HL2TreeEvaluator.cs @ 2212

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

GP Refactoring: #713

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