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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Text;
|
---|
26 | using HeuristicLab.DataAnalysis;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using System.Xml;
|
---|
29 | using System.Diagnostics;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Functions {
|
---|
32 | internal class BakedTreeEvaluator : IEvaluator {
|
---|
33 | private const int MAX_TREE_SIZE = 4096;
|
---|
34 | private const double EPSILON = 1.0e-7;
|
---|
35 |
|
---|
36 | private class Instr {
|
---|
37 | public double d_arg0;
|
---|
38 | public int i_arg0;
|
---|
39 | public int i_arg1;
|
---|
40 | public int arity;
|
---|
41 | public int symbol;
|
---|
42 | }
|
---|
43 |
|
---|
44 | private Instr[] codeArr;
|
---|
45 | private int PC;
|
---|
46 | private Dataset dataset;
|
---|
47 | private int sampleIndex;
|
---|
48 |
|
---|
49 |
|
---|
50 | public BakedTreeEvaluator() {
|
---|
51 | codeArr = new Instr[MAX_TREE_SIZE];
|
---|
52 | for(int i = 0; i < MAX_TREE_SIZE; i++) {
|
---|
53 | codeArr[i] = new Instr();
|
---|
54 | }
|
---|
55 | }
|
---|
56 |
|
---|
57 | public void ResetEvaluator(IFunctionTree functionTree, Dataset dataset) {
|
---|
58 | this.dataset = dataset;
|
---|
59 | List<LightWeightFunction> linearRepresentation = ((BakedFunctionTree)functionTree).LinearRepresentation;
|
---|
60 | int i = 0;
|
---|
61 | foreach(LightWeightFunction f in linearRepresentation) {
|
---|
62 | TranslateToInstr(f, codeArr[i++]);
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | private Instr TranslateToInstr(LightWeightFunction f, Instr instr) {
|
---|
67 | instr.arity = f.arity;
|
---|
68 | instr.symbol = EvaluatorSymbolTable.MapFunction(f.functionType);
|
---|
69 | switch(instr.symbol) {
|
---|
70 | case EvaluatorSymbolTable.DIFFERENTIAL:
|
---|
71 | case EvaluatorSymbolTable.VARIABLE: {
|
---|
72 | instr.i_arg0 = (int)f.data[0]; // var
|
---|
73 | instr.d_arg0 = f.data[1]; // weight
|
---|
74 | instr.i_arg1 = (int)f.data[2]; // sample-offset
|
---|
75 | break;
|
---|
76 | }
|
---|
77 | case EvaluatorSymbolTable.CONSTANT: {
|
---|
78 | instr.d_arg0 = f.data[0]; // value
|
---|
79 | break;
|
---|
80 | }
|
---|
81 | }
|
---|
82 | return instr;
|
---|
83 | }
|
---|
84 |
|
---|
85 | public double Evaluate(int sampleIndex) {
|
---|
86 | PC = 0;
|
---|
87 | this.sampleIndex = sampleIndex;
|
---|
88 | return EvaluateBakedCode();
|
---|
89 | }
|
---|
90 |
|
---|
91 | // skips a whole branch
|
---|
92 | private void SkipBakedCode() {
|
---|
93 | int i = 1;
|
---|
94 | while(i > 0) {
|
---|
95 | i+=codeArr[PC++].arity;
|
---|
96 | i--;
|
---|
97 | }
|
---|
98 | }
|
---|
99 |
|
---|
100 | private double EvaluateBakedCode() {
|
---|
101 | Instr currInstr = codeArr[PC++];
|
---|
102 | switch(currInstr.symbol) {
|
---|
103 | case EvaluatorSymbolTable.VARIABLE: {
|
---|
104 | int row = sampleIndex + currInstr.i_arg1;
|
---|
105 | if(row < 0 || row >= dataset.Rows) return double.NaN;
|
---|
106 | else return currInstr.d_arg0 * dataset.GetValue(row, currInstr.i_arg0);
|
---|
107 | }
|
---|
108 | case EvaluatorSymbolTable.CONSTANT: {
|
---|
109 | return currInstr.d_arg0;
|
---|
110 | }
|
---|
111 | case EvaluatorSymbolTable.DIFFERENTIAL: {
|
---|
112 | int row = sampleIndex + currInstr.i_arg1;
|
---|
113 | if(row < 1 || row >= dataset.Rows) return double.NaN;
|
---|
114 | else return currInstr.d_arg0 * (dataset.GetValue(row, currInstr.i_arg0) - dataset.GetValue(row - 1, currInstr.i_arg0));
|
---|
115 | }
|
---|
116 | case EvaluatorSymbolTable.MULTIPLICATION: {
|
---|
117 | double result = EvaluateBakedCode();
|
---|
118 | for(int i = 1; i < currInstr.arity; i++) {
|
---|
119 | result *= EvaluateBakedCode();
|
---|
120 | }
|
---|
121 | return result;
|
---|
122 | }
|
---|
123 | case EvaluatorSymbolTable.ADDITION: {
|
---|
124 | double sum = EvaluateBakedCode();
|
---|
125 | for(int i = 1; i < currInstr.arity; i++) {
|
---|
126 | sum += EvaluateBakedCode();
|
---|
127 | }
|
---|
128 | return sum;
|
---|
129 | }
|
---|
130 | case EvaluatorSymbolTable.SUBTRACTION: {
|
---|
131 | if(currInstr.arity == 1) {
|
---|
132 | return -EvaluateBakedCode();
|
---|
133 | } else {
|
---|
134 | double result = EvaluateBakedCode();
|
---|
135 | for(int i = 1; i < currInstr.arity; i++) {
|
---|
136 | result -= EvaluateBakedCode();
|
---|
137 | }
|
---|
138 | return result;
|
---|
139 | }
|
---|
140 | }
|
---|
141 | case EvaluatorSymbolTable.DIVISION: {
|
---|
142 | double result;
|
---|
143 | if(currInstr.arity == 1) {
|
---|
144 | result = 1.0 / EvaluateBakedCode();
|
---|
145 | } else {
|
---|
146 | result = EvaluateBakedCode();
|
---|
147 | for(int i = 1; i < currInstr.arity; i++) {
|
---|
148 | result /= EvaluateBakedCode();
|
---|
149 | }
|
---|
150 | }
|
---|
151 | if(double.IsInfinity(result)) return 0.0;
|
---|
152 | else return result;
|
---|
153 | }
|
---|
154 | case EvaluatorSymbolTable.AVERAGE: {
|
---|
155 | double sum = EvaluateBakedCode();
|
---|
156 | for(int i = 1; i < currInstr.arity; i++) {
|
---|
157 | sum += EvaluateBakedCode();
|
---|
158 | }
|
---|
159 | return sum / currInstr.arity;
|
---|
160 | }
|
---|
161 | case EvaluatorSymbolTable.COSINUS: {
|
---|
162 | return Math.Cos(EvaluateBakedCode());
|
---|
163 | }
|
---|
164 | case EvaluatorSymbolTable.SINUS: {
|
---|
165 | return Math.Sin(EvaluateBakedCode());
|
---|
166 | }
|
---|
167 | case EvaluatorSymbolTable.EXP: {
|
---|
168 | return Math.Exp(EvaluateBakedCode());
|
---|
169 | }
|
---|
170 | case EvaluatorSymbolTable.LOG: {
|
---|
171 | return Math.Log(EvaluateBakedCode());
|
---|
172 | }
|
---|
173 | case EvaluatorSymbolTable.POWER: {
|
---|
174 | double x = EvaluateBakedCode();
|
---|
175 | double p = EvaluateBakedCode();
|
---|
176 | return Math.Pow(x, p);
|
---|
177 | }
|
---|
178 | case EvaluatorSymbolTable.SIGNUM: {
|
---|
179 | double value = EvaluateBakedCode();
|
---|
180 | if(double.IsNaN(value)) return double.NaN;
|
---|
181 | else return Math.Sign(value);
|
---|
182 | }
|
---|
183 | case EvaluatorSymbolTable.SQRT: {
|
---|
184 | return Math.Sqrt(EvaluateBakedCode());
|
---|
185 | }
|
---|
186 | case EvaluatorSymbolTable.TANGENS: {
|
---|
187 | return Math.Tan(EvaluateBakedCode());
|
---|
188 | }
|
---|
189 | case EvaluatorSymbolTable.AND: { // only defined for inputs 1 and 0
|
---|
190 | double result = EvaluateBakedCode();
|
---|
191 | for(int i = 1; i < currInstr.arity; i++) {
|
---|
192 | if(result == 0.0) SkipBakedCode();
|
---|
193 | else {
|
---|
194 | result = EvaluateBakedCode();
|
---|
195 | }
|
---|
196 | Debug.Assert(result == 0.0 || result == 1.0);
|
---|
197 | }
|
---|
198 | return result;
|
---|
199 | }
|
---|
200 | case EvaluatorSymbolTable.EQU: {
|
---|
201 | double x = EvaluateBakedCode();
|
---|
202 | double y = EvaluateBakedCode();
|
---|
203 | if(Math.Abs(x - y) < EPSILON) return 1.0; else return 0.0;
|
---|
204 | }
|
---|
205 | case EvaluatorSymbolTable.GT: {
|
---|
206 | double x = EvaluateBakedCode();
|
---|
207 | double y = EvaluateBakedCode();
|
---|
208 | if(x > y) return 1.0;
|
---|
209 | else return 0.0;
|
---|
210 | }
|
---|
211 | case EvaluatorSymbolTable.IFTE: { // only defined for condition 0 or 1
|
---|
212 | double condition = EvaluateBakedCode();
|
---|
213 | Debug.Assert(condition == 0.0 || condition == 1.0);
|
---|
214 | double result;
|
---|
215 | if(condition == 0.0) {
|
---|
216 | result = EvaluateBakedCode(); SkipBakedCode();
|
---|
217 | } else {
|
---|
218 | SkipBakedCode(); result = EvaluateBakedCode();
|
---|
219 | }
|
---|
220 | return result;
|
---|
221 | }
|
---|
222 | case EvaluatorSymbolTable.LT: {
|
---|
223 | double x = EvaluateBakedCode();
|
---|
224 | double y = EvaluateBakedCode();
|
---|
225 | if(x < y) return 1.0;
|
---|
226 | else return 0.0;
|
---|
227 | }
|
---|
228 | case EvaluatorSymbolTable.NOT: { // only defined for inputs 0 or 1
|
---|
229 | double result = EvaluateBakedCode();
|
---|
230 | Debug.Assert(result == 0.0 || result == 1.0);
|
---|
231 | return Math.Abs(result - 1.0);
|
---|
232 | }
|
---|
233 | case EvaluatorSymbolTable.OR: { // only defined for inputs 0 or 1
|
---|
234 | double result = EvaluateBakedCode();
|
---|
235 | for(int i = 1; i < currInstr.arity; i++) {
|
---|
236 | if(result > 0.0) SkipBakedCode();
|
---|
237 | else {
|
---|
238 | result = EvaluateBakedCode();
|
---|
239 | Debug.Assert(result == 0.0 || result == 1.0);
|
---|
240 | }
|
---|
241 | }
|
---|
242 | return result;
|
---|
243 | }
|
---|
244 | case EvaluatorSymbolTable.XOR: { // only defined for inputs 0 or 1
|
---|
245 | double x = EvaluateBakedCode();
|
---|
246 | double y = EvaluateBakedCode();
|
---|
247 | return Math.Abs(x - y);
|
---|
248 | }
|
---|
249 | case EvaluatorSymbolTable.UNKNOWN:
|
---|
250 | default: {
|
---|
251 | throw new NotImplementedException();
|
---|
252 | }
|
---|
253 | }
|
---|
254 | }
|
---|
255 | }
|
---|
256 | }
|
---|