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