- Timestamp:
- 02/09/18 15:32:44 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2886_SymRegGrammarEnumeration/Test/GrammarEnumerationTest.cs
r15734 r15746 1 1 using System; 2 using System.Diagnostics; 2 3 using System.Linq; 3 4 using HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration; … … 24 25 [TestInitialize] 25 26 public void InitTest() { 26 Console.Write("init called... ");27 27 rand = new FastRandom(Seed); 28 28 … … 33 33 } 34 34 35 [TestCleanup ()]35 [TestCleanup] 36 36 public void Cleanup() { 37 37 if (alg.BestTrainingSentence != null) { 38 38 Console.WriteLine("Training: " + alg.Grammar.PostfixToInfixParser(alg.BestTrainingSentence)); 39 39 } 40 if (alg.BestTestSentence != null) {41 Console.WriteLine("Test: " + alg.Grammar.PostfixToInfixParser(alg.BestTestSentence));42 }43 40 } 44 41 … … 49 46 50 47 // Check if algorithm terminated correctly 51 Assert.IsTrue(alg.Results.ContainsKey("Best solution quality (training)"), "No training solution returned!"); 52 Assert.IsTrue(alg.Results.ContainsKey("Best solution quality (test)"), "No test solution returned!"); 48 Assert.IsTrue(alg.Results.ContainsKey("Best solution (Training)"), "No training solution returned!"); 53 49 54 50 // Check resultss 55 Assert.AreEqual(1.0, ((DoubleValue)alg.Results["Best solution quality (training)"].Value).Value, eps, "Training quality too low!"); 56 Assert.AreEqual(1.0, ((DoubleValue)alg.Results["Best solution quality (test)"].Value).Value, eps, "Test quality too low!"); 57 58 // Check overfitting 59 Assert.AreEqual(alg.Grammar.CalcHashCode(alg.BestTrainingSentence), 60 alg.Grammar.CalcHashCode(alg.BestTestSentence), 61 "Training and Test solution are not equal!"); 51 Assert.AreEqual(1.0, ((IRegressionSolution)alg.Results["Best solution (Training)"].Value).TestRSquared, eps, "Test quality too low!"); 62 52 } 63 53 … … 68 58 public void MctsSymbReg_NoConstants_Nguyen1() { 69 59 // x³ + x² + x 70 alg.MaxTreeSize = 3;60 alg.MaxTreeSize = 15; 71 61 Console.WriteLine(alg.MaxTreeSize); 72 62 var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider(Seed); … … 75 65 76 66 alg.Start(); 77 78 // Evaluate79 // EvaluateGrammarEnumeration();80 67 81 68 TerminalSymbol varSymbol = alg.Grammar.Var.VariableTerminalSymbols.First(); … … 90 77 91 78 int targetSolutionHash = alg.Grammar.CalcHashCode(targetSolution); 92 int actualSolutionHash = alg.Grammar.CalcHashCode(alg.BestTestSentence); 93 94 Assert.IsTrue(alg.distinctSentences.Select(tuple => tuple.Item2).Contains(actualSolutionHash)); 95 96 // last because long sentences are overwritten by short ones. 97 Console.WriteLine(alg.Grammar.PostfixToInfixParser(alg.distinctSentences.Last(tuple => tuple.Item2 == targetSolutionHash).Item1)); 98 99 100 Assert.AreEqual(targetSolutionHash, actualSolutionHash); 79 int actualSolutionHash = alg.Grammar.CalcHashCode(alg.BestTrainingSentence); 80 81 Assert.IsTrue(alg.DistinctSentences.ContainsKey(actualSolutionHash), "Actual solution was not generated!"); 82 83 Assert.AreEqual(targetSolutionHash, actualSolutionHash, "Actual solution was not recognized as best one."); 84 85 // Evaluate 86 EvaluateGrammarEnumeration(); 101 87 } 102 88 … … 105 91 public void MctsSymbReg_NoConstants_Nguyen2() { 106 92 // x^4 + x³ + x² + x 107 alg.MaxTreeSize = 30;93 alg.MaxTreeSize = 25; 108 94 var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider(Seed); 109 95 var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F2 "))); … … 111 97 112 98 alg.Start(); 99 100 TerminalSymbol varSymbol = alg.Grammar.Var.VariableTerminalSymbols.First(); 101 TerminalSymbol mulSymbol = alg.Grammar.Multiplication; 102 TerminalSymbol addSymbol = alg.Grammar.Addition; 103 104 SymbolString targetSolution = new SymbolString(new[] { 105 varSymbol, varSymbol, varSymbol, varSymbol, mulSymbol, mulSymbol, mulSymbol, 106 varSymbol, varSymbol, varSymbol, mulSymbol, mulSymbol, addSymbol, 107 varSymbol, varSymbol, mulSymbol, addSymbol, 108 varSymbol, addSymbol 109 }); 110 111 int targetSolutionHash = alg.Grammar.CalcHashCode(targetSolution); 112 int actualSolutionHash = alg.Grammar.CalcHashCode(alg.BestTrainingSentence); 113 114 var targetSolutionStr = alg.Grammar.PostfixToInfixParser(targetSolution).ToString(); 115 var foundTargetSolutionStr = alg.Grammar.PostfixToInfixParser(alg.DistinctSentences[targetSolutionHash]).ToString(); 116 var actualSolutionStr = alg.Grammar.PostfixToInfixParser(alg.DistinctSentences[targetSolutionHash]).ToString(); 117 118 Assert.IsTrue(alg.DistinctSentences.ContainsKey(targetSolutionHash), "Target solution was not generated!"); 119 120 Assert.AreEqual(targetSolutionHash, actualSolutionHash, "Actual solution was not recognized as best one."); 113 121 114 122 // Evaluate … … 126 134 alg.Start(); 127 135 128 Assert.AreEqual(alg. distinctSentences.Count(), 110);136 Assert.AreEqual(alg.DistinctSentences.Count(), 110); 129 137 130 138 // Evaluate … … 142 150 alg.Start(); 143 151 144 Assert.AreEqual(alg. distinctSentences.Count(), 16);152 Assert.AreEqual(alg.DistinctSentences.Count(), 16); 145 153 146 154 // Evaluate
Note: See TracChangeset
for help on using the changeset viewer.