- Timestamp:
- 02/06/18 22:11:56 (7 years ago)
- Location:
- branches/2886_SymRegGrammarEnumeration
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/GrammarEnumerationAlgorithm.cs
r15725 r15726 57 57 public SymbolString BestTestSentence; 58 58 59 public List<Tuple<SymbolString, int>> DistinctGenerated; 60 public List<Tuple<SymbolString, int>> AllGenerated; 59 61 #endregion 60 62 … … 88 90 BestTrainingSentence = null; 89 91 BestTrainingSentence = null; 90 91 List<Tuple<SymbolString, int>> allGenerated = new List<Tuple<SymbolString, int>>(); 92 List<SymbolString> distinctGenerated = new List<SymbolString>(); 93 94 HashSet<int> evaluatedHashes = new HashSet<int>(); 92 AllGenerated = new List<Tuple<SymbolString, int>>(); 93 DistinctGenerated = new List<Tuple<SymbolString, int>>(); 94 95 Dictionary<int, SymbolString> evaluatedHashes = new Dictionary<int, SymbolString>(); 95 96 96 97 Grammar = new Grammar(Problem.ProblemData.AllowedInputVariables.ToArray()); … … 105 106 106 107 if (currSymbolString.IsSentence()) { 107 allGenerated.Add(new Tuple<SymbolString, int>(currSymbolString, Grammar.CalcHashCode(currSymbolString))); 108 109 if (evaluatedHashes.Add(Grammar.CalcHashCode(currSymbolString))) { 108 AllGenerated.Add(new Tuple<SymbolString, int>(currSymbolString, Grammar.CalcHashCode(currSymbolString))); 109 110 int currSymbolStringHash = Grammar.CalcHashCode(currSymbolString); 111 if (!evaluatedHashes.ContainsKey(currSymbolStringHash) 112 || evaluatedHashes[currSymbolStringHash].Count > currSymbolString.Count) { 113 evaluatedHashes[currSymbolStringHash] = currSymbolString; 114 115 DistinctGenerated.Add(new Tuple<SymbolString, int>(currSymbolString, Grammar.CalcHashCode(currSymbolString))); 110 116 EvaluateSentence(currSymbolString); 111 distinctGenerated.Add(Grammar.PostfixToInfixParser(currSymbolString));112 117 } 113 114 UpdateView(allGenerated, distinctGenerated); 118 UpdateView(AllGenerated, DistinctGenerated); 115 119 116 120 } else { … … 131 135 } 132 136 133 UpdateView( allGenerated, distinctGenerated, force: true);134 135 string[,] sentences = new string[ allGenerated.Count, 3];136 for (int i = 0; i < allGenerated.Count; i++) {137 sentences[i, 0] = allGenerated[i].Item1.ToString();138 sentences[i, 1] = Grammar.PostfixToInfixParser( allGenerated[i].Item1).ToString();139 sentences[i, 2] = allGenerated[i].Item2.ToString();137 UpdateView(AllGenerated, DistinctGenerated, force: true); 138 139 string[,] sentences = new string[AllGenerated.Count, 3]; 140 for (int i = 0; i < AllGenerated.Count; i++) { 141 sentences[i, 0] = AllGenerated[i].Item1.ToString(); 142 sentences[i, 1] = Grammar.PostfixToInfixParser(AllGenerated[i].Item1).ToString(); 143 sentences[i, 2] = AllGenerated[i].Item2.ToString(); 140 144 } 141 145 Results.Add(new Result("All generated sentences", new StringMatrix(sentences))); 142 146 143 StringArray distinctSentences = new StringArray(distinctGenerated.Select(r => r.ToString()).ToArray()); 144 Results.Add(new Result("Distinct generated sentences", distinctSentences)); 145 } 146 147 148 private void UpdateView(List<Tuple<SymbolString, int>> allGenerated, List<SymbolString> distinctGenerated, bool force = false) { 147 string[,] distinctSentences = new string[DistinctGenerated.Count, 3]; 148 for (int i = 0; i < DistinctGenerated.Count; i++) { 149 distinctSentences[i, 0] = DistinctGenerated[i].Item1.ToString(); 150 distinctSentences[i, 1] = Grammar.PostfixToInfixParser(DistinctGenerated[i].Item1).ToString(); 151 distinctSentences[i, 2] = DistinctGenerated[i].Item2.ToString(); 152 } 153 Results.Add(new Result("Distinct generated sentences", new StringMatrix(distinctSentences))); 154 } 155 156 157 private void UpdateView(List<Tuple<SymbolString, int>> allGenerated, 158 List<Tuple<SymbolString, int>> distinctGenerated, bool force = false) { 149 159 int generatedSolutions = allGenerated.Count; 150 160 int distinctSolutions = distinctGenerated.Count; … … 183 193 } else { 184 194 IRegressionSolution currBestTrainingSolution = (IRegressionSolution)currBestTrainingSolutionResult.Value; 185 if (currBestTrainingSolution.TrainingRSquared < newSolution.TrainingRSquared) {195 if (currBestTrainingSolution.TrainingRSquared <= newSolution.TrainingRSquared) { 186 196 BestTrainingSentence = symbolString; 187 197 currBestTrainingSolutionResult.Value = newSolution; … … 190 200 191 201 IRegressionSolution currBestTestSolution = (IRegressionSolution)currBestTestSolutionResult.Value; 192 if (currBestTestSolution.TestRSquared < newSolution.TestRSquared) {202 if (currBestTestSolution.TestRSquared <= newSolution.TestRSquared) { 193 203 BestTestSentence = symbolString; 194 204 currBestTestSolutionResult.Value = newSolution; -
branches/2886_SymRegGrammarEnumeration/Test/GrammarEnumerationTest.cs
r15724 r15726 2 2 using System.Linq; 3 3 using HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration; 4 using HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration.GrammarEnumeration; 4 5 using HeuristicLab.Common; 5 6 using HeuristicLab.Core; … … 43 44 44 45 45 private void TestGrammarEnumeration(IRegressionProblemData problemData) { 46 alg.Problem.ProblemData = problemData; 47 48 // Run 49 alg.Start(); 50 46 private void EvaluateGrammarEnumeration() { 51 47 // Evaluate results 52 48 var eps = 1.0 - SuccessThreshold; … … 72 68 public void MctsSymbReg_NoConstants_Nguyen1() { 73 69 // x³ + x² + x 74 alg.MaxTreeSize = 15; 70 alg.MaxTreeSize = 30; 71 Console.WriteLine(alg.MaxTreeSize); 75 72 var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider(Seed); 76 73 var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F1 "))); 77 TestGrammarEnumeration(regProblem); 74 alg.Problem.ProblemData = regProblem; 75 76 alg.Start(); 77 78 // Evaluate 79 // EvaluateGrammarEnumeration(); 80 81 TerminalSymbol varSymbol = alg.Grammar.Var.VariableTerminalSymbols.First(); 82 TerminalSymbol mulSymbol = alg.Grammar.Multiplication; 83 TerminalSymbol addSymbol = alg.Grammar.Addition; 84 85 SymbolString targetSolution = new SymbolString(new[] { 86 varSymbol, varSymbol, varSymbol, mulSymbol, mulSymbol, 87 varSymbol, varSymbol, mulSymbol, addSymbol, 88 varSymbol, addSymbol 89 }); 90 91 int targetSolutionHash = alg.Grammar.CalcHashCode(targetSolution); 92 int actualSolutionHash = alg.Grammar.CalcHashCode(alg.BestTestSentence); 93 94 Assert.IsTrue(alg.DistinctGenerated.Select(tuple => tuple.Item2).Contains(actualSolutionHash)); 95 96 // last because long sentences are overwritten by short ones. 97 Console.WriteLine(alg.Grammar.PostfixToInfixParser(alg.DistinctGenerated.Last(tuple => tuple.Item2 == targetSolutionHash).Item1)); 98 99 100 Assert.AreEqual(targetSolutionHash, actualSolutionHash); 78 101 } 79 102 … … 82 105 public void MctsSymbReg_NoConstants_Nguyen2() { 83 106 // x^4 + x³ + x² + x 84 alg.MaxTreeSize = 20;107 alg.MaxTreeSize = 30; 85 108 var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider(Seed); 86 109 var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F2 "))); 87 TestGrammarEnumeration(regProblem); 110 alg.Problem.ProblemData = regProblem; 111 112 alg.Start(); 113 114 // Evaluate 115 EvaluateGrammarEnumeration(); 88 116 } 89 117
Note: See TracChangeset
for help on using the changeset viewer.