Ignore:
Timestamp:
02/06/18 16:21:16 (3 years ago)
Author:
lkammere
Message:

#2886: Refactor tree hash function.

File:
1 edited

Legend:

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

    r15724 r15725  
    1 using System.Collections.Generic;
     1using System;
     2using System.Collections.Generic;
    23using System.Linq;
    34using System.Threading;
     
    2829    private readonly string UseMemoizationParameterName = "Use Memoization?";
    2930
    30 
    3131    #region properties
    3232    protected IValueParameter<IntValue> MaxTreeSizeParameter {
     
    8989      BestTrainingSentence = null;
    9090
    91       List<SymbolString> allGenerated = new List<SymbolString>();
     91      List<Tuple<SymbolString, int>> allGenerated = new List<Tuple<SymbolString, int>>();
    9292      List<SymbolString> distinctGenerated = new List<SymbolString>();
    93 
    94       int expansions = 0;
    9593
    9694      HashSet<int> evaluatedHashes = new HashSet<int>();
     
    107105
    108106        if (currSymbolString.IsSentence()) {
    109           allGenerated.Add(Grammar.PostfixToInfixParser(currSymbolString));
    110 
    111           //if (evaluatedHashes.Add(grammar.CalcHashCode(currSymbolString))) {
    112           EvaluateSentence(currSymbolString);
    113           distinctGenerated.Add(Grammar.PostfixToInfixParser(currSymbolString));
    114           //}
    115 
    116           UpdateView(allGenerated, distinctGenerated, expansions);
     107          allGenerated.Add(new Tuple<SymbolString, int>(currSymbolString, Grammar.CalcHashCode(currSymbolString)));
     108
     109          if (evaluatedHashes.Add(Grammar.CalcHashCode(currSymbolString))) {
     110            EvaluateSentence(currSymbolString);
     111            distinctGenerated.Add(Grammar.PostfixToInfixParser(currSymbolString));
     112          }
     113
     114          UpdateView(allGenerated, distinctGenerated);
    117115
    118116        } else {
     
    133131      }
    134132
    135       UpdateView(allGenerated, distinctGenerated, expansions, force: true);
    136 
    137       StringArray sentences = new StringArray(allGenerated.Select(r => r.ToString()).ToArray());
    138       Results.Add(new Result("All generated sentences", sentences));
     133      UpdateView(allGenerated, distinctGenerated, force: true);
     134
     135      string[,] sentences = new string[allGenerated.Count, 3];
     136      for (int i = 0; i < allGenerated.Count; i++) {
     137        sentences[i, 0] = allGenerated[i].Item1.ToString();
     138        sentences[i, 1] = Grammar.PostfixToInfixParser(allGenerated[i].Item1).ToString();
     139        sentences[i, 2] = allGenerated[i].Item2.ToString();
     140      }
     141      Results.Add(new Result("All generated sentences", new StringMatrix(sentences)));
     142
    139143      StringArray distinctSentences = new StringArray(distinctGenerated.Select(r => r.ToString()).ToArray());
    140144      Results.Add(new Result("Distinct generated sentences", distinctSentences));
     
    142146
    143147
    144     private void UpdateView(List<SymbolString> allGenerated, List<SymbolString> distinctGenerated, int expansions, bool force = false) {
     148    private void UpdateView(List<Tuple<SymbolString, int>> allGenerated, List<SymbolString> distinctGenerated, bool force = false) {
    145149      int generatedSolutions = allGenerated.Count;
    146150      int distinctSolutions = distinctGenerated.Count;
     
    150154        Results.AddOrUpdateResult("Distinct Solutions", new IntValue(distinctSolutions));
    151155
    152         DoubleValue averageTreeLength = new DoubleValue(allGenerated.Select(r => r.Count).Average());
     156        DoubleValue averageTreeLength = new DoubleValue(allGenerated.Select(r => r.Item1.Count).Average());
    153157        Results.AddOrUpdateResult("Average Tree Length of Solutions", averageTreeLength);
    154 
    155         IntValue expansionsValue = new IntValue(expansions);
    156         Results.AddOrUpdateResult("Expansions", expansionsValue);
    157158      }
    158159    }
Note: See TracChangeset for help on using the changeset viewer.