Changeset 15726 for branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration
- Timestamp:
- 02/06/18 22:11:56 (7 years ago)
- File:
-
- 1 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;
Note: See TracChangeset
for help on using the changeset viewer.