Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2283: solution reorganization

File size: 5.1 KB
Line 
1using System;
2using HeuristicLab.Algorithms.GeneticProgramming;
3using HeuristicLab.Algorithms.GrammaticalOptimization;
4using Microsoft.VisualStudio.TestTools.UnitTesting;
5
6namespace HeuristicLab.Problems.GrammaticalOptimization.Test {
7  [TestClass]
8  public class TestSolvers {
9    [TestMethod]
10    public void TestExhaustiveBFS() {
11      {
12        // G(A): A -> l | r | m | ?(A)(A) | lA | rA | mA
13        var prob = new SantaFeAntProblem();
14        var comb = 3;
15        TestBFS(prob, 1, comb);
16
17        comb = comb * 3 + 3;
18        TestBFS(prob, 2, comb);
19
20        comb = comb * 3 + 3;
21        TestBFS(prob, 3, comb);
22
23        comb = comb * 3 + 3;
24        TestBFS(prob, 4, comb);
25
26        comb = comb * 3 + 3;
27        TestBFS(prob, 5, comb);
28
29        comb = comb * 3 + 3;
30        TestBFS(prob, 6, comb);
31
32        comb = comb * 3 + 3 + (3 * 3); // additionally for ?(A)(A)
33        TestBFS(prob, 7, comb);
34      }
35      {
36        // G(E):
37        // E -> V | V+E | V-E | V*E | V/E | (E)
38        // V -> a .. j
39        /* grammar has been change ... unit test not yet adapted
40        var prob = new SymbolicRegressionPoly10Problem();
41        var comb = 10;
42        TestDFS(prob, 1, comb);
43        TestDFS(prob, 2, comb);
44       
45        comb = comb + 10 * 4 * comb + comb;
46        TestDFS(prob, 3, comb);
47        TestDFS(prob, 4, comb);
48       
49        comb = comb + 10 * 4 * comb + 10; // ((E))
50        TestDFS(prob, 5, comb);
51        TestDFS(prob, 6, comb);
52       
53        comb = comb + 10 * 4 * comb + 10; // (((E)))  */
54        // takes too long
55        //TestDFS(prob, 7, comb);
56        //TestDFS(prob, 8, comb);
57      }
58    }
59
60    private void TestBFS(IProblem prob, int len, int numExpectedSols) {
61      var solver = new ExhaustiveBreadthFirstSearch(prob, len);
62      int numSols = 0;
63
64      solver.SolutionEvaluated += (s, d) => { numSols++; };
65
66      solver.Run(int.MaxValue);
67      Assert.AreEqual(numExpectedSols, numSols);
68    }
69
70    [TestMethod]
71    public void TestExhaustiveDFS() {
72      {
73        // G(A): A -> l | r | m | ?(A)(A) | lA | rA | mA
74        var prob = new SantaFeAntProblem();
75        var comb = 3;
76        TestDFS(prob, 1, comb);
77
78        comb = comb * 3 + 3;
79        TestDFS(prob, 2, comb);
80
81        comb = comb * 3 + 3;
82        TestDFS(prob, 3, comb);
83
84        comb = comb * 3 + 3;
85        TestDFS(prob, 4, comb);
86
87        comb = comb * 3 + 3;
88        TestDFS(prob, 5, comb);
89
90        comb = comb * 3 + 3;
91        TestDFS(prob, 6, comb);
92
93        comb = comb * 3 + 3 + (3 * 3); // additionally for ?(A)(A)
94        TestDFS(prob, 7, comb);
95      }
96      {
97        // G(E):
98        // E -> V | V+E | V-E | V*E | V/E | (E)
99        // V -> a .. j
100        /* grammar has been change ... unit test not yet adapted
101        var prob = new SymbolicRegressionPoly10Problem();
102        var comb = 10;
103        TestDFS(prob, 1, comb);
104        TestDFS(prob, 2, comb);
105
106        comb = comb + 10 * 4 * comb + comb;
107        TestDFS(prob, 3, comb);
108        TestDFS(prob, 4, comb);
109
110        comb = comb + 10 * 4 * comb + 10; // ((E))
111        TestDFS(prob, 5, comb);
112        TestDFS(prob, 6, comb);
113
114        comb = comb + 10 * 4 * comb + 10; // (((E))) */
115        // takes too long
116        //TestDFS(prob, 7, comb);
117        //TestDFS(prob, 8, comb);
118      }
119    }
120
121    private void TestDFS(IProblem prob, int len, int numExpectedSols) {
122      var solver = new ExhaustiveDepthFirstSearch(prob, len);
123      int numSols = 0;
124
125      solver.SolutionEvaluated += (s, d) => { numSols++; Console.WriteLine(s); };
126
127      solver.Run(int.MaxValue);
128      Assert.AreEqual(numExpectedSols, numSols);
129    }
130
131    [TestMethod]
132    public void TestStandardGP() {
133      var prob = new SymbolicRegressionPoly10Problem();
134      var rand = new Random(31415);
135      var sgp = new StandardGP(prob, rand);
136      sgp.PopulationSize = 10000;
137      sgp.MaxSolutionSize = 50;
138      sgp.MaxSolutionDepth = 20;
139
140      string bestSentence = string.Empty;
141      double bestQuality = double.NegativeInfinity;
142
143      sgp.FoundNewBestSolution += (sentence, quality) => {
144        Assert.Inconclusive(string.Format("{0:N3} {1}", quality, sentence));
145        bestSentence = sentence;
146        bestQuality = quality;
147      };
148
149      sgp.Run(100000);
150
151      Assert.AreEqual(1.0, bestQuality, 1E-6);
152      Assert.AreEqual("", bestSentence);
153    }
154
155    [TestMethod]
156    public void TestOSGP() {
157      var prob = new SymbolicRegressionPoly10Problem();
158      var rand = new Random(31415);
159      var osgp = new OffspringSelectionGP(prob, rand);
160      osgp.PopulationSize = 1000;
161      osgp.MaxSolutionSize = 50;
162      osgp.MaxSolutionDepth = 20;
163
164      string bestSentence = string.Empty;
165      double bestQuality = double.NegativeInfinity;
166
167      osgp.FoundNewBestSolution += (sentence, quality) => {
168        Assert.Inconclusive(string.Format("{0:N3} {1}", quality, sentence));
169        bestSentence = sentence;
170        bestQuality = quality;
171      };
172
173      osgp.Run(100000);
174
175      Assert.AreEqual(1.0, bestQuality, 1E-6);
176      Assert.AreEqual("", bestSentence);
177    }
178  }
179}
Note: See TracBrowser for help on using the repository browser.