Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/HL3TreeEvaluator.cs @ 3155

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

Implemented #824 (Refactor: ITreeEvaluator interface to provide a method that evaluates a tree on a range of samples.)

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