using System; using System.Diagnostics; using System.Linq; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace HeuristicLab.Problems.GrammaticalOptimization.Test { [TestClass] public class TestInstances { [TestMethod] public void TestGrammarFromString() { { var g = Grammar.FromString(@" G(S): S -> aA | bB A -> a | aA B -> Bb | b "); Assert.AreEqual('S', g.SentenceSymbol); Assert.AreEqual(2, g.TerminalSymbols.Count()); Assert.AreEqual(3, g.NonTerminalSymbols.Count()); Assert.IsTrue(g.TerminalSymbols.All(s => new char[] { 'a', 'b' }.Contains(s))); Assert.IsTrue(g.NonTerminalSymbols.All(s => new char[] { 'S', 'A', 'B' }.Contains(s))); Assert.AreEqual(2, g.GetAlternatives('S').Count()); Assert.AreEqual(2, g.GetAlternatives('A').Count()); Assert.AreEqual(2, g.GetAlternatives('B').Count()); Assert.IsTrue(g.GetAlternatives('S').Any(s => s.ToString() == "aA")); Assert.IsTrue(g.GetAlternatives('S').Any(s => s.ToString() == "bB")); Assert.IsTrue(g.GetAlternatives('A').Any(s => s.ToString() == "aA")); Assert.IsTrue(g.GetAlternatives('A').Any(s => s.ToString() == "a")); Assert.IsTrue(g.GetAlternatives('B').Any(s => s.ToString() == "Bb")); Assert.IsTrue(g.GetAlternatives('B').Any(s => s.ToString() == "b")); Assert.AreEqual(2, g.MinPhraseLength(new Sequence("S"))); Assert.AreEqual(short.MaxValue, g.MaxPhraseLength(new Sequence("S"))); Assert.AreEqual(1, g.MinPhraseLength(new Sequence("A"))); Assert.AreEqual(short.MaxValue, g.MaxPhraseLength(new Sequence("A"))); Assert.AreEqual(1, g.MinPhraseLength(new Sequence("B"))); Assert.AreEqual(short.MaxValue, g.MaxPhraseLength(new Sequence("B"))); } { var g = Grammar.FromString(@" G(S): S -> sS "); Assert.AreEqual('S', g.SentenceSymbol); Assert.AreEqual(1, g.TerminalSymbols.Count()); Assert.AreEqual(1, g.NonTerminalSymbols.Count()); Assert.IsTrue(g.TerminalSymbols.All(s => new char[] { 's' }.Contains(s))); Assert.IsTrue(g.NonTerminalSymbols.All(s => new char[] { 'S' }.Contains(s))); Assert.AreEqual(1, g.GetAlternatives('S').Count()); Assert.IsTrue(g.GetAlternatives('S').Any(s => s.ToString() == "sS")); Assert.AreEqual(short.MaxValue, g.MinPhraseLength(new Sequence("S"))); Assert.AreEqual(short.MaxValue, g.MaxPhraseLength(new Sequence("S"))); } { var g = Grammar.FromString(@" G(S): S->sss|sS"); Assert.AreEqual('S', g.SentenceSymbol); Assert.AreEqual(1, g.TerminalSymbols.Count()); Assert.AreEqual(1, g.NonTerminalSymbols.Count()); Assert.IsTrue(g.TerminalSymbols.All(s => new char[] { 's' }.Contains(s))); Assert.IsTrue(g.NonTerminalSymbols.All(s => new char[] { 'S' }.Contains(s))); Assert.AreEqual(2, g.GetAlternatives('S').Count()); Assert.IsTrue(g.GetAlternatives('S').Any(s => s.ToString() == "sss")); Assert.IsTrue(g.GetAlternatives('S').Any(s => s.ToString() == "sS")); Assert.AreEqual(3, g.MinPhraseLength(new Sequence("S"))); Assert.AreEqual(short.MaxValue, g.MaxPhraseLength(new Sequence("S"))); Assert.AreEqual(4, g.MinPhraseLength(new Sequence("sS"))); Assert.AreEqual(7, g.MinPhraseLength(new Sequence("sSS"))); Assert.AreEqual(short.MaxValue, g.MaxPhraseLength(new Sequence("sSS"))); Assert.AreEqual(3, g.MaxPhraseLength(new Sequence("sss"))); Assert.AreEqual(3, g.MinPhraseLength(new Sequence("sss"))); } { var g = Grammar.FromString(@" G(S): S->T|TS T-> a..z "); Assert.AreEqual('S', g.SentenceSymbol); Assert.AreEqual(26, g.TerminalSymbols.Count()); Assert.AreEqual(2, g.NonTerminalSymbols.Count()); Assert.IsTrue(g.NonTerminalSymbols.All(s => new char[] { 'S', 'T' }.Contains(s))); Assert.AreEqual(2, g.GetAlternatives('S').Count()); Assert.IsTrue(g.GetAlternatives('S').Any(s => s.ToString() == "T")); Assert.IsTrue(g.GetAlternatives('S').Any(s => s.ToString() == "TS")); Assert.AreEqual(1, g.MinPhraseLength(new Sequence("S"))); Assert.AreEqual(short.MaxValue, g.MaxPhraseLength(new Sequence("S"))); Assert.AreEqual(1, g.MinPhraseLength(new Sequence("T"))); Assert.AreEqual(1, g.MaxPhraseLength(new Sequence("T"))); } } [TestMethod] public void TestRoyalSymbolProblem() { var p = new RoyalSymbolProblem(); Console.WriteLine(p.Grammar); Assert.AreEqual(1, p.Evaluate("ab")); Assert.AreEqual(0, p.Evaluate("bb")); Assert.AreEqual(2, p.Evaluate("aa")); Assert.AreEqual(3, p.Evaluate("aaa")); } [TestMethod] public void TestRoyalPairProblem() { var p = new RoyalPairProblem(); Console.WriteLine(p.Grammar); Assert.AreEqual(1, p.Evaluate("ab")); Assert.AreEqual(0, p.Evaluate("bb")); Assert.AreEqual(0, p.Evaluate("aa")); Assert.AreEqual(0, p.Evaluate("aaa")); Assert.AreEqual(2, p.Evaluate("aba")); Assert.AreEqual(2, p.Evaluate("abba")); Assert.AreEqual(3, p.Evaluate("abab")); } [TestMethod] public void TestSantaFeAntProblem() { var p = new SantaFeAntProblem(); Console.WriteLine(p.Grammar); // all possible minimal solutions to the artificial ant problem Assert.AreEqual(89, p.Evaluate("?(m)(ll?(m)(r))mr")); Assert.AreEqual(89, p.Evaluate("?(m)(rr?(m)(l))ml")); Assert.AreEqual(89, p.Evaluate("r?(m)(ll?(m)(r))m")); Assert.AreEqual(89, p.Evaluate("l?(m)(rr?(m)(l))m")); Assert.AreEqual(89, p.Evaluate("mr?(m)(ll?(m)(r))")); Assert.AreEqual(89, p.Evaluate("ml?(m)(rr?(m)(l))")); Assert.AreEqual(89, p.Evaluate("?(m)(ll?(m)(l))ml")); Assert.AreEqual(89, p.Evaluate("?(m)(rr?(m)(r))mr")); Assert.AreEqual(89, p.Evaluate("l?(m)(ll?(m)(l))m")); Assert.AreEqual(89, p.Evaluate("r?(m)(rr?(m)(r))m")); Assert.AreEqual(89, p.Evaluate("ml?(m)(ll?(m)(l))")); Assert.AreEqual(89, p.Evaluate("mr?(m)(rr?(m)(r))")); } [TestMethod] public void TestRoyalRoadProblem() { var p = new RoyalRoadProblem(); Console.WriteLine(p.Grammar); } [TestMethod] public void TestPalindromeProblem() { Assert.AreEqual("aba", PalindromeProblem.LongestPalindromicSubstring("aba")); Assert.AreEqual("aba", PalindromeProblem.LongestPalindromicSubstring("aaba")); Assert.AreEqual("aba", PalindromeProblem.LongestPalindromicSubstring("abaa")); Assert.AreEqual("aabaa", PalindromeProblem.LongestPalindromicSubstring("aaaabaa")); Assert.AreEqual("aabaa", PalindromeProblem.LongestPalindromicSubstring("aabaaaa")); Assert.AreEqual("baaaab", PalindromeProblem.LongestPalindromicSubstring("aabaaaab")); var p = new PalindromeProblem(); Console.WriteLine(p.Grammar); Assert.AreEqual(1, p.Evaluate("a")); Assert.AreEqual(2, p.Evaluate("aa")); Assert.AreEqual(3, p.Evaluate("aaa")); Assert.AreEqual(3, p.Evaluate("aba")); Assert.AreEqual(3, p.Evaluate("baba")); Assert.AreEqual(3, p.Evaluate("abab")); Assert.AreEqual(5, p.Evaluate("babab")); } [TestMethod] public void TestHardPalindromeProblem() { var p = new HardPalindromeProblem(); Console.WriteLine(p.Grammar); Assert.AreEqual(1 + 1, p.Evaluate("a")); Assert.AreEqual(2 + 1, p.Evaluate("aa")); Assert.AreEqual(3 + 1, p.Evaluate("aaa")); Assert.AreEqual(3 + 2, p.Evaluate("aba")); Assert.AreEqual(3 + 2, p.Evaluate("baba")); Assert.AreEqual(3 + 2, p.Evaluate("abab")); Assert.AreEqual(5 + 2, p.Evaluate("babab")); Assert.AreEqual(5 + 3, p.Evaluate("bacab")); } [TestMethod] public void TestEvenParityProblem() { var p = new EvenParityProblem(); Console.WriteLine(p.Grammar); // a | b | c | d | res // 0 | 0 | 0 | 0 | 1 // 0 | 0 | 0 | 1 | 0 // 0 | 0 | 1 | 0 | 0 // 0 | 0 | 1 | 1 | 1 // 0 | 1 | 0 | 0 | 0 // 0 | 1 | 0 | 1 | 1 // 0 | 1 | 1 | 0 | 1 // 0 | 1 | 1 | 1 | 0 // 1 | 0 | 0 | 0 | 0 // 1 | 0 | 0 | 1 | 1 // 1 | 0 | 1 | 0 | 1 // 1 | 0 | 1 | 1 | 0 // 1 | 1 | 0 | 0 | 1 // 1 | 1 | 0 | 1 | 0 // 1 | 1 | 1 | 0 | 0 // 1 | 1 | 1 | 1 | 1 Assert.AreEqual(8, p.Evaluate("a")); Assert.AreEqual(8, p.Evaluate("b")); Assert.AreEqual(8, p.Evaluate("c")); Assert.AreEqual(8, p.Evaluate("d")); Assert.AreEqual(8, p.Evaluate("a*b")); // a and b Assert.AreEqual(8, p.Evaluate("a+b")); // a or b Assert.AreEqual(8, p.Evaluate("!a*b+a*!b")); // a xor b Assert.AreEqual(8, p.Evaluate("!(a*b)+(a*!b)")); // a xor b Assert.AreEqual(16, p.Evaluate("a^b^c^d")); // a xor b } [TestMethod] public void TestSymbolicRegressionPoly10Problem() { var p = new SymbolicRegressionPoly10Problem(); Console.WriteLine(p.Grammar); Assert.AreEqual(0.00319925877644673, p.Evaluate("a"), 1.0E-7); Assert.AreEqual(0.00632775454329686, p.Evaluate("b"), 1.0E-7); Assert.AreEqual(0.00569030954291292, p.Evaluate("c"), 1.0E-7); Assert.AreEqual(3.32317092313123E-05, p.Evaluate("d"), 1.0E-7); Assert.AreEqual(0.000562538829174826, p.Evaluate("e"), 1.0E-7); Assert.AreEqual(3.29458942533772E-06, p.Evaluate("f"), 1.0E-7); Assert.AreEqual(0.000193866006166768, p.Evaluate("g"), 1.0E-7); Assert.AreEqual(2.83339004096246E-05, p.Evaluate("h"), 1.0E-7); Assert.AreEqual(0.00291584574914239, p.Evaluate("i"), 1.0E-7); Assert.AreEqual(0.00504038616701691, p.Evaluate("j"), 1.0E-7); Assert.AreEqual(0.252718466940018, p.Evaluate("a*b"), 1.0E-7); Assert.AreEqual(0.290635611728845, p.Evaluate("c*d"), 1.0E-7); Assert.AreEqual(0.25737325167716, p.Evaluate("e*f"), 1.0E-7); Assert.AreEqual(0.00173739472363473, p.Evaluate("b*c"), 1.0E-7); Assert.AreEqual(3.15450564064128E-05, p.Evaluate("d*e"), 1.0E-7); Assert.AreEqual(0.0943358163760454, p.Evaluate("a*g*i"), 1.0E-7); Assert.AreEqual(0.116199534934045, p.Evaluate("c*f*j"), 1.0E-7); Assert.AreEqual(0.824522210419616, p.Evaluate("a*b+c*d+e*f"), 1E-7); Assert.AreEqual(1.0, p.Evaluate("a*b+c*d+e*f+a*g*i+c*f*j"), 1.0E-7); } [TestMethod] public void TestRoyalTreeProblem() { var p = new RoyalTreeProblem(7); Console.WriteLine(p.Grammar); // examples from The Royal Tree Problem, a Benchmark for Single and Multi-population Genetic Programming (Punch, Zongker, Goodman) Assert.AreEqual(4, p.Evaluate("ax"), 1.0E-7); Assert.AreEqual(384.0, p.Evaluate("cbaxaxbaxaxbaxax"), 1.0E-7); Assert.AreEqual(128.66, p.Evaluate("cbaxaxbaxaxbxx"), 1.0E-7); Assert.AreEqual(64.66, p.Evaluate("cbaxaxxx"), 1.0E-7); } } }