Free cookie consent management tool by TermsFeed Policy Generator

Changeset 16026


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

#2886:

  • replace functionally-overlapping classes Production and SymbolString with a single class SymbolList
  • refactor methods from Grammar class as methods and properties of SymbolList
  • add parameter for the number of constant optimization iterations
  • refactor code
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  
    11using System;
    22using System.Diagnostics;
    3 using HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration.GrammarEnumeration;
    43using HeuristicLab.Analysis;
    54using HeuristicLab.Common;
     
    5150    private void AlgorithmOnDistinctSentenceGenerated(object sender, PhraseAddedEventArgs phraseAddedEventArgs) {
    5251      GrammarEnumerationAlgorithm algorithm = (GrammarEnumerationAlgorithm)sender;
    53       EvaluateSentence(algorithm, phraseAddedEventArgs.NewPhrase, algorithm.OptimizeConstants);
     52      EvaluateSentence(algorithm, phraseAddedEventArgs.NewPhrase, algorithm.OptimizeConstants, algorithm.ConstantOptimizationIterations);
    5453    }
    5554
     
    6968    }
    7069
    71     private void EvaluateSentence(GrammarEnumerationAlgorithm algorithm, SymbolString symbolString, bool optimizeConstants) {
     70    private void EvaluateSentence(GrammarEnumerationAlgorithm algorithm, SymbolList sentence, bool optimizeConstants, int maxIterations) {
    7271      var results = algorithm.Results;
    7372      var grammar = algorithm.Grammar;
    7473      var problemData = algorithm.Problem.ProblemData;
    7574
    76       SymbolicExpressionTree tree = algorithm.Grammar.ParseSymbolicExpressionTree(symbolString);
     75      SymbolicExpressionTree tree = algorithm.Grammar.ParseSymbolicExpressionTree(sentence);
    7776      Debug.Assert(SymbolicRegressionConstantOptimizationEvaluator.CanOptimizeConstants(tree));
    7877
    79       double r2 = Evaluate(problemData, tree, optimizeConstants);
     78      double r2 = Evaluate(problemData, tree, optimizeConstants, maxIterations);
    8079      double bestR2 = results.ContainsKey(BestTrainingQualityResultName) ? GetValue<double>(results[BestTrainingQualityResultName].Value) : 0.0;
    8180      if (r2 < bestR2)
     
    8382
    8483      var bestComplexity = results.ContainsKey(BestComplexityResultName) ? GetValue<int>(results[BestComplexityResultName].Value) : int.MaxValue;
    85       var complexity = grammar.GetComplexity(symbolString);
     84      var complexity = sentence.Complexity;
    8685
    8786      if (algorithm.BestTrainingSentence == null || r2 > bestR2 || (r2.IsAlmost(bestR2) && complexity < bestComplexity)) {
    88         algorithm.BestTrainingSentence = symbolString;
     87        algorithm.BestTrainingSentence = sentence;
    8988
    9089        var model = new SymbolicRegressionModel(problemData.TargetVariable, tree, expressionTreeLinearInterpreter);
     
    110109        dt = (DataTable)results[BestSolutions].Value;
    111110        dt.Rows["Quality"].Values.Add(r2);
    112         dt.Rows["Relative Length"].Values.Add((double)symbolString.Count() / algorithm.MaxSentenceLength);
     111        dt.Rows["Relative Length"].Values.Add((double)sentence.Count);
    113112        dt.Rows["Complexity"].Values.Add(complexity);
    114113        dt.Rows["Timestamp"].Values.Add(algorithm.ExecutionTime.TotalMilliseconds / 1000d);
     
    116115    }
    117116
    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) {
    119118      double r2;
    120119
     
    127126          problemData.TrainingIndices,
    128127          applyLinearScaling: true,
    129           maxIterations: 10,
     128          maxIterations: maxIterations,
    130129          updateVariableWeights: false,
    131130          updateConstantsInTree: true);
  • branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/Analysis/SearchGraphVisualizer.cs

    r15949 r16026  
    5555      try {
    5656        var alg = (GrammarEnumerationAlgorithm)sender;
    57         var phrase0 = new SymbolString(new[] { alg.Grammar.StartSymbol });
     57        var phrase0 = new SymbolList(new[] { alg.Grammar.StartSymbol });
    5858        var phrase0Hash = alg.Grammar.Hasher.CalcHashCode(phrase0);
    5959
  • branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/Analysis/SentenceLogger.cs

    r15982 r16026  
    8989      distinctSentencesFileTrace.WriteLine(ToCsvLine(
    9090        ((uint)phraseAddedEventArgs.NewHash).ToString("D10"),
    91         phraseAddedEventArgs.NewPhrase.Count().ToString("D3"),
     91        phraseAddedEventArgs.NewPhrase.Count.ToString("D3"),
    9292        //phraseAddedEventArgs.NewPhrase.ToString(),
    9393        ((GrammarEnumerationAlgorithm)sender).Grammar.ToInfixString(phraseAddedEventArgs.NewPhrase)));
     
    9999      allSentencesFileTrace.WriteLine(ToCsvLine(
    100100        ((uint)phraseAddedEventArgs.NewHash).ToString("D10"),
    101         phraseAddedEventArgs.NewPhrase.Count().ToString("D3"),
     101        phraseAddedEventArgs.NewPhrase.Count.ToString("D3"),
    102102        //phraseAddedEventArgs.NewPhrase.ToString(),
    103103        ((GrammarEnumerationAlgorithm)sender).Grammar.ToInfixString(phraseAddedEventArgs.NewPhrase)));
  • branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/Grammar.cs

    r15993 r16026  
    2828    #region Symbols
    2929
    30     public IReadOnlyDictionary<Symbol, IReadOnlyList<Production>> Productions { get; private set; }
     30    public IReadOnlyDictionary<Symbol, IReadOnlyList<SymbolList>> Productions { get; private set; }
    3131
    3232    public NonterminalSymbol Var;
     
    8585      infixExpressionFormatter = cloner.Clone(original.infixExpressionFormatter);
    8686
    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());
    8888      VarTerminals = original.VarTerminals.Select(cloner.Clone).ToList();
    8989
     
    144144      StartSymbol = Expr;
    145145
    146       Dictionary<Symbol, IReadOnlyList<Production>> productions = new Dictionary<Symbol, IReadOnlyList<Production>>();
     146      Dictionary<Symbol, IReadOnlyList<SymbolList>> productions = new Dictionary<Symbol, IReadOnlyList<SymbolList>>();
    147147
    148148      // Map each variable to a separate production rule of the "Var" nonterminal symbol.
    149149      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();
    151151
    152152      // Expression Grammar Rules
    153       var exprProductions = new List<Production>();
     153      var exprProductions = new List<SymbolList>();
    154154      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));
    158158      productions[Expr] = exprProductions.ToArray();
    159159
    160160      // Term Grammar Rules
    161       var termProductions = new List<Production>();
     161      var termProductions = new List<SymbolList>();
    162162      if (includedRules.Contains(GrammarRule.MultipleFactors))
    163         termProductions.Add(new Production(Factor, Term, Multiplication));
     163        termProductions.Add(new SymbolList(Factor, Term, Multiplication));
    164164      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));
    167167      productions[Term] = termProductions.ToArray();
    168168
    169169      // 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));
    172172      if (includedRules.Contains(GrammarRule.Logarithm))
    173         factorProductions.Add(new Production(LogFactor));
     173        factorProductions.Add(new SymbolList(LogFactor));
    174174      if (includedRules.Contains(GrammarRule.Exponentiation))
    175         factorProductions.Add(new Production(ExpFactor));
     175        factorProductions.Add(new SymbolList(ExpFactor));
    176176      if (includedRules.Contains(GrammarRule.Sine))
    177         factorProductions.Add(new Production(SinFactor));
     177        factorProductions.Add(new SymbolList(SinFactor));
    178178      productions[Factor] = factorProductions.ToArray();
    179179
    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) };
    183183
    184184      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)
    187187      };
    188188
    189189      productions[SimpleTerm] = new[] {
    190         new Production(Var, SimpleTerm, Multiplication),
    191         new Production(Var)
     190        new SymbolList(Var, SimpleTerm, Multiplication),
     191        new SymbolList(Var)
    192192      };
    193193
    194194      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)
    197197      };
    198198
    199199      productions[InvTerm] = new[] {
    200         new Production(Factor, InvTerm, Multiplication),
    201         new Production(Factor)
     200        new SymbolList(Factor, InvTerm, Multiplication),
     201        new SymbolList(Factor)
    202202      };
    203203
     
    234234    }
    235235
    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 
    245236    // returns the maximum achievable sentence length below the maximum complexity
    246237    public int GetMaxSentenceLength(int maxComplexity) {
    247       SymbolString 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) {
    250241        int expandedSymbolIndex = s.NextNonterminalIndex();
    251242        NonterminalSymbol expandedSymbol = (NonterminalSymbol)s[expandedSymbolIndex];
    252243
    253244        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)
    257248          .First();
    258249
    259250        s = s.DerivePhrase(expandedSymbolIndex, longestProduction);
    260251      }
    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) {
    274256      SymbolicExpressionTree tree = ParseSymbolicExpressionTree(s);
    275257
    276       return RSquaredEvaluator.Evaluate(problemData, tree, optimizeConstants);
     258      return RSquaredEvaluator.Evaluate(problemData, tree, optimizeConstants, iterations);
    277259    }
    278260
    279261    #region Parse to SymbolicExpressionTree
    280262
    281     public string ToInfixString(SymbolString sentence) {
     263    public string ToInfixString(SymbolList sentence) {
    282264      Debug.Assert(sentence.Any(), "Trying to evaluate empty sentence!");
    283265      Debug.Assert(sentence.All(s => s is TerminalSymbol), "Trying to evaluate symbol sequence with nonterminalsymbols!");
     
    286268    }
    287269
    288     public SymbolicExpressionTree ParseSymbolicExpressionTree(SymbolString sentence) {
     270    public SymbolicExpressionTree ParseSymbolicExpressionTree(SymbolList sentence) {
    289271      Debug.Assert(sentence.Any(), "Trying to evaluate empty sentence!");
    290272
  • branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/GrammarEnumerationAlgorithm.cs

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

    r15993 r16026  
    1313
    1414    [Storable]
    15     public readonly SymbolString SymbolString;
     15    public readonly SymbolList SymbolList;
    1616
    1717    [Storable]
     
    2323    private SearchNode() { }
    2424
    25     public SearchNode(int hash, double priority, double r2, SymbolString symbolString) {
     25    public SearchNode(int hash, double priority, double r2, SymbolList symbolList) {
    2626      Hash = hash;
    2727      Priority = priority;
    28       SymbolString = symbolString;
     28      SymbolList = symbolList;
    2929      R2 = r2;
    3030    }
     
    3333      Hash = original.Hash;
    3434      Priority = original.Priority;
    35       SymbolString = original.SymbolString;
     35      SymbolList = original.SymbolList;
    3636      R2 = original.R2;
    3737    }
  • branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/Sentence.cs

    r15974 r16026  
    5555    }
    5656
    57     public SymbolString DerivePhrase(int nonterminalSymbolIndex, Production production) {
     57    public SymbolString DerivePhrase(int nonterminalSymbolIndex, SymbolList production) {
    5858      int productionLength = production.Count;
    5959      int newStringLength = Count() + productionLength - 1;
  • branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/Symbol.cs

    r16019 r16026  
    22using System.Collections;
    33using System.Collections.Generic;
     4using System.Diagnostics;
    45using System.Linq;
    56using HeuristicLab.Common;
     
    102103  }
    103104
    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 
    189106}
  • branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/Hashing/Hasher.cs

    r15975 r16026  
    2222    protected abstract THashType AggregateHashes(Symbol operatorSym, IList<THashType> hashes);
    2323
    24     public int CalcHashCode(SymbolString sentence) {
     24    public int CalcHashCode(SymbolList sentence) {
    2525      Debug.Assert(sentence.Any(), "Trying to evaluate empty sentence!");
    2626
  • branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration.csproj

    r15974 r16026  
    123123    <Compile Include="GrammarEnumeration\GrammarEnumerationAlgorithm.cs" />
    124124    <Compile Include="GrammarEnumeration\LruCache.cs" />
     125    <Compile Include="GrammarEnumeration\SymbolList.cs" />
    125126    <Compile Include="Hashing\Hasher.cs" />
    126127    <Compile Include="GrammarEnumeration\SearchDataStructure.cs" />
    127     <Compile Include="GrammarEnumeration\Sentence.cs" />
    128128    <Compile Include="ProblemInstances\AircraftMaximumLift.cs" />
    129129    <Compile Include="ProblemInstances\FluidDynamics.cs" />
  • branches/2886_SymRegGrammarEnumeration/Test/GrammarEnumerationTest.cs

    r15915 r16026  
    4444    }
    4545
    46 
    4746    private void EvaluateGrammarEnumeration() {
    4847      // Evaluate results
     
    5554      Assert.AreEqual(1.0, ((IRegressionSolution)alg.Results["Best solution (Training)"].Value).TestRSquared, eps, "Test quality too low!");
    5655    }
    57 
    5856
    5957    [TestMethod]
     
    7270      TerminalSymbol addSymbol = alg.Grammar.Addition;
    7371
    74       SymbolString targetSolution = new SymbolString(new[] {
     72      SymbolList targetSolution = new SymbolList(new[] {
    7573        constSymbol, varSymbol, varSymbol, varSymbol, mulSymbol, mulSymbol, mulSymbol,
    7674        constSymbol, varSymbol, varSymbol, mulSymbol, mulSymbol, addSymbol,
     
    132130
    133131      // c * sin(c x + c) + c * sin(c * x * x + c * x) + c
    134       SymbolString targetSolution = new SymbolString(new[] {
     132      SymbolList targetSolution = new SymbolList(new[] {
    135133        varSymbol, constSymbol, mulSymbol, constSymbol, addSymbol, sinSymbol, constSymbol, mulSymbol,
    136134        varSymbol, varSymbol, mulSymbol, constSymbol, mulSymbol, varSymbol, constSymbol, mulSymbol, addSymbol, constSymbol, addSymbol, sinSymbol, constSymbol, mulSymbol, addSymbol,
     
    165163
    166164      // c*sin(c*x + c) + c*sin(c*y*y + c) + c
    167       SymbolString targetSolution = new SymbolString(new[] {
     165      SymbolList targetSolution = new SymbolList(new[] {
    168166        xSymbol, constSymbol, mulSymbol, constSymbol, addSymbol, sinSymbol, constSymbol, mulSymbol,
    169167        ySymbol, ySymbol, mulSymbol, constSymbol, mulSymbol, constSymbol, addSymbol, sinSymbol, constSymbol, mulSymbol, addSymbol,
     
    222220      TerminalSymbol logSymbol = alg.Grammar.Log;
    223221
    224       SymbolString targetSolution = new SymbolString(new[] {
     222      SymbolList targetSolution = new SymbolList(new[] {
    225223        varSymbol, constSymbol, mulSymbol, constSymbol, addSymbol, logSymbol, constSymbol, mulSymbol,
    226224        varSymbol, varSymbol, mulSymbol, constSymbol, mulSymbol, constSymbol, addSymbol, logSymbol, constSymbol, mulSymbol, addSymbol,
     
    266264      TerminalSymbol addSymbol = alg.Grammar.Addition;
    267265
    268       SymbolString targetSolution = new SymbolString(new[] {
     266      SymbolList targetSolution = new SymbolList(new[] {
    269267        constSymbol, varSymbol, mulSymbol, constSymbol, addSymbol, alg.Grammar.Sin,
    270268        varSymbol, mulSymbol,
     
    303301      // 30 * x * z * 1/(x*y*y - 10*y*y)
    304302      // --> x z * c *     x y * y * c *  y y * c * + c + inv c +
    305       SymbolString targetSolution = new SymbolString(new[] {
     303      SymbolList targetSolution = new SymbolList(new[] {
    306304        xSymbol, zSymbol, mulSymbol, constSymbol, mulSymbol,
    307305        xSymbol, ySymbol, mulSymbol, ySymbol, mulSymbol, constSymbol, mulSymbol,
     
    337335      TerminalSymbol addSymbol = alg.Grammar.Addition;
    338336
    339       SymbolString targetSolution = new SymbolString(new[] {
     337      SymbolList targetSolution = new SymbolList(new[] {
    340338        xSymbol, xSymbol, mulSymbol, xSymbol, mulSymbol, xSymbol, mulSymbol, constSymbol, mulSymbol,
    341339        xSymbol, xSymbol, mulSymbol, xSymbol, mulSymbol, constSymbol, mulSymbol, addSymbol,
     
    376374
    377375      // x x mul c mul y y mul c mul add const add inv const mul const add
    378       SymbolString targetSolution = new SymbolString(new[] {
     376      SymbolList targetSolution = new SymbolList(new[] {
    379377        xSymbol, xSymbol, mulSymbol, constSymbol, mulSymbol,
    380378        ySymbol, ySymbol, mulSymbol, constSymbol, mulSymbol, addSymbol,
     
    412410
    413411      // x x * x * const * y y * y * const * + y const * + x const * const +
    414       SymbolString targetSolution = new SymbolString(new[] {
     412      SymbolList targetSolution = new SymbolList(new[] {
    415413        xSymbol, xSymbol, mulSymbol, xSymbol, mulSymbol, constSymbol, mulSymbol,
    416414        ySymbol, ySymbol, mulSymbol, ySymbol, mulSymbol, constSymbol, mulSymbol, addSymbol,
  • branches/2886_SymRegGrammarEnumeration/Test/TreeHashingTest.cs

    r15849 r16026  
    2727    [TestCategory("TreeHashing")]
    2828    public void SimpleEqualityAddition() {
    29       SymbolString s1 = new SymbolString(new[] { varA, varB, grammar.Addition, varC, grammar.Addition });
    30       SymbolString 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 });
    3131
    3232      int hash1 = grammar.Hasher.CalcHashCode(s1);
     
    3939    [TestCategory("TreeHashing")]
    4040    public void SimpleInequalityAddition() {
    41       SymbolString s1 = new SymbolString(new[] { varA, varB, grammar.Addition, varC, grammar.Addition });
    42       SymbolString 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 });
    4343
    4444      int hash1 = grammar.Hasher.CalcHashCode(s1);
     
    5151    [TestCategory("TreeHashing")]
    5252    public void CommutativityAddition() {
    53       SymbolString s1 = new SymbolString(new[] { varA, varB, grammar.Addition });
    54       SymbolString 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 });
    5555
    5656      int hash1 = grammar.Hasher.CalcHashCode(s1);
     
    6363    [TestCategory("TreeHashing")]
    6464    public void AssociativityAddition() {
    65       SymbolString s1 = new SymbolString(new[] { varA, varB, grammar.Addition, varA, grammar.Addition });
    66       SymbolString 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 });
    6767
    6868      int hash1 = grammar.Hasher.CalcHashCode(s1);
     
    7575    [TestCategory("TreeHashing")]
    7676    public void RepeatedAddition() {
    77       SymbolString s1 = new SymbolString(new[] { varA, varA, grammar.Addition, varA, grammar.Addition });
    78       SymbolString 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 });
    7979
    8080      int hash1 = grammar.Hasher.CalcHashCode(s1);
     
    8787    [TestCategory("TreeHashing")]
    8888    public void ComplexInequality() {
    89       SymbolString s1 = new SymbolString(new[] { varA, varA, varA, grammar.Multiplication, grammar.Multiplication });
    90       SymbolString 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 });
    9191
    9292      int hash1 = grammar.Hasher.CalcHashCode(s1);
     
    9999    [TestCategory("TreeHashing")]
    100100    public void NonterminalHashing() {
    101       SymbolString s1 = new SymbolString(new Symbol[] { varA, varA, grammar.Expr, grammar.Addition, grammar.Addition });
    102       SymbolString 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 });
    103103
    104104      int hash1 = grammar.Hasher.CalcHashCode(s1);
     
    112112    public void InverseFactorCancelationSimple() {
    113113      // 1/a * b * a * a
    114       SymbolString 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 });
    115115      // a * b
    116       SymbolString s2 = new SymbolString(new Symbol[] { varA, varB, grammar.Multiplication });
     116      SymbolList s2 = new SymbolList(new Symbol[] { varA, varB, grammar.Multiplication });
    117117
    118118      int hash1 = grammar.Hasher.CalcHashCode(s1);
     
    125125    [TestCategory("TreeHashing")]
    126126    public void InverseFactorCancelationComplex() {
    127       SymbolString s1 = new SymbolString(new Symbol[] { varA, grammar.Sin, varA, varA, grammar.Multiplication, varA, grammar.Addition, grammar.Sin, grammar.Addition });
    128       SymbolString 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 });
    129129
    130130      int hash1 = grammar.Hasher.CalcHashCode(s1);
     
    138138    [TestCategory("TreeHashing")]
    139139    public void SimpleConst() {
    140       SymbolString s1 = new SymbolString(new Symbol[] { c, varA, grammar.Multiplication, c, grammar.Addition});
    141       SymbolString 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 });
    142142
    143143      int hash1 = grammar.Hasher.CalcHashCode(s1);
     
    151151    [TestCategory("TreeHashing")]
    152152    public void CompoundInverseCancellationToSingleInverse() {
    153       SymbolString s1 = new SymbolString(new Symbol[] { varA, varB, grammar.Addition, grammar.Inv, grammar.Inv, grammar.Inv });
    154       SymbolString 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 });
    155155
    156156      int hash1 = grammar.CalcHashCode(s1);
     
    163163    [TestCategory("TreeHashing")]
    164164    public void CompoundInverseCancellationToDivisor() {
    165       SymbolString s1 = new SymbolString(new Symbol[] { varA, varB, grammar.Addition, grammar.Inv, grammar.Inv });
    166       SymbolString 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 });
    167167
    168168      int hash1 = grammar.CalcHashCode(s1);
     
    176176    public void UncancelableCompoundInverse() {
    177177      // 1 / ( 1/b + sin(a*c) )
    178       SymbolString 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 });
    179179      // b + sin(a*c)
    180       SymbolString 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 });
    181181
    182182      int hash1 = grammar.CalcHashCode(s1);
Note: See TracChangeset for help on using the changeset viewer.