Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization.Test/TestSolvers.cs @ 11842

Last change on this file since 11842 was 11730, checked in by gkronber, 9 years ago

#2283: several major extensions for grammatical optimization

File size: 3.6 KB
Line 
1using System;
2using HeuristicLab.Algorithms.GrammaticalOptimization;
3using Microsoft.VisualStudio.TestTools.UnitTesting;
4
5namespace HeuristicLab.Problems.GrammaticalOptimization.Test {
6  [TestClass]
7  public class TestSolvers {
8    [TestMethod]
9    public void TestExhaustiveBFS() {
10      {
11        // G(A): A -> l | r | m | ?(A)(A) | lA | rA | mA
12        var prob = new SantaFeAntProblem();
13        var comb = 3;
14        TestBFS(prob, 1, comb);
15
16        comb = comb * 3 + 3;
17        TestBFS(prob, 2, comb);
18
19        comb = comb * 3 + 3;
20        TestBFS(prob, 3, comb);
21
22        comb = comb * 3 + 3;
23        TestBFS(prob, 4, comb);
24
25        comb = comb * 3 + 3;
26        TestBFS(prob, 5, comb);
27
28        comb = comb * 3 + 3;
29        TestBFS(prob, 6, comb);
30
31        comb = comb * 3 + 3 + (3 * 3); // additionally for ?(A)(A)
32        TestBFS(prob, 7, comb);
33      }
34      {
35        // G(E):
36        // E -> V | V+E | V-E | V*E | V/E | (E)
37        // V -> a .. j
38        /* grammar has been change ... unit test not yet adapted
39        var prob = new SymbolicRegressionPoly10Problem();
40        var comb = 10;
41        TestDFS(prob, 1, comb);
42        TestDFS(prob, 2, comb);
43       
44        comb = comb + 10 * 4 * comb + comb;
45        TestDFS(prob, 3, comb);
46        TestDFS(prob, 4, comb);
47       
48        comb = comb + 10 * 4 * comb + 10; // ((E))
49        TestDFS(prob, 5, comb);
50        TestDFS(prob, 6, comb);
51       
52        comb = comb + 10 * 4 * comb + 10; // (((E)))  */
53        // takes too long
54        //TestDFS(prob, 7, comb);
55        //TestDFS(prob, 8, comb);
56      }
57    }
58
59    private void TestBFS(IProblem prob, int len, int numExpectedSols) {
60      var solver = new ExhaustiveBreadthFirstSearch(prob, len);
61      int numSols = 0;
62
63      solver.SolutionEvaluated += (s, d) => { numSols++; };
64
65      solver.Run(int.MaxValue);
66      Assert.AreEqual(numExpectedSols, numSols);
67    }
68
69    [TestMethod]
70    public void TestExhaustiveDFS() {
71      {
72        // G(A): A -> l | r | m | ?(A)(A) | lA | rA | mA
73        var prob = new SantaFeAntProblem();
74        var comb = 3;
75        TestDFS(prob, 1, comb);
76
77        comb = comb * 3 + 3;
78        TestDFS(prob, 2, comb);
79
80        comb = comb * 3 + 3;
81        TestDFS(prob, 3, comb);
82
83        comb = comb * 3 + 3;
84        TestDFS(prob, 4, comb);
85
86        comb = comb * 3 + 3;
87        TestDFS(prob, 5, comb);
88
89        comb = comb * 3 + 3;
90        TestDFS(prob, 6, comb);
91
92        comb = comb * 3 + 3 + (3 * 3); // additionally for ?(A)(A)
93        TestDFS(prob, 7, comb);
94      }
95      {
96        // G(E):
97        // E -> V | V+E | V-E | V*E | V/E | (E)
98        // V -> a .. j
99        /* grammar has been change ... unit test not yet adapted
100        var prob = new SymbolicRegressionPoly10Problem();
101        var comb = 10;
102        TestDFS(prob, 1, comb);
103        TestDFS(prob, 2, comb);
104
105        comb = comb + 10 * 4 * comb + comb;
106        TestDFS(prob, 3, comb);
107        TestDFS(prob, 4, comb);
108
109        comb = comb + 10 * 4 * comb + 10; // ((E))
110        TestDFS(prob, 5, comb);
111        TestDFS(prob, 6, comb);
112
113        comb = comb + 10 * 4 * comb + 10; // (((E))) */
114        // takes too long
115        //TestDFS(prob, 7, comb);
116        //TestDFS(prob, 8, comb);
117      }
118    }
119
120    private void TestDFS(IProblem prob, int len, int numExpectedSols) {
121      var solver = new ExhaustiveDepthFirstSearch(prob, len);
122      int numSols = 0;
123
124      solver.SolutionEvaluated += (s, d) => { numSols++; Console.WriteLine(s); };
125
126      solver.Run(int.MaxValue);
127      Assert.AreEqual(numExpectedSols, numSols);
128    }
129
130  }
131}
Note: See TracBrowser for help on using the repository browser.