Free cookie consent management tool by TermsFeed Policy Generator

Changeset 9402


Ignore:
Timestamp:
04/29/13 14:33:43 (11 years ago)
Author:
gkronber
Message:

#2037 fixed a bug in the caching for minimum tree lengths and depths in the grammar

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionGrammarBase.cs

    r7660 r9402  
    387387    private readonly Dictionary<string, int> cachedMinExpressionLength;
    388388    public int GetMinimumExpressionLength(ISymbol symbol) {
     389      int res;
     390      if (cachedMinExpressionLength.TryGetValue(symbol.Name, out res))
     391        return res;
     392
     393      res = GetMinimumExpressionLengthRec(symbol);
     394      foreach (var entry in cachedMinExpressionLength.Where(e => e.Value >= int.MaxValue).ToList()) {
     395        if (entry.Key != symbol.Name) cachedMinExpressionLength.Remove(entry.Key);
     396      }
     397      return res;
     398    }
     399
     400    public int GetMinimumExpressionLengthRec(ISymbol symbol) {
    389401      int temp;
    390402      if (!cachedMinExpressionLength.TryGetValue(symbol.Name, out temp)) {
     
    393405                                              let minForSlot = (long)(from s in GetAllowedChildSymbols(symbol, argIndex)
    394406                                                                      where s.InitialFrequency > 0.0
    395                                                                       select GetMinimumExpressionLength(s)).DefaultIfEmpty(0).Min()
     407                                                                      select GetMinimumExpressionLengthRec(s)).DefaultIfEmpty(0).Min()
    396408                                              select minForSlot).DefaultIfEmpty(0).Sum();
    397409
     
    422434    private readonly Dictionary<string, int> cachedMinExpressionDepth;
    423435    public int GetMinimumExpressionDepth(ISymbol symbol) {
     436      int res;
     437      if (cachedMinExpressionDepth.TryGetValue(symbol.Name, out res))
     438        return res;
     439
     440      res = GetMinimumExpressionDepthRec(symbol);
     441      foreach (var entry in cachedMinExpressionDepth.Where(e => e.Value >= int.MaxValue).ToList()) {
     442        if (entry.Key != symbol.Name) cachedMinExpressionDepth.Remove(entry.Key);
     443      }
     444      return res;
     445    }
     446    private int GetMinimumExpressionDepthRec(ISymbol symbol) {
    424447      int temp;
    425448      if (!cachedMinExpressionDepth.TryGetValue(symbol.Name, out temp)) {
     
    428451                             let minForSlot = (long)(from s in GetAllowedChildSymbols(symbol, argIndex)
    429452                                                     where s.InitialFrequency > 0.0
    430                                                      select GetMinimumExpressionDepth(s)).DefaultIfEmpty(0).Min()
     453                                                     select GetMinimumExpressionDepthRec(s)).DefaultIfEmpty(0).Min()
    431454                             select minForSlot).DefaultIfEmpty(0).Max();
    432455        cachedMinExpressionDepth[symbol.Name] = (int)Math.Min(minDepth, int.MaxValue);
Note: See TracChangeset for help on using the changeset viewer.