Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Symbolic/SimpleArithmeticExpressionEvaluator.cs @ 3376

Last change on this file since 3376 was 3376, checked in by swagner, 14 years ago

Moved interfaces and classes for deep cloning from HeuristicLab.Core to HeuristicLab.Common (#975).

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