Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/06/18 22:11:56 (7 years ago)
Author:
lkammere
Message:

#2886: Overwrite long sentences when a shorter one with same hash was found.

File:
1 edited

Legend:

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

    r15725 r15726  
    5757    public SymbolString BestTestSentence;
    5858
     59    public List<Tuple<SymbolString, int>> DistinctGenerated;
     60    public List<Tuple<SymbolString, int>> AllGenerated;
    5961    #endregion
    6062
     
    8890      BestTrainingSentence = null;
    8991      BestTrainingSentence = null;
    90 
    91       List<Tuple<SymbolString, int>> allGenerated = new List<Tuple<SymbolString, int>>();
    92       List<SymbolString> distinctGenerated = new List<SymbolString>();
    93 
    94       HashSet<int> evaluatedHashes = new HashSet<int>();
     92      AllGenerated = new List<Tuple<SymbolString, int>>();
     93      DistinctGenerated = new List<Tuple<SymbolString, int>>();
     94
     95      Dictionary<int, SymbolString> evaluatedHashes = new Dictionary<int, SymbolString>();
    9596
    9697      Grammar = new Grammar(Problem.ProblemData.AllowedInputVariables.ToArray());
     
    105106
    106107        if (currSymbolString.IsSentence()) {
    107           allGenerated.Add(new Tuple<SymbolString, int>(currSymbolString, Grammar.CalcHashCode(currSymbolString)));
    108 
    109           if (evaluatedHashes.Add(Grammar.CalcHashCode(currSymbolString))) {
     108          AllGenerated.Add(new Tuple<SymbolString, int>(currSymbolString, Grammar.CalcHashCode(currSymbolString)));
     109
     110          int currSymbolStringHash = Grammar.CalcHashCode(currSymbolString);
     111          if (!evaluatedHashes.ContainsKey(currSymbolStringHash)
     112              || evaluatedHashes[currSymbolStringHash].Count > currSymbolString.Count) {
     113            evaluatedHashes[currSymbolStringHash] = currSymbolString;
     114
     115            DistinctGenerated.Add(new Tuple<SymbolString, int>(currSymbolString, Grammar.CalcHashCode(currSymbolString)));
    110116            EvaluateSentence(currSymbolString);
    111             distinctGenerated.Add(Grammar.PostfixToInfixParser(currSymbolString));
    112117          }
    113 
    114           UpdateView(allGenerated, distinctGenerated);
     118          UpdateView(AllGenerated, DistinctGenerated);
    115119
    116120        } else {
     
    131135      }
    132136
    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();
     137      UpdateView(AllGenerated, DistinctGenerated, force: true);
     138
     139      string[,] sentences = new string[AllGenerated.Count, 3];
     140      for (int i = 0; i < AllGenerated.Count; i++) {
     141        sentences[i, 0] = AllGenerated[i].Item1.ToString();
     142        sentences[i, 1] = Grammar.PostfixToInfixParser(AllGenerated[i].Item1).ToString();
     143        sentences[i, 2] = AllGenerated[i].Item2.ToString();
    140144      }
    141145      Results.Add(new Result("All generated sentences", new StringMatrix(sentences)));
    142146
    143       StringArray distinctSentences = new StringArray(distinctGenerated.Select(r => r.ToString()).ToArray());
    144       Results.Add(new Result("Distinct generated sentences", distinctSentences));
    145     }
    146 
    147 
    148     private void UpdateView(List<Tuple<SymbolString, int>> allGenerated, List<SymbolString> distinctGenerated, bool force = false) {
     147      string[,] distinctSentences = new string[DistinctGenerated.Count, 3];
     148      for (int i = 0; i < DistinctGenerated.Count; i++) {
     149        distinctSentences[i, 0] = DistinctGenerated[i].Item1.ToString();
     150        distinctSentences[i, 1] = Grammar.PostfixToInfixParser(DistinctGenerated[i].Item1).ToString();
     151        distinctSentences[i, 2] = DistinctGenerated[i].Item2.ToString();
     152      }
     153      Results.Add(new Result("Distinct generated sentences", new StringMatrix(distinctSentences)));
     154    }
     155
     156
     157    private void UpdateView(List<Tuple<SymbolString, int>> allGenerated,
     158        List<Tuple<SymbolString, int>> distinctGenerated, bool force = false) {
    149159      int generatedSolutions = allGenerated.Count;
    150160      int distinctSolutions = distinctGenerated.Count;
     
    183193      } else {
    184194        IRegressionSolution currBestTrainingSolution = (IRegressionSolution)currBestTrainingSolutionResult.Value;
    185         if (currBestTrainingSolution.TrainingRSquared < newSolution.TrainingRSquared) {
     195        if (currBestTrainingSolution.TrainingRSquared <= newSolution.TrainingRSquared) {
    186196          BestTrainingSentence = symbolString;
    187197          currBestTrainingSolutionResult.Value = newSolution;
     
    190200
    191201        IRegressionSolution currBestTestSolution = (IRegressionSolution)currBestTestSolutionResult.Value;
    192         if (currBestTestSolution.TestRSquared < newSolution.TestRSquared) {
     202        if (currBestTestSolution.TestRSquared <= newSolution.TestRSquared) {
    193203          BestTestSentence = symbolString;
    194204          currBestTestSolutionResult.Value = newSolution;
Note: See TracChangeset for help on using the changeset viewer.