[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;
|
---|
[15957] | 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;
|
---|
[15974] | 14 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
| 15 | using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
|
---|
[15712] | 16 |
|
---|
| 17 | namespace HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration {
|
---|
| 18 | [Item("Grammar Enumeration Symbolic Regression", "Iterates all possible model structures for a fixed grammar.")]
|
---|
| 19 | [StorableClass]
|
---|
| 20 | [Creatable(CreatableAttribute.Categories.DataAnalysisRegression, Priority = 250)]
|
---|
| 21 | public class GrammarEnumerationAlgorithm : FixedDataAnalysisAlgorithm<IRegressionProblem> {
|
---|
[15746] | 22 | #region properties and result names
|
---|
[15950] | 23 | private readonly string VariableImportanceWeightName = "Variable Importance Weight";
|
---|
[15803] | 24 | private readonly string SearchStructureSizeName = "Search Structure Size";
|
---|
| 25 | private readonly string GeneratedPhrasesName = "Generated/Archived Phrases";
|
---|
[15746] | 26 | private readonly string GeneratedSentencesName = "Generated Sentences";
|
---|
| 27 | private readonly string DistinctSentencesName = "Distinct Sentences";
|
---|
| 28 | private readonly string PhraseExpansionsName = "Phrase Expansions";
|
---|
[15860] | 29 | private readonly string AverageSentenceComplexityName = "Avg. Sentence Complexity among Distinct";
|
---|
[15821] | 30 | private readonly string OverwrittenSentencesName = "Sentences overwritten";
|
---|
| 31 | private readonly string AnalyzersParameterName = "Analyzers";
|
---|
[15824] | 32 | private readonly string ExpansionsPerSecondName = "Expansions per second";
|
---|
[15712] | 33 |
|
---|
[15861] | 34 | private readonly string OptimizeConstantsParameterName = "Optimize Constants";
|
---|
[15910] | 35 | private readonly string ErrorWeightParameterName = "Error Weight";
|
---|
[15746] | 36 | private readonly string SearchDataStructureParameterName = "Search Data Structure";
|
---|
[15860] | 37 | private readonly string MaxComplexityParameterName = "Max. Complexity";
|
---|
[15722] | 38 | private readonly string GuiUpdateIntervalParameterName = "GUI Update Interval";
|
---|
[15930] | 39 | private readonly string GrammarSymbolsParameterName = "Grammar Symbols";
|
---|
[15974] | 40 | private readonly string SearchCacheSizeParameterName = "Search Cache Size";
|
---|
| 41 | private readonly string SearchDataStructureSizeParameterName = "Search Data Structure Size";
|
---|
[15765] | 42 |
|
---|
[15974] | 43 | // result names
|
---|
| 44 | public static readonly string BestTrainingModelResultName = "Best model (Training)";
|
---|
| 45 | public static readonly string BestTrainingSolutionResultName = "Best solution (Training)";
|
---|
| 46 | public static readonly string BestComplexityResultName = "Best solution complexity";
|
---|
| 47 |
|
---|
[15960] | 48 | public override bool SupportsPause { get { return true; } }
|
---|
[15712] | 49 |
|
---|
[15974] | 50 | protected IFixedValueParameter<DoubleValue> VariableImportanceWeightParameter {
|
---|
| 51 | get { return (IFixedValueParameter<DoubleValue>)Parameters[VariableImportanceWeightName]; }
|
---|
[15950] | 52 | }
|
---|
| 53 |
|
---|
| 54 | protected double VariableImportanceWeight {
|
---|
| 55 | get { return VariableImportanceWeightParameter.Value.Value; }
|
---|
| 56 | }
|
---|
| 57 |
|
---|
[15974] | 58 | protected IFixedValueParameter<BoolValue> OptimizeConstantsParameter {
|
---|
| 59 | get { return (IFixedValueParameter<BoolValue>)Parameters[OptimizeConstantsParameterName]; }
|
---|
[15861] | 60 | }
|
---|
| 61 |
|
---|
| 62 | public bool OptimizeConstants {
|
---|
| 63 | get { return OptimizeConstantsParameter.Value.Value; }
|
---|
| 64 | set { OptimizeConstantsParameter.Value.Value = value; }
|
---|
| 65 | }
|
---|
| 66 |
|
---|
[15974] | 67 | protected IFixedValueParameter<IntValue> MaxComplexityParameter {
|
---|
| 68 | get { return (IFixedValueParameter<IntValue>)Parameters[MaxComplexityParameterName]; }
|
---|
[15712] | 69 | }
|
---|
[15974] | 70 |
|
---|
[15860] | 71 | public int MaxComplexity {
|
---|
| 72 | get { return MaxComplexityParameter.Value.Value; }
|
---|
| 73 | set { MaxComplexityParameter.Value.Value = value; }
|
---|
[15722] | 74 | }
|
---|
[15712] | 75 |
|
---|
[15974] | 76 | protected IFixedValueParameter<DoubleValue> ErrorWeightParameter {
|
---|
| 77 | get { return (IFixedValueParameter<DoubleValue>)Parameters[ErrorWeightParameterName]; }
|
---|
[15910] | 78 | }
|
---|
[15974] | 79 |
|
---|
[15910] | 80 | public double ErrorWeight {
|
---|
| 81 | get { return ErrorWeightParameter.Value.Value; }
|
---|
| 82 | set { ErrorWeightParameter.Value.Value = value; }
|
---|
| 83 | }
|
---|
| 84 |
|
---|
[15974] | 85 | protected IFixedValueParameter<IntValue> GuiUpdateIntervalParameter {
|
---|
| 86 | get { return (IFixedValueParameter<IntValue>)Parameters[GuiUpdateIntervalParameterName]; }
|
---|
[15722] | 87 | }
|
---|
[15974] | 88 |
|
---|
[15722] | 89 | public int GuiUpdateInterval {
|
---|
| 90 | get { return GuiUpdateIntervalParameter.Value.Value; }
|
---|
[15723] | 91 | set { GuiUpdateIntervalParameter.Value.Value = value; }
|
---|
[15722] | 92 | }
|
---|
[15712] | 93 |
|
---|
[15974] | 94 | protected IFixedValueParameter<EnumValue<StorageType>> SearchDataStructureParameter {
|
---|
| 95 | get { return (IFixedValueParameter<EnumValue<StorageType>>)Parameters[SearchDataStructureParameterName]; }
|
---|
[15723] | 96 | }
|
---|
[15974] | 97 |
|
---|
| 98 | public IFixedValueParameter<IntValue> SearchDataStructureSizeParameter {
|
---|
| 99 | get { return (IFixedValueParameter<IntValue>)Parameters[SearchDataStructureSizeParameterName]; }
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | public int SearchDataStructureSize {
|
---|
| 103 | get { return SearchDataStructureSizeParameter.Value.Value; }
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | public IFixedValueParameter<IntValue> SearchCacheSizeParameter {
|
---|
| 107 | get { return (IFixedValueParameter<IntValue>)Parameters[SearchCacheSizeParameterName]; }
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | public int SearchCacheSize {
|
---|
| 111 | get { return SearchCacheSizeParameter.Value.Value; }
|
---|
| 112 | }
|
---|
| 113 |
|
---|
[15746] | 114 | public StorageType SearchDataStructure {
|
---|
| 115 | get { return SearchDataStructureParameter.Value.Value; }
|
---|
| 116 | set { SearchDataStructureParameter.Value.Value = value; }
|
---|
[15723] | 117 | }
|
---|
| 118 |
|
---|
[15821] | 119 | public IFixedValueParameter<ReadOnlyCheckedItemCollection<IGrammarEnumerationAnalyzer>> AnalyzersParameter {
|
---|
| 120 | get { return (IFixedValueParameter<ReadOnlyCheckedItemCollection<IGrammarEnumerationAnalyzer>>)Parameters[AnalyzersParameterName]; }
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | public ICheckedItemCollection<IGrammarEnumerationAnalyzer> Analyzers {
|
---|
| 124 | get { return AnalyzersParameter.Value; }
|
---|
| 125 | }
|
---|
| 126 |
|
---|
[15930] | 127 | public IFixedValueParameter<ReadOnlyCheckedItemCollection<EnumValue<GrammarRule>>> GrammarSymbolsParameter {
|
---|
| 128 | get { return (IFixedValueParameter<ReadOnlyCheckedItemCollection<EnumValue<GrammarRule>>>)Parameters[GrammarSymbolsParameterName]; }
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | public ReadOnlyCheckedItemCollection<EnumValue<GrammarRule>> GrammarSymbols {
|
---|
| 132 | get { return GrammarSymbolsParameter.Value; }
|
---|
| 133 | }
|
---|
| 134 |
|
---|
[15824] | 135 | public SymbolString BestTrainingSentence { get; set; } // Currently set in RSquaredEvaluator: quite hacky, but makes testing much easier for now...
|
---|
[15722] | 136 | #endregion
|
---|
[15712] | 137 |
|
---|
[15960] | 138 | [Storable]
|
---|
[15860] | 139 | public Dictionary<int, int> DistinctSentencesComplexity { get; private set; } // Semantically distinct sentences and their length in a run.
|
---|
[15960] | 140 |
|
---|
| 141 | [Storable]
|
---|
[15812] | 142 | public HashSet<int> ArchivedPhrases { get; private set; }
|
---|
[15960] | 143 |
|
---|
| 144 | [Storable]
|
---|
[15821] | 145 | internal SearchDataStore OpenPhrases { get; private set; } // Stack/Queue/etc. for fetching the next node in the search tree.
|
---|
[15812] | 146 |
|
---|
[15974] | 147 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 148 | private void AfterDeserialization() {
|
---|
| 149 | variableImportance = CalculateVariableImportances();
|
---|
| 150 | }
|
---|
| 151 |
|
---|
[15821] | 152 | #region execution stats
|
---|
[15974] | 153 | [Storable]
|
---|
[15821] | 154 | public int AllGeneratedSentencesCount { get; private set; }
|
---|
[15746] | 155 |
|
---|
[15974] | 156 | [Storable]
|
---|
[15821] | 157 | public int OverwrittenSentencesCount { get; private set; } // It is not guaranteed that shorter solutions are found first.
|
---|
| 158 | // When longer solutions are overwritten with shorter ones,
|
---|
| 159 | // this counter is increased.
|
---|
[15974] | 160 | [Storable]
|
---|
[15821] | 161 | public int PhraseExpansionCount { get; private set; } // Number, how many times a nonterminal symbol is replaced with a production rule.
|
---|
| 162 | #endregion
|
---|
| 163 |
|
---|
[15800] | 164 | public Grammar Grammar { get; private set; }
|
---|
[15712] | 165 |
|
---|
[15722] | 166 | #region ctors
|
---|
| 167 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 168 | return new GrammarEnumerationAlgorithm(this, cloner);
|
---|
| 169 | }
|
---|
[15712] | 170 |
|
---|
[15722] | 171 | public GrammarEnumerationAlgorithm() {
|
---|
[15974] | 172 | Parameters.Add(new FixedValueParameter<DoubleValue>(VariableImportanceWeightName, "Variable Weight.", new DoubleValue(1.0)));
|
---|
| 173 | Parameters.Add(new FixedValueParameter<BoolValue>(OptimizeConstantsParameterName, "Run constant optimization in sentence evaluation.", new BoolValue(false)));
|
---|
| 174 | Parameters.Add(new FixedValueParameter<DoubleValue>(ErrorWeightParameterName, "Defines, how much weight is put on a phrase's r² value when priorizing phrases during search.", new DoubleValue(0.8)));
|
---|
| 175 | Parameters.Add(new FixedValueParameter<IntValue>(MaxComplexityParameterName, "The maximum number of variable symbols in a sentence.", new IntValue(12)));
|
---|
| 176 | Parameters.Add(new FixedValueParameter<IntValue>(GuiUpdateIntervalParameterName, "Number of generated sentences, until GUI is refreshed.", new IntValue(5000)));
|
---|
| 177 | Parameters.Add(new FixedValueParameter<IntValue>(SearchCacheSizeParameterName, "The size of the search node cache.", new IntValue((int)1e5)));
|
---|
| 178 | Parameters.Add(new FixedValueParameter<IntValue>(SearchDataStructureSizeParameterName, "The size of the search data structure.", new IntValue((int)1e5)));
|
---|
| 179 | Parameters.Add(new FixedValueParameter<EnumValue<StorageType>>(SearchDataStructureParameterName, new EnumValue<StorageType>(StorageType.PriorityQueue)));
|
---|
[15821] | 180 |
|
---|
[15974] | 181 | SearchDataStructureParameter.Value.ValueChanged += (o, e) => Prepare();
|
---|
| 182 | SearchDataStructureSizeParameter.Value.ValueChanged += (o, e) => Prepare();
|
---|
| 183 | SearchCacheSizeParameter.Value.ValueChanged += (o, e) => Prepare();
|
---|
| 184 |
|
---|
[15821] | 185 | var availableAnalyzers = new IGrammarEnumerationAnalyzer[] {
|
---|
| 186 | new SearchGraphVisualizer(),
|
---|
[15824] | 187 | new SentenceLogger(),
|
---|
| 188 | new RSquaredEvaluator()
|
---|
[15821] | 189 | };
|
---|
[15974] | 190 |
|
---|
[15821] | 191 | Parameters.Add(new FixedValueParameter<ReadOnlyCheckedItemCollection<IGrammarEnumerationAnalyzer>>(
|
---|
| 192 | AnalyzersParameterName,
|
---|
| 193 | new CheckedItemCollection<IGrammarEnumerationAnalyzer>(availableAnalyzers).AsReadOnly()));
|
---|
| 194 |
|
---|
[15957] | 195 | Analyzers.CheckedItemsChanged += Analyzers_CheckedItemsChanged;
|
---|
| 196 |
|
---|
[15821] | 197 | foreach (var analyzer in Analyzers) {
|
---|
| 198 | Analyzers.SetItemCheckedState(analyzer, false);
|
---|
| 199 | }
|
---|
[15824] | 200 | Analyzers.SetItemCheckedState(Analyzers.First(analyzer => analyzer is RSquaredEvaluator), true);
|
---|
[15930] | 201 |
|
---|
| 202 | var grammarSymbols = Enum.GetValues(typeof(GrammarRule))
|
---|
| 203 | .Cast<GrammarRule>()
|
---|
| 204 | .Select(v => new EnumValue<GrammarRule>(v));
|
---|
| 205 |
|
---|
| 206 | Parameters.Add(new FixedValueParameter<ReadOnlyCheckedItemCollection<EnumValue<GrammarRule>>>(
|
---|
| 207 | GrammarSymbolsParameterName,
|
---|
| 208 | new ReadOnlyCheckedItemCollection<EnumValue<GrammarRule>>(new CheckedItemCollection<EnumValue<GrammarRule>>(grammarSymbols))
|
---|
| 209 | ));
|
---|
| 210 | foreach (EnumValue<GrammarRule> grammarSymbol in GrammarSymbols) {
|
---|
| 211 | GrammarSymbols.SetItemCheckedState(grammarSymbol, true);
|
---|
| 212 | }
|
---|
[15957] | 213 |
|
---|
| 214 | // set a default problem
|
---|
| 215 | Problem = new RegressionProblem() {
|
---|
[15974] | 216 | ProblemData = new Problems.Instances.DataAnalysis.PolyTen(seed: 1234).GenerateRegressionData()
|
---|
[15957] | 217 | };
|
---|
[15722] | 218 | }
|
---|
[15712] | 219 |
|
---|
[15910] | 220 | public GrammarEnumerationAlgorithm(GrammarEnumerationAlgorithm original, Cloner cloner) : base(original, cloner) {
|
---|
[15957] | 221 | foreach (var analyzer in Analyzers.CheckedItems)
|
---|
| 222 | analyzer.Register(this);
|
---|
| 223 | Analyzers.CheckedItemsChanged += Analyzers_CheckedItemsChanged;
|
---|
[15960] | 224 |
|
---|
| 225 | DistinctSentencesComplexity = new Dictionary<int, int>(original.DistinctSentencesComplexity);
|
---|
| 226 | ArchivedPhrases = new HashSet<int>(original.ArchivedPhrases);
|
---|
| 227 | OpenPhrases = cloner.Clone(original.OpenPhrases);
|
---|
[15974] | 228 | Grammar = cloner.Clone(original.Grammar);
|
---|
[15960] | 229 |
|
---|
| 230 | AllGeneratedSentencesCount = original.AllGeneratedSentencesCount;
|
---|
| 231 | OverwrittenSentencesCount = original.OverwrittenSentencesCount;
|
---|
| 232 | PhraseExpansionCount = original.PhraseExpansionCount;
|
---|
| 233 |
|
---|
| 234 | if (original.variableImportance != null)
|
---|
| 235 | variableImportance = new Dictionary<VariableTerminalSymbol, double>(original.variableImportance);
|
---|
[15910] | 236 | }
|
---|
[15722] | 237 | #endregion
|
---|
[15712] | 238 |
|
---|
[15950] | 239 | private Dictionary<VariableTerminalSymbol, double> variableImportance;
|
---|
| 240 |
|
---|
[15957] | 241 | public override void Prepare() {
|
---|
| 242 | DistinctSentencesComplexity = new Dictionary<int, int>();
|
---|
[15812] | 243 | ArchivedPhrases = new HashSet<int>();
|
---|
[15821] | 244 | AllGeneratedSentencesCount = 0;
|
---|
| 245 | OverwrittenSentencesCount = 0;
|
---|
| 246 | PhraseExpansionCount = 0;
|
---|
[15746] | 247 |
|
---|
[15957] | 248 | Analyzers.OfType<RSquaredEvaluator>().First().OptimizeConstants = OptimizeConstants;
|
---|
[15930] | 249 | Grammar = new Grammar(Problem.ProblemData.AllowedInputVariables.ToArray(), GrammarSymbols.CheckedItems.Select(v => v.Value));
|
---|
[15974] | 250 | OpenPhrases = new SearchDataStore(SearchDataStructure, SearchDataStructureSize, SearchCacheSize); // Select search strategy
|
---|
[15712] | 251 |
|
---|
[15957] | 252 | base.Prepare(); // this actually clears the results which will get reinitialized on Run()
|
---|
| 253 | }
|
---|
| 254 |
|
---|
[15960] | 255 | private Dictionary<VariableTerminalSymbol, double> CalculateVariableImportances() {
|
---|
[15950] | 256 | variableImportance = new Dictionary<VariableTerminalSymbol, double>();
|
---|
| 257 |
|
---|
| 258 | RandomForestRegression rf = new RandomForestRegression();
|
---|
| 259 | rf.Problem = Problem;
|
---|
| 260 | rf.Start();
|
---|
| 261 | IRegressionSolution rfSolution = (RandomForestRegressionSolution)rf.Results["Random forest regression solution"].Value;
|
---|
| 262 | var rfImpacts = RegressionSolutionVariableImpactsCalculator.CalculateImpacts(
|
---|
| 263 | rfSolution,
|
---|
| 264 | RegressionSolutionVariableImpactsCalculator.DataPartitionEnum.Training,
|
---|
| 265 | RegressionSolutionVariableImpactsCalculator.ReplacementMethodEnum.Shuffle);
|
---|
| 266 |
|
---|
| 267 | // save the normalized importances
|
---|
| 268 | var sum = rfImpacts.Sum(x => x.Item2);
|
---|
| 269 | foreach (Tuple<string, double> rfImpact in rfImpacts) {
|
---|
| 270 | VariableTerminalSymbol varSym = Grammar.VarTerminals.First(v => v.StringRepresentation == rfImpact.Item1);
|
---|
| 271 | variableImportance[varSym] = rfImpact.Item2 / sum;
|
---|
| 272 | }
|
---|
[15960] | 273 | return variableImportance;
|
---|
| 274 | }
|
---|
[15950] | 275 |
|
---|
[15960] | 276 | protected override void Run(CancellationToken cancellationToken) {
|
---|
| 277 | // do not reinitialize the algorithm if we're resuming from pause
|
---|
| 278 | if (previousExecutionState != ExecutionState.Paused) {
|
---|
| 279 | CalculateVariableImportances();
|
---|
| 280 | InitResults();
|
---|
| 281 | var phrase0 = new SymbolString(new[] { Grammar.StartSymbol });
|
---|
| 282 | var phrase0Hash = Grammar.Hasher.CalcHashCode(phrase0);
|
---|
[15974] | 283 |
|
---|
[15960] | 284 | OpenPhrases.Store(new SearchNode(phrase0Hash, 0.0, 0.0, phrase0));
|
---|
| 285 | }
|
---|
| 286 |
|
---|
[15910] | 287 | int maxSentenceLength = GetMaxSentenceLength();
|
---|
[15950] | 288 | var errorWeight = ErrorWeight;
|
---|
| 289 | var variableImportanceWeight = VariableImportanceWeight;
|
---|
[15960] | 290 | // main search loop
|
---|
[15821] | 291 | while (OpenPhrases.Count > 0) {
|
---|
[15960] | 292 | if (cancellationToken.IsCancellationRequested)
|
---|
| 293 | break;
|
---|
[15746] | 294 |
|
---|
[15915] | 295 | SearchNode fetchedSearchNode = OpenPhrases.GetNext();
|
---|
[15974] | 296 |
|
---|
| 297 | if (fetchedSearchNode == null)
|
---|
| 298 | continue;
|
---|
| 299 |
|
---|
[15915] | 300 | SymbolString currPhrase = fetchedSearchNode.SymbolString;
|
---|
[15722] | 301 |
|
---|
[15915] | 302 | OnPhraseFetched(fetchedSearchNode.Hash, currPhrase);
|
---|
[15765] | 303 |
|
---|
[15915] | 304 | ArchivedPhrases.Add(fetchedSearchNode.Hash);
|
---|
[15726] | 305 |
|
---|
[15821] | 306 | // expand next nonterminal symbols
|
---|
[15827] | 307 | int nonterminalSymbolIndex = currPhrase.NextNonterminalIndex();
|
---|
| 308 | NonterminalSymbol expandedSymbol = (NonterminalSymbol)currPhrase[nonterminalSymbolIndex];
|
---|
[15834] | 309 | var appliedProductions = Grammar.Productions[expandedSymbol];
|
---|
[15734] | 310 |
|
---|
[15827] | 311 | for (int i = 0; i < appliedProductions.Count; i++) {
|
---|
[15821] | 312 | PhraseExpansionCount++;
|
---|
[15734] | 313 |
|
---|
[15827] | 314 | SymbolString newPhrase = currPhrase.DerivePhrase(nonterminalSymbolIndex, appliedProductions[i]);
|
---|
[15860] | 315 | int newPhraseComplexity = Grammar.GetComplexity(newPhrase);
|
---|
[15712] | 316 |
|
---|
[15957] | 317 | if (newPhraseComplexity > MaxComplexity)
|
---|
| 318 | continue;
|
---|
[15765] | 319 |
|
---|
[15957] | 320 | var phraseHash = Grammar.Hasher.CalcHashCode(newPhrase);
|
---|
[15800] | 321 |
|
---|
[15957] | 322 | OnPhraseDerived(fetchedSearchNode.Hash, fetchedSearchNode.SymbolString, phraseHash, newPhrase, expandedSymbol, appliedProductions[i]);
|
---|
[15821] | 323 |
|
---|
[15957] | 324 | if (newPhrase.IsSentence()) {
|
---|
| 325 | AllGeneratedSentencesCount++;
|
---|
[15821] | 326 |
|
---|
[15957] | 327 | OnSentenceGenerated(fetchedSearchNode.Hash, fetchedSearchNode.SymbolString, phraseHash, newPhrase, expandedSymbol, appliedProductions[i]);
|
---|
| 328 |
|
---|
| 329 | // Is the best solution found? (only if RSquaredEvaluator is activated)
|
---|
| 330 | if (Results.ContainsKey(RSquaredEvaluator.BestTrainingQualityResultName)) {
|
---|
| 331 | double r2 = ((DoubleValue)Results[RSquaredEvaluator.BestTrainingQualityResultName].Value).Value;
|
---|
| 332 | if (r2.IsAlmost(1.0)) {
|
---|
| 333 | UpdateView(force: true);
|
---|
| 334 | return;
|
---|
[15883] | 335 | }
|
---|
[15957] | 336 | }
|
---|
[15883] | 337 |
|
---|
[15957] | 338 | if (!DistinctSentencesComplexity.ContainsKey(phraseHash) || DistinctSentencesComplexity[phraseHash] > newPhraseComplexity) {
|
---|
| 339 | if (DistinctSentencesComplexity.ContainsKey(phraseHash)) OverwrittenSentencesCount++; // for analysis only
|
---|
[15821] | 340 |
|
---|
[15957] | 341 | DistinctSentencesComplexity[phraseHash] = newPhraseComplexity;
|
---|
| 342 | OnDistinctSentenceGenerated(fetchedSearchNode.Hash, fetchedSearchNode.SymbolString, phraseHash, newPhrase, expandedSymbol, appliedProductions[i]);
|
---|
| 343 | }
|
---|
| 344 | UpdateView();
|
---|
[15821] | 345 |
|
---|
[15957] | 346 | } else if (!OpenPhrases.Contains(phraseHash) && !ArchivedPhrases.Contains(phraseHash)) {
|
---|
[15915] | 347 |
|
---|
[15957] | 348 | double r2 = GetR2(newPhrase, fetchedSearchNode.R2);
|
---|
| 349 | double phrasePriority = GetPriority(newPhrase, r2, maxSentenceLength, errorWeight, variableImportanceWeight);
|
---|
[15915] | 350 |
|
---|
[15957] | 351 | SearchNode newSearchNode = new SearchNode(phraseHash, phrasePriority, r2, newPhrase);
|
---|
| 352 | OpenPhrases.Store(newSearchNode);
|
---|
[15712] | 353 | }
|
---|
| 354 | }
|
---|
| 355 | }
|
---|
[15812] | 356 | UpdateView(force: true);
|
---|
[15746] | 357 | }
|
---|
[15723] | 358 |
|
---|
[15950] | 359 | protected double GetPriority(SymbolString phrase, double r2, int maxSentenceLength, double errorWeight, double variableImportanceWeight) {
|
---|
| 360 | var distinctVars = phrase.OfType<VariableTerminalSymbol>().Distinct();
|
---|
| 361 |
|
---|
| 362 | var sum = 0d;
|
---|
| 363 | foreach (var v in distinctVars) {
|
---|
| 364 | sum += variableImportance[v];
|
---|
| 365 | }
|
---|
| 366 | var phraseVariableImportance = 1 - sum;
|
---|
| 367 |
|
---|
[15910] | 368 | double relLength = (double)phrase.Count() / maxSentenceLength;
|
---|
[15907] | 369 | double error = 1.0 - r2;
|
---|
| 370 |
|
---|
[15950] | 371 | return relLength + errorWeight * error + variableImportanceWeight * phraseVariableImportance;
|
---|
[15910] | 372 | }
|
---|
| 373 |
|
---|
[15915] | 374 | private double GetR2(SymbolString phrase, double parentR2) {
|
---|
| 375 | int length = phrase.Count();
|
---|
| 376 |
|
---|
| 377 | // If the only nonterminal symbol is Expr, we can need to evaluate the sentence. Otherwise
|
---|
| 378 | // the phrase has the same r2 as its parent, from which it was derived.
|
---|
| 379 | for (int i = 0; i < length; i++) {
|
---|
| 380 | if (phrase[i] is NonterminalSymbol && phrase[i] != Grammar.Expr) {
|
---|
| 381 | return parentR2;
|
---|
| 382 | }
|
---|
| 383 | }
|
---|
| 384 |
|
---|
| 385 | return Grammar.EvaluatePhrase(phrase, Problem.ProblemData, OptimizeConstants);
|
---|
| 386 | }
|
---|
| 387 |
|
---|
[15910] | 388 | private int GetMaxSentenceLength() {
|
---|
| 389 | SymbolString s = new SymbolString(Grammar.StartSymbol);
|
---|
| 390 |
|
---|
[15930] | 391 | while (!s.IsSentence() && Grammar.GetComplexity(s) <= MaxComplexity) {
|
---|
[15910] | 392 | int expandedSymbolIndex = s.NextNonterminalIndex();
|
---|
| 393 | NonterminalSymbol expandedSymbol = (NonterminalSymbol)s[expandedSymbolIndex];
|
---|
| 394 |
|
---|
| 395 | var productions = Grammar.Productions[expandedSymbol];
|
---|
| 396 | var longestProduction = productions // Find production with most terminal symbols to expand as much as possible...
|
---|
| 397 | .OrderBy(CountTerminals) // but with lowest complexity/nonterminal count to keep complexity low.
|
---|
| 398 | .ThenByDescending(CountNonTerminals)
|
---|
| 399 | .First();
|
---|
| 400 |
|
---|
| 401 | s = s.DerivePhrase(expandedSymbolIndex, longestProduction);
|
---|
[15907] | 402 | }
|
---|
| 403 |
|
---|
[15910] | 404 | return s.Count();
|
---|
| 405 | }
|
---|
[15907] | 406 |
|
---|
[15910] | 407 | private int CountTerminals(Production p) {
|
---|
| 408 | return p.Count(s => s is TerminalSymbol);
|
---|
[15883] | 409 | }
|
---|
| 410 |
|
---|
[15910] | 411 | private int CountNonTerminals(Production p) {
|
---|
| 412 | return p.Count(s => s is NonterminalSymbol);
|
---|
| 413 | }
|
---|
| 414 |
|
---|
[15960] | 415 | #region pause support
|
---|
| 416 | private ExecutionState previousExecutionState;
|
---|
| 417 | protected override void OnPaused() {
|
---|
| 418 | previousExecutionState = this.ExecutionState;
|
---|
| 419 | base.OnPaused();
|
---|
| 420 | }
|
---|
| 421 | protected override void OnPrepared() {
|
---|
| 422 | previousExecutionState = this.ExecutionState;
|
---|
| 423 | base.OnPrepared();
|
---|
| 424 | }
|
---|
| 425 | protected override void OnStarted() {
|
---|
| 426 | previousExecutionState = this.ExecutionState;
|
---|
| 427 | base.OnStarted();
|
---|
| 428 | }
|
---|
| 429 | protected override void OnStopped() {
|
---|
| 430 | previousExecutionState = this.ExecutionState;
|
---|
[15974] | 431 |
|
---|
| 432 | if (BestTrainingSentence == null) {
|
---|
| 433 | base.OnStopped();
|
---|
| 434 | return;
|
---|
| 435 | }
|
---|
| 436 |
|
---|
| 437 | var tree = Grammar.ParseSymbolicExpressionTree(BestTrainingSentence);
|
---|
| 438 | var model = new SymbolicRegressionModel(Problem.ProblemData.TargetVariable, tree, new SymbolicDataAnalysisExpressionTreeLinearInterpreter());
|
---|
| 439 | model.Scale(Problem.ProblemData);
|
---|
| 440 | var bestTrainingSolution = new SymbolicRegressionSolution(model, Problem.ProblemData);
|
---|
| 441 | Results.AddOrUpdateResult(BestTrainingModelResultName, model);
|
---|
| 442 | Results.AddOrUpdateResult(BestTrainingSolutionResultName, bestTrainingSolution);
|
---|
| 443 | Results.AddOrUpdateResult(BestComplexityResultName, new IntValue(Grammar.GetComplexity(BestTrainingSentence)));
|
---|
[15960] | 444 | base.OnStopped();
|
---|
| 445 | }
|
---|
| 446 | #endregion
|
---|
| 447 |
|
---|
[15821] | 448 | #region Visualization in HL
|
---|
[15746] | 449 | // Initialize entries in result set.
|
---|
| 450 | private void InitResults() {
|
---|
[15957] | 451 | Results.Clear();
|
---|
[15803] | 452 | Results.Add(new Result(GeneratedPhrasesName, new IntValue(0)));
|
---|
| 453 | Results.Add(new Result(SearchStructureSizeName, new IntValue(0)));
|
---|
[15746] | 454 | Results.Add(new Result(GeneratedSentencesName, new IntValue(0)));
|
---|
| 455 | Results.Add(new Result(DistinctSentencesName, new IntValue(0)));
|
---|
| 456 | Results.Add(new Result(PhraseExpansionsName, new IntValue(0)));
|
---|
[15821] | 457 | Results.Add(new Result(OverwrittenSentencesName, new IntValue(0)));
|
---|
[15860] | 458 | Results.Add(new Result(AverageSentenceComplexityName, new DoubleValue(1.0)));
|
---|
[15824] | 459 | Results.Add(new Result(ExpansionsPerSecondName, "In Thousand expansions per second", new IntValue(0)));
|
---|
[15712] | 460 | }
|
---|
[15746] | 461 |
|
---|
| 462 | // Update the view for intermediate results in an algorithm run.
|
---|
| 463 | private int updates;
|
---|
[15812] | 464 | private void UpdateView(bool force = false) {
|
---|
[15746] | 465 | updates++;
|
---|
| 466 |
|
---|
[15812] | 467 | if (force || updates % GuiUpdateInterval == 1) {
|
---|
[15803] | 468 | ((IntValue)Results[GeneratedPhrasesName].Value).Value = ArchivedPhrases.Count;
|
---|
| 469 | ((IntValue)Results[SearchStructureSizeName].Value).Value = OpenPhrases.Count;
|
---|
[15821] | 470 | ((IntValue)Results[GeneratedSentencesName].Value).Value = AllGeneratedSentencesCount;
|
---|
[15860] | 471 | ((IntValue)Results[DistinctSentencesName].Value).Value = DistinctSentencesComplexity.Count;
|
---|
[15821] | 472 | ((IntValue)Results[PhraseExpansionsName].Value).Value = PhraseExpansionCount;
|
---|
[15974] | 473 | ((DoubleValue)Results[AverageSentenceComplexityName].Value).Value = DistinctSentencesComplexity.Count > 0
|
---|
| 474 | ? DistinctSentencesComplexity.Select(pair => pair.Value).Average()
|
---|
| 475 | : 0;
|
---|
[15821] | 476 | ((IntValue)Results[OverwrittenSentencesName].Value).Value = OverwrittenSentencesCount;
|
---|
[15824] | 477 | ((IntValue)Results[ExpansionsPerSecondName].Value).Value = (int)((PhraseExpansionCount /
|
---|
| 478 | ExecutionTime.TotalSeconds) / 1000.0);
|
---|
[15746] | 479 | }
|
---|
| 480 | }
|
---|
[15821] | 481 | #endregion
|
---|
[15746] | 482 |
|
---|
[15821] | 483 | #region events
|
---|
[15957] | 484 |
|
---|
| 485 | // private event handlers for analyzers
|
---|
| 486 | private void Analyzers_CheckedItemsChanged(object sender, CollectionItemsChangedEventArgs<IGrammarEnumerationAnalyzer> args) {
|
---|
| 487 | // newly added items
|
---|
| 488 | foreach (var item in args.Items.Except(args.OldItems).Union(args.OldItems.Except(args.Items))) {
|
---|
| 489 | if (Analyzers.ItemChecked(item)) {
|
---|
| 490 | item.Register(this);
|
---|
| 491 | } else {
|
---|
| 492 | item.Deregister(this);
|
---|
| 493 | }
|
---|
| 494 | }
|
---|
| 495 | }
|
---|
| 496 |
|
---|
[15821] | 497 | public event EventHandler<PhraseEventArgs> PhraseFetched;
|
---|
| 498 | private void OnPhraseFetched(int hash, SymbolString symbolString) {
|
---|
| 499 | if (PhraseFetched != null) {
|
---|
| 500 | PhraseFetched(this, new PhraseEventArgs(hash, symbolString));
|
---|
[15746] | 501 | }
|
---|
| 502 | }
|
---|
[15812] | 503 |
|
---|
[15821] | 504 | public event EventHandler<PhraseAddedEventArgs> PhraseDerived;
|
---|
| 505 | private void OnPhraseDerived(int parentHash, SymbolString parentSymbolString, int addedHash, SymbolString addedSymbolString, Symbol expandedSymbol, Production expandedProduction) {
|
---|
| 506 | if (PhraseDerived != null) {
|
---|
| 507 | PhraseDerived(this, new PhraseAddedEventArgs(parentHash, parentSymbolString, addedHash, addedSymbolString, expandedSymbol, expandedProduction));
|
---|
| 508 | }
|
---|
[15803] | 509 | }
|
---|
[15765] | 510 |
|
---|
[15821] | 511 | public event EventHandler<PhraseAddedEventArgs> SentenceGenerated;
|
---|
| 512 | private void OnSentenceGenerated(int parentHash, SymbolString parentSymbolString, int addedHash, SymbolString addedSymbolString, Symbol expandedSymbol, Production expandedProduction) {
|
---|
| 513 | if (SentenceGenerated != null) {
|
---|
| 514 | SentenceGenerated(this, new PhraseAddedEventArgs(parentHash, parentSymbolString, addedHash, addedSymbolString, expandedSymbol, expandedProduction));
|
---|
| 515 | }
|
---|
| 516 | }
|
---|
| 517 |
|
---|
| 518 | public event EventHandler<PhraseAddedEventArgs> DistinctSentenceGenerated;
|
---|
| 519 | private void OnDistinctSentenceGenerated(int parentHash, SymbolString parentSymbolString, int addedHash, SymbolString addedSymbolString, Symbol expandedSymbol, Production expandedProduction) {
|
---|
| 520 | if (DistinctSentenceGenerated != null) {
|
---|
| 521 | DistinctSentenceGenerated(this, new PhraseAddedEventArgs(parentHash, parentSymbolString, addedHash, addedSymbolString, expandedSymbol, expandedProduction));
|
---|
| 522 | }
|
---|
| 523 | }
|
---|
| 524 |
|
---|
| 525 | #endregion
|
---|
| 526 |
|
---|
[15712] | 527 | }
|
---|
[15821] | 528 |
|
---|
| 529 | #region events for analysis
|
---|
| 530 |
|
---|
| 531 | public class PhraseEventArgs : EventArgs {
|
---|
| 532 | public int Hash { get; }
|
---|
| 533 |
|
---|
| 534 | public SymbolString Phrase { get; }
|
---|
| 535 |
|
---|
| 536 | public PhraseEventArgs(int hash, SymbolString phrase) {
|
---|
| 537 | Hash = hash;
|
---|
| 538 | Phrase = phrase;
|
---|
| 539 | }
|
---|
| 540 | }
|
---|
| 541 |
|
---|
| 542 | public class PhraseAddedEventArgs : EventArgs {
|
---|
| 543 | public int ParentHash { get; }
|
---|
| 544 | public int NewHash { get; }
|
---|
| 545 |
|
---|
| 546 | public SymbolString ParentPhrase { get; }
|
---|
| 547 | public SymbolString NewPhrase { get; }
|
---|
| 548 |
|
---|
| 549 | public Symbol ExpandedSymbol { get; }
|
---|
| 550 |
|
---|
| 551 | public Production ExpandedProduction { get; }
|
---|
| 552 |
|
---|
| 553 | public PhraseAddedEventArgs(int parentHash, SymbolString parentPhrase, int newHash, SymbolString newPhrase, Symbol expandedSymbol, Production expandedProduction) {
|
---|
| 554 | ParentHash = parentHash;
|
---|
| 555 | ParentPhrase = parentPhrase;
|
---|
| 556 | NewHash = newHash;
|
---|
| 557 | NewPhrase = newPhrase;
|
---|
| 558 | ExpandedSymbol = expandedSymbol;
|
---|
| 559 | ExpandedProduction = expandedProduction;
|
---|
| 560 | }
|
---|
| 561 | }
|
---|
| 562 |
|
---|
| 563 | #endregion
|
---|
[15712] | 564 | } |
---|