Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
05/10/10 08:15:00 (15 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:
4 added
4 copied

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Tests/SimpleArithmeticExpressionInterpreterTest.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.
     
    2020#endregion
    2121
    22 using HeuristicLab.GP.StructureIdentification;
    23 using Microsoft.VisualStudio.TestTools.UnitTesting;
    24 using HeuristicLab.GP.Interfaces;
    2522using System.IO;
    26 using HeuristicLab.DataAnalysis;
    2723using System;
    2824using HeuristicLab.Random;
    2925using System.Collections.Generic;
    30 using HeuristicLab.GP.Operators;
    3126using System.Diagnostics;
    32 namespace HeuristicLab.GP.Test {
     27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
     28using HeuristicLab.Problems.DataAnalysis.Symbolic;
     29using Microsoft.VisualStudio.TestTools.UnitTesting;
     30using System.Linq;
     31namespace HeuristicLab.Problems.DataAnalysis.Tests {
    3332
    3433
    3534  /// <summary>
    36   ///This is a test class for HL3TreeEvaluatorTest and is intended
    37   ///to contain all HL3TreeEvaluatorTest Unit Tests
     35  ///This is a test class for SimpleArithmeticExpressionInterpreter and is intended
     36  ///to contain all SimpleArithmeticExpressionInterpreter Unit Tests
    3837  ///</summary>
    3938  [TestClass()]
    40   public class HL3TreeEvaluatorTest {
     39  public class SimpleArithmeticExpressionInterpreterTest {
    4140    private const int N = 1000;
    4241    private const int Rows = 1000;
    4342    private const int Columns = 50;
    44     private static IFunctionTree[] randomTrees;
     43    private static SymbolicExpressionTree[] randomTrees;
    4544    private static Dataset dataset;
    4645    private static MersenneTwister twister;
     
    6463      twister = new MersenneTwister();
    6564      dataset = Util.CreateRandomDataset(twister, Rows, Columns);
    66       randomTrees = Util.CreateRandomTrees(twister, dataset, N, 1, 100);
     65      var grammar = new GlobalSymbolicExpressionGrammar(new ArithmeticExpressionGrammar());
     66      grammar.MaxFunctionArguments = 0;
     67      grammar.MaxFunctionDefinitions = 0;
     68      grammar.MinFunctionArguments = 0;
     69      grammar.MinFunctionDefinitions = 0;
     70      randomTrees = Util.CreateRandomTrees(twister, dataset, grammar, N, 1, 100, 0, 0);
    6771    }
    6872
    6973
    7074    [TestMethod()]
    71     public void PerformanceTest() {
     75    public void SimpleArithmeticExpressionInterpreterPerformanceTest() {
    7276      double[] estimation = new double[Rows];
    73       foreach (IFunctionTree tree in randomTrees) {
     77      foreach (SymbolicExpressionTree tree in randomTrees) {
    7478        Util.InitTree(tree, twister, new List<string>(dataset.VariableNames));
    7579      }
    76       HL3TreeEvaluator hl3TreeEvaluator = new HL3TreeEvaluator();
    77       Util.EvaluateTrees(randomTrees, hl3TreeEvaluator, dataset, 10);
     80      SimpleArithmeticExpressionInterpreter interpreter = new SimpleArithmeticExpressionInterpreter();
     81      Util.EvaluateTrees(randomTrees, interpreter, dataset, 10);
    7882    }
    7983
     
    8387    ///</summary>
    8488    [TestMethod()]
    85     [DeploymentItem("HeuristicLab.GP.StructureIdentification-3.3.dll")]
    86     public void HL3TreeEvaluatorEvaluateTest() {
     89    public void SimpleArithmeticExpressionInterpreterEvaluateTest() {
    8790
    88       Dataset ds = new Dataset(new double[,] {
     91      Dataset ds = new Dataset(new string[] { "y", "a", "b" }, new double[,] {
    8992        { 1.0, 1.0, 1.0 },
    9093        { 2.0, 2.0, 2.0 },
     
    9295      });
    9396
    94       ds.SetVariableName(0, "y");
    95       ds.SetVariableName(1, "a");
    96       ds.SetVariableName(2, "b");
    97 
    98       HL3TreeEvaluator evaluator = new HL3TreeEvaluator();
     97      SimpleArithmeticExpressionInterpreter interpreter = new SimpleArithmeticExpressionInterpreter();
    9998
    10099      // constants
    101       Evaluate(evaluator, ds, "(+ 1,5 3,5)", 0, 5.0);
     100      Evaluate(interpreter, ds, "(+ 1.5 3.5)", 0, 5.0);
    102101
    103102      // variables
    104       Evaluate(evaluator, ds, "(variable 2,0 a 0)", 0, 2.0);
    105       Evaluate(evaluator, ds, "(variable 2,0 a 0)", 1, 4.0);
    106       Evaluate(evaluator, ds, "(variable 2,0 a -1)", 1, 2.0);
    107       Evaluate(evaluator, ds, "(variable 2,0 a -2)", 2, 2.0);
     103      Evaluate(interpreter, ds, "(variable 2.0 a)", 0, 2.0);
     104      Evaluate(interpreter, ds, "(variable 2.0 a)", 1, 4.0);
    108105
    109       // differentials
    110       Evaluate(evaluator, ds, "(differential 2,0 a 0)", 1, 2.0);
    111       Evaluate(evaluator, ds, "(differential 2,0 a 0)", 2, -2.0);
    112       Evaluate(evaluator, ds, "(differential 2,0 a -1)", 2, 2.0);
    113       Evaluate(evaluator, ds, "(differential 2,0 a -2)", 3, 2.0);
    114106
    115107      // addition
    116       Evaluate(evaluator, ds, "(+ (variable 2,0 a 0))", 1, 4.0);
    117       Evaluate(evaluator, ds, "(+ (variable 2,0 a 0) (variable 3,0 b 0))", 0, 5.0);
    118       Evaluate(evaluator, ds, "(+ (variable 2,0 a 0) (variable 3,0 b 0))", 1, 10.0);
    119       Evaluate(evaluator, ds, "(+ (variable 2,0 a 0) (variable 3,0 b 0))", 2, 8.0);
    120       Evaluate(evaluator, ds, "(+ 8,0 2,0 2,0)", 0, 12.0);
     108      Evaluate(interpreter, ds, "(+ (variable 2.0 a ))", 1, 4.0);
     109      Evaluate(interpreter, ds, "(+ (variable 2.0 a ) (variable 3.0 b ))", 0, 5.0);
     110      Evaluate(interpreter, ds, "(+ (variable 2.0 a ) (variable 3.0 b ))", 1, 10.0);
     111      Evaluate(interpreter, ds, "(+ (variable 2.0 a) (variable 3.0 b ))", 2, 8.0);
     112      Evaluate(interpreter, ds, "(+ 8.0 2.0 2.0)", 0, 12.0);
    121113
    122114      // subtraction
    123       Evaluate(evaluator, ds, "(- (variable 2,0 a 0))", 1, -4.0);
    124       Evaluate(evaluator, ds, "(- (variable 2,0 a 0) (variable 3,0 b 0))", 0, -1.0);
    125       Evaluate(evaluator, ds, "(- (variable 2,0 a 0) (variable 3,0 b 0))", 1, -2.0);
    126       Evaluate(evaluator, ds, "(- (variable 2,0 a 0) (variable 3,0 b 0))", 2, -4.0);
    127       Evaluate(evaluator, ds, "(- 8,0 2,0 2,0)", 0, 4.0);
     115      Evaluate(interpreter, ds, "(- (variable 2.0 a ))", 1, -4.0);
     116      Evaluate(interpreter, ds, "(- (variable 2.0 a ) (variable 3.0 b))", 0, -1.0);
     117      Evaluate(interpreter, ds, "(- (variable 2.0 a ) (variable 3.0 b ))", 1, -2.0);
     118      Evaluate(interpreter, ds, "(- (variable 2.0 a ) (variable 3.0 b ))", 2, -4.0);
     119      Evaluate(interpreter, ds, "(- 8.0 2.0 2.0)", 0, 4.0);
    128120
    129121      // multiplication
    130       Evaluate(evaluator, ds, "(* (variable 2,0 a 0))", 0, 2.0);
    131       Evaluate(evaluator, ds, "(* (variable 2,0 a 0) (variable 3,0 b 0))", 0, 6.0);
    132       Evaluate(evaluator, ds, "(* (variable 2,0 a 0) (variable 3,0 b 0))", 1, 24.0);
    133       Evaluate(evaluator, ds, "(* (variable 2,0 a 0) (variable 3,0 b 0))", 2, 12.0);
    134       Evaluate(evaluator, ds, "(* 8,0 2,0 2,0)", 0, 32.0);
     122      Evaluate(interpreter, ds, "(* (variable 2.0 a ))", 0, 2.0);
     123      Evaluate(interpreter, ds, "(* (variable 2.0 a ) (variable 3.0 b ))", 0, 6.0);
     124      Evaluate(interpreter, ds, "(* (variable 2.0 a ) (variable 3.0 b ))", 1, 24.0);
     125      Evaluate(interpreter, ds, "(* (variable 2.0 a ) (variable 3.0 b ))", 2, 12.0);
     126      Evaluate(interpreter, ds, "(* 8.0 2.0 2.0)", 0, 32.0);
    135127
    136128      // division
    137       Evaluate(evaluator, ds, "(/ (variable 2,0 a 0))", 1, 1.0 / 4.0);
    138       Evaluate(evaluator, ds, "(/ (variable 2,0 a 0) 2,0)", 0, 1.0);
    139       Evaluate(evaluator, ds, "(/ (variable 2,0 a 0) 2,0)", 1, 2.0);
    140       Evaluate(evaluator, ds, "(/ (variable 3,0 b 0) 2,0)", 2, 3.0);
    141       Evaluate(evaluator, ds, "(/ 8,0 2,0 2,0)", 0, 2.0);
     129      Evaluate(interpreter, ds, "(/ (variable 2.0 a ))", 1, 1.0 / 4.0);
     130      Evaluate(interpreter, ds, "(/ (variable 2.0 a ) 2.0)", 0, 1.0);
     131      Evaluate(interpreter, ds, "(/ (variable 2.0 a ) 2.0)", 1, 2.0);
     132      Evaluate(interpreter, ds, "(/ (variable 3.0 b ) 2.0)", 2, 3.0);
     133      Evaluate(interpreter, ds, "(/ 8.0 2.0 2.0)", 0, 2.0);
    142134
    143       // boolean values semantic: false = x <= 0 , true x > 0
    144       // equ
    145       Evaluate(evaluator, ds, "(equ (variable 2,0 a 0) 2,0)", 0, 1.0);
    146       Evaluate(evaluator, ds, "(equ (variable 2,0 a 0) 1,0)", 0, -1.0);
    147       Evaluate(evaluator, ds, "(equ (sqrt -1,0) (log -1,0))", 0, -1.0); // (equ nan nan) should be false
    148 
    149 
    150       // gt
    151       Evaluate(evaluator, ds, "(> (variable 2,0 a 0) 2,0)", 0, -1.0);
    152       Evaluate(evaluator, ds, "(> 2,0 (variable 2,0 a 0))", 0, -1.0);
    153       Evaluate(evaluator, ds, "(> (variable 2,0 a 0) 1,9)", 0, 1.0);
    154       Evaluate(evaluator, ds, "(> 1,9 (variable 2,0 a 0))", 0, -1.0);
    155       Evaluate(evaluator, ds, "(> (sqrt -1,0) (log -1,0))", 0, -1.0); // (> nan nan) should be false
    156 
    157       // lt
    158       Evaluate(evaluator, ds, "(< (variable 2,0 a 0) 2,0)", 0, -1.0);
    159       Evaluate(evaluator, ds, "(< 2,0 (variable 2,0 a 0))", 0, -1.0);
    160       Evaluate(evaluator, ds, "(< (variable 2,0 a 0) 1,9)", 0, -1.0);
    161       Evaluate(evaluator, ds, "(< 1,9 (variable 2,0 a 0))", 0, 1.0);
    162       Evaluate(evaluator, ds, "(< (sqrt -1,0) (log -1,0))", 0, -1.0); // (< nan nan) should be false
    163 
    164       // If
    165       Evaluate(evaluator, ds, "(if -10 2,0 3,0)", 0, 3.0);
    166       Evaluate(evaluator, ds, "(if -1,0 2,0 3,0)", 0, 3.0);
    167       Evaluate(evaluator, ds, "(if 0,0 2,0 3,0)", 0, 3.0);
    168       Evaluate(evaluator, ds, "(if 1,0 2,0 3,0)", 0, 2.0);
    169       Evaluate(evaluator, ds, "(if 10 2,0 3,0)", 0, 2.0);
    170       Evaluate(evaluator, ds, "(if (sqrt -1,0) 2,0 3,0)", 0, 3.0); // if(nan) should return the else branch
    171 
    172       // signum
    173       Evaluate(evaluator, ds, "(sign -1,0)", 0, -1.0);
    174       Evaluate(evaluator, ds, "(sign -2,0)", 0, -1.0);
    175       Evaluate(evaluator, ds, "(sign 1,0)", 0, 1.0);
    176       Evaluate(evaluator, ds, "(sign 2,0)", 0, 1.0);
    177       Evaluate(evaluator, ds, "(sign 0,0)", 0, 0.0);
    178 
    179       // NOT
    180       Evaluate(evaluator, ds, "(not -1,0)", 0, 1.0);
    181       Evaluate(evaluator, ds, "(not -2,0)", 0, 2.0);
    182       Evaluate(evaluator, ds, "(not 1,0)", 0, -1.0);
    183       Evaluate(evaluator, ds, "(not 2,0)", 0, -2.0);
    184       Evaluate(evaluator, ds, "(not 0,0)", 0, 0.0);
    185 
    186       // AND
    187       Evaluate(evaluator, ds, "(and -1,0 -2,0)", 0, -1.0);
    188       Evaluate(evaluator, ds, "(and -1,0 2,0)", 0, -1.0);
    189       Evaluate(evaluator, ds, "(and 1,0 -2,0)", 0, -1.0);
    190       Evaluate(evaluator, ds, "(and 1,0 0,0)", 0, -1.0);
    191       Evaluate(evaluator, ds, "(and 0,0 0,0)", 0, -1.0);
    192       Evaluate(evaluator, ds, "(and 1,0 2,0)", 0, 1.0);
    193       Evaluate(evaluator, ds, "(and 1,0 2,0 3,0)", 0, 1.0);
    194       Evaluate(evaluator, ds, "(and 1,0 -2,0 3,0)", 0, -1.0);
    195 
    196       // OR
    197       Evaluate(evaluator, ds, "(or -1,0 -2,0)", 0, -1.0);
    198       Evaluate(evaluator, ds, "(or -1,0 2,0)", 0, 1.0);
    199       Evaluate(evaluator, ds, "(or 1,0 -2,0)", 0, 1.0);
    200       Evaluate(evaluator, ds, "(or 1,0 2,0)", 0, 1.0);
    201       Evaluate(evaluator, ds, "(or 0,0 0,0)", 0, -1.0);
    202       Evaluate(evaluator, ds, "(or -1,0 -2,0 -3,0)", 0, -1.0);
    203       Evaluate(evaluator, ds, "(or -1,0 -2,0 3,0)", 0, 1.0);
    204 
    205       // XOR
    206       Evaluate(evaluator, ds, "(xor -1,0 -2,0)", 0, -1.0);
    207       Evaluate(evaluator, ds, "(xor -1,0 2,0)", 0, 1.0);
    208       Evaluate(evaluator, ds, "(xor 1,0 -2,0)", 0, 1.0);
    209       Evaluate(evaluator, ds, "(xor 1,0 2,0)", 0, -1.0);
    210       Evaluate(evaluator, ds, "(xor 0,0 0,0)", 0, -1.0);
    211 
    212       // sin, cos, tan
    213       Evaluate(evaluator, ds, "(sin " + Math.PI + ")", 0, 0.0);
    214       Evaluate(evaluator, ds, "(sin 0,0)", 0, 0.0);
    215       Evaluate(evaluator, ds, "(cos " + Math.PI + ")", 0, -1.0);
    216       Evaluate(evaluator, ds, "(cos 0,0)", 0, 1.0);
    217       Evaluate(evaluator, ds, "(tan " + Math.PI + ")", 0, Math.Tan(Math.PI));
    218       Evaluate(evaluator, ds, "(tan 0,0)", 0, Math.Tan(Math.PI));
    219 
    220 
    221       // expt
    222       Evaluate(evaluator, ds, "(expt 2,0 2,0)", 0, 4.0);
    223       Evaluate(evaluator, ds, "(expt 2,0 3,0)", 0, 8.0);
    224       Evaluate(evaluator, ds, "(expt 3,0 2,0)", 0, 9.0);
    225 
    226       // exp, log
    227       Evaluate(evaluator, ds, "(log (exp 7,0))", 0, Math.Log(Math.Exp(7)));
    228       Evaluate(evaluator, ds, "(exp (log 7,0))", 0, Math.Exp(Math.Log(7)));
    229       Evaluate(evaluator, ds, "(log -3,0)", 0, Math.Log(-3));
    230 
    231       // sqrt
    232       Evaluate(evaluator, ds, "(sqrt 4,0)", 0, 2.0);
    233       Evaluate(evaluator, ds, "(sqrt (sqrt 16,0))", 0, 2.0);
    234 
     135      // TODO ADF     
    235136    }
    236137
    237     private void Evaluate(HL3TreeEvaluator evaluator, Dataset ds, string expr, int index, double expected) {
     138    private void Evaluate(SimpleArithmeticExpressionInterpreter interpreter, Dataset ds, string expr, int index, double expected) {
    238139      var importer = new SymbolicExpressionImporter();
    239       IFunctionTree tree = importer.Import(expr);
     140      SymbolicExpressionTree tree = importer.Import(expr);
    240141
    241       evaluator.PrepareForEvaluation(ds, tree);
     142      double actual = interpreter.GetSymbolicExpressionTreeValues(tree, ds, Enumerable.Range(index, 1)).First();
    242143
    243       double actual = evaluator.Evaluate(index);
    244144      Assert.AreEqual(expected, actual, 1.0E-12, expr);
    245145    }
  • 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  }
  • trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Tests/Token.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.
     
    2525using System.Text;
    2626using System.IO;
    27 using HeuristicLab.GP;
    28 using HeuristicLab.GP.Interfaces;
    29 using HeuristicLab.GP.StructureIdentification;
    3027using System.Diagnostics;
    3128using System.Globalization;
    3229
    33 namespace HeuristicLab.GP.Test {
    34   public enum TokenSymbol { LPAR, RPAR, SYMB, NUMBER };
    35   public class Token {
     30namespace HeuristicLab.Problems.DataAnalysis.Tests {
     31  internal enum TokenSymbol { LPAR, RPAR, SYMB, NUMBER };
     32  internal class Token {
    3633    public static readonly Token LPAR = Token.Parse("(");
    3734    public static readonly Token RPAR = Token.Parse(")");
  • trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Tests/Util.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.
     
    2020#endregion
    2121
    22 using HeuristicLab.GP.StructureIdentification;
    2322using Microsoft.VisualStudio.TestTools.UnitTesting;
    24 using HeuristicLab.DataAnalysis;
    2523using System;
    26 using HeuristicLab.GP.Interfaces;
    2724using HeuristicLab.Random;
    28 using HeuristicLab.GP.Operators;
    2925using System.Collections.Generic;
    3026using System.Text;
    3127using System.Diagnostics;
    32 namespace HeuristicLab.GP.Test {
    33   public class Util {
     28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
     29using HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols;
     30using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Creators;
     31using HeuristicLab.Problems.DataAnalysis.Symbolic;
     32using System.Linq;
     33namespace HeuristicLab.Problems.DataAnalysis.Tests {
     34  internal class Util {
    3435
    35     public static void InitTree(IFunctionTree tree, MersenneTwister twister, List<string> varNames) {
    36       foreach (var node in FunctionTreeIterator.IteratePostfix(tree)) {
    37         if (node is VariableFunctionTree) {
    38           var varNode = node as VariableFunctionTree;
     36    public static void InitTree(SymbolicExpressionTree tree, MersenneTwister twister, List<string> varNames) {
     37      foreach (var node in tree.IterateNodesPostfix()) {
     38        if (node is VariableTreeNode) {
     39          var varNode = node as VariableTreeNode;
    3940          varNode.Weight = twister.NextDouble() * 20.0 - 10.0;
    40           varNode.SampleOffset = 0;
    4141          varNode.VariableName = varNames[twister.Next(varNames.Count)];
    42         } else if (node is ConstantFunctionTree) {
    43           var constantNode = node as ConstantFunctionTree;
     42        } else if (node is ConstantTreeNode) {
     43          var constantNode = node as ConstantTreeNode;
    4444          constantNode.Value = twister.NextDouble() * 20.0 - 10.0;
    4545        }
     
    4747    }
    4848
    49     public static FunctionLibrary CreateFunctionLibrary() {
    50       FunctionLibrary functionLibrary = new FunctionLibrary();
    5149
    52       Variable variable = new Variable();
    53       Constant constant = new Constant();
    54       Differential differential = new Differential();
    55       Addition addition = new Addition();
    56       And and = new And();
    57       //Average average = new Average();
    58       Cosinus cosinus = new Cosinus();
    59       Division division = new Division();
    60       Equal equal = new Equal();
    61       Exponential exponential = new Exponential();
    62       GreaterThan greaterThan = new GreaterThan();
    63       IfThenElse ifThenElse = new IfThenElse();
    64       LessThan lessThan = new LessThan();
    65       Logarithm logarithm = new Logarithm();
    66       Multiplication multiplication = new Multiplication();
    67       Not not = new Not();
    68       Or or = new Or();
    69       Power power = new Power();
    70       Signum signum = new Signum();
    71       Sinus sinus = new Sinus();
    72       Sqrt sqrt = new Sqrt();
    73       Subtraction subtraction = new Subtraction();
    74       Tangens tangens = new Tangens();
    75       Xor xor = new Xor();
    76 
    77 
    78       List<IFunction> booleanFunctions = new List<IFunction>();
    79       booleanFunctions.Add(and);
    80       booleanFunctions.Add(equal);
    81       booleanFunctions.Add(greaterThan);
    82       booleanFunctions.Add(lessThan);
    83       booleanFunctions.Add(not);
    84       booleanFunctions.Add(or);
    85       booleanFunctions.Add(xor);
    86 
    87       List<IFunction> doubleFunctions = new List<IFunction>();
    88       doubleFunctions.Add(differential);
    89       doubleFunctions.Add(variable);
    90       doubleFunctions.Add(constant);
    91       doubleFunctions.Add(addition);
    92       // doubleFunctions.Add(average);
    93       doubleFunctions.Add(cosinus);
    94       doubleFunctions.Add(division);
    95       doubleFunctions.Add(exponential);
    96       doubleFunctions.Add(ifThenElse);
    97       doubleFunctions.Add(logarithm);
    98       doubleFunctions.Add(multiplication);
    99       doubleFunctions.Add(power);
    100       doubleFunctions.Add(signum);
    101       doubleFunctions.Add(sinus);
    102       doubleFunctions.Add(sqrt);
    103       doubleFunctions.Add(subtraction);
    104       doubleFunctions.Add(tangens);
    105 
    106       SetAllowedSubOperators(and, booleanFunctions);
    107       SetAllowedSubOperators(equal, doubleFunctions);
    108       SetAllowedSubOperators(greaterThan, doubleFunctions);
    109       SetAllowedSubOperators(lessThan, doubleFunctions);
    110       SetAllowedSubOperators(not, booleanFunctions);
    111       SetAllowedSubOperators(or, booleanFunctions);
    112       SetAllowedSubOperators(xor, booleanFunctions);
    113       SetAllowedSubOperators(addition, doubleFunctions);
    114       //SetAllowedSubOperators(average, doubleFunctions);
    115       SetAllowedSubOperators(cosinus, doubleFunctions);
    116       SetAllowedSubOperators(division, doubleFunctions);
    117       SetAllowedSubOperators(exponential, doubleFunctions);
    118       SetAllowedSubOperators(ifThenElse, 0, booleanFunctions);
    119       SetAllowedSubOperators(ifThenElse, 1, doubleFunctions);
    120       SetAllowedSubOperators(ifThenElse, 2, doubleFunctions);
    121       SetAllowedSubOperators(logarithm, doubleFunctions);
    122       SetAllowedSubOperators(multiplication, doubleFunctions);
    123       SetAllowedSubOperators(power, doubleFunctions);
    124       SetAllowedSubOperators(signum, doubleFunctions);
    125       SetAllowedSubOperators(sinus, doubleFunctions);
    126       SetAllowedSubOperators(sqrt, doubleFunctions);
    127       SetAllowedSubOperators(subtraction, doubleFunctions);
    128       SetAllowedSubOperators(tangens, doubleFunctions);
    129 
    130       functionLibrary.AddFunction(differential);
    131       functionLibrary.AddFunction(variable);
    132       functionLibrary.AddFunction(constant);
    133       functionLibrary.AddFunction(addition);
    134       // functionLibrary.AddFunction(average);
    135       functionLibrary.AddFunction(and);
    136       functionLibrary.AddFunction(cosinus);
    137       functionLibrary.AddFunction(division);
    138       functionLibrary.AddFunction(equal);
    139       functionLibrary.AddFunction(exponential);
    140       functionLibrary.AddFunction(greaterThan);
    141       functionLibrary.AddFunction(ifThenElse);
    142       functionLibrary.AddFunction(lessThan);
    143       functionLibrary.AddFunction(logarithm);
    144       functionLibrary.AddFunction(multiplication);
    145       functionLibrary.AddFunction(not);
    146       functionLibrary.AddFunction(power);
    147       functionLibrary.AddFunction(or);
    148       functionLibrary.AddFunction(signum);
    149       functionLibrary.AddFunction(sinus);
    150       functionLibrary.AddFunction(sqrt);
    151       functionLibrary.AddFunction(subtraction);
    152       functionLibrary.AddFunction(tangens);
    153       functionLibrary.AddFunction(xor);
    154 
    155       variable.MinTimeOffset = variable.MaxTimeOffset = 0;
    156       differential.MinTimeOffset = differential.MaxTimeOffset = 0;
    157 
    158       return functionLibrary;
    159 
     50    public static SymbolicExpressionTree[] CreateRandomTrees(MersenneTwister twister, Dataset dataset, ISymbolicExpressionGrammar grammar, int popSize) {
     51      return CreateRandomTrees(twister, dataset, grammar, popSize, 1, 200, 3, 3);
    16052    }
    16153
    162     private static void SetAllowedSubOperators(IFunction f, IEnumerable<IFunction> gs) {
    163       for (int i = 0; i < f.MaxSubTrees; i++) {
    164         SetAllowedSubOperators(f, i, gs);
     54    public static SymbolicExpressionTree[] CreateRandomTrees(MersenneTwister twister, Dataset dataset, ISymbolicExpressionGrammar grammar,
     55      int popSize, int minSize, int maxSize,
     56      int maxFunctionDefinitions, int maxFunctionArguments) {
     57      foreach (Variable variableSymbol in grammar.Symbols.OfType<Variable>()) {
     58        variableSymbol.VariableNames = dataset.VariableNames.Skip(1);
    16559      }
    166     }
    167 
    168     private static void SetAllowedSubOperators(IFunction f, int i, IEnumerable<IFunction> gs) {
    169       foreach (var g in gs) {
    170         f.AddAllowedSubFunction(g, i);
    171       }
    172     }
    173 
    174     public static IFunctionTree[] CreateRandomTrees(MersenneTwister twister, Dataset dataset, FunctionLibrary funLib, int popSize) {
    175       return CreateRandomTrees(twister, dataset, funLib, popSize, 1, 200);
    176     }
    177 
    178     public static IFunctionTree[] CreateRandomTrees(MersenneTwister twister, Dataset dataset, int popSize) {
    179       return CreateRandomTrees(twister, dataset, popSize, 1, 200);
    180     }
    181 
    182     public static IFunctionTree[] CreateRandomTrees(MersenneTwister twister, Dataset dataset, int popSize, int minSize, int maxSize) {
    183       return CreateRandomTrees(twister, dataset, Util.CreateFunctionLibrary(), popSize, minSize, maxSize);
    184     }
    185 
    186     public static IFunctionTree[] CreateRandomTrees(MersenneTwister twister, Dataset dataset, FunctionLibrary funLib, int popSize, int minSize, int maxSize) {
    187       IFunctionTree[] randomTrees = new IFunctionTree[popSize];
     60      SymbolicExpressionTree[] randomTrees = new SymbolicExpressionTree[popSize];
    18861      for (int i = 0; i < randomTrees.Length; i++) {
    189         randomTrees[i] = ProbabilisticTreeCreator.Create(twister, funLib, minSize, maxSize, maxSize + 1);
     62        randomTrees[i] = ProbabilisticTreeCreator.Create(twister, grammar, maxSize, 10, maxFunctionDefinitions, maxFunctionArguments);
    19063      }
    19164      return randomTrees;
     
    20073        }
    20174      }
    202       Dataset ds = new Dataset(data);
    203       ds.SetVariableName(0, "y");
     75      IEnumerable<string> variableNames = new string[] { "y" }.Concat(Enumerable.Range(0, columns - 1).Select(x => "x" + x.ToString()));
     76      Dataset ds = new Dataset(variableNames, data);
    20477      return ds;
    20578    }
     
    20982    }
    21083
    211     public static void EvaluateTrees(IFunctionTree[] trees, ITreeEvaluator evaluator, Dataset dataset, int repetitions) {
     84    public static void EvaluateTrees(SymbolicExpressionTree[] trees, ISymbolicExpressionTreeInterpreter interpreter, Dataset dataset, int repetitions) {
    21285      double[] estimation = new double[dataset.Rows];
    21386      // warm up
    21487      for (int i = 0; i < trees.Length; i++) {
    215         evaluator.PrepareForEvaluation(dataset, trees[i]);
    216         for (int row = 1; row < dataset.Rows; row++) {
    217           estimation[row] = evaluator.Evaluate(row);
    218         }
     88        estimation = interpreter.GetSymbolicExpressionTreeValues(trees[i], dataset, Enumerable.Range(0, dataset.Rows)).ToArray();
    21989      }
    22090
    22191      Stopwatch watch = new Stopwatch();
    222       Stopwatch compileWatch = new Stopwatch();
    22392      long nNodes = 0;
    22493      for (int rep = 0; rep < repetitions; rep++) {
    22594        watch.Start();
    22695        for (int i = 0; i < trees.Length; i++) {
    227           compileWatch.Start();
    228           evaluator.PrepareForEvaluation(dataset, trees[i]);
    229           nNodes += trees[i].GetSize() * (dataset.Rows - 1);
    230           compileWatch.Stop();
    231           for (int row = 1; row < dataset.Rows; row++) {
    232             estimation[row] = evaluator.Evaluate(row);
    233           }
     96          nNodes += trees[i].Size * (dataset.Rows - 1);
     97          estimation = interpreter.GetSymbolicExpressionTreeValues(trees[i], dataset, Enumerable.Range(0, dataset.Rows)).ToArray();
    23498        }
    23599        watch.Stop();
    236100      }
    237       Assert.Inconclusive("Random tree evaluation performance of " + evaluator.GetType() + ":" +
    238         watch.ElapsedMilliseconds + "ms (" + compileWatch.ElapsedMilliseconds + " ms) " +
     101      Assert.Inconclusive("Random tree evaluation performance of " + interpreter.GetType() + ":" +
     102        watch.ElapsedMilliseconds + "ms " +
    239103        Util.NodesPerSecond(nNodes, watch) + " nodes/sec");
    240104    }
Note: See TracChangeset for help on using the changeset viewer.