Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4/InfixExpressionParserTest.cs @ 18203

Last change on this file since 18203 was 18203, checked in by gkronber, 2 years ago

#3145: fixed a bug in the infix formatter introduced in my earlier commit

File size: 12.2 KB
RevLine 
[14024]1#region License Information
2/* HeuristicLab
[17180]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[14024]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
23using System;
[18203]24using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
[14024]25using Microsoft.VisualStudio.TestTools.UnitTesting;
26namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Tests {
27
28
29  [TestClass]
30  public class InfixExpressionParserTest {
31    [TestMethod]
32    [TestCategory("Problems.DataAnalysis.Symbolic")]
33    [TestProperty("Time", "short")]
[14109]34    public void InfixExpressionParserTestFormatting() {
[14024]35      var formatter = new InfixExpressionFormatter();
36      var parser = new InfixExpressionParser();
[18167]37      Assert.AreEqual("3", formatter.Format(parser.Parse("3")));
[18170]38      Assert.AreEqual("3 * 3", formatter.Format(parser.Parse("3*3")));
39      Assert.AreEqual("3 * 4",formatter.Format(parser.Parse("3 * 4")));
[18167]40      Assert.AreEqual("0.123",formatter.Format(parser.Parse("123E-03")));
41      Assert.AreEqual("0.123",formatter.Format(parser.Parse("123e-03")));
42      Assert.AreEqual("123000",formatter.Format(parser.Parse("123e+03")));
43      Assert.AreEqual("123000",formatter.Format(parser.Parse("123E+03")));
44      Assert.AreEqual("0.123",formatter.Format(parser.Parse("123.0E-03")));
45      Assert.AreEqual("0.123",formatter.Format(parser.Parse("123.0e-03")));
46      Assert.AreEqual("123000",formatter.Format(parser.Parse("123.0e+03")));
47      Assert.AreEqual("123000",formatter.Format(parser.Parse("123.0E+03")));
48      Assert.AreEqual("0.123",formatter.Format(parser.Parse("123.0E-3")));
49      Assert.AreEqual("0.123",formatter.Format(parser.Parse("123.0e-3")));
50      Assert.AreEqual("123000",formatter.Format(parser.Parse("123.0e+3")));
51      Assert.AreEqual("123000",formatter.Format(parser.Parse("123.0E+3")));
[14024]52
[18170]53      Assert.AreEqual("3.1415 + 2",formatter.Format(parser.Parse("3.1415+2.0")));
54      Assert.AreEqual("3.1415 / 2",formatter.Format(parser.Parse("3.1415/2.0")));
55      Assert.AreEqual("3.1415 * 2",formatter.Format(parser.Parse("3.1415*2.0")));
56      Assert.AreEqual("3.1415 - 2", formatter.Format(parser.Parse("3.1415-2.0")));
[14024]57      // round-trip
[18170]58      Assert.AreEqual("3.1415 - 2",formatter.Format(parser.Parse(formatter.Format(parser.Parse("3.1415-2.0")))));
59      Assert.AreEqual("3.1415 + 2",formatter.Format(parser.Parse("3.1415+(2.0)")));
60      Assert.AreEqual("3.1415 + 2", formatter.Format(parser.Parse("(3.1415+(2.0))")));
[14024]61
62
[18167]63      Assert.AreEqual("LOG(3)",formatter.Format(parser.Parse("log(3)")));
[18170]64      Assert.AreEqual("LOG(-3)",formatter.Format(parser.Parse("log(-3)")));
[18167]65      Assert.AreEqual("EXP(3)",formatter.Format(parser.Parse("exp(3)")));
[18170]66      Assert.AreEqual("EXP(-3)",formatter.Format(parser.Parse("exp(-3)")));
[18167]67      Assert.AreEqual("SQRT(3)", formatter.Format(parser.Parse("sqrt(3)")));
[14024]68
[18170]69      Assert.AreEqual("SQR(-3)", formatter.Format(parser.Parse("sqr((-3))")));
[14024]70
[18170]71      Assert.AreEqual("3 / 3 + 2 / 2 + 1 / 1",formatter.Format(parser.Parse("3/3+2/2+1/1")));
72      Assert.AreEqual("-3 + 30 - 2 + 20 - 1 + 10", formatter.Format(parser.Parse("-3+30-2+20-1+10")));
[14024]73
[18169]74      // 'flattening' of nested addition, subtraction, multiplication, or division
[18170]75      Assert.AreEqual("1 + 2 + 3 + 4", formatter.Format(parser.Parse("1 + 2 + 3 + 4")));
76      Assert.AreEqual("1 - 2 - 3 - 4", formatter.Format(parser.Parse("1 - 2 - 3 - 4")));
77      Assert.AreEqual("1 * 2 * 3 * 4", formatter.Format(parser.Parse("1 * 2 * 3 * 4")));
78      Assert.AreEqual("1 / 2 / 3 / 4", formatter.Format(parser.Parse("1 / 2 / 3 / 4")));
[18169]79
80      // signed variables / constants
[18170]81      Assert.AreEqual("-1*'x1'", formatter.Format(parser.Parse("-x1")));
[18169]82      Assert.AreEqual("1", formatter.Format(parser.Parse("--1.0")));
83      Assert.AreEqual("1", formatter.Format(parser.Parse("----1.0")));
84      Assert.AreEqual("1", formatter.Format(parser.Parse("-+-1.0")));
85      Assert.AreEqual("1", formatter.Format(parser.Parse("+-+-1.0")));
[18170]86      Assert.AreEqual("-3 + -1", formatter.Format(parser.Parse("-3 + -1.0")));
[18169]87
88
[18167]89      Assert.AreEqual("'x1'",formatter.Format(parser.Parse("\"x1\"")));
90      Assert.AreEqual("'var name'",formatter.Format(parser.Parse("\'var name\'")));
91      Assert.AreEqual("'var name'",formatter.Format(parser.Parse("\"var name\"")));
92      Assert.AreEqual("'1'",formatter.Format(parser.Parse("\"1\"")));
[14024]93
[18167]94      Assert.AreEqual("'var \" name'",formatter.Format(parser.Parse("'var \" name\'")));
95      Assert.AreEqual("\"var ' name\"", formatter.Format(parser.Parse("\"var \' name\"")));
[14024]96
97
[18170]98      Assert.AreEqual("'x1' * 'x2'",formatter.Format(parser.Parse("\"x1\"*\"x2\"")));
99      Assert.AreEqual("'x1' * 'x2' + 'x3' * 'x4'",formatter.Format(parser.Parse("\"x1\"*\"x2\"+\"x3\"*\"x4\"")));
100      Assert.AreEqual("'x1' * 'x2' + 'x3' * 'x4'", formatter.Format(parser.Parse("x1*x2+x3*x4")));
[14024]101
[14347]102
[18170]103      Assert.AreEqual("3 ^ 2",formatter.Format(parser.Parse("POW(3, 2)")));
104      Assert.AreEqual("3.1 ^ 2.1",formatter.Format(parser.Parse("POW(3.1, 2.1)")));
105      Assert.AreEqual("3.1 ^ 2.1",formatter.Format(parser.Parse("POW(3.1 , 2.1)")));
106      Assert.AreEqual("3.1 ^ 2.1",formatter.Format(parser.Parse("POW(3.1 ,2.1)")));
107      Assert.AreEqual("-3.1 ^ -2.1",formatter.Format(parser.Parse("POW(-3.1 , - 2.1)")));
[18167]108      Assert.AreEqual("ROOT(3, 2)",formatter.Format(parser.Parse("ROOT(3, 2)")));
109      Assert.AreEqual("ROOT(3.1, 2.1)",formatter.Format(parser.Parse("ROOT(3.1, 2.1)")));
110      Assert.AreEqual("ROOT(3.1, 2.1)",formatter.Format(parser.Parse("ROOT(3.1 , 2.1)")));
111      Assert.AreEqual("ROOT(3.1, 2.1)",formatter.Format(parser.Parse("ROOT(3.1 ,2.1)")));
[18170]112      Assert.AreEqual("ROOT(-3.1, -2.1)", formatter.Format(parser.Parse("ROOT(-3.1 , -2.1)")));
[14347]113
[18167]114      Assert.AreEqual("IF(GT(0, 1), 1, 0)",formatter.Format(parser.Parse("IF(GT( 0, 1), 1, 0)")));
115      Assert.AreEqual("IF(LT(0, 1), 1, 0)",formatter.Format(parser.Parse("IF(LT(0,1), 1 , 0)")));
116      Assert.AreEqual("LAG('x', 1)",formatter.Format(parser.Parse("LAG(x, 1)")));
117      Assert.AreEqual("LAG('x', -1)",formatter.Format(parser.Parse("LAG(x, -1)")));
118      Assert.AreEqual("LAG('x', 1)",formatter.Format(parser.Parse("LAG(x, +1)")));
[18170]119      Assert.AreEqual("'x' * LAG('x', 1)", formatter.Format(parser.Parse("x * LAG('x', +1)")));
[14347]120
[18167]121      // factor variables
[18170]122      Assert.AreEqual("'x'[1] * 'y'",formatter.Format(parser.Parse("x [1.0] * y")));
123      Assert.AreEqual("'x'[1, 2] * 'y'[1, 2]",formatter.Format(parser.Parse("x [1.0, 2.0] * y [1.0, 2.0]")));
124      Assert.AreEqual("'x'[1] * 'y'",formatter.Format(parser.Parse("x[1] * y")));
125      Assert.AreEqual("'x'[1, 2] * 'y'[1, 2]",formatter.Format(parser.Parse("x[1, 2] * y [1, 2]")));
126      Assert.AreEqual("'x'[1] * 'y'",formatter.Format(parser.Parse("x [+1.0] * y")));
127      Assert.AreEqual("'x'[-1] * 'y'",formatter.Format(parser.Parse("x [-1.0] * y")));
128      Assert.AreEqual("'x'[-1, -2] * 'y'[1, 2]", formatter.Format(parser.Parse("x [-1.0, -2.0] * y [+1.0, +2.0]")));
[14350]129
[18167]130      // one-hot for factor
[18170]131      Assert.AreEqual("'x' = 'val' * 'y'",formatter.Format(parser.Parse("x='val' * y")));
[18167]132      Assert.AreEqual("'x' = 'val'",formatter.Format(parser.Parse("x = 'val'")));
133      Assert.AreEqual("'x' = 'val'",formatter.Format(parser.Parse("x = \"val\"")));
[18170]134      Assert.AreEqual("1 * 'x' = 'val'",formatter.Format(parser.Parse("1.0 * x = val")));
135      Assert.AreEqual("-1 * 'x' = 'val'",formatter.Format(parser.Parse("-1.0 * x = val")));
136      Assert.AreEqual("1 * 'x' = 'val1' + 'y' = 'val2'", formatter.Format(parser.Parse("+1.0 * \"x\" = val1 + y = \"val2\"")));
[18169]137
138      // numeric parameters
139      Assert.AreEqual("0", formatter.Format(parser.Parse("<num>"))); // default initial value is zero
140      Assert.AreEqual("0", formatter.Format(parser.Parse("< num >")));
141      Assert.AreEqual("1", formatter.Format(parser.Parse("< num=1.0>")));
142      Assert.AreEqual("1", formatter.Format(parser.Parse("< num = 1.0>")));
[18170]143      Assert.AreEqual("-1", formatter.Format(parser.Parse("< num =-1.0>")));
144      Assert.AreEqual("-1", formatter.Format(parser.Parse("< num = - 1.0>")));
[18169]145     
146      // numeric parameter with sign
147      Assert.AreEqual("1", formatter.Format(parser.Parse("-<num=-1.0>")));
[18171]148
149      // nested functions
150      Assert.AreEqual("SIN(SIN(SIN('X1')))", formatter.Format(parser.Parse("SIN(SIN(SIN(X1)))")));
[18203]151
152      {
153        // a tree with single-arity multiplication and addition
154        //   ...
155        //    *
156        //    |
157        //    +
158        //   / \
159        //  v1 v2
160        //
161        // is still formatted as (v1 + v2) even though it is not strictly necessary
162        var root = new ProgramRootSymbol().CreateTreeNode();
163        var start = new StartSymbol().CreateTreeNode();
164        var mul = new Multiplication().CreateTreeNode();
165        var add = new Addition().CreateTreeNode();
166        var var1 = (VariableTreeNode)new Variable().CreateTreeNode(); var1.VariableName = "x1"; var1.Weight = 1.0;
167        var var2 = (VariableTreeNode)new Variable().CreateTreeNode(); var2.VariableName = "x2"; var2.Weight = 1.0;
168        add.AddSubtree(var1);
169        add.AddSubtree(var2);
170        mul.AddSubtree(add);
171        start.AddSubtree(mul);
172        root.AddSubtree(start);
173        var t = new SymbolicExpressionTree(root);
174
175        Assert.AreEqual("('x1' + 'x2')", formatter.Format(t)); // TODO parenthesis not strictly required here
176      }
177      {
178        //    *
179        //    |\
180        //    * v3
181        //    |
182        //    +
183        //   / \
184        //  v1 v2
185        //
186        // is still formatted as (v1 + v2) even though it is not strictly necessary
187        var root = new ProgramRootSymbol().CreateTreeNode();
188        var start = new StartSymbol().CreateTreeNode();
189        var mul1 = new Multiplication().CreateTreeNode();
190        var mul2 = new Multiplication().CreateTreeNode();
191        var add = new Addition().CreateTreeNode();
192        var var1 = (VariableTreeNode)new Variable().CreateTreeNode(); var1.VariableName = "x1"; var1.Weight = 1.0;
193        var var2 = (VariableTreeNode)new Variable().CreateTreeNode(); var2.VariableName = "x2"; var2.Weight = 1.0;
194        var var3 = (VariableTreeNode)new Variable().CreateTreeNode(); var3.VariableName = "x3"; var3.Weight = 1.0;
195        add.AddSubtree(var1);
196        add.AddSubtree(var2);
197        mul2.AddSubtree(add);
198        mul1.AddSubtree(mul2);
199        mul1.AddSubtree(var3);
200        start.AddSubtree(mul1);
201        root.AddSubtree(start);
202        var t = new SymbolicExpressionTree(root);
203
204        Assert.AreEqual("('x1' + 'x2') * 'x3'", formatter.Format(t));
205      }
206
207      {
208        //   sin
209        //    |
210        //    *
211        //    |
212        //    +
213        //   / \
214        //  v1 v2
215        //
216        // is still formatted as (v1 + v2) even though it is not strictly necessary
217        var root = new ProgramRootSymbol().CreateTreeNode();
218        var start = new StartSymbol().CreateTreeNode();
219        var sin = new Sine().CreateTreeNode();
220        var mul = new Multiplication().CreateTreeNode();
221        var add = new Addition().CreateTreeNode();
222        var var1 = (VariableTreeNode)new Variable().CreateTreeNode(); var1.VariableName = "x1"; var1.Weight = 1.0;
223        var var2 = (VariableTreeNode)new Variable().CreateTreeNode(); var2.VariableName = "x2"; var2.Weight = 1.0;
224        add.AddSubtree(var1);
225        add.AddSubtree(var2);
226        mul.AddSubtree(add);
227        sin.AddSubtree(mul);
228        start.AddSubtree(sin);
229        root.AddSubtree(start);
230        var t = new SymbolicExpressionTree(root);
231
232        Assert.AreEqual("SIN(('x1' + 'x2'))", formatter.Format(t)); // TODO would be better to prevent double parenthesis here
233      }
[14024]234    }
235  }
236}
Note: See TracBrowser for help on using the repository browser.