[15765] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
[15712] | 3 | using System.Linq;
|
---|
| 4 | using System.Threading;
|
---|
| 5 | using HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration.GrammarEnumeration;
|
---|
[15821] | 6 | using HeuristicLab.Collections;
|
---|
[15712] | 7 | using HeuristicLab.Common;
|
---|
| 8 | using HeuristicLab.Core;
|
---|
| 9 | using HeuristicLab.Data;
|
---|
| 10 | using HeuristicLab.Optimization;
|
---|
[15722] | 11 | using HeuristicLab.Parameters;
|
---|
[15712] | 12 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 13 | using HeuristicLab.Problems.DataAnalysis;
|
---|
| 14 |
|
---|
| 15 | namespace HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration {
|
---|
| 16 | [Item("Grammar Enumeration Symbolic Regression", "Iterates all possible model structures for a fixed grammar.")]
|
---|
| 17 | [StorableClass]
|
---|
| 18 | [Creatable(CreatableAttribute.Categories.DataAnalysisRegression, Priority = 250)]
|
---|
| 19 | public class GrammarEnumerationAlgorithm : FixedDataAnalysisAlgorithm<IRegressionProblem> {
|
---|
[15746] | 20 | #region properties and result names
|
---|
[15803] | 21 | private readonly string SearchStructureSizeName = "Search Structure Size";
|
---|
| 22 | private readonly string GeneratedPhrasesName = "Generated/Archived Phrases";
|
---|
[15746] | 23 | private readonly string GeneratedSentencesName = "Generated Sentences";
|
---|
| 24 | private readonly string DistinctSentencesName = "Distinct Sentences";
|
---|
| 25 | private readonly string PhraseExpansionsName = "Phrase Expansions";
|
---|
[15860] | 26 | private readonly string AverageSentenceComplexityName = "Avg. Sentence Complexity among Distinct";
|
---|
[15821] | 27 | private readonly string OverwrittenSentencesName = "Sentences overwritten";
|
---|
| 28 | private readonly string AnalyzersParameterName = "Analyzers";
|
---|
[15824] | 29 | private readonly string ExpansionsPerSecondName = "Expansions per second";
|
---|
[15712] | 30 |
|
---|
[15746] | 31 |
|
---|
[15861] | 32 | private readonly string OptimizeConstantsParameterName = "Optimize Constants";
|
---|
[15910] | 33 | private readonly string ErrorWeightParameterName = "Error Weight";
|
---|
[15746] | 34 | private readonly string SearchDataStructureParameterName = "Search Data Structure";
|
---|
[15860] | 35 | private readonly string MaxComplexityParameterName = "Max. Complexity";
|
---|
[15722] | 36 | private readonly string GuiUpdateIntervalParameterName = "GUI Update Interval";
|
---|
[15765] | 37 |
|
---|
[15746] | 38 | public override bool SupportsPause { get { return false; } }
|
---|
[15712] | 39 |
|
---|
[15861] | 40 | protected IValueParameter<BoolValue> OptimizeConstantsParameter {
|
---|
| 41 | get { return (IValueParameter<BoolValue>)Parameters[OptimizeConstantsParameterName]; }
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | public bool OptimizeConstants {
|
---|
| 45 | get { return OptimizeConstantsParameter.Value.Value; }
|
---|
| 46 | set { OptimizeConstantsParameter.Value.Value = value; }
|
---|
| 47 | }
|
---|
| 48 |
|
---|
[15860] | 49 | protected IValueParameter<IntValue> MaxComplexityParameter {
|
---|
| 50 | get { return (IValueParameter<IntValue>)Parameters[MaxComplexityParameterName]; }
|
---|
[15712] | 51 | }
|
---|
[15860] | 52 | public int MaxComplexity {
|
---|
| 53 | get { return MaxComplexityParameter.Value.Value; }
|
---|
| 54 | set { MaxComplexityParameter.Value.Value = value; }
|
---|
[15722] | 55 | }
|
---|
[15712] | 56 |
|
---|
[15910] | 57 | protected IValueParameter<DoubleValue> ErrorWeightParameter {
|
---|
| 58 | get { return (IValueParameter<DoubleValue>)Parameters[ErrorWeightParameterName]; }
|
---|
| 59 | }
|
---|
| 60 | public double ErrorWeight {
|
---|
| 61 | get { return ErrorWeightParameter.Value.Value; }
|
---|
| 62 | set { ErrorWeightParameter.Value.Value = value; }
|
---|
| 63 | }
|
---|
| 64 |
|
---|
[15723] | 65 | protected IValueParameter<IntValue> GuiUpdateIntervalParameter {
|
---|
| 66 | get { return (IValueParameter<IntValue>)Parameters[GuiUpdateIntervalParameterName]; }
|
---|
[15722] | 67 | }
|
---|
| 68 | public int GuiUpdateInterval {
|
---|
| 69 | get { return GuiUpdateIntervalParameter.Value.Value; }
|
---|
[15723] | 70 | set { GuiUpdateIntervalParameter.Value.Value = value; }
|
---|
[15722] | 71 | }
|
---|
[15712] | 72 |
|
---|
[15746] | 73 | protected IValueParameter<EnumValue<StorageType>> SearchDataStructureParameter {
|
---|
| 74 | get { return (IValueParameter<EnumValue<StorageType>>)Parameters[SearchDataStructureParameterName]; }
|
---|
[15723] | 75 | }
|
---|
[15746] | 76 | public StorageType SearchDataStructure {
|
---|
| 77 | get { return SearchDataStructureParameter.Value.Value; }
|
---|
| 78 | set { SearchDataStructureParameter.Value.Value = value; }
|
---|
[15723] | 79 | }
|
---|
| 80 |
|
---|
[15821] | 81 | public IFixedValueParameter<ReadOnlyCheckedItemCollection<IGrammarEnumerationAnalyzer>> AnalyzersParameter {
|
---|
| 82 | get { return (IFixedValueParameter<ReadOnlyCheckedItemCollection<IGrammarEnumerationAnalyzer>>)Parameters[AnalyzersParameterName]; }
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | public ICheckedItemCollection<IGrammarEnumerationAnalyzer> Analyzers {
|
---|
| 86 | get { return AnalyzersParameter.Value; }
|
---|
| 87 | }
|
---|
| 88 |
|
---|
[15824] | 89 | public SymbolString BestTrainingSentence { get; set; } // Currently set in RSquaredEvaluator: quite hacky, but makes testing much easier for now...
|
---|
[15722] | 90 | #endregion
|
---|
[15712] | 91 |
|
---|
[15860] | 92 | public Dictionary<int, int> DistinctSentencesComplexity { get; private set; } // Semantically distinct sentences and their length in a run.
|
---|
[15812] | 93 | public HashSet<int> ArchivedPhrases { get; private set; }
|
---|
[15821] | 94 | internal SearchDataStore OpenPhrases { get; private set; } // Stack/Queue/etc. for fetching the next node in the search tree.
|
---|
[15812] | 95 |
|
---|
[15821] | 96 | #region execution stats
|
---|
| 97 | public int AllGeneratedSentencesCount { get; private set; }
|
---|
[15746] | 98 |
|
---|
[15821] | 99 | public int OverwrittenSentencesCount { get; private set; } // It is not guaranteed that shorter solutions are found first.
|
---|
| 100 | // When longer solutions are overwritten with shorter ones,
|
---|
| 101 | // this counter is increased.
|
---|
| 102 | public int PhraseExpansionCount { get; private set; } // Number, how many times a nonterminal symbol is replaced with a production rule.
|
---|
| 103 | #endregion
|
---|
| 104 |
|
---|
[15800] | 105 | public Grammar Grammar { get; private set; }
|
---|
[15712] | 106 |
|
---|
[15765] | 107 |
|
---|
[15722] | 108 | #region ctors
|
---|
| 109 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 110 | return new GrammarEnumerationAlgorithm(this, cloner);
|
---|
| 111 | }
|
---|
[15712] | 112 |
|
---|
[15722] | 113 | public GrammarEnumerationAlgorithm() {
|
---|
[15723] | 114 | Problem = new RegressionProblem() {
|
---|
[15910] | 115 | ProblemData = new HeuristicLab.Problems.Instances.DataAnalysis.PolyTen(seed: 1234).GenerateRegressionData()
|
---|
[15723] | 116 | };
|
---|
| 117 |
|
---|
[15910] | 118 | Parameters.Add(new ValueParameter<BoolValue>(OptimizeConstantsParameterName, "Run constant optimization in sentence evaluation.", new BoolValue(false)));
|
---|
| 119 | Parameters.Add(new ValueParameter<DoubleValue>(ErrorWeightParameterName, "Defines, how much weight is put on a phrase's r² value when priorizing phrases during search.", new DoubleValue(0.8)));
|
---|
| 120 | Parameters.Add(new ValueParameter<IntValue>(MaxComplexityParameterName, "The maximum number of variable symbols in a sentence.", new IntValue(12)));
|
---|
| 121 | Parameters.Add(new ValueParameter<IntValue>(GuiUpdateIntervalParameterName, "Number of generated sentences, until GUI is refreshed.", new IntValue(5000)));
|
---|
| 122 | Parameters.Add(new ValueParameter<EnumValue<StorageType>>(SearchDataStructureParameterName, new EnumValue<StorageType>(StorageType.PriorityQueue)));
|
---|
[15821] | 123 |
|
---|
| 124 | var availableAnalyzers = new IGrammarEnumerationAnalyzer[] {
|
---|
| 125 | new SearchGraphVisualizer(),
|
---|
[15824] | 126 | new SentenceLogger(),
|
---|
| 127 | new RSquaredEvaluator()
|
---|
[15821] | 128 | };
|
---|
| 129 | Parameters.Add(new FixedValueParameter<ReadOnlyCheckedItemCollection<IGrammarEnumerationAnalyzer>>(
|
---|
| 130 | AnalyzersParameterName,
|
---|
| 131 | new CheckedItemCollection<IGrammarEnumerationAnalyzer>(availableAnalyzers).AsReadOnly()));
|
---|
| 132 |
|
---|
| 133 | foreach (var analyzer in Analyzers) {
|
---|
| 134 | Analyzers.SetItemCheckedState(analyzer, false);
|
---|
| 135 | }
|
---|
[15824] | 136 | Analyzers.SetItemCheckedState(Analyzers.First(analyzer => analyzer is RSquaredEvaluator), true);
|
---|
[15910] | 137 | //Analyzers.SetItemCheckedState(Analyzers.First(analyzer => analyzer is SentenceLogger), true);
|
---|
[15722] | 138 | }
|
---|
[15712] | 139 |
|
---|
[15910] | 140 | public GrammarEnumerationAlgorithm(GrammarEnumerationAlgorithm original, Cloner cloner) : base(original, cloner) {
|
---|
| 141 |
|
---|
| 142 |
|
---|
| 143 | }
|
---|
[15722] | 144 | #endregion
|
---|
[15712] | 145 |
|
---|
[15722] | 146 | protected override void Run(CancellationToken cancellationToken) {
|
---|
[15746] | 147 | #region init
|
---|
| 148 | InitResults();
|
---|
[15723] | 149 |
|
---|
[15910] | 150 | foreach (IGrammarEnumerationAnalyzer grammarEnumerationAnalyzer in Analyzers) {
|
---|
| 151 | if (Analyzers.ItemChecked(grammarEnumerationAnalyzer)) {
|
---|
| 152 | grammarEnumerationAnalyzer.Register(this);
|
---|
| 153 | } else {
|
---|
| 154 | grammarEnumerationAnalyzer.Deregister(this);
|
---|
| 155 | }
|
---|
| 156 | }
|
---|
| 157 |
|
---|
[15861] | 158 | Analyzers.OfType<RSquaredEvaluator>().First().OptimizeConstants = OptimizeConstants;
|
---|
| 159 |
|
---|
[15812] | 160 | ArchivedPhrases = new HashSet<int>();
|
---|
| 161 |
|
---|
[15860] | 162 | DistinctSentencesComplexity = new Dictionary<int, int>();
|
---|
[15821] | 163 | AllGeneratedSentencesCount = 0;
|
---|
| 164 | OverwrittenSentencesCount = 0;
|
---|
| 165 | PhraseExpansionCount = 0;
|
---|
[15746] | 166 |
|
---|
[15724] | 167 | Grammar = new Grammar(Problem.ProblemData.AllowedInputVariables.ToArray());
|
---|
[15712] | 168 |
|
---|
[15746] | 169 | OpenPhrases = new SearchDataStore(SearchDataStructure); // Select search strategy
|
---|
[15734] | 170 | var phrase0 = new SymbolString(new[] { Grammar.StartSymbol });
|
---|
[15832] | 171 | var phrase0Hash = Grammar.Hasher.CalcHashCode(phrase0);
|
---|
[15746] | 172 | #endregion
|
---|
[15712] | 173 |
|
---|
[15910] | 174 | int maxSentenceLength = GetMaxSentenceLength();
|
---|
| 175 |
|
---|
[15883] | 176 | OpenPhrases.Store(phrase0Hash, 0.0, phrase0);
|
---|
[15821] | 177 | while (OpenPhrases.Count > 0) {
|
---|
| 178 | if (cancellationToken.IsCancellationRequested) break;
|
---|
[15746] | 179 |
|
---|
[15821] | 180 | StoredSymbolString fetchedPhrase = OpenPhrases.GetNext();
|
---|
| 181 | SymbolString currPhrase = fetchedPhrase.SymbolString;
|
---|
[15722] | 182 |
|
---|
[15821] | 183 | OnPhraseFetched(fetchedPhrase.Hash, currPhrase);
|
---|
[15765] | 184 |
|
---|
[15821] | 185 | ArchivedPhrases.Add(fetchedPhrase.Hash);
|
---|
[15726] | 186 |
|
---|
[15821] | 187 | // expand next nonterminal symbols
|
---|
[15827] | 188 | int nonterminalSymbolIndex = currPhrase.NextNonterminalIndex();
|
---|
| 189 | NonterminalSymbol expandedSymbol = (NonterminalSymbol)currPhrase[nonterminalSymbolIndex];
|
---|
[15834] | 190 | var appliedProductions = Grammar.Productions[expandedSymbol];
|
---|
[15734] | 191 |
|
---|
[15827] | 192 | for (int i = 0; i < appliedProductions.Count; i++) {
|
---|
[15821] | 193 | PhraseExpansionCount++;
|
---|
[15734] | 194 |
|
---|
[15827] | 195 | SymbolString newPhrase = currPhrase.DerivePhrase(nonterminalSymbolIndex, appliedProductions[i]);
|
---|
[15860] | 196 | int newPhraseComplexity = Grammar.GetComplexity(newPhrase);
|
---|
[15712] | 197 |
|
---|
[15860] | 198 | if (newPhraseComplexity <= MaxComplexity) {
|
---|
[15832] | 199 | var phraseHash = Grammar.Hasher.CalcHashCode(newPhrase);
|
---|
[15765] | 200 |
|
---|
[15827] | 201 | OnPhraseDerived(fetchedPhrase.Hash, fetchedPhrase.SymbolString, phraseHash, newPhrase, expandedSymbol, appliedProductions[i]);
|
---|
[15800] | 202 |
|
---|
[15821] | 203 | if (newPhrase.IsSentence()) {
|
---|
| 204 | AllGeneratedSentencesCount++;
|
---|
| 205 |
|
---|
[15827] | 206 | OnSentenceGenerated(fetchedPhrase.Hash, fetchedPhrase.SymbolString, phraseHash, newPhrase, expandedSymbol, appliedProductions[i]);
|
---|
[15821] | 207 |
|
---|
[15883] | 208 | // Is the best solution found? (only if RSquaredEvaluator is activated)
|
---|
[15907] | 209 | if (Results.ContainsKey(RSquaredEvaluator.BestTrainingQualityResultName)) {
|
---|
| 210 | double r2 = ((DoubleValue)Results[RSquaredEvaluator.BestTrainingQualityResultName].Value).Value;
|
---|
| 211 | if (r2.IsAlmost(1.0)) {
|
---|
| 212 | UpdateView(force: true);
|
---|
| 213 | return;
|
---|
| 214 | }
|
---|
[15883] | 215 | }
|
---|
| 216 |
|
---|
[15860] | 217 | if (!DistinctSentencesComplexity.ContainsKey(phraseHash) || DistinctSentencesComplexity[phraseHash] > newPhraseComplexity) {
|
---|
| 218 | if (DistinctSentencesComplexity.ContainsKey(phraseHash)) OverwrittenSentencesCount++; // for analysis only
|
---|
[15821] | 219 |
|
---|
[15860] | 220 | DistinctSentencesComplexity[phraseHash] = newPhraseComplexity;
|
---|
[15827] | 221 | OnDistinctSentenceGenerated(fetchedPhrase.Hash, fetchedPhrase.SymbolString, phraseHash, newPhrase, expandedSymbol, appliedProductions[i]);
|
---|
[15746] | 222 | }
|
---|
[15821] | 223 | UpdateView();
|
---|
| 224 |
|
---|
| 225 | } else if (!OpenPhrases.Contains(phraseHash) && !ArchivedPhrases.Contains(phraseHash)) {
|
---|
[15910] | 226 | double phrasePriority = GetPriority(newPhrase, maxSentenceLength);
|
---|
[15883] | 227 | OpenPhrases.Store(phraseHash, phrasePriority, newPhrase);
|
---|
[15712] | 228 | }
|
---|
| 229 | }
|
---|
| 230 | }
|
---|
| 231 | }
|
---|
[15812] | 232 | UpdateView(force: true);
|
---|
[15746] | 233 | }
|
---|
[15723] | 234 |
|
---|
[15910] | 235 | protected double GetPriority(SymbolString phrase, int maxSentenceLength) {
|
---|
| 236 | double relLength = (double)phrase.Count() / maxSentenceLength;
|
---|
[15907] | 237 |
|
---|
| 238 | double r2 = Grammar.EvaluatePhrase(phrase, Problem.ProblemData, OptimizeConstants);
|
---|
| 239 | double error = 1.0 - r2;
|
---|
| 240 |
|
---|
[15910] | 241 | return relLength + ErrorWeight * error;
|
---|
| 242 | }
|
---|
| 243 |
|
---|
| 244 | private int GetMaxSentenceLength() {
|
---|
| 245 | SymbolString s = new SymbolString(Grammar.StartSymbol);
|
---|
| 246 |
|
---|
| 247 | while (Grammar.GetComplexity(s) <= MaxComplexity) {
|
---|
| 248 | int expandedSymbolIndex = s.NextNonterminalIndex();
|
---|
| 249 | NonterminalSymbol expandedSymbol = (NonterminalSymbol)s[expandedSymbolIndex];
|
---|
| 250 |
|
---|
| 251 | var productions = Grammar.Productions[expandedSymbol];
|
---|
| 252 | var longestProduction = productions // Find production with most terminal symbols to expand as much as possible...
|
---|
| 253 | .OrderBy(CountTerminals) // but with lowest complexity/nonterminal count to keep complexity low.
|
---|
| 254 | .ThenByDescending(CountNonTerminals)
|
---|
| 255 | .First();
|
---|
| 256 |
|
---|
| 257 | s = s.DerivePhrase(expandedSymbolIndex, longestProduction);
|
---|
[15907] | 258 | }
|
---|
| 259 |
|
---|
[15910] | 260 | return s.Count();
|
---|
| 261 | }
|
---|
[15907] | 262 |
|
---|
[15910] | 263 | private int CountTerminals(Production p) {
|
---|
| 264 | return p.Count(s => s is TerminalSymbol);
|
---|
[15883] | 265 | }
|
---|
| 266 |
|
---|
[15910] | 267 | private int CountNonTerminals(Production p) {
|
---|
| 268 | return p.Count(s => s is NonterminalSymbol);
|
---|
| 269 | }
|
---|
| 270 |
|
---|
[15821] | 271 | #region Visualization in HL
|
---|
[15746] | 272 | // Initialize entries in result set.
|
---|
| 273 | private void InitResults() {
|
---|
[15803] | 274 | Results.Add(new Result(GeneratedPhrasesName, new IntValue(0)));
|
---|
| 275 | Results.Add(new Result(SearchStructureSizeName, new IntValue(0)));
|
---|
[15746] | 276 | Results.Add(new Result(GeneratedSentencesName, new IntValue(0)));
|
---|
| 277 | Results.Add(new Result(DistinctSentencesName, new IntValue(0)));
|
---|
| 278 | Results.Add(new Result(PhraseExpansionsName, new IntValue(0)));
|
---|
[15821] | 279 | Results.Add(new Result(OverwrittenSentencesName, new IntValue(0)));
|
---|
[15860] | 280 | Results.Add(new Result(AverageSentenceComplexityName, new DoubleValue(1.0)));
|
---|
[15824] | 281 | Results.Add(new Result(ExpansionsPerSecondName, "In Thousand expansions per second", new IntValue(0)));
|
---|
[15712] | 282 | }
|
---|
[15746] | 283 |
|
---|
| 284 | // Update the view for intermediate results in an algorithm run.
|
---|
| 285 | private int updates;
|
---|
[15812] | 286 | private void UpdateView(bool force = false) {
|
---|
[15746] | 287 | updates++;
|
---|
| 288 |
|
---|
[15812] | 289 | if (force || updates % GuiUpdateInterval == 1) {
|
---|
[15803] | 290 | ((IntValue)Results[GeneratedPhrasesName].Value).Value = ArchivedPhrases.Count;
|
---|
| 291 | ((IntValue)Results[SearchStructureSizeName].Value).Value = OpenPhrases.Count;
|
---|
[15821] | 292 | ((IntValue)Results[GeneratedSentencesName].Value).Value = AllGeneratedSentencesCount;
|
---|
[15860] | 293 | ((IntValue)Results[DistinctSentencesName].Value).Value = DistinctSentencesComplexity.Count;
|
---|
[15821] | 294 | ((IntValue)Results[PhraseExpansionsName].Value).Value = PhraseExpansionCount;
|
---|
[15860] | 295 | ((DoubleValue)Results[AverageSentenceComplexityName].Value).Value = DistinctSentencesComplexity.Select(pair => pair.Value).Average();
|
---|
[15821] | 296 | ((IntValue)Results[OverwrittenSentencesName].Value).Value = OverwrittenSentencesCount;
|
---|
[15824] | 297 | ((IntValue)Results[ExpansionsPerSecondName].Value).Value = (int)((PhraseExpansionCount /
|
---|
| 298 | ExecutionTime.TotalSeconds) / 1000.0);
|
---|
[15746] | 299 | }
|
---|
| 300 | }
|
---|
[15821] | 301 | #endregion
|
---|
[15746] | 302 |
|
---|
[15821] | 303 | #region events
|
---|
| 304 | public event EventHandler<PhraseEventArgs> PhraseFetched;
|
---|
| 305 | private void OnPhraseFetched(int hash, SymbolString symbolString) {
|
---|
| 306 | if (PhraseFetched != null) {
|
---|
| 307 | PhraseFetched(this, new PhraseEventArgs(hash, symbolString));
|
---|
[15746] | 308 | }
|
---|
| 309 | }
|
---|
[15812] | 310 |
|
---|
[15821] | 311 | public event EventHandler<PhraseAddedEventArgs> PhraseDerived;
|
---|
| 312 | private void OnPhraseDerived(int parentHash, SymbolString parentSymbolString, int addedHash, SymbolString addedSymbolString, Symbol expandedSymbol, Production expandedProduction) {
|
---|
| 313 | if (PhraseDerived != null) {
|
---|
| 314 | PhraseDerived(this, new PhraseAddedEventArgs(parentHash, parentSymbolString, addedHash, addedSymbolString, expandedSymbol, expandedProduction));
|
---|
| 315 | }
|
---|
[15803] | 316 | }
|
---|
[15765] | 317 |
|
---|
[15821] | 318 | public event EventHandler<PhraseAddedEventArgs> SentenceGenerated;
|
---|
| 319 | private void OnSentenceGenerated(int parentHash, SymbolString parentSymbolString, int addedHash, SymbolString addedSymbolString, Symbol expandedSymbol, Production expandedProduction) {
|
---|
| 320 | if (SentenceGenerated != null) {
|
---|
| 321 | SentenceGenerated(this, new PhraseAddedEventArgs(parentHash, parentSymbolString, addedHash, addedSymbolString, expandedSymbol, expandedProduction));
|
---|
| 322 | }
|
---|
| 323 | }
|
---|
| 324 |
|
---|
| 325 | public event EventHandler<PhraseAddedEventArgs> DistinctSentenceGenerated;
|
---|
| 326 | private void OnDistinctSentenceGenerated(int parentHash, SymbolString parentSymbolString, int addedHash, SymbolString addedSymbolString, Symbol expandedSymbol, Production expandedProduction) {
|
---|
| 327 | if (DistinctSentenceGenerated != null) {
|
---|
| 328 | DistinctSentenceGenerated(this, new PhraseAddedEventArgs(parentHash, parentSymbolString, addedHash, addedSymbolString, expandedSymbol, expandedProduction));
|
---|
| 329 | }
|
---|
| 330 | }
|
---|
| 331 |
|
---|
| 332 | #endregion
|
---|
| 333 |
|
---|
[15712] | 334 | }
|
---|
[15821] | 335 |
|
---|
| 336 | #region events for analysis
|
---|
| 337 |
|
---|
| 338 | public class PhraseEventArgs : EventArgs {
|
---|
| 339 | public int Hash { get; }
|
---|
| 340 |
|
---|
| 341 | public SymbolString Phrase { get; }
|
---|
| 342 |
|
---|
| 343 | public PhraseEventArgs(int hash, SymbolString phrase) {
|
---|
| 344 | Hash = hash;
|
---|
| 345 | Phrase = phrase;
|
---|
| 346 | }
|
---|
| 347 | }
|
---|
| 348 |
|
---|
| 349 | public class PhraseAddedEventArgs : EventArgs {
|
---|
| 350 | public int ParentHash { get; }
|
---|
| 351 | public int NewHash { get; }
|
---|
| 352 |
|
---|
| 353 | public SymbolString ParentPhrase { get; }
|
---|
| 354 | public SymbolString NewPhrase { get; }
|
---|
| 355 |
|
---|
| 356 | public Symbol ExpandedSymbol { get; }
|
---|
| 357 |
|
---|
| 358 | public Production ExpandedProduction { get; }
|
---|
| 359 |
|
---|
| 360 | public PhraseAddedEventArgs(int parentHash, SymbolString parentPhrase, int newHash, SymbolString newPhrase, Symbol expandedSymbol, Production expandedProduction) {
|
---|
| 361 | ParentHash = parentHash;
|
---|
| 362 | ParentPhrase = parentPhrase;
|
---|
| 363 | NewHash = newHash;
|
---|
| 364 | NewPhrase = newPhrase;
|
---|
| 365 | ExpandedSymbol = expandedSymbol;
|
---|
| 366 | ExpandedProduction = expandedProduction;
|
---|
| 367 | }
|
---|
| 368 | }
|
---|
| 369 |
|
---|
| 370 | #endregion
|
---|
[15712] | 371 | } |
---|