[3253] | 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;
|
---|
[3376] | 24 | using HeuristicLab.Common;
|
---|
[3253] | 25 | using HeuristicLab.Core;
|
---|
| 26 | using System.Collections.Generic;
|
---|
| 27 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
[3257] | 28 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.GeneralSymbols;
|
---|
[3373] | 29 | using HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols;
|
---|
[3253] | 30 |
|
---|
[3373] | 31 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
[3253] | 32 | [StorableClass]
|
---|
| 33 | [Item("SimpleArithmeticExpressionEvaluator", "Default evaluator for arithmetic symbolic expression trees.")]
|
---|
[3294] | 34 | public class SimpleArithmeticExpressionEvaluator {
|
---|
[3257] | 35 | private Dataset dataset;
|
---|
| 36 | private int row;
|
---|
[3294] | 37 | private Instruction[] code;
|
---|
| 38 | private int pc;
|
---|
[3253] | 39 | public IEnumerable<double> EstimatedValues(SymbolicExpressionTree tree, Dataset dataset, IEnumerable<int> rows) {
|
---|
[3257] | 40 | this.dataset = dataset;
|
---|
[3294] | 41 | var compiler = new SymbolicExpressionTreeCompiler();
|
---|
| 42 | code = compiler.Compile(tree);
|
---|
[3253] | 43 | foreach (var row in rows) {
|
---|
[3257] | 44 | this.row = row;
|
---|
[3294] | 45 | pc = 0;
|
---|
[3409] | 46 | arguments.Clear();
|
---|
[3294] | 47 | var estimatedValue = Evaluate();
|
---|
[3253] | 48 | if (double.IsNaN(estimatedValue) || double.IsInfinity(estimatedValue)) yield return 0.0;
|
---|
| 49 | else yield return estimatedValue;
|
---|
| 50 | }
|
---|
| 51 | }
|
---|
| 52 |
|
---|
[3409] | 53 | private List<double> arguments = new List<double>();
|
---|
[3294] | 54 | public double Evaluate() {
|
---|
| 55 | var currentInstr = code[pc++];
|
---|
| 56 | switch (currentInstr.symbol) {
|
---|
| 57 | case CodeSymbol.Add: {
|
---|
| 58 | double s = 0.0;
|
---|
| 59 | for (int i = 0; i < currentInstr.nArguments; i++) {
|
---|
| 60 | s += Evaluate();
|
---|
| 61 | }
|
---|
| 62 | return s;
|
---|
| 63 | }
|
---|
| 64 | case CodeSymbol.Sub: {
|
---|
| 65 | double s = Evaluate();
|
---|
| 66 | for (int i = 1; i < currentInstr.nArguments; i++) {
|
---|
| 67 | s -= Evaluate();
|
---|
| 68 | }
|
---|
| 69 | return s;
|
---|
| 70 | }
|
---|
| 71 | case CodeSymbol.Mul: {
|
---|
| 72 | double p = Evaluate();
|
---|
| 73 | for (int i = 1; i < currentInstr.nArguments; i++) {
|
---|
| 74 | p *= Evaluate();
|
---|
| 75 | }
|
---|
| 76 | return p;
|
---|
| 77 | }
|
---|
| 78 | case CodeSymbol.Div: {
|
---|
| 79 | double p = Evaluate();
|
---|
| 80 | for (int i = 1; i < currentInstr.nArguments; i++) {
|
---|
| 81 | p /= Evaluate();
|
---|
| 82 | }
|
---|
| 83 | return p;
|
---|
| 84 | }
|
---|
[3409] | 85 | case CodeSymbol.Call: {
|
---|
| 86 | // save current arguments
|
---|
| 87 | var oldArgs = new List<double>(arguments);
|
---|
| 88 | arguments.Clear();
|
---|
| 89 | // evaluate sub-trees
|
---|
| 90 | for (int i = 0; i < currentInstr.nArguments; i++) {
|
---|
| 91 | arguments.Add(Evaluate());
|
---|
| 92 | }
|
---|
| 93 | // save the pc
|
---|
| 94 | int nextPc = pc;
|
---|
| 95 | // set pc to start of function
|
---|
| 96 | pc = currentInstr.iArg0;
|
---|
| 97 | // evaluate the function
|
---|
| 98 | double v = Evaluate();
|
---|
| 99 | // restore the pc => evaluation will continue at point after my subtrees
|
---|
| 100 | pc = nextPc;
|
---|
| 101 | // restore arguments
|
---|
| 102 | arguments = oldArgs;
|
---|
| 103 | return v;
|
---|
| 104 | }
|
---|
| 105 | case CodeSymbol.Arg: {
|
---|
| 106 | return arguments[currentInstr.iArg0];
|
---|
| 107 | }
|
---|
[3294] | 108 | case CodeSymbol.Dynamic: {
|
---|
[3373] | 109 | var variableTreeNode = currentInstr.dynamicNode as VariableTreeNode;
|
---|
| 110 | if (variableTreeNode != null) {
|
---|
[3294] | 111 | return dataset[row, dataset.GetVariableIndex(variableTreeNode.VariableName)] * variableTreeNode.Weight;
|
---|
[3373] | 112 | }
|
---|
| 113 | var constTreeNode = currentInstr.dynamicNode as ConstantTreeNode;
|
---|
| 114 | if (constTreeNode != null) {
|
---|
| 115 | return constTreeNode.Value;
|
---|
[3294] | 116 | } else throw new NotSupportedException();
|
---|
| 117 | }
|
---|
| 118 | default: throw new NotSupportedException();
|
---|
[3253] | 119 | }
|
---|
| 120 | }
|
---|
| 121 | }
|
---|
| 122 | }
|
---|