Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/19/15 20:09:12 (9 years ago)
Author:
gkronber
Message:

#2283: performance tuning and reactivated random-roll-out policy in sequential search

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/SymbolicRegressionPoly10Problem.cs

    r11792 r11799  
    11using System;
     2using System.Collections.Concurrent;
    23using System.Collections.Generic;
     4using System.Collections.Specialized;
    35using System.Linq;
    46using System.Net;
     
    7274
    7375
     76    // most-recently-used caching (with limited capacity) for canonical representations
     77    MostRecentlyUsedCache<string, string> canonicalPhraseCache = new MostRecentlyUsedCache<string, string>(100000);
    7478    // right now only + and * is supported
    75     //private Dictionary<string, string> cache = new Dictionary<string, string>();
    7679    public string CanonicalRepresentation(string phrase) {
    77       string res;
    78       //if (!cache.TryGetValue(phrase, out res)) {
    79       var terms = phrase.Split('+').Select(t => t.Replace("*", ""));
    80       var terminalTerms = terms.Where(t => t.All(ch => grammar.IsTerminal(ch)));
    81       var nonTerminalTerms = terms.Where(t => t.Any(ch => grammar.IsNonTerminal(ch)));
     80      string canonicalPhrase;
     81      if (!canonicalPhraseCache.TryGetValue(phrase, out canonicalPhrase)) {
     82        var terms = phrase.Split('+');
     83        var canonicalTerms = new SortedSet<string>();
     84        // only the last term might contain a NT-symbol. make sure this term is added at the end
     85        for (int i = 0; i < terms.Length - 1; i++) {
     86          canonicalTerms.Add(CanonicalTerm(terms[i]));
     87        }
    8288
    83       res = string.Join("+", terminalTerms.Select(term => CanonicalTerm(term)).OrderBy(term => term).Concat(nonTerminalTerms.Select(term => CanonicalTerm(term))));
    84       //cache[phrase] = res;
    85       //}
    86       return res;
     89        var sb = new StringBuilder(phrase.Length);
     90        foreach (var t in canonicalTerms)
     91          sb.Append(t).Append('+');
     92
     93        sb.Append(CanonicalTerm(terms[terms.Length - 1]));
     94        sb.Append(phrase.Length - sb.Length);
     95        canonicalPhrase = sb.ToString();
     96        canonicalPhraseCache.Add(phrase, canonicalPhrase);
     97      }
     98      return canonicalPhrase;
    8799    }
    88100
     101    // cache the canonical form of terms for performance reasons
     102    private Dictionary<string, string> canonicalTermDictionary = new Dictionary<string, string>();
    89103    private string CanonicalTerm(string term) {
    90       return string.Join("", term.OrderByDescending(ch => (byte)ch)); // we want to have the up-case characters last
     104      string canonicalTerm;
     105      if (!canonicalTermDictionary.TryGetValue(term, out canonicalTerm)) {
     106        // add
     107        var chars = term.ToCharArray();
     108        Array.Sort(chars);
     109        var sb = new StringBuilder(chars.Length);
     110        // we want to have the up-case characters last
     111        for (int i = chars.Length - 1; i >= 0; i--) {
     112          if (chars[i] != '*') sb.Append(chars[i]);
     113        }
     114        canonicalTerm = sb.ToString();
     115        canonicalTermDictionary.Add(term, canonicalTerm);
     116      }
     117      return canonicalTerm;
    91118    }
    92119  }
Note: See TracChangeset for help on using the changeset viewer.