Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
05/10/10 08:15:00 (14 years ago)
Author:
gkronber
Message:

Added test cases for arithmetic expression interpreter. And fixed minor problems in the interpreter. #791

Location:
trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Tests
Files:
1 added
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Tests/SymbolicExpressionImporter.cs

    r3728 r3733  
    11#region License Information
    22/* HeuristicLab
    3  * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
    44 *
    55 * This file is part of HeuristicLab.
     
    2424using System.Linq;
    2525using System.Text;
    26 using HeuristicLab.GP.Interfaces;
    2726using System.IO;
    2827using System.Diagnostics;
    29 using HeuristicLab.GP.StructureIdentification;
     28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
     29using HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols;
     30using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    3031
    31 namespace HeuristicLab.GP.Test {
    32   class SymbolicExpressionImporter {
    33     private const string DIFFSTART = "dif";
     32namespace HeuristicLab.Problems.DataAnalysis.Tests {
     33  internal class SymbolicExpressionImporter {
    3434    private const string VARSTART = "var";
    35     private const string OPENPARAMSTART = "open-param";
    36     private Dictionary<string, IFunction> knownFunctions = new Dictionary<string, IFunction>()
     35    private Dictionary<string, Symbol> knownSymbols = new Dictionary<string, Symbol>()
    3736      {
    3837        {"+", new Addition()},
    39         {"and", new And()},
    40         {"mean", new Average()},
    41         {"cos", new Cosinus()},
    4238        {"/", new Division()},
    43         {"equ", new Equal()},
    44         {"exp", new Exponential()},
    45         {">", new GreaterThan()},
    46         {"if", new IfThenElse()},
    47         {"<", new LessThan()},
    48         {"log", new Logarithm()},
    4939        {"*", new Multiplication()},
    50         {"not", new Not()},
    51         {"or", new Or()},
    52         {"expt", new Power()},
    53         {"sign", new Signum()},
    54         {"sin",new Sinus()},
    55         {"sqrt", new Sqrt()},
    5640        {"-", new Subtraction()},
    57         {"tan", new Tangens()},
    58         {"xor", new Xor()},
    59         {"open-param", new HeuristicLab.GP.StructureIdentification.Networks.OpenParameter()},
    60         {"open-+", new HeuristicLab.GP.StructureIdentification.Networks.OpenAddition()},
    61         {"open--", new HeuristicLab.GP.StructureIdentification.Networks.OpenSubtraction()},
    62         {"open-*", new HeuristicLab.GP.StructureIdentification.Networks.OpenMultiplication()},
    63         {"open-/", new HeuristicLab.GP.StructureIdentification.Networks.OpenDivision()},
    64         {"open-log", new HeuristicLab.GP.StructureIdentification.Networks.OpenLog()},
    65         {"open-exp", new HeuristicLab.GP.StructureIdentification.Networks.OpenExp()},
    66         //{"open-sqr", new HeuristicLab.GP.StructureIdentification.Networks.OpenSqr()},
    67         //{"open-sqrt", new HeuristicLab.GP.StructureIdentification.Networks.OpenSqrt()},
    68         {"f1-+", new HeuristicLab.GP.StructureIdentification.Networks.AdditionF1()},
    69         {"f1--", new HeuristicLab.GP.StructureIdentification.Networks.SubtractionF1()},
    70         {"f1-/", new HeuristicLab.GP.StructureIdentification.Networks.DivisionF1()},
    71         {"f1-*", new HeuristicLab.GP.StructureIdentification.Networks.MultiplicationF1()},
    72         {"cycle", new HeuristicLab.GP.StructureIdentification.Networks.Cycle()},
    73         {"flip", new HeuristicLab.GP.StructureIdentification.Networks.Flip()},
    7441
    7542      };
    7643    Constant constant = new Constant();
    77     HeuristicLab.GP.StructureIdentification.Variable variable = new HeuristicLab.GP.StructureIdentification.Variable();
    78     Differential differential = new Differential();
    79     HeuristicLab.GP.StructureIdentification.Networks.OpenParameter openParam = new HeuristicLab.GP.StructureIdentification.Networks.OpenParameter();
     44    Variable variable = new Variable();
     45    ProgramRootSymbol programRootSymbol = new ProgramRootSymbol();
     46    StartSymbol startSymbol = new StartSymbol();
     47
    8048    public SymbolicExpressionImporter() {
    8149    }
    8250
    83     internal IFunctionTree Import(string str) {
     51    internal SymbolicExpressionTree Import(string str) {
    8452      str = str.Replace("(", " ( ").Replace(")", " ) ");
    85       return ParseSexp(new Queue<Token>(GetTokenStream(str)));
     53      SymbolicExpressionTreeNode root = programRootSymbol.CreateTreeNode();
     54      SymbolicExpressionTreeNode start = startSymbol.CreateTreeNode();
     55      SymbolicExpressionTreeNode mainBranch = ParseSexp(new Queue<Token>(GetTokenStream(str)));
     56      root.AddSubTree(start);
     57      start.AddSubTree(mainBranch);
     58      return new SymbolicExpressionTree(root);
    8659    }
    8760
     
    9467    }
    9568
    96     private HeuristicLab.GP.Interfaces.IFunctionTree ParseSexp(Queue<Token> tokens) {
     69    private SymbolicExpressionTreeNode ParseSexp(Queue<Token> tokens) {
    9770      if (tokens.Peek().Symbol == TokenSymbol.LPAR) {
    98         IFunctionTree tree;
     71        SymbolicExpressionTreeNode tree;
    9972        Expect(Token.LPAR, tokens);
    10073        if (tokens.Peek().StringValue.StartsWith(VARSTART)) {
    10174          tree = ParseVariable(tokens);
    102         } else if (tokens.Peek().StringValue.StartsWith(DIFFSTART)) {
    103           tree = ParseDifferential(tokens);
    104         } else if (tokens.Peek().StringValue.StartsWith(OPENPARAMSTART)) {
    105           tree = ParseOpenParameter(tokens);
    10675        } else {
    10776          Token curToken = tokens.Dequeue();
     
    11483        return tree;
    11584      } else if (tokens.Peek().Symbol == TokenSymbol.NUMBER) {
    116         ConstantFunctionTree t = (ConstantFunctionTree)constant.GetTreeNode();
     85        ConstantTreeNode t = (ConstantTreeNode)constant.CreateTreeNode();
    11786        t.Value = tokens.Dequeue().DoubleValue;
    11887        return t;
     
    12089    }
    12190
    122     private IFunctionTree ParseOpenParameter(Queue<Token> tokens) {
    123       Token tok = tokens.Dequeue();
    124       Debug.Assert(tok.StringValue == "open-param");
    125       HeuristicLab.GP.StructureIdentification.Networks.OpenParameterFunctionTree t = (HeuristicLab.GP.StructureIdentification.Networks.OpenParameterFunctionTree)openParam.GetTreeNode();
     91    private SymbolicExpressionTreeNode ParseVariable(Queue<Token> tokens) {
     92      Token varTok = tokens.Dequeue();
     93      Debug.Assert(varTok.StringValue == "variable");
     94      VariableTreeNode t = (VariableTreeNode)variable.CreateTreeNode();
    12695      t.Weight = tokens.Dequeue().DoubleValue;
    12796      t.VariableName = tokens.Dequeue().StringValue;
    128       t.SampleOffset = (int)tokens.Dequeue().DoubleValue;
    12997      return t;
    13098    }
    13199
    132     private IFunctionTree ParseDifferential(Queue<Token> tokens) {
    133       Token diffTok = tokens.Dequeue();
    134       Debug.Assert(diffTok.StringValue == "differential");
    135       VariableFunctionTree t = (VariableFunctionTree)differential.GetTreeNode();
    136       t.Weight = tokens.Dequeue().DoubleValue;
    137       t.VariableName = tokens.Dequeue().StringValue;
    138       t.SampleOffset = (int)tokens.Dequeue().DoubleValue;
    139       return t;
    140     }
    141 
    142     private IFunctionTree ParseVariable(Queue<Token> tokens) {
    143       Token varTok = tokens.Dequeue();
    144       Debug.Assert(varTok.StringValue == "variable");
    145       VariableFunctionTree t = (VariableFunctionTree)variable.GetTreeNode();
    146       t.Weight = tokens.Dequeue().DoubleValue;
    147       t.VariableName = tokens.Dequeue().StringValue;
    148       t.SampleOffset = (int)tokens.Dequeue().DoubleValue;
    149       return t;
    150     }
    151 
    152     private IFunctionTree CreateTree(Token token) {
     100    private SymbolicExpressionTreeNode CreateTree(Token token) {
    153101      if (token.Symbol != TokenSymbol.SYMB) throw new FormatException("Expected function symbol, but got: " + token.StringValue);
    154       return knownFunctions[token.StringValue].GetTreeNode();
     102      return knownSymbols[token.StringValue].CreateTreeNode();
    155103    }
    156104
    157105    private void Expect(Token token, Queue<Token> tokens) {
    158106      Token cur = tokens.Dequeue();
    159       if (!token.Equals(cur)) throw new FormatException("Expected: " + token.StringValue + ", but got found: " + cur.StringValue);
     107      if (!token.Equals(cur)) throw new FormatException("Expected: " + token.StringValue + ", but got: " + cur.StringValue);
    160108    }
    161109  }
Note: See TracChangeset for help on using the changeset viewer.