Changeset 16026 for branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/GrammarEnumerationAlgorithm.cs
- Timestamp:
- 07/27/18 19:28:18 (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/GrammarEnumerationAlgorithm.cs
r16022 r16026 32 32 33 33 private readonly string OptimizeConstantsParameterName = "Optimize Constants"; 34 private readonly string ConstantOptimizationIterationsParameterName = "Constant Optimization Iterations"; 34 35 private readonly string ErrorWeightParameterName = "Error Weight"; 35 36 private readonly string SearchDataStructureParameterName = "Search Data Structure"; … … 55 56 } 56 57 58 public int ConstantOptimizationIterations { 59 get { return ConstantOptimizationIterationsParameter.Value.Value; } 60 set { ConstantOptimizationIterationsParameter.Value.Value = value; } 61 } 62 63 public IFixedValueParameter<IntValue> ConstantOptimizationIterationsParameter { 64 get { return (IFixedValueParameter<IntValue>)Parameters[ConstantOptimizationIterationsParameterName]; } 65 } 66 57 67 protected IFixedValueParameter<IntValue> MaxComplexityParameter { 58 68 get { return (IFixedValueParameter<IntValue>)Parameters[MaxComplexityParameterName]; } … … 116 126 117 127 [Storable] 118 public Symbol StringBestTrainingSentence { get; set; } // Currently set in RSquaredEvaluator: quite hacky, but makes testing much easier for now...128 public SymbolList BestTrainingSentence { get; set; } // Currently set in RSquaredEvaluator: quite hacky, but makes testing much easier for now... 119 129 #endregion 120 130 … … 185 195 Parameters.Add(new FixedValueParameter<IntValue>(SearchDataStructureSizeParameterName, "The size of the search data structure.", new IntValue((int)1e5))); 186 196 Parameters.Add(new FixedValueParameter<EnumValue<StorageType>>(SearchDataStructureParameterName, new EnumValue<StorageType>(StorageType.SortedSet))); 197 Parameters.Add(new FixedValueParameter<IntValue>(ConstantOptimizationIterationsParameterName, new IntValue(10))); 187 198 188 199 SearchDataStructureParameter.Value.ValueChanged += (o, e) => Prepare(); … … 240 251 #endregion 241 252 242 [Storable]243 private Dictionary<VariableTerminalSymbol, double> variableImportance;244 245 253 public override void Prepare() { 246 254 DistinctSentencesComplexity = new Dictionary<int, int>(); … … 259 267 if (previousExecutionState != ExecutionState.Paused) { 260 268 InitResults(); 261 var phrase0 = new Symbol String(new[] { Grammar.StartSymbol });269 var phrase0 = new SymbolList(new[] { Grammar.StartSymbol }); 262 270 var phrase0Hash = Grammar.Hasher.CalcHashCode(phrase0); 263 271 … … 268 276 var errorWeight = ErrorWeight; 269 277 var optimizeConstants = OptimizeConstants; // cache value to avoid parameter lookup 278 var iterations = ConstantOptimizationIterations; 270 279 // main search loop 271 280 while (OpenPhrases.Count > 0) { … … 278 287 continue; 279 288 280 Symbol String currPhrase = fetchedSearchNode.SymbolString;289 SymbolList currPhrase = fetchedSearchNode.SymbolList; 281 290 282 291 OnPhraseFetched(fetchedSearchNode.Hash, currPhrase); … … 292 301 PhraseExpansionCount++; 293 302 294 Symbol StringnewPhrase = currPhrase.DerivePhrase(nonterminalSymbolIndex, appliedProductions[i]);295 int newPhraseComplexity = Grammar.GetComplexity(newPhrase);303 SymbolList newPhrase = currPhrase.DerivePhrase(nonterminalSymbolIndex, appliedProductions[i]); 304 int newPhraseComplexity = newPhrase.Complexity; 296 305 297 306 if (newPhraseComplexity > MaxComplexity) … … 300 309 var phraseHash = Grammar.Hasher.CalcHashCode(newPhrase); 301 310 302 OnPhraseDerived(fetchedSearchNode.Hash, fetchedSearchNode.Symbol String, phraseHash, newPhrase, expandedSymbol, appliedProductions[i]);311 OnPhraseDerived(fetchedSearchNode.Hash, fetchedSearchNode.SymbolList, phraseHash, newPhrase, expandedSymbol, appliedProductions[i]); 303 312 304 313 if (newPhrase.IsSentence()) { 305 314 AllGeneratedSentencesCount++; 306 315 307 OnSentenceGenerated(fetchedSearchNode.Hash, fetchedSearchNode.Symbol String, phraseHash, newPhrase, expandedSymbol, appliedProductions[i]);316 OnSentenceGenerated(fetchedSearchNode.Hash, fetchedSearchNode.SymbolList, phraseHash, newPhrase, expandedSymbol, appliedProductions[i]); 308 317 309 318 // Is the best solution found? (only if RSquaredEvaluator is activated) … … 320 329 321 330 DistinctSentencesComplexity[phraseHash] = newPhraseComplexity; 322 OnDistinctSentenceGenerated(fetchedSearchNode.Hash, fetchedSearchNode.Symbol String, phraseHash, newPhrase, expandedSymbol, appliedProductions[i]);331 OnDistinctSentenceGenerated(fetchedSearchNode.Hash, fetchedSearchNode.SymbolList, phraseHash, newPhrase, expandedSymbol, appliedProductions[i]); 323 332 } 324 333 UpdateView(); … … 327 336 328 337 bool isCompleteSentence = IsCompleteSentence(newPhrase); 329 double r2 = isCompleteSentence ? Grammar.EvaluatePhrase(newPhrase, Problem.ProblemData, optimizeConstants ) : fetchedSearchNode.R2;330 double phrasePriority = GetPriority(newPhrase, r2 , MaxSentenceLength);338 double r2 = isCompleteSentence ? Grammar.EvaluatePhrase(newPhrase, Problem.ProblemData, optimizeConstants, iterations) : fetchedSearchNode.R2; 339 double phrasePriority = GetPriority(newPhrase, r2); 331 340 332 341 SearchNode newSearchNode = new SearchNode(phraseHash, phrasePriority, r2, newPhrase); … … 338 347 } 339 348 340 protected static double GetPriority(Symbol String phrase, double r2, int maxSentenceLength) {341 return (1 - r2) * phrase.Count ();342 } 343 344 private bool IsCompleteSentence(Symbol Stringphrase) {349 protected static double GetPriority(SymbolList phrase, double r2) { 350 return (1 - r2) * phrase.Count; 351 } 352 353 private bool IsCompleteSentence(SymbolList phrase) { 345 354 return !phrase.Any(x => x is NonterminalSymbol && x != Grammar.Expr); 346 355 } … … 386 395 Problem.ProblemData.TrainingIndices, 387 396 applyLinearScaling: true, 388 maxIterations: 10,397 maxIterations: ConstantOptimizationIterations, 389 398 updateVariableWeights: false, 390 399 updateConstantsInTree: true); … … 395 404 Results.AddOrUpdateResult(BestTrainingModelResultName, model); 396 405 Results.AddOrUpdateResult(BestTrainingSolutionResultName, bestTrainingSolution); 397 Results.AddOrUpdateResult(BestComplexityResultName, new IntValue( Grammar.GetComplexity(BestTrainingSentence)));406 Results.AddOrUpdateResult(BestComplexityResultName, new IntValue(BestTrainingSentence.Complexity)); 398 407 base.OnStopped(); 399 408 } … … 450 459 451 460 public event EventHandler<PhraseEventArgs> PhraseFetched; 452 private void OnPhraseFetched(int hash, Symbol String symbolString) {461 private void OnPhraseFetched(int hash, SymbolList phrase) { 453 462 if (PhraseFetched != null) { 454 PhraseFetched(this, new PhraseEventArgs(hash, symbolString));463 PhraseFetched(this, new PhraseEventArgs(hash, phrase)); 455 464 } 456 465 } 457 466 458 467 public event EventHandler<PhraseAddedEventArgs> PhraseDerived; 459 private void OnPhraseDerived(int parentHash, Symbol String parentSymbolString, int addedHash, SymbolString addedSymbolString, Symbol expandedSymbol, ProductionexpandedProduction) {468 private void OnPhraseDerived(int parentHash, SymbolList parentSymbolList, int addedHash, SymbolList addedSymbolList, Symbol expandedSymbol, SymbolList expandedProduction) { 460 469 if (PhraseDerived != null) { 461 PhraseDerived(this, new PhraseAddedEventArgs(parentHash, parentSymbol String, addedHash, addedSymbolString, expandedSymbol, expandedProduction));470 PhraseDerived(this, new PhraseAddedEventArgs(parentHash, parentSymbolList, addedHash, addedSymbolList, expandedSymbol, expandedProduction)); 462 471 } 463 472 } 464 473 465 474 public event EventHandler<PhraseAddedEventArgs> SentenceGenerated; 466 private void OnSentenceGenerated(int parentHash, Symbol String parentSymbolString, int addedHash, SymbolString addedSymbolString, Symbol expandedSymbol, ProductionexpandedProduction) {475 private void OnSentenceGenerated(int parentHash, SymbolList parentSymbolList, int addedHash, SymbolList addedSymbolList, Symbol expandedSymbol, SymbolList expandedProduction) { 467 476 if (SentenceGenerated != null) { 468 SentenceGenerated(this, new PhraseAddedEventArgs(parentHash, parentSymbol String, addedHash, addedSymbolString, expandedSymbol, expandedProduction));477 SentenceGenerated(this, new PhraseAddedEventArgs(parentHash, parentSymbolList, addedHash, addedSymbolList, expandedSymbol, expandedProduction)); 469 478 } 470 479 } 471 480 472 481 public event EventHandler<PhraseAddedEventArgs> DistinctSentenceGenerated; 473 private void OnDistinctSentenceGenerated(int parentHash, Symbol String parentSymbolString, int addedHash, SymbolString addedSymbolString, Symbol expandedSymbol, ProductionexpandedProduction) {482 private void OnDistinctSentenceGenerated(int parentHash, SymbolList parentSymbolList, int addedHash, SymbolList addedSymbolList, Symbol expandedSymbol, SymbolList expandedProduction) { 474 483 if (DistinctSentenceGenerated != null) { 475 DistinctSentenceGenerated(this, new PhraseAddedEventArgs(parentHash, parentSymbol String, addedHash, addedSymbolString, expandedSymbol, expandedProduction));484 DistinctSentenceGenerated(this, new PhraseAddedEventArgs(parentHash, parentSymbolList, addedHash, addedSymbolList, expandedSymbol, expandedProduction)); 476 485 } 477 486 } … … 486 495 public int Hash { get; } 487 496 488 public Symbol StringPhrase { get; }489 490 public PhraseEventArgs(int hash, Symbol Stringphrase) {497 public SymbolList Phrase { get; } 498 499 public PhraseEventArgs(int hash, SymbolList phrase) { 491 500 Hash = hash; 492 501 Phrase = phrase; … … 498 507 public int NewHash { get; } 499 508 500 public Symbol StringParentPhrase { get; }501 public Symbol StringNewPhrase { get; }509 public SymbolList ParentPhrase { get; } 510 public SymbolList NewPhrase { get; } 502 511 503 512 public Symbol ExpandedSymbol { get; } 504 513 505 public ProductionExpandedProduction { get; }506 507 public PhraseAddedEventArgs(int parentHash, Symbol String parentPhrase, int newHash, SymbolString newPhrase, Symbol expandedSymbol, ProductionexpandedProduction) {514 public SymbolList ExpandedProduction { get; } 515 516 public PhraseAddedEventArgs(int parentHash, SymbolList parentPhrase, int newHash, SymbolList newPhrase, Symbol expandedSymbol, SymbolList expandedProduction) { 508 517 ParentHash = parentHash; 509 518 ParentPhrase = parentPhrase;
Note: See TracChangeset
for help on using the changeset viewer.