Changeset 16026
- Timestamp:
- 07/27/18 19:28:18 (6 years ago)
- Location:
- branches/2886_SymRegGrammarEnumeration
- Files:
-
- 1 added
- 12 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/Analysis/RSquaredEvaluator.cs
r15994 r16026 1 1 using System; 2 2 using System.Diagnostics; 3 using HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration.GrammarEnumeration;4 3 using HeuristicLab.Analysis; 5 4 using HeuristicLab.Common; … … 51 50 private void AlgorithmOnDistinctSentenceGenerated(object sender, PhraseAddedEventArgs phraseAddedEventArgs) { 52 51 GrammarEnumerationAlgorithm algorithm = (GrammarEnumerationAlgorithm)sender; 53 EvaluateSentence(algorithm, phraseAddedEventArgs.NewPhrase, algorithm.OptimizeConstants );52 EvaluateSentence(algorithm, phraseAddedEventArgs.NewPhrase, algorithm.OptimizeConstants, algorithm.ConstantOptimizationIterations); 54 53 } 55 54 … … 69 68 } 70 69 71 private void EvaluateSentence(GrammarEnumerationAlgorithm algorithm, Symbol String symbolString, bool optimizeConstants) {70 private void EvaluateSentence(GrammarEnumerationAlgorithm algorithm, SymbolList sentence, bool optimizeConstants, int maxIterations) { 72 71 var results = algorithm.Results; 73 72 var grammar = algorithm.Grammar; 74 73 var problemData = algorithm.Problem.ProblemData; 75 74 76 SymbolicExpressionTree tree = algorithm.Grammar.ParseSymbolicExpressionTree(s ymbolString);75 SymbolicExpressionTree tree = algorithm.Grammar.ParseSymbolicExpressionTree(sentence); 77 76 Debug.Assert(SymbolicRegressionConstantOptimizationEvaluator.CanOptimizeConstants(tree)); 78 77 79 double r2 = Evaluate(problemData, tree, optimizeConstants );78 double r2 = Evaluate(problemData, tree, optimizeConstants, maxIterations); 80 79 double bestR2 = results.ContainsKey(BestTrainingQualityResultName) ? GetValue<double>(results[BestTrainingQualityResultName].Value) : 0.0; 81 80 if (r2 < bestR2) … … 83 82 84 83 var bestComplexity = results.ContainsKey(BestComplexityResultName) ? GetValue<int>(results[BestComplexityResultName].Value) : int.MaxValue; 85 var complexity = grammar.GetComplexity(symbolString);84 var complexity = sentence.Complexity; 86 85 87 86 if (algorithm.BestTrainingSentence == null || r2 > bestR2 || (r2.IsAlmost(bestR2) && complexity < bestComplexity)) { 88 algorithm.BestTrainingSentence = s ymbolString;87 algorithm.BestTrainingSentence = sentence; 89 88 90 89 var model = new SymbolicRegressionModel(problemData.TargetVariable, tree, expressionTreeLinearInterpreter); … … 110 109 dt = (DataTable)results[BestSolutions].Value; 111 110 dt.Rows["Quality"].Values.Add(r2); 112 dt.Rows["Relative Length"].Values.Add((double)s ymbolString.Count() / algorithm.MaxSentenceLength);111 dt.Rows["Relative Length"].Values.Add((double)sentence.Count); 113 112 dt.Rows["Complexity"].Values.Add(complexity); 114 113 dt.Rows["Timestamp"].Values.Add(algorithm.ExecutionTime.TotalMilliseconds / 1000d); … … 116 115 } 117 116 118 public static double Evaluate(IRegressionProblemData problemData, SymbolicExpressionTree tree, bool optimizeConstants = true ) {117 public static double Evaluate(IRegressionProblemData problemData, SymbolicExpressionTree tree, bool optimizeConstants = true, int maxIterations = 10) { 119 118 double r2; 120 119 … … 127 126 problemData.TrainingIndices, 128 127 applyLinearScaling: true, 129 maxIterations: 10,128 maxIterations: maxIterations, 130 129 updateVariableWeights: false, 131 130 updateConstantsInTree: true); -
branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/Analysis/SearchGraphVisualizer.cs
r15949 r16026 55 55 try { 56 56 var alg = (GrammarEnumerationAlgorithm)sender; 57 var phrase0 = new Symbol String(new[] { alg.Grammar.StartSymbol });57 var phrase0 = new SymbolList(new[] { alg.Grammar.StartSymbol }); 58 58 var phrase0Hash = alg.Grammar.Hasher.CalcHashCode(phrase0); 59 59 -
branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/Analysis/SentenceLogger.cs
r15982 r16026 89 89 distinctSentencesFileTrace.WriteLine(ToCsvLine( 90 90 ((uint)phraseAddedEventArgs.NewHash).ToString("D10"), 91 phraseAddedEventArgs.NewPhrase.Count ().ToString("D3"),91 phraseAddedEventArgs.NewPhrase.Count.ToString("D3"), 92 92 //phraseAddedEventArgs.NewPhrase.ToString(), 93 93 ((GrammarEnumerationAlgorithm)sender).Grammar.ToInfixString(phraseAddedEventArgs.NewPhrase))); … … 99 99 allSentencesFileTrace.WriteLine(ToCsvLine( 100 100 ((uint)phraseAddedEventArgs.NewHash).ToString("D10"), 101 phraseAddedEventArgs.NewPhrase.Count ().ToString("D3"),101 phraseAddedEventArgs.NewPhrase.Count.ToString("D3"), 102 102 //phraseAddedEventArgs.NewPhrase.ToString(), 103 103 ((GrammarEnumerationAlgorithm)sender).Grammar.ToInfixString(phraseAddedEventArgs.NewPhrase))); -
branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/Grammar.cs
r15993 r16026 28 28 #region Symbols 29 29 30 public IReadOnlyDictionary<Symbol, IReadOnlyList< Production>> Productions { get; private set; }30 public IReadOnlyDictionary<Symbol, IReadOnlyList<SymbolList>> Productions { get; private set; } 31 31 32 32 public NonterminalSymbol Var; … … 85 85 infixExpressionFormatter = cloner.Clone(original.infixExpressionFormatter); 86 86 87 Productions = original.Productions.ToDictionary(x => cloner.Clone(x.Key), x => (IReadOnlyList< Production>)x.Value.Select(cloner.Clone).ToList());87 Productions = original.Productions.ToDictionary(x => cloner.Clone(x.Key), x => (IReadOnlyList<SymbolList>)x.Value.Select(cloner.Clone).ToList()); 88 88 VarTerminals = original.VarTerminals.Select(cloner.Clone).ToList(); 89 89 … … 144 144 StartSymbol = Expr; 145 145 146 Dictionary<Symbol, IReadOnlyList< Production>> productions = new Dictionary<Symbol, IReadOnlyList<Production>>();146 Dictionary<Symbol, IReadOnlyList<SymbolList>> productions = new Dictionary<Symbol, IReadOnlyList<SymbolList>>(); 147 147 148 148 // Map each variable to a separate production rule of the "Var" nonterminal symbol. 149 149 VarTerminals = variables.Select(v => new VariableTerminalSymbol(v)).ToArray(); 150 productions[Var] = VarTerminals.Select(v => new Production(v)).ToArray();150 productions[Var] = VarTerminals.Select(v => new SymbolList(v)).ToArray(); 151 151 152 152 // Expression Grammar Rules 153 var exprProductions = new List< Production>();153 var exprProductions = new List<SymbolList>(); 154 154 if (includedRules.Contains(GrammarRule.MultipleTerms)) 155 exprProductions.Add(new Production(Const, Term, Multiplication, Expr, Addition));156 157 exprProductions.Add(new Production(Const, Term, Multiplication, Const, Addition));155 exprProductions.Add(new SymbolList(Const, Term, Multiplication, Expr, Addition)); 156 157 exprProductions.Add(new SymbolList(Const, Term, Multiplication, Const, Addition)); 158 158 productions[Expr] = exprProductions.ToArray(); 159 159 160 160 // Term Grammar Rules 161 var termProductions = new List< Production>();161 var termProductions = new List<SymbolList>(); 162 162 if (includedRules.Contains(GrammarRule.MultipleFactors)) 163 termProductions.Add(new Production(Factor, Term, Multiplication));163 termProductions.Add(new SymbolList(Factor, Term, Multiplication)); 164 164 if (includedRules.Contains(GrammarRule.InverseTerm)) 165 termProductions.Add(new Production(InvExpr, Inv));166 termProductions.Add(new Production(Factor));165 termProductions.Add(new SymbolList(InvExpr, Inv)); 166 termProductions.Add(new SymbolList(Factor)); 167 167 productions[Term] = termProductions.ToArray(); 168 168 169 169 // Factor Grammar Rules 170 var factorProductions = new List< Production>();171 factorProductions.Add(new Production(Var));170 var factorProductions = new List<SymbolList>(); 171 factorProductions.Add(new SymbolList(Var)); 172 172 if (includedRules.Contains(GrammarRule.Logarithm)) 173 factorProductions.Add(new Production(LogFactor));173 factorProductions.Add(new SymbolList(LogFactor)); 174 174 if (includedRules.Contains(GrammarRule.Exponentiation)) 175 factorProductions.Add(new Production(ExpFactor));175 factorProductions.Add(new SymbolList(ExpFactor)); 176 176 if (includedRules.Contains(GrammarRule.Sine)) 177 factorProductions.Add(new Production(SinFactor));177 factorProductions.Add(new SymbolList(SinFactor)); 178 178 productions[Factor] = factorProductions.ToArray(); 179 179 180 productions[LogFactor] = new[] { new Production(SimpleExpr, Log) };181 productions[ExpFactor] = new[] { new Production(Const, SimpleTerm, Multiplication, Exp) };182 productions[SinFactor] = new[] { new Production(SimpleExpr, Sin) };180 productions[LogFactor] = new[] { new SymbolList(SimpleExpr, Log) }; 181 productions[ExpFactor] = new[] { new SymbolList(Const, SimpleTerm, Multiplication, Exp) }; 182 productions[SinFactor] = new[] { new SymbolList(SimpleExpr, Sin) }; 183 183 184 184 productions[SimpleExpr] = new[] { 185 new Production(Const, SimpleTerm, Multiplication, SimpleExpr, Addition),186 new Production(Const, SimpleTerm, Multiplication, Const, Addition)185 new SymbolList(Const, SimpleTerm, Multiplication, SimpleExpr, Addition), 186 new SymbolList(Const, SimpleTerm, Multiplication, Const, Addition) 187 187 }; 188 188 189 189 productions[SimpleTerm] = new[] { 190 new Production(Var, SimpleTerm, Multiplication),191 new Production(Var)190 new SymbolList(Var, SimpleTerm, Multiplication), 191 new SymbolList(Var) 192 192 }; 193 193 194 194 productions[InvExpr] = new[] { 195 new Production(Const, InvTerm, Multiplication, InvExpr, Addition),196 new Production(Const, InvTerm, Multiplication, Const, Addition)195 new SymbolList(Const, InvTerm, Multiplication, InvExpr, Addition), 196 new SymbolList(Const, InvTerm, Multiplication, Const, Addition) 197 197 }; 198 198 199 199 productions[InvTerm] = new[] { 200 new Production(Factor, InvTerm, Multiplication),201 new Production(Factor)200 new SymbolList(Factor, InvTerm, Multiplication), 201 new SymbolList(Factor) 202 202 }; 203 203 … … 234 234 } 235 235 236 public int GetComplexity(SymbolString s) {237 int c = 0;238 int length = s.Count();239 for (int i = 0; i < length; i++) {240 if (s[i] is NonterminalSymbol || s[i] is VariableTerminalSymbol) c++;241 }242 return c;243 }244 245 236 // returns the maximum achievable sentence length below the maximum complexity 246 237 public int GetMaxSentenceLength(int maxComplexity) { 247 Symbol String s = new SymbolString(StartSymbol);248 249 while (!s.IsSentence() && GetComplexity(s)<= maxComplexity) {238 SymbolList s = new SymbolList(StartSymbol); 239 240 while (!s.IsSentence() && s.Complexity <= maxComplexity) { 250 241 int expandedSymbolIndex = s.NextNonterminalIndex(); 251 242 NonterminalSymbol expandedSymbol = (NonterminalSymbol)s[expandedSymbolIndex]; 252 243 253 244 var productions = Productions[expandedSymbol]; 254 var longestProduction = productions // Find production with most terminal symbols to expand as much as possible...255 .OrderBy( CountTerminals)// but with lowest complexity/nonterminal count to keep complexity low.256 .ThenByDescending( CountNonTerminals)245 var longestProduction = productions // Find production with most terminal symbols to expand as much as possible... 246 .OrderBy(x => x.TerminalSymbolCount) // but with lowest complexity/nonterminal count to keep complexity low. 247 .ThenByDescending(x => x.NonTerminalSymbolCount) 257 248 .First(); 258 249 259 250 s = s.DerivePhrase(expandedSymbolIndex, longestProduction); 260 251 } 261 262 return s.Count(); 263 } 264 265 private int CountTerminals(Production p) { 266 return p.Count(s => s is TerminalSymbol); 267 } 268 269 private int CountNonTerminals(Production p) { 270 return p.Count(s => s is NonterminalSymbol); 271 } 272 273 public double EvaluatePhrase(SymbolString s, IRegressionProblemData problemData, bool optimizeConstants) { 252 return s.Count; 253 } 254 255 public double EvaluatePhrase(SymbolList s, IRegressionProblemData problemData, bool optimizeConstants, int iterations) { 274 256 SymbolicExpressionTree tree = ParseSymbolicExpressionTree(s); 275 257 276 return RSquaredEvaluator.Evaluate(problemData, tree, optimizeConstants );258 return RSquaredEvaluator.Evaluate(problemData, tree, optimizeConstants, iterations); 277 259 } 278 260 279 261 #region Parse to SymbolicExpressionTree 280 262 281 public string ToInfixString(Symbol Stringsentence) {263 public string ToInfixString(SymbolList sentence) { 282 264 Debug.Assert(sentence.Any(), "Trying to evaluate empty sentence!"); 283 265 Debug.Assert(sentence.All(s => s is TerminalSymbol), "Trying to evaluate symbol sequence with nonterminalsymbols!"); … … 286 268 } 287 269 288 public SymbolicExpressionTree ParseSymbolicExpressionTree(Symbol Stringsentence) {270 public SymbolicExpressionTree ParseSymbolicExpressionTree(SymbolList sentence) { 289 271 Debug.Assert(sentence.Any(), "Trying to evaluate empty sentence!"); 290 272 -
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; -
branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/SearchDataStructure.cs
r15993 r16026 13 13 14 14 [Storable] 15 public readonly Symbol String SymbolString;15 public readonly SymbolList SymbolList; 16 16 17 17 [Storable] … … 23 23 private SearchNode() { } 24 24 25 public SearchNode(int hash, double priority, double r2, Symbol String symbolString) {25 public SearchNode(int hash, double priority, double r2, SymbolList symbolList) { 26 26 Hash = hash; 27 27 Priority = priority; 28 Symbol String = symbolString;28 SymbolList = symbolList; 29 29 R2 = r2; 30 30 } … … 33 33 Hash = original.Hash; 34 34 Priority = original.Priority; 35 Symbol String = original.SymbolString;35 SymbolList = original.SymbolList; 36 36 R2 = original.R2; 37 37 } -
branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/Sentence.cs
r15974 r16026 55 55 } 56 56 57 public SymbolString DerivePhrase(int nonterminalSymbolIndex, Productionproduction) {57 public SymbolString DerivePhrase(int nonterminalSymbolIndex, SymbolList production) { 58 58 int productionLength = production.Count; 59 59 int newStringLength = Count() + productionLength - 1; -
branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/Symbol.cs
r16019 r16026 2 2 using System.Collections; 3 3 using System.Collections.Generic; 4 using System.Diagnostics; 4 5 using System.Linq; 5 6 using HeuristicLab.Common; … … 102 103 } 103 104 104 [StorableClass] 105 public class Production : DeepCloneable, IList<Symbol> { 106 [Storable] 107 private List<Symbol> symbols; 108 109 public int Count { get { return symbols.Count; } } 110 111 public bool IsReadOnly { 112 get { return false; } 113 } 114 115 public Symbol this[int index] { 116 get { return symbols[index]; } 117 set { symbols[index] = value; } 118 } 119 120 public Production(params Symbol[] symbols) { 121 this.symbols = symbols.ToList(); 122 } 123 124 public Production(IEnumerable<Symbol> symbols) { 125 this.symbols = symbols.ToList(); 126 } 127 128 [StorableConstructor] 129 protected Production(bool deserializing) { } 130 131 protected Production(Production original, Cloner cloner) { 132 symbols = original.symbols.Select(cloner.Clone).ToList(); 133 } 134 135 public override IDeepCloneable Clone(Cloner cloner) { 136 return new Production(this, cloner); 137 } 138 139 public override string ToString() { 140 return string.Join(" ", this); 141 } 142 143 #region IList<Symbol> methods 144 public IEnumerator<Symbol> GetEnumerator() { 145 return symbols.GetEnumerator(); 146 } 147 148 IEnumerator IEnumerable.GetEnumerator() { 149 return GetEnumerator(); 150 } 151 152 public int IndexOf(Symbol item) { 153 return symbols.IndexOf(item); 154 } 155 156 public void Insert(int index, Symbol item) { 157 symbols.Insert(index, item); 158 } 159 160 public void RemoveAt(int index) { 161 symbols.RemoveAt(index); 162 } 163 164 public void Add(Symbol item) { 165 symbols.Add(item); 166 } 167 168 public void Clear() { 169 symbols.Clear(); 170 } 171 172 public bool Contains(Symbol item) { 173 return symbols.Contains(item); 174 } 175 176 public void CopyTo(Symbol[] array, int arrayIndex) { 177 symbols.CopyTo(array, arrayIndex); 178 } 179 180 public void CopyTo(int index, Symbol[] array, int arrayIndex, int count) { 181 symbols.CopyTo(index, array, arrayIndex, count); 182 } 183 184 public bool Remove(Symbol item) { 185 return symbols.Remove(item); 186 } 187 #endregion 188 } 105 189 106 } -
branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/Hashing/Hasher.cs
r15975 r16026 22 22 protected abstract THashType AggregateHashes(Symbol operatorSym, IList<THashType> hashes); 23 23 24 public int CalcHashCode(Symbol Stringsentence) {24 public int CalcHashCode(SymbolList sentence) { 25 25 Debug.Assert(sentence.Any(), "Trying to evaluate empty sentence!"); 26 26 -
branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration.csproj
r15974 r16026 123 123 <Compile Include="GrammarEnumeration\GrammarEnumerationAlgorithm.cs" /> 124 124 <Compile Include="GrammarEnumeration\LruCache.cs" /> 125 <Compile Include="GrammarEnumeration\SymbolList.cs" /> 125 126 <Compile Include="Hashing\Hasher.cs" /> 126 127 <Compile Include="GrammarEnumeration\SearchDataStructure.cs" /> 127 <Compile Include="GrammarEnumeration\Sentence.cs" />128 128 <Compile Include="ProblemInstances\AircraftMaximumLift.cs" /> 129 129 <Compile Include="ProblemInstances\FluidDynamics.cs" /> -
branches/2886_SymRegGrammarEnumeration/Test/GrammarEnumerationTest.cs
r15915 r16026 44 44 } 45 45 46 47 46 private void EvaluateGrammarEnumeration() { 48 47 // Evaluate results … … 55 54 Assert.AreEqual(1.0, ((IRegressionSolution)alg.Results["Best solution (Training)"].Value).TestRSquared, eps, "Test quality too low!"); 56 55 } 57 58 56 59 57 [TestMethod] … … 72 70 TerminalSymbol addSymbol = alg.Grammar.Addition; 73 71 74 Symbol String targetSolution = new SymbolString(new[] {72 SymbolList targetSolution = new SymbolList(new[] { 75 73 constSymbol, varSymbol, varSymbol, varSymbol, mulSymbol, mulSymbol, mulSymbol, 76 74 constSymbol, varSymbol, varSymbol, mulSymbol, mulSymbol, addSymbol, … … 132 130 133 131 // c * sin(c x + c) + c * sin(c * x * x + c * x) + c 134 Symbol String targetSolution = new SymbolString(new[] {132 SymbolList targetSolution = new SymbolList(new[] { 135 133 varSymbol, constSymbol, mulSymbol, constSymbol, addSymbol, sinSymbol, constSymbol, mulSymbol, 136 134 varSymbol, varSymbol, mulSymbol, constSymbol, mulSymbol, varSymbol, constSymbol, mulSymbol, addSymbol, constSymbol, addSymbol, sinSymbol, constSymbol, mulSymbol, addSymbol, … … 165 163 166 164 // c*sin(c*x + c) + c*sin(c*y*y + c) + c 167 Symbol String targetSolution = new SymbolString(new[] {165 SymbolList targetSolution = new SymbolList(new[] { 168 166 xSymbol, constSymbol, mulSymbol, constSymbol, addSymbol, sinSymbol, constSymbol, mulSymbol, 169 167 ySymbol, ySymbol, mulSymbol, constSymbol, mulSymbol, constSymbol, addSymbol, sinSymbol, constSymbol, mulSymbol, addSymbol, … … 222 220 TerminalSymbol logSymbol = alg.Grammar.Log; 223 221 224 Symbol String targetSolution = new SymbolString(new[] {222 SymbolList targetSolution = new SymbolList(new[] { 225 223 varSymbol, constSymbol, mulSymbol, constSymbol, addSymbol, logSymbol, constSymbol, mulSymbol, 226 224 varSymbol, varSymbol, mulSymbol, constSymbol, mulSymbol, constSymbol, addSymbol, logSymbol, constSymbol, mulSymbol, addSymbol, … … 266 264 TerminalSymbol addSymbol = alg.Grammar.Addition; 267 265 268 Symbol String targetSolution = new SymbolString(new[] {266 SymbolList targetSolution = new SymbolList(new[] { 269 267 constSymbol, varSymbol, mulSymbol, constSymbol, addSymbol, alg.Grammar.Sin, 270 268 varSymbol, mulSymbol, … … 303 301 // 30 * x * z * 1/(x*y*y - 10*y*y) 304 302 // --> x z * c * x y * y * c * y y * c * + c + inv c + 305 Symbol String targetSolution = new SymbolString(new[] {303 SymbolList targetSolution = new SymbolList(new[] { 306 304 xSymbol, zSymbol, mulSymbol, constSymbol, mulSymbol, 307 305 xSymbol, ySymbol, mulSymbol, ySymbol, mulSymbol, constSymbol, mulSymbol, … … 337 335 TerminalSymbol addSymbol = alg.Grammar.Addition; 338 336 339 Symbol String targetSolution = new SymbolString(new[] {337 SymbolList targetSolution = new SymbolList(new[] { 340 338 xSymbol, xSymbol, mulSymbol, xSymbol, mulSymbol, xSymbol, mulSymbol, constSymbol, mulSymbol, 341 339 xSymbol, xSymbol, mulSymbol, xSymbol, mulSymbol, constSymbol, mulSymbol, addSymbol, … … 376 374 377 375 // x x mul c mul y y mul c mul add const add inv const mul const add 378 Symbol String targetSolution = new SymbolString(new[] {376 SymbolList targetSolution = new SymbolList(new[] { 379 377 xSymbol, xSymbol, mulSymbol, constSymbol, mulSymbol, 380 378 ySymbol, ySymbol, mulSymbol, constSymbol, mulSymbol, addSymbol, … … 412 410 413 411 // x x * x * const * y y * y * const * + y const * + x const * const + 414 Symbol String targetSolution = new SymbolString(new[] {412 SymbolList targetSolution = new SymbolList(new[] { 415 413 xSymbol, xSymbol, mulSymbol, xSymbol, mulSymbol, constSymbol, mulSymbol, 416 414 ySymbol, ySymbol, mulSymbol, ySymbol, mulSymbol, constSymbol, mulSymbol, addSymbol, -
branches/2886_SymRegGrammarEnumeration/Test/TreeHashingTest.cs
r15849 r16026 27 27 [TestCategory("TreeHashing")] 28 28 public void SimpleEqualityAddition() { 29 Symbol String s1 = new SymbolString(new[] { varA, varB, grammar.Addition, varC, grammar.Addition });30 Symbol String s2 = new SymbolString(new[] { varA, varB, grammar.Addition, varC, grammar.Addition });29 SymbolList s1 = new SymbolList(new[] { varA, varB, grammar.Addition, varC, grammar.Addition }); 30 SymbolList s2 = new SymbolList(new[] { varA, varB, grammar.Addition, varC, grammar.Addition }); 31 31 32 32 int hash1 = grammar.Hasher.CalcHashCode(s1); … … 39 39 [TestCategory("TreeHashing")] 40 40 public void SimpleInequalityAddition() { 41 Symbol String s1 = new SymbolString(new[] { varA, varB, grammar.Addition, varC, grammar.Addition });42 Symbol String s2 = new SymbolString(new[] { varB, varB, grammar.Addition, varB, grammar.Addition });41 SymbolList s1 = new SymbolList(new[] { varA, varB, grammar.Addition, varC, grammar.Addition }); 42 SymbolList s2 = new SymbolList(new[] { varB, varB, grammar.Addition, varB, grammar.Addition }); 43 43 44 44 int hash1 = grammar.Hasher.CalcHashCode(s1); … … 51 51 [TestCategory("TreeHashing")] 52 52 public void CommutativityAddition() { 53 Symbol String s1 = new SymbolString(new[] { varA, varB, grammar.Addition });54 Symbol String s2 = new SymbolString(new[] { varB, varA, grammar.Addition });53 SymbolList s1 = new SymbolList(new[] { varA, varB, grammar.Addition }); 54 SymbolList s2 = new SymbolList(new[] { varB, varA, grammar.Addition }); 55 55 56 56 int hash1 = grammar.Hasher.CalcHashCode(s1); … … 63 63 [TestCategory("TreeHashing")] 64 64 public void AssociativityAddition() { 65 Symbol String s1 = new SymbolString(new[] { varA, varB, grammar.Addition, varA, grammar.Addition });66 Symbol String s2 = new SymbolString(new[] { varA, varB, varA, grammar.Addition, grammar.Addition });65 SymbolList s1 = new SymbolList(new[] { varA, varB, grammar.Addition, varA, grammar.Addition }); 66 SymbolList s2 = new SymbolList(new[] { varA, varB, varA, grammar.Addition, grammar.Addition }); 67 67 68 68 int hash1 = grammar.Hasher.CalcHashCode(s1); … … 75 75 [TestCategory("TreeHashing")] 76 76 public void RepeatedAddition() { 77 Symbol String s1 = new SymbolString(new[] { varA, varA, grammar.Addition, varA, grammar.Addition });78 Symbol String s2 = new SymbolString(new[] { varA });77 SymbolList s1 = new SymbolList(new[] { varA, varA, grammar.Addition, varA, grammar.Addition }); 78 SymbolList s2 = new SymbolList(new[] { varA }); 79 79 80 80 int hash1 = grammar.Hasher.CalcHashCode(s1); … … 87 87 [TestCategory("TreeHashing")] 88 88 public void ComplexInequality() { 89 Symbol String s1 = new SymbolString(new[] { varA, varA, varA, grammar.Multiplication, grammar.Multiplication });90 Symbol String s2 = new SymbolString(new[] { varA, varA, varA, grammar.Multiplication, grammar.Addition });89 SymbolList s1 = new SymbolList(new[] { varA, varA, varA, grammar.Multiplication, grammar.Multiplication }); 90 SymbolList s2 = new SymbolList(new[] { varA, varA, varA, grammar.Multiplication, grammar.Addition }); 91 91 92 92 int hash1 = grammar.Hasher.CalcHashCode(s1); … … 99 99 [TestCategory("TreeHashing")] 100 100 public void NonterminalHashing() { 101 Symbol String s1 = new SymbolString(new Symbol[] { varA, varA, grammar.Expr, grammar.Addition, grammar.Addition });102 Symbol String s2 = new SymbolString(new Symbol[] { varA, grammar.Expr, grammar.Addition });101 SymbolList s1 = new SymbolList(new Symbol[] { varA, varA, grammar.Expr, grammar.Addition, grammar.Addition }); 102 SymbolList s2 = new SymbolList(new Symbol[] { varA, grammar.Expr, grammar.Addition }); 103 103 104 104 int hash1 = grammar.Hasher.CalcHashCode(s1); … … 112 112 public void InverseFactorCancelationSimple() { 113 113 // 1/a * b * a * a 114 Symbol String s1 = new SymbolString(new Symbol[] { varA, grammar.Inv, varB, grammar.Multiplication, varA, grammar.Multiplication, varA, grammar.Multiplication });114 SymbolList s1 = new SymbolList(new Symbol[] { varA, grammar.Inv, varB, grammar.Multiplication, varA, grammar.Multiplication, varA, grammar.Multiplication }); 115 115 // a * b 116 Symbol String s2 = new SymbolString(new Symbol[] { varA, varB, grammar.Multiplication });116 SymbolList s2 = new SymbolList(new Symbol[] { varA, varB, grammar.Multiplication }); 117 117 118 118 int hash1 = grammar.Hasher.CalcHashCode(s1); … … 125 125 [TestCategory("TreeHashing")] 126 126 public void InverseFactorCancelationComplex() { 127 Symbol String s1 = new SymbolString(new Symbol[] { varA, grammar.Sin, varA, varA, grammar.Multiplication, varA, grammar.Addition, grammar.Sin, grammar.Addition });128 Symbol String s2 = new SymbolString(new Symbol[] { varA, varA, varA, grammar.Multiplication, grammar.Addition, grammar.Sin, varA, grammar.Inv, varA, grammar.Sin, varA, grammar.Multiplication, grammar.Multiplication, grammar.Addition });127 SymbolList s1 = new SymbolList(new Symbol[] { varA, grammar.Sin, varA, varA, grammar.Multiplication, varA, grammar.Addition, grammar.Sin, grammar.Addition }); 128 SymbolList s2 = new SymbolList(new Symbol[] { varA, varA, varA, grammar.Multiplication, grammar.Addition, grammar.Sin, varA, grammar.Inv, varA, grammar.Sin, varA, grammar.Multiplication, grammar.Multiplication, grammar.Addition }); 129 129 130 130 int hash1 = grammar.Hasher.CalcHashCode(s1); … … 138 138 [TestCategory("TreeHashing")] 139 139 public void SimpleConst() { 140 Symbol String s1 = new SymbolString(new Symbol[] { c, varA, grammar.Multiplication, c, grammar.Addition});141 Symbol String s2 = new SymbolString(new Symbol[] { c, varA, grammar.Multiplication, c, varA, grammar.Multiplication, grammar.Addition, c, grammar.Addition });140 SymbolList s1 = new SymbolList(new Symbol[] { c, varA, grammar.Multiplication, c, grammar.Addition}); 141 SymbolList s2 = new SymbolList(new Symbol[] { c, varA, grammar.Multiplication, c, varA, grammar.Multiplication, grammar.Addition, c, grammar.Addition }); 142 142 143 143 int hash1 = grammar.Hasher.CalcHashCode(s1); … … 151 151 [TestCategory("TreeHashing")] 152 152 public void CompoundInverseCancellationToSingleInverse() { 153 Symbol String s1 = new SymbolString(new Symbol[] { varA, varB, grammar.Addition, grammar.Inv, grammar.Inv, grammar.Inv });154 Symbol String s2 = new SymbolString(new Symbol[] { varA, varB, grammar.Addition, grammar.Inv });153 SymbolList s1 = new SymbolList(new Symbol[] { varA, varB, grammar.Addition, grammar.Inv, grammar.Inv, grammar.Inv }); 154 SymbolList s2 = new SymbolList(new Symbol[] { varA, varB, grammar.Addition, grammar.Inv }); 155 155 156 156 int hash1 = grammar.CalcHashCode(s1); … … 163 163 [TestCategory("TreeHashing")] 164 164 public void CompoundInverseCancellationToDivisor() { 165 Symbol String s1 = new SymbolString(new Symbol[] { varA, varB, grammar.Addition, grammar.Inv, grammar.Inv });166 Symbol String s2 = new SymbolString(new Symbol[] { varA, varB, grammar.Addition });165 SymbolList s1 = new SymbolList(new Symbol[] { varA, varB, grammar.Addition, grammar.Inv, grammar.Inv }); 166 SymbolList s2 = new SymbolList(new Symbol[] { varA, varB, grammar.Addition }); 167 167 168 168 int hash1 = grammar.CalcHashCode(s1); … … 176 176 public void UncancelableCompoundInverse() { 177 177 // 1 / ( 1/b + sin(a*c) ) 178 Symbol String s1 = new SymbolString(new Symbol[] { varB, grammar.Inv, varA, varC, grammar.Multiplication, grammar.Sin, grammar.Addition, grammar.Inv });178 SymbolList s1 = new SymbolList(new Symbol[] { varB, grammar.Inv, varA, varC, grammar.Multiplication, grammar.Sin, grammar.Addition, grammar.Inv }); 179 179 // b + sin(a*c) 180 Symbol String s2 = new SymbolString(new Symbol[] { varB, varA, varC, grammar.Multiplication, grammar.Sin, grammar.Addition });180 SymbolList s2 = new SymbolList(new Symbol[] { varB, varA, varC, grammar.Multiplication, grammar.Sin, grammar.Addition }); 181 181 182 182 int hash1 = grammar.CalcHashCode(s1);
Note: See TracChangeset
for help on using the changeset viewer.