Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3442 was 3442, checked in by gkronber, 15 years ago

Implemented views for DataAnalysisProblems and DataAnalysisSolutions. #938 (Data types and operators for regression problems)

File size: 4.5 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.Persistence.Default.CompositeSerializers.Storable;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using System.Collections.Generic;
27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.GeneralSymbols;
29using HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols;
30
31namespace 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        argumentStack.Clear();
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    private Stack<List<double>> argumentStack = new Stack<List<double>>();
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          }
85        case CodeSymbol.Call: {
86            // save current arguments
87            List<double> arguments = new List<double>();
88            // evaluate sub-trees
89            for (int i = 0; i < currentInstr.nArguments; i++) {
90              arguments.Add(Evaluate());
91            }
92            argumentStack.Push(arguments);
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            argumentStack.Pop();
100            // restore the pc => evaluation will continue at point after my subtrees 
101            pc = nextPc;
102            return v;
103          }
104        case CodeSymbol.Arg: {
105            return argumentStack.Peek()[currentInstr.iArg0];
106          }
107        case CodeSymbol.Dynamic: {
108            var variableTreeNode = currentInstr.dynamicNode as VariableTreeNode;
109            if (variableTreeNode != null) {
110              return dataset[row, dataset.GetVariableIndex(variableTreeNode.VariableName)] * variableTreeNode.Weight;
111            }
112            var constTreeNode = currentInstr.dynamicNode as ConstantTreeNode;
113            if (constTreeNode != null) {
114              return constTreeNode.Value;
115            } else throw new NotSupportedException();
116          }
117        default: throw new NotSupportedException();
118      }
119    }
120  }
121}
Note: See TracBrowser for help on using the repository browser.