Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/15/18 14:53:46 (6 years ago)
Author:
bburlacu
Message:

#2886: Fix serialization and cloning and plugin properties.

File:
1 edited

Legend:

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

    r15957 r15960  
    3737    private readonly string GrammarSymbolsParameterName = "Grammar Symbols";
    3838
    39     public override bool SupportsPause { get { return false; } }
     39    public override bool SupportsPause { get { return true; } }
    4040
    4141    protected IValueParameter<DoubleValue> VariableImportanceWeightParameter {
     
    107107    #endregion
    108108
     109    [Storable]
    109110    public Dictionary<int, int> DistinctSentencesComplexity { get; private set; }  // Semantically distinct sentences and their length in a run.
     111
     112    [Storable]
    110113    public HashSet<int> ArchivedPhrases { get; private set; }
     114
     115    [Storable]
    111116    internal SearchDataStore OpenPhrases { get; private set; }           // Stack/Queue/etc. for fetching the next node in the search tree. 
    112117
     
    121126
    122127    public Grammar Grammar { get; private set; }
    123 
    124128
    125129    #region ctors
     
    174178        analyzer.Register(this);
    175179      Analyzers.CheckedItemsChanged += Analyzers_CheckedItemsChanged;
     180
     181      DistinctSentencesComplexity = new Dictionary<int, int>(original.DistinctSentencesComplexity);
     182      ArchivedPhrases = new HashSet<int>(original.ArchivedPhrases);
     183      OpenPhrases = cloner.Clone(original.OpenPhrases);
     184
     185      AllGeneratedSentencesCount = original.AllGeneratedSentencesCount;
     186      OverwrittenSentencesCount = original.OverwrittenSentencesCount;
     187      PhraseExpansionCount = original.PhraseExpansionCount;
     188      Grammar = cloner.Clone(original.Grammar);
     189
     190      if (original.variableImportance != null)
     191        variableImportance = new Dictionary<VariableTerminalSymbol, double>(original.variableImportance);
    176192    }
    177193    #endregion
     
    193209    }
    194210
    195     protected override void Run(CancellationToken cancellationToken) {
    196       InitResults();
    197       var phrase0 = new SymbolString(new[] { Grammar.StartSymbol });
    198       var phrase0Hash = Grammar.Hasher.CalcHashCode(phrase0);
    199 
    200       #region Variable Importance
     211    private Dictionary<VariableTerminalSymbol, double> CalculateVariableImportances() {
    201212      variableImportance = new Dictionary<VariableTerminalSymbol, double>();
    202213
     
    216227        variableImportance[varSym] = rfImpact.Item2 / sum;
    217228      }
    218       #endregion
     229      return variableImportance;
     230    }
     231
     232    protected override void Run(CancellationToken cancellationToken) {
     233      // do not reinitialize the algorithm if we're resuming from pause
     234      if (previousExecutionState != ExecutionState.Paused) {
     235        CalculateVariableImportances();
     236        InitResults();
     237        var phrase0 = new SymbolString(new[] { Grammar.StartSymbol });
     238        var phrase0Hash = Grammar.Hasher.CalcHashCode(phrase0);
     239        OpenPhrases.Store(new SearchNode(phrase0Hash, 0.0, 0.0, phrase0));
     240      }
    219241
    220242      int maxSentenceLength = GetMaxSentenceLength();
    221 
    222       OpenPhrases.Store(new SearchNode(phrase0Hash, 0.0, 0.0, phrase0));
    223 
    224243      var errorWeight = ErrorWeight;
    225244      var variableImportanceWeight = VariableImportanceWeight;
    226 
     245      // main search loop
    227246      while (OpenPhrases.Count > 0) {
    228         if (cancellationToken.IsCancellationRequested) break;
     247        if (cancellationToken.IsCancellationRequested)
     248          break;
    229249
    230250        SearchNode fetchedSearchNode = OpenPhrases.GetNext();
     
    343363      return p.Count(s => s is NonterminalSymbol);
    344364    }
     365
     366    #region pause support
     367    private ExecutionState previousExecutionState;
     368    protected override void OnPaused() {
     369      previousExecutionState = this.ExecutionState;
     370      base.OnPaused();
     371    }
     372    protected override void OnPrepared() {
     373      previousExecutionState = this.ExecutionState;
     374      base.OnPrepared();
     375    }
     376    protected override void OnStarted() {
     377      previousExecutionState = this.ExecutionState;
     378      base.OnStarted();
     379    }
     380    protected override void OnStopped() {
     381      previousExecutionState = this.ExecutionState;
     382      base.OnStopped();
     383    }
     384    #endregion
    345385
    346386    #region Visualization in HL
Note: See TracChangeset for help on using the changeset viewer.