Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/Test/TestCanonicalExpressions.cs @ 13808

Last change on this file since 13808 was 12024, checked in by gkronber, 10 years ago

#2283: changed handling of inverse expressions in transformation of expressions to canonical form

File size: 5.9 KB
RevLine 
[11902]1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using HeuristicLab.Algorithms.Bandits;
[12014]7using HeuristicLab.Algorithms.Bandits.GrammarPolicies;
8using HeuristicLab.Algorithms.GrammaticalOptimization;
9using HeuristicLab.Problems.GrammaticalOptimization.SymbReg;
[11902]10using Microsoft.VisualStudio.TestTools.UnitTesting;
11
12namespace HeuristicLab.Problems.GrammaticalOptimization.Test {
13  [TestClass]
14  public class TestCanonicalExpressions {
15    [TestMethod]
16    public void TestFactorReordering() {
17      var extender = new ExpressionExtender();
18      Assert.AreEqual("a*b", extender.CanonicalRepresentation("b*a"));
19      Assert.AreEqual("a*b*c", extender.CanonicalRepresentation("b*a*c"));
20      Assert.AreEqual("a*b*E", extender.CanonicalRepresentation("b*a*E"));
21    }
22
23    [TestMethod]
24    public void TestDuplicateTerms() {
25      var extender = new ExpressionExtender();
26      Assert.AreEqual("a", extender.CanonicalRepresentation("a+a"));
27      Assert.AreEqual("a+b", extender.CanonicalRepresentation("b+a+b"));
28      Assert.AreEqual("a+b", extender.CanonicalRepresentation("b+a-b")); // even though b-b canceles should keep this as we might find a non-zero factor for b
29    }
30
31    [TestMethod]
32    public void TestSimpleTermReordering() {
33      var extender = new ExpressionExtender();
34      Assert.AreEqual("a+b", extender.CanonicalRepresentation("b+a"));
35      Assert.AreEqual("a+b+c", extender.CanonicalRepresentation("b+a+c"));
36      Assert.AreEqual("a+b+E", extender.CanonicalRepresentation("b+a+E"));
37    }
38
39    [TestMethod]
40    public void TestCombinedTermReordering() {
41      var extender = new ExpressionExtender();
42      Assert.AreEqual("a+b+a*b+E", extender.CanonicalRepresentation("b*a+b+a+E"));
43      Assert.AreEqual("a+b+b*c+E", extender.CanonicalRepresentation("c*b+b+a+E"));
44      Assert.AreEqual("a+b+b*c+a*b*c+E", extender.CanonicalRepresentation("c*b+b*a*c+b+a+E"));
45    }
46    [TestMethod]
47    public void TestNegativeAndReorderTerms() {
48      var extender = new ExpressionExtender();
49      // when using constant opt the negative sign is not necessary because a negative factor can be produced
50      Assert.AreEqual("a+b", extender.CanonicalRepresentation("b-a"));
51      Assert.AreEqual("a+b+c", extender.CanonicalRepresentation("b-a-c"));
52      Assert.AreEqual("a+b+E", extender.CanonicalRepresentation("b-a-E"));
53    }
54    [TestMethod]
55    public void TestFactorExpansion() {
56      var extender = new ExpressionExtender();
57      Assert.AreEqual("a+b", extender.CanonicalRepresentation("(b+a)"));
58      Assert.AreEqual("a+b", extender.CanonicalRepresentation("(b)+(a)"));
59      Assert.AreEqual("a+b", extender.CanonicalRepresentation("(b+(a))"));
60      Assert.AreEqual("a+b", extender.CanonicalRepresentation("((b)+a)"));
61
62      Assert.AreEqual("a+b+c", extender.CanonicalRepresentation("b-(a-c)"));
63      Assert.AreEqual("a+b+c", extender.CanonicalRepresentation("b-(a+c)"));
64      Assert.AreEqual("a+b+E", extender.CanonicalRepresentation("b-(a-E)"));
65
66      // when using constant opt the negative sign is not necessary because a negative factor can be produced
[11966]67      Assert.AreEqual("a*b+b*c", extender.CanonicalRepresentation("b*(c-a)"));
[11902]68      Assert.AreEqual("a*b+a*d+b*c+c*d", extender.CanonicalRepresentation("(b-d)*(c-a)"));
[12024]69      Assert.AreEqual("/(a+b)", extender.CanonicalRepresentation("c%c%(a+b)"));
[11902]70    }
71    [TestMethod]
72    public void TestDivisionExpansion() {
73      var extender = new ExpressionExtender();
[12024]74      Assert.AreEqual("a*/c+b*/c", extender.CanonicalRepresentation("(b+a)%c"));
75      Assert.AreEqual("a*/c*/d+b*/c*/d", extender.CanonicalRepresentation("(b+a)%(d*c)"));
76      Assert.AreEqual("a*/(c+d)+b*/(c+d)", extender.CanonicalRepresentation("(b-a)%(d-c)"));
77      Assert.AreEqual("a*b*/(c+d)", extender.CanonicalRepresentation("(b*a)%(d-c)"));
[11902]78
[12024]79      Assert.AreEqual("a*b*/(a+b)*/(c+d)", extender.CanonicalRepresentation("(b*a)%(d-c)%(a+b)"));
80      Assert.AreEqual("a*b*/(c+d)*/(a*/e+b*/e)", extender.CanonicalRepresentation("((b*a)%(d-c))%((a+b)%e)"));
[11902]81      // a*b*e%(c+d)%(a+b)
82    }
83    [TestMethod]
84    public void TestDivisionCancellation() {
85      var extender = new ExpressionExtender();
[12024]86      Assert.AreEqual("1", extender.CanonicalRepresentation("a%a"));
[11966]87      Assert.AreEqual("a", extender.CanonicalRepresentation("a*a%a"));
[12024]88      Assert.AreEqual("/a", extender.CanonicalRepresentation("(a%a)%a"));
89      Assert.AreEqual("/a", extender.CanonicalRepresentation("a%a%a"));
[11902]90      Assert.AreEqual("a", extender.CanonicalRepresentation("a%(a%a)"));
[12024]91      Assert.AreEqual("1", extender.CanonicalRepresentation("(a+b)%(b+a)"));
92      Assert.AreEqual("/a+/b", extender.CanonicalRepresentation("(a+b)%(a*b)"));
93      Assert.AreEqual("a*/(a*c*/b+e*/d*/f)+b*/(a*c*/b+e*/d*/f)", extender.CanonicalRepresentation("(a+b)%(a%b*c+e%f%d)"));
94      Assert.AreEqual("1", extender.CanonicalRepresentation("(a%a%a+b%b%b)%(a%a*a%a%a+b%b*b%b%b)"));
95      Assert.AreEqual("1", extender.CanonicalRepresentation("(a%(a%a)+b%(b%b))%(a+b)"));
[11902]96    }
[12014]97
98    [TestMethod]
99    public void TestRandomExpressions() {
100      // samples sentences for the Tower dataset with the random MCTS policy
101      // and evaluates the original and the extended expression
102      // the results must be the same
103
104      var problem = new SymbolicRegressionProblem(new Random(), "Tower");
105      var random = new Random(31415);
106      var solver = new SequentialSearch(problem, 30, random, 0, new RandomPolicy(problem, true));
107
108      var extender = new ExpressionExtender();
109
110      solver.SolutionEvaluated += (sentence, quality) => {
111        var canonicalSentence = extender.CanonicalRepresentation(sentence);
112
113        Assert.AreEqual(problem.SimpleEvaluate(sentence), problem.SimpleEvaluate(canonicalSentence), 1E-4, string.Format("{0} <> {1}", sentence, canonicalSentence));
114      };
115
116      solver.Run(10000);
117    }
[11902]118  }
119}
Note: See TracBrowser for help on using the repository browser.