1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2010 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 HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using System.Collections.Generic;
|
---|
27 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
28 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.GeneralSymbols;
|
---|
29 | using HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
32 | [StorableClass]
|
---|
33 | [Item("SimpleArithmeticExpressionEvaluator", "Default evaluator for arithmetic symbolic expression trees.")]
|
---|
34 | public class SimpleArithmeticExpressionEvaluator {
|
---|
35 | private Dataset dataset;
|
---|
36 | private int row;
|
---|
37 | private Instruction[] code;
|
---|
38 | private int pc;
|
---|
39 | public IEnumerable<double> EstimatedValues(SymbolicExpressionTree tree, Dataset dataset, IEnumerable<int> rows) {
|
---|
40 | this.dataset = dataset;
|
---|
41 | var compiler = new SymbolicExpressionTreeCompiler();
|
---|
42 | code = compiler.Compile(tree);
|
---|
43 | foreach (var row in rows) {
|
---|
44 | this.row = row;
|
---|
45 | pc = 0;
|
---|
46 | var estimatedValue = Evaluate();
|
---|
47 | if (double.IsNaN(estimatedValue) || double.IsInfinity(estimatedValue)) yield return 0.0;
|
---|
48 | else yield return estimatedValue;
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 | public double Evaluate() {
|
---|
53 | var currentInstr = code[pc++];
|
---|
54 | switch (currentInstr.symbol) {
|
---|
55 | case CodeSymbol.Add: {
|
---|
56 | double s = 0.0;
|
---|
57 | for (int i = 0; i < currentInstr.nArguments; i++) {
|
---|
58 | s += Evaluate();
|
---|
59 | }
|
---|
60 | return s;
|
---|
61 | }
|
---|
62 | case CodeSymbol.Sub: {
|
---|
63 | double s = Evaluate();
|
---|
64 | for (int i = 1; i < currentInstr.nArguments; i++) {
|
---|
65 | s -= Evaluate();
|
---|
66 | }
|
---|
67 | return s;
|
---|
68 | }
|
---|
69 | case CodeSymbol.Mul: {
|
---|
70 | double p = Evaluate();
|
---|
71 | for (int i = 1; i < currentInstr.nArguments; i++) {
|
---|
72 | p *= Evaluate();
|
---|
73 | }
|
---|
74 | return p;
|
---|
75 | }
|
---|
76 | case CodeSymbol.Div: {
|
---|
77 | double p = Evaluate();
|
---|
78 | for (int i = 1; i < currentInstr.nArguments; i++) {
|
---|
79 | p /= Evaluate();
|
---|
80 | }
|
---|
81 | return p;
|
---|
82 | }
|
---|
83 | case CodeSymbol.Dynamic: {
|
---|
84 | var variableTreeNode = currentInstr.dynamicNode as VariableTreeNode;
|
---|
85 | if (variableTreeNode != null) {
|
---|
86 | return dataset[row, dataset.GetVariableIndex(variableTreeNode.VariableName)] * variableTreeNode.Weight;
|
---|
87 | }
|
---|
88 | var constTreeNode = currentInstr.dynamicNode as ConstantTreeNode;
|
---|
89 | if (constTreeNode != null) {
|
---|
90 | return constTreeNode.Value;
|
---|
91 | } else throw new NotSupportedException();
|
---|
92 | }
|
---|
93 | default: throw new NotSupportedException();
|
---|
94 | }
|
---|
95 | }
|
---|
96 | }
|
---|
97 | }
|
---|