Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Tests/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4/GrammarsTest.cs @ 14341

Last change on this file since 14341 was 14341, checked in by mkommend, 7 years ago

#2685: Added unit test for min expression length and depth of configured grammars.

File size: 3.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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.Linq;
23using Microsoft.VisualStudio.TestTools.UnitTesting;
24
25namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Tests {
26  [TestClass]
27  public class GrammarsTest {
28    [TestMethod]
29    [TestCategory("Encodings.SymbolicExpressionTree")]
30    [TestProperty("Time", "short")]
31    public void MinimumExpressionLengthTest() {
32      var grammar = CreateAbstractGrammar();
33
34      var prs = grammar.ProgramRootSymbol;
35      var a = grammar.Symbols.First(s => s.Name == "<a>");
36      var b = grammar.Symbols.First(s => s.Name == "<b>");
37
38      Assert.AreEqual(4, grammar.GetMinimumExpressionLength(prs));
39      Assert.AreEqual(4, grammar.GetMinimumExpressionLength(a));
40      Assert.AreEqual(3, grammar.GetMinimumExpressionLength(b));
41    }
42
43    [TestMethod]
44    [TestCategory("Encodings.SymbolicExpressionTree")]
45    [TestProperty("Time", "short")]
46    public void MinimumExpressionDepthTest() {
47      var grammar = CreateAbstractGrammar();
48
49      var prs = grammar.ProgramRootSymbol;
50      var a = grammar.Symbols.First(s => s.Name == "<a>");
51      var b = grammar.Symbols.First(s => s.Name == "<b>");
52
53      Assert.AreEqual(4, grammar.GetMinimumExpressionDepth(prs));
54      Assert.AreEqual(4, grammar.GetMinimumExpressionDepth(a));
55      Assert.AreEqual(3, grammar.GetMinimumExpressionDepth(b));
56    }
57
58    private static ISymbolicExpressionGrammar CreateAbstractGrammar() {
59      var grammar = new SimpleSymbolicExpressionGrammar();
60      var x = new SimpleSymbol("<x>", 1);
61      var s = new SimpleSymbol("<s>", 1);
62      var a = new SimpleSymbol("<a>", 1);
63      var b = new SimpleSymbol("<b>", 1);
64      var c = new SimpleSymbol("<c>", 1);
65      var d = new SimpleSymbol("<d>", 1);
66      var e = new SimpleSymbol("<e>", 1);
67
68      var _x = new SimpleSymbol("x", 0);
69      var _y = new SimpleSymbol("y", 0);
70
71      grammar.AddSymbol(x);
72      grammar.AddSymbol(s);
73      grammar.AddSymbol(a);
74      grammar.AddSymbol(b);
75      grammar.AddSymbol(c);
76      grammar.AddSymbol(d);
77      grammar.AddSymbol(e);
78      grammar.AddSymbol(_x);
79      grammar.AddSymbol(_y);
80
81      grammar.AddAllowedChildSymbol(grammar.StartSymbol, x);
82      grammar.AddAllowedChildSymbol(x, s);
83      grammar.AddAllowedChildSymbol(x, _x);
84      grammar.AddAllowedChildSymbol(s, a);
85      grammar.AddAllowedChildSymbol(a, b);
86      grammar.AddAllowedChildSymbol(a, c);
87      grammar.AddAllowedChildSymbol(b, x);
88      grammar.AddAllowedChildSymbol(c, d);
89      grammar.AddAllowedChildSymbol(d, e);
90      grammar.AddAllowedChildSymbol(e, _y);
91
92      return grammar;
93    }
94  }
95}
Note: See TracBrowser for help on using the repository browser.