Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/27/18 19:28:18 (6 years ago)
Author:
bburlacu
Message:

#2886:

  • replace functionally-overlapping classes Production and SymbolString with a single class SymbolList
  • refactor methods from Grammar class as methods and properties of SymbolList
  • add parameter for the number of constant optimization iterations
  • refactor code
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/GrammarEnumerationAlgorithm.cs

    r16022 r16026  
    3232
    3333    private readonly string OptimizeConstantsParameterName = "Optimize Constants";
     34    private readonly string ConstantOptimizationIterationsParameterName = "Constant Optimization Iterations";
    3435    private readonly string ErrorWeightParameterName = "Error Weight";
    3536    private readonly string SearchDataStructureParameterName = "Search Data Structure";
     
    5556    }
    5657
     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
    5767    protected IFixedValueParameter<IntValue> MaxComplexityParameter {
    5868      get { return (IFixedValueParameter<IntValue>)Parameters[MaxComplexityParameterName]; }
     
    116126
    117127    [Storable]
    118     public SymbolString BestTrainingSentence { 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...
    119129    #endregion
    120130
     
    185195      Parameters.Add(new FixedValueParameter<IntValue>(SearchDataStructureSizeParameterName, "The size of the search data structure.", new IntValue((int)1e5)));
    186196      Parameters.Add(new FixedValueParameter<EnumValue<StorageType>>(SearchDataStructureParameterName, new EnumValue<StorageType>(StorageType.SortedSet)));
     197      Parameters.Add(new FixedValueParameter<IntValue>(ConstantOptimizationIterationsParameterName, new IntValue(10)));
    187198
    188199      SearchDataStructureParameter.Value.ValueChanged += (o, e) => Prepare();
     
    240251    #endregion
    241252
    242     [Storable]
    243     private Dictionary<VariableTerminalSymbol, double> variableImportance;
    244 
    245253    public override void Prepare() {
    246254      DistinctSentencesComplexity = new Dictionary<int, int>();
     
    259267      if (previousExecutionState != ExecutionState.Paused) {
    260268        InitResults();
    261         var phrase0 = new SymbolString(new[] { Grammar.StartSymbol });
     269        var phrase0 = new SymbolList(new[] { Grammar.StartSymbol });
    262270        var phrase0Hash = Grammar.Hasher.CalcHashCode(phrase0);
    263271
     
    268276      var errorWeight = ErrorWeight;
    269277      var optimizeConstants = OptimizeConstants; // cache value to avoid parameter lookup
     278      var iterations = ConstantOptimizationIterations;
    270279      // main search loop
    271280      while (OpenPhrases.Count > 0) {
     
    278287          continue;
    279288
    280         SymbolString currPhrase = fetchedSearchNode.SymbolString;
     289        SymbolList currPhrase = fetchedSearchNode.SymbolList;
    281290
    282291        OnPhraseFetched(fetchedSearchNode.Hash, currPhrase);
     
    292301          PhraseExpansionCount++;
    293302
    294           SymbolString newPhrase = currPhrase.DerivePhrase(nonterminalSymbolIndex, appliedProductions[i]);
    295           int newPhraseComplexity = Grammar.GetComplexity(newPhrase);
     303          SymbolList newPhrase = currPhrase.DerivePhrase(nonterminalSymbolIndex, appliedProductions[i]);
     304          int newPhraseComplexity = newPhrase.Complexity;
    296305
    297306          if (newPhraseComplexity > MaxComplexity)
     
    300309          var phraseHash = Grammar.Hasher.CalcHashCode(newPhrase);
    301310
    302           OnPhraseDerived(fetchedSearchNode.Hash, fetchedSearchNode.SymbolString, phraseHash, newPhrase, expandedSymbol, appliedProductions[i]);
     311          OnPhraseDerived(fetchedSearchNode.Hash, fetchedSearchNode.SymbolList, phraseHash, newPhrase, expandedSymbol, appliedProductions[i]);
    303312
    304313          if (newPhrase.IsSentence()) {
    305314            AllGeneratedSentencesCount++;
    306315
    307             OnSentenceGenerated(fetchedSearchNode.Hash, fetchedSearchNode.SymbolString, phraseHash, newPhrase, expandedSymbol, appliedProductions[i]);
     316            OnSentenceGenerated(fetchedSearchNode.Hash, fetchedSearchNode.SymbolList, phraseHash, newPhrase, expandedSymbol, appliedProductions[i]);
    308317
    309318            // Is the best solution found? (only if RSquaredEvaluator is activated)
     
    320329
    321330              DistinctSentencesComplexity[phraseHash] = newPhraseComplexity;
    322               OnDistinctSentenceGenerated(fetchedSearchNode.Hash, fetchedSearchNode.SymbolString, phraseHash, newPhrase, expandedSymbol, appliedProductions[i]);
     331              OnDistinctSentenceGenerated(fetchedSearchNode.Hash, fetchedSearchNode.SymbolList, phraseHash, newPhrase, expandedSymbol, appliedProductions[i]);
    323332            }
    324333            UpdateView();
     
    327336
    328337            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);
    331340
    332341            SearchNode newSearchNode = new SearchNode(phraseHash, phrasePriority, r2, newPhrase);
     
    338347    }
    339348
    340     protected static double GetPriority(SymbolString phrase, double r2, int maxSentenceLength) {
    341       return (1 - r2) * phrase.Count();
    342     }
    343 
    344     private bool IsCompleteSentence(SymbolString phrase) {
     349    protected static double GetPriority(SymbolList phrase, double r2) {
     350      return (1 - r2) * phrase.Count;
     351    }
     352
     353    private bool IsCompleteSentence(SymbolList phrase) {
    345354      return !phrase.Any(x => x is NonterminalSymbol && x != Grammar.Expr);
    346355    }
     
    386395        Problem.ProblemData.TrainingIndices,
    387396        applyLinearScaling: true,
    388         maxIterations: 10,
     397        maxIterations: ConstantOptimizationIterations,
    389398        updateVariableWeights: false,
    390399        updateConstantsInTree: true);
     
    395404      Results.AddOrUpdateResult(BestTrainingModelResultName, model);
    396405      Results.AddOrUpdateResult(BestTrainingSolutionResultName, bestTrainingSolution);
    397       Results.AddOrUpdateResult(BestComplexityResultName, new IntValue(Grammar.GetComplexity(BestTrainingSentence)));
     406      Results.AddOrUpdateResult(BestComplexityResultName, new IntValue(BestTrainingSentence.Complexity));
    398407      base.OnStopped();
    399408    }
     
    450459
    451460    public event EventHandler<PhraseEventArgs> PhraseFetched;
    452     private void OnPhraseFetched(int hash, SymbolString symbolString) {
     461    private void OnPhraseFetched(int hash, SymbolList phrase) {
    453462      if (PhraseFetched != null) {
    454         PhraseFetched(this, new PhraseEventArgs(hash, symbolString));
     463        PhraseFetched(this, new PhraseEventArgs(hash, phrase));
    455464      }
    456465    }
    457466
    458467    public event EventHandler<PhraseAddedEventArgs> PhraseDerived;
    459     private void OnPhraseDerived(int parentHash, SymbolString parentSymbolString, int addedHash, SymbolString addedSymbolString, Symbol expandedSymbol, Production expandedProduction) {
     468    private void OnPhraseDerived(int parentHash, SymbolList parentSymbolList, int addedHash, SymbolList addedSymbolList, Symbol expandedSymbol, SymbolList expandedProduction) {
    460469      if (PhraseDerived != null) {
    461         PhraseDerived(this, new PhraseAddedEventArgs(parentHash, parentSymbolString, addedHash, addedSymbolString, expandedSymbol, expandedProduction));
     470        PhraseDerived(this, new PhraseAddedEventArgs(parentHash, parentSymbolList, addedHash, addedSymbolList, expandedSymbol, expandedProduction));
    462471      }
    463472    }
    464473
    465474    public event EventHandler<PhraseAddedEventArgs> SentenceGenerated;
    466     private void OnSentenceGenerated(int parentHash, SymbolString parentSymbolString, int addedHash, SymbolString addedSymbolString, Symbol expandedSymbol, Production expandedProduction) {
     475    private void OnSentenceGenerated(int parentHash, SymbolList parentSymbolList, int addedHash, SymbolList addedSymbolList, Symbol expandedSymbol, SymbolList expandedProduction) {
    467476      if (SentenceGenerated != null) {
    468         SentenceGenerated(this, new PhraseAddedEventArgs(parentHash, parentSymbolString, addedHash, addedSymbolString, expandedSymbol, expandedProduction));
     477        SentenceGenerated(this, new PhraseAddedEventArgs(parentHash, parentSymbolList, addedHash, addedSymbolList, expandedSymbol, expandedProduction));
    469478      }
    470479    }
    471480
    472481    public event EventHandler<PhraseAddedEventArgs> DistinctSentenceGenerated;
    473     private void OnDistinctSentenceGenerated(int parentHash, SymbolString parentSymbolString, int addedHash, SymbolString addedSymbolString, Symbol expandedSymbol, Production expandedProduction) {
     482    private void OnDistinctSentenceGenerated(int parentHash, SymbolList parentSymbolList, int addedHash, SymbolList addedSymbolList, Symbol expandedSymbol, SymbolList expandedProduction) {
    474483      if (DistinctSentenceGenerated != null) {
    475         DistinctSentenceGenerated(this, new PhraseAddedEventArgs(parentHash, parentSymbolString, addedHash, addedSymbolString, expandedSymbol, expandedProduction));
     484        DistinctSentenceGenerated(this, new PhraseAddedEventArgs(parentHash, parentSymbolList, addedHash, addedSymbolList, expandedSymbol, expandedProduction));
    476485      }
    477486    }
     
    486495    public int Hash { get; }
    487496
    488     public SymbolString Phrase { get; }
    489 
    490     public PhraseEventArgs(int hash, SymbolString phrase) {
     497    public SymbolList Phrase { get; }
     498
     499    public PhraseEventArgs(int hash, SymbolList phrase) {
    491500      Hash = hash;
    492501      Phrase = phrase;
     
    498507    public int NewHash { get; }
    499508
    500     public SymbolString ParentPhrase { get; }
    501     public SymbolString NewPhrase { get; }
     509    public SymbolList ParentPhrase { get; }
     510    public SymbolList NewPhrase { get; }
    502511
    503512    public Symbol ExpandedSymbol { get; }
    504513
    505     public Production ExpandedProduction { get; }
    506 
    507     public PhraseAddedEventArgs(int parentHash, SymbolString parentPhrase, int newHash, SymbolString newPhrase, Symbol expandedSymbol, Production expandedProduction) {
     514    public SymbolList ExpandedProduction { get; }
     515
     516    public PhraseAddedEventArgs(int parentHash, SymbolList parentPhrase, int newHash, SymbolList newPhrase, Symbol expandedSymbol, SymbolList expandedProduction) {
    508517      ParentHash = parentHash;
    509518      ParentPhrase = parentPhrase;
Note: See TracChangeset for help on using the changeset viewer.