Changeset 15812
- Timestamp:
- 02/23/18 18:10:30 (7 years ago)
- Location:
- branches/2886_SymRegGrammarEnumeration
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2886_SymRegGrammarEnumeration
- Property svn:ignore
-
old new 1 1 TestResults 2 *.gitignore
-
-
Property
svn:global-ignores
set to
.git
- Property svn:ignore
-
branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/Grammar.cs
r15806 r15812 143 143 } 144 144 145 private int CalcHashCode<THashType>(SymbolString sentence, Func<Symbol, IEnumerable<THashType>, THashType> aggregateFunction) {145 private int CalcHashCode<THashType>(SymbolString sentence, Func<Symbol, THashType[], THashType> aggregateFunction) { 146 146 Debug.Assert(sentence.Any(), "Trying to evaluate empty sentence!"); 147 147 … … 152 152 } 153 153 154 private THashType[] GetSubtreeHashes<THashType>(Stack<Symbol> parseStack, Func<Symbol, IEnumerable<THashType>, THashType> aggregateHashes) {154 private THashType[] GetSubtreeHashes<THashType>(Stack<Symbol> parseStack, Func<Symbol, THashType[], THashType> aggregateHashes) { 155 155 Symbol currentSymbol = parseStack.Pop(); 156 156 … … 207 207 208 208 var currFactor = childHashes[i]; 209 var invFactor = aggregateHashes(Inv, currFactor.ToEnumerable());209 var invFactor = aggregateHashes(Inv, new[] { currFactor }); 210 210 211 211 int indexOfInv = childHashes.IndexOf(invFactor); … … 228 228 229 229 // var or nonterminal symbol 230 return new[] { aggregateHashes(currentSymbol, Enumerable.Empty<THashType>()) };231 } 232 233 private string AggregateStringHashes(Symbol operatorSym, IEnumerable<string>hashes) {230 return new[] { aggregateHashes(currentSymbol, new THashType[0]) }; 231 } 232 233 private string AggregateStringHashes(Symbol operatorSym, string[] hashes) { 234 234 var hashesArray = hashes.ToArray(); 235 235 … … 237 237 return hashesArray[0]; 238 238 } 239 if (operatorSym is NonterminalSymbol || Var.VariableTerminalSymbols.Contains(operatorSym)) {239 if (operatorSym is NonterminalSymbol || ((TerminalSymbol)operatorSym).IsVariable) { 240 240 return operatorSym.StringRepresentation; 241 241 } … … 244 244 } 245 245 246 private int AggregateIntHashes(Symbol operatorSym, IEnumerable<int> hashes) { 247 var hashesArray = hashes.ToArray(); 248 246 private int AggregateIntHashes(Symbol operatorSym, int[] hashes) { 249 247 int start; 250 248 if ((ReferenceEquals(operatorSym, Addition) || ReferenceEquals(operatorSym, Multiplication)) && 251 hashes Array.Count()<= 1) {249 hashes.Length <= 1) { 252 250 start = 0; 253 251 254 } else if (operatorSym is NonterminalSymbol || Var.VariableTerminalSymbols.Contains(operatorSym)) {252 } else if (operatorSym is NonterminalSymbol || ((TerminalSymbol)operatorSym).IsVariable) { 255 253 return operatorSym.StringRepresentation.GetHashCode(); 256 254 … … 259 257 } 260 258 261 return hashesArray.Aggregate(start, (result, ti) => ((result << 5) + result) ^ ti.GetHashCode()); 259 for (int i = 0; i < hashes.Length; i++) { 260 start = ((start << 5) + start) ^ hashes[i]; 261 } 262 return start; 262 263 } 263 264 #endregion … … 314 315 parsedSubTree.AddSubtree(ParseSymbolicExpressionTree(parseStack)); 315 316 316 } else if ( Var.VariableTerminalSymbols.Contains(currentSymbol)) {317 } else if (currentSymbol.IsVariable) { 317 318 VariableTreeNode varNode = (VariableTreeNode)varSy.CreateTreeNode(); 318 319 varNode.Weight = 1.0; … … 337 338 Symbol head = parseStack.Pop(); 338 339 339 SymbolString result = new SymbolString( );340 SymbolString result = new SymbolString(parseStack.Count); 340 341 341 342 if (ReferenceEquals(head, Addition) || ReferenceEquals(head, Multiplication)) { -
branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/GrammarEnumerationAlgorithm.cs
r15806 r15812 70 70 public Dictionary<int, SymbolString> DistinctSentences { get; private set; } // Semantically distinct sentences in a run. 71 71 public Dictionary<int, List<SymbolString>> AllSentences { get; private set; } // All sentences ever generated in a run. 72 public Dictionary<int, SymbolString> ArchivedPhrases { get; private set; } // Nodes in the search tree 72 public HashSet<int> ArchivedPhrases { get; private set; } 73 73 74 internal SearchDataStore OpenPhrases { get; private set; } // Stack/Queue/etc. for fetching the next node in the search tree. 74 75 … … 104 105 105 106 AllSentences = new Dictionary<int, List<SymbolString>>(); 106 ArchivedPhrases = new Dictionary<int, SymbolString>(); // Nodes in the search tree 107 ArchivedPhrases = new HashSet<int>(); 108 107 109 DistinctSentences = new Dictionary<int, SymbolString>(); 108 110 Expansions = 0; … … 125 127 StoredSymbolString fetchedPhrase = OpenPhrases.GetNext(); 126 128 SymbolString currPhrase = fetchedPhrase.SymbolString; 127 129 #if DEBUG 128 130 LogSearchGraph(dotFileTrace, $"{fetchedPhrase.Hash} [label=\"{Grammar.PostfixToInfixParser(fetchedPhrase.SymbolString)}\"];"); 129 130 ArchivedPhrases.Add(fetchedPhrase.Hash , currPhrase);131 #endif 132 ArchivedPhrases.Add(fetchedPhrase.Hash); 131 133 132 134 // expand next nonterminal symbols … … 135 137 136 138 foreach (Production productionAlternative in expandedSymbol.Alternatives) { 137 SymbolString newPhrase = new SymbolString(currPhrase); 139 SymbolString newPhrase = new SymbolString(currPhrase.Count + productionAlternative.Count); 140 newPhrase.AddRange(currPhrase); 138 141 newPhrase.RemoveAt(nonterminalSymbolIndex); // TODO: removeat and insertRange are both O(n) 139 142 newPhrase.InsertRange(nonterminalSymbolIndex, productionAlternative); … … 142 145 if (newPhrase.Count <= MaxTreeSize) { 143 146 var phraseHash = Grammar.CalcHashCode(newPhrase); 144 147 #if DEBUG 145 148 LogSearchGraph(dotFileTrace, $"{fetchedPhrase.Hash} -> {phraseHash} [label=\"{expandedSymbol.StringRepresentation} + → {productionAlternative}\"];"); 146 149 #endif 147 150 if (newPhrase.IsSentence()) { 148 151 // Sentence was generated. … … 155 158 EvaluateSentence(newPhrase); 156 159 160 #if DEBUG 157 161 LogSearchGraph(dotFileTrace, $"{phraseHash} [label=\"{Grammar.PostfixToInfixParser(newPhrase)}\", style=\"filled\"];"); 162 #endif 158 163 } 159 UpdateView( AllSentences, DistinctSentences);160 161 } else if (!OpenPhrases.Contains(phraseHash) && !ArchivedPhrases.Contains Key(phraseHash)) {164 UpdateView(); 165 166 } else if (!OpenPhrases.Contains(phraseHash) && !ArchivedPhrases.Contains(phraseHash)) { 162 167 OpenPhrases.Store(phraseHash, newPhrase); 163 168 } … … 165 170 } 166 171 } 167 172 #if DEBUG 168 173 // Overwrite formatting of start search node and best found solution. 169 174 LogSearchGraph(dotFileTrace, $"{Grammar.CalcHashCode(BestTrainingSentence)} [label=\"{Grammar.PostfixToInfixParser(BestTrainingSentence)}\", shape=Mcircle, style=\"filled,bold\"];"); 170 175 LogSearchGraph(dotFileTrace, $"{phrase0Hash} [label=\"{Grammar.PostfixToInfixParser(phrase0)}\", shape=doublecircle];}}"); 171 176 dotFileTrace.Flush(); 172 } 173 174 UpdateView(AllSentences, DistinctSentences, force: true); 177 #endif 178 } 179 180 UpdateView(force: true); 175 181 UpdateFinalResults(); 176 182 } … … 184 190 } 185 191 186 192 #region Evaluation of generated models. 187 193 188 194 // Evaluate sentence within an algorithm run. … … 208 214 } 209 215 210 211 212 216 #endregion 217 218 #region Visualization in HL 213 219 // Initialize entries in result set. 214 220 private void InitResults() { … … 228 234 // Update the view for intermediate results in an algorithm run. 229 235 private int updates; 230 private void UpdateView(Dictionary<int, List<SymbolString>> allGenerated, 231 Dictionary<int, SymbolString> distinctGenerated, bool force = false) { 236 private void UpdateView(bool force = false) { 232 237 updates++; 233 238 234 if (force || updates % GuiUpdateInterval == 0) {235 var allGeneratedEnum = allGenerated.Values.SelectMany(x => x).ToArray();239 if (force || updates % GuiUpdateInterval == 1) { 240 var allGeneratedEnum = AllSentences.Values.SelectMany(x => x).ToArray(); 236 241 ((IntValue)Results[GeneratedPhrasesName].Value).Value = ArchivedPhrases.Count; 237 242 ((IntValue)Results[SearchStructureSizeName].Value).Value = OpenPhrases.Count; 238 243 ((IntValue)Results[GeneratedSentencesName].Value).Value = allGeneratedEnum.Length; 239 ((IntValue)Results[DistinctSentencesName].Value).Value = distinctGenerated.Count;244 ((IntValue)Results[DistinctSentencesName].Value).Value = DistinctSentences.Count; 240 245 ((IntValue)Results[PhraseExpansionsName].Value).Value = Expansions; 241 246 ((IntValue)Results[GeneratedEqualSentencesName].Value).Value = EqualGeneratedSentences; … … 279 284 Results.Add(new Result("Distinct generated sentences", new StringMatrix(distinctSentencesMatrix))); 280 285 } 281 282 private void LogSearchGraph(TraceListener listener, String msg) { 283 #if false 286 287 private void LogSearchGraph(TraceListener listener, string msg) { 284 288 listener.Write(msg); 285 #endif 286 } 287 #endregion 289 } 290 #endregion 288 291 289 292 } -
branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/Sentence.cs
r15723 r15812 8 8 public class SymbolString : List<Symbol> { 9 9 10 public SymbolString( ) { }10 public SymbolString(int capacity) : base(capacity) { } 11 11 12 12 public SymbolString(IEnumerable<Symbol> symbols) : base(symbols) { } -
branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/Symbol.cs
r15800 r15812 16 16 17 17 public class TerminalSymbol : Symbol { 18 public TerminalSymbol(string representation) : base(representation) { } 18 public readonly bool IsVariable; 19 20 public TerminalSymbol(string representation, bool isVariable = false) : base(representation) { 21 IsVariable = isVariable; 22 } 19 23 } 20 24 … … 39 43 40 44 foreach (string variableName in variableNames) { 41 TerminalSymbol s = new TerminalSymbol(variableName );45 TerminalSymbol s = new TerminalSymbol(variableName, isVariable: true); 42 46 createdSymbols.Add(s); 43 47 AddProduction(s);
Note: See TracChangeset
for help on using the changeset viewer.