Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Functions/BakedTreeEvaluator.cs @ 318

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

adapted and simplified evaluation code for ticket #168

File size: 9.0 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;
29
30namespace HeuristicLab.Functions {
31  internal class BakedTreeEvaluator : StorableBase {
32    private struct Instr {
33      public double d_arg0;
34      public int i_arg0;
35      public int i_arg1;
36      public int arity;
37      public int symbol;
38    }
39
40    private Instr[] codeArr;
41    private int PC;
42    private Dataset dataset;
43    private int sampleIndex;
44
45    // for persistence mechanism only
46    public BakedTreeEvaluator() {
47    }
48
49    public BakedTreeEvaluator(List<LightWeightFunction> linearRepresentation) {
50      codeArr = new Instr[linearRepresentation.Count];
51      int i = 0;
52      foreach(LightWeightFunction f in linearRepresentation) {
53        codeArr[i++] = TranslateToInstr(f);
54      }
55    }
56
57    private Instr TranslateToInstr(LightWeightFunction f) {
58      Instr instr = new Instr();
59      instr.arity = f.arity;
60      instr.symbol = EvaluatorSymbolTable.SymbolTable.MapFunction(f.functionType);
61      switch(instr.symbol) {
62        case EvaluatorSymbolTable.VARIABLE: {
63            instr.i_arg0 = (int)f.data[0]; // var
64            instr.d_arg0 = f.data[1]; // weight
65            instr.i_arg1 = (int)f.data[2]; // sample-offset
66            break;
67          }
68        case EvaluatorSymbolTable.CONSTANT: {
69            instr.d_arg0 = f.data[0]; // value
70            break;
71          }
72      }
73      return instr;
74    }
75
76    internal double Evaluate(Dataset dataset, int sampleIndex) {
77      PC = 0;
78      this.sampleIndex = sampleIndex;
79      this.dataset = dataset;
80      return EvaluateBakedCode();
81    }
82
83    private double EvaluateBakedCode() {
84      Instr currInstr = codeArr[PC++];
85      switch(currInstr.symbol) {
86        case EvaluatorSymbolTable.VARIABLE: {
87            int row = sampleIndex + currInstr.i_arg1;
88            if(row < 0 || row >= dataset.Rows) return double.NaN;
89            else return currInstr.d_arg0 * dataset.GetValue(row, currInstr.i_arg0);
90          }
91        case EvaluatorSymbolTable.CONSTANT: {
92            return currInstr.d_arg0;
93          }
94        case EvaluatorSymbolTable.MULTIPLICATION: {
95            double result = EvaluateBakedCode();
96            for(int i = 1; i < currInstr.arity; i++) {
97              result *= EvaluateBakedCode();
98            }
99            return result;
100          }
101        case EvaluatorSymbolTable.ADDITION: {
102            double sum = EvaluateBakedCode();
103            for(int i = 1; i < currInstr.arity; i++) {
104              sum += EvaluateBakedCode();
105            }
106            return sum;
107          }
108        case EvaluatorSymbolTable.SUBTRACTION: {
109            if(currInstr.arity == 1) {
110              return -EvaluateBakedCode();
111            } else {
112              double result = EvaluateBakedCode();
113              for(int i = 1; i < currInstr.arity; i++) {
114                result -= EvaluateBakedCode();
115              }
116              return result;
117            }
118          }
119        case EvaluatorSymbolTable.DIVISION: {
120            double result;
121            if(currInstr.arity == 1) {
122              result = 1.0 / EvaluateBakedCode();
123            } else {
124              result = EvaluateBakedCode();
125              for(int i = 1; i < currInstr.arity; i++) {
126                result /= EvaluateBakedCode();
127              }
128            }
129            if(double.IsInfinity(result)) return 0.0;
130            else return result;
131          }
132        case EvaluatorSymbolTable.AVERAGE: {
133            double sum = EvaluateBakedCode();
134            for(int i = 1; i < currInstr.arity; i++) {
135              sum += EvaluateBakedCode();
136            }
137            return sum / currInstr.arity;
138          }
139        case EvaluatorSymbolTable.COSINUS: {
140            return Math.Cos(EvaluateBakedCode());
141          }
142        case EvaluatorSymbolTable.SINUS: {
143            return Math.Sin(EvaluateBakedCode());
144          }
145        case EvaluatorSymbolTable.EXP: {
146            return Math.Exp(EvaluateBakedCode());
147          }
148        case EvaluatorSymbolTable.LOG: {
149            return Math.Log(EvaluateBakedCode());
150          }
151        case EvaluatorSymbolTable.POWER: {
152            double x = EvaluateBakedCode();
153            double p = EvaluateBakedCode();
154            return Math.Pow(x, p);
155          }
156        case EvaluatorSymbolTable.SIGNUM: {
157            double value = EvaluateBakedCode();
158            if(double.IsNaN(value)) return double.NaN;
159            else return Math.Sign(value);
160          }
161        case EvaluatorSymbolTable.SQRT: {
162            return Math.Sqrt(EvaluateBakedCode());
163          }
164        case EvaluatorSymbolTable.TANGENS: {
165            return Math.Tan(EvaluateBakedCode());
166          }
167        case EvaluatorSymbolTable.AND: {
168            double result = 1.0;
169            // have to evaluate all sub-trees, skipping would probably not lead to a big gain because
170            // we have to iterate over the linear structure anyway
171            for(int i = 0; i < currInstr.arity; i++) {
172              double x = Math.Round(EvaluateBakedCode());
173              if(x == 0 || x == 1.0) result *= x;
174              else result = double.NaN;
175            }
176            return result;
177          }
178        case EvaluatorSymbolTable.EQU: {
179            double x = EvaluateBakedCode();
180            double y = EvaluateBakedCode();
181            if(x == y) return 1.0; else return 0.0;
182          }
183        case EvaluatorSymbolTable.GT: {
184            double x = EvaluateBakedCode();
185            double y = EvaluateBakedCode();
186            if(x > y) return 1.0;
187            else return 0.0;
188          }
189        case EvaluatorSymbolTable.IFTE: {
190            double condition = Math.Round(EvaluateBakedCode());
191            double x = EvaluateBakedCode();
192            double y = EvaluateBakedCode();
193            if(condition < .5) return x;
194            else if(condition >= .5) return y;
195            else return double.NaN;
196          }
197        case EvaluatorSymbolTable.LT: {
198            double x = EvaluateBakedCode();
199            double y = EvaluateBakedCode();
200            if(x < y) return 1.0;
201            else return 0.0;
202          }
203        case EvaluatorSymbolTable.NOT: {
204            double result = Math.Round(EvaluateBakedCode());
205            if(result == 0.0) return 1.0;
206            else if(result == 1.0) return 0.0;
207            else return double.NaN;
208          }
209        case EvaluatorSymbolTable.OR: {
210            double result = 0.0; // default is false
211            for(int i = 0; i < currInstr.arity; i++) {
212              double x = Math.Round(EvaluateBakedCode());
213              if(x == 1.0 && result == 0.0) result = 1.0; // found first true (1.0) => set to true
214              else if(x != 0.0) result = double.NaN; // if it was not true it can only be false (0.0) all other cases are undefined => (NaN)
215            }
216            return result;
217          }
218        case EvaluatorSymbolTable.XOR: {
219            double x = Math.Round(EvaluateBakedCode());
220            double y = Math.Round(EvaluateBakedCode());
221            if(x == 0.0 && y == 0.0) return 0.0;
222            if(x == 1.0 && y == 0.0) return 1.0;
223            if(x == 0.0 && y == 1.0) return 1.0;
224            if(x == 1.0 && y == 1.0) return 0.0;
225            return double.NaN;
226          }
227        default: {
228            throw new NotImplementedException();
229          }
230      }
231    }
232
233    public override object Clone(IDictionary<Guid, object> clonedObjects) {
234      throw new NotImplementedException();
235    }
236
237    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
238      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
239      node.AppendChild(PersistenceManager.Persist("SymbolTable", EvaluatorSymbolTable.SymbolTable, document, persistedObjects));
240      return node;
241    }
242
243    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
244      base.Populate(node, restoredObjects);
245      PersistenceManager.Restore(node.SelectSingleNode("SymbolTable"), restoredObjects);
246    }
247  }
248}
Note: See TracBrowser for help on using the repository browser.