Free cookie consent management tool by TermsFeed Policy Generator

Changeset 10985


Ignore:
Timestamp:
06/11/14 14:03:06 (10 years ago)
Author:
gkronber
Message:

#2196: added synchronization in the grammar when caching values

File:
1 edited

Legend:

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

    r9456 r10985  
    323323      if (cachedIsAllowedChildSymbol.TryGetValue(key, out result)) return result;
    324324
    325       List<string> temp;
    326       if (allowedChildSymbols.TryGetValue(parent.Name, out temp)) {
    327         //if (temp.Contains(child.Name)) return true;
    328         if (temp.SelectMany(s => GetSymbol(s).Flatten()).Any(s => s.Name == child.Name)) {
    329           cachedIsAllowedChildSymbol.Add(key, true);
    330           return true;
     325      // value has to be calculated and cached make sure this is done in only one thread
     326      lock (cachedIsAllowedChildSymbol) {
     327        // in case the value has been calculated on another thread in the meanwhile
     328        if (cachedIsAllowedChildSymbol.TryGetValue(key, out result)) return result;
     329
     330        List<string> temp;
     331        if (allowedChildSymbols.TryGetValue(parent.Name, out temp)) {
     332          if (temp.SelectMany(s => GetSymbol(s).Flatten()).Any(s => s.Name == child.Name)) {
     333            cachedIsAllowedChildSymbol.Add(key, true);
     334            return true;
     335          }
    331336        }
    332       }
    333       cachedIsAllowedChildSymbol.Add(key, false);
    334       return false;
     337        cachedIsAllowedChildSymbol.Add(key, false);
     338        return false;
     339      }
    335340    }
    336341
     
    345350      if (cachedIsAllowedChildSymbolIndex.TryGetValue(key, out result)) return result;
    346351
    347       List<string> temp;
    348       if (allowedChildSymbolsPerIndex.TryGetValue(Tuple.Create(parent.Name, argumentIndex), out temp)) {
    349         if (temp.SelectMany(s => GetSymbol(s).Flatten()).Any(s => s.Name == child.Name)) {
    350           cachedIsAllowedChildSymbolIndex.Add(key, true);
    351           return true;
     352      // value has to be calculated and cached make sure this is done in only one thread
     353      lock (cachedIsAllowedChildSymbolIndex) {
     354        // in case the value has been calculated on another thread in the meanwhile
     355        if (cachedIsAllowedChildSymbolIndex.TryGetValue(key, out result)) return result;
     356
     357        List<string> temp;
     358        if (allowedChildSymbolsPerIndex.TryGetValue(Tuple.Create(parent.Name, argumentIndex), out temp)) {
     359          if (temp.SelectMany(s => GetSymbol(s).Flatten()).Any(s => s.Name == child.Name)) {
     360            cachedIsAllowedChildSymbolIndex.Add(key, true);
     361            return true;
     362          }
    352363        }
    353       }
    354       cachedIsAllowedChildSymbolIndex.Add(key, false);
    355       return false;
     364        cachedIsAllowedChildSymbolIndex.Add(key, false);
     365        return false;
     366      }
    356367    }
    357368
     
    391402        return res;
    392403
    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;
     404      // value has to be calculated and cached make sure this is done in only one thread
     405      lock (cachedMinExpressionLength) {
     406        // in case the value has been calculated on another thread in the meanwhile
     407        if (cachedMinExpressionLength.TryGetValue(symbol.Name, out res)) return res;
     408
     409        res = GetMinimumExpressionLengthRec(symbol);
     410        foreach (var entry in cachedMinExpressionLength.Where(e => e.Value >= int.MaxValue).ToList()) {
     411          if (entry.Key != symbol.Name) cachedMinExpressionLength.Remove(entry.Key);
     412        }
     413        return res;
     414      }
    398415    }
    399416
     
    418435      int temp;
    419436      var key = Tuple.Create(symbol.Name, maxDepth);
    420       if (!cachedMaxExpressionLength.TryGetValue(key, out temp)) {
     437      if (cachedMaxExpressionLength.TryGetValue(key, out temp)) return temp;
     438      // value has to be calculated and cached make sure this is done in only one thread
     439      lock (cachedMaxExpressionLength) {
     440        // in case the value has been calculated on another thread in the meanwhile
     441        if (cachedMaxExpressionLength.TryGetValue(key, out temp)) return temp;
     442
    421443        cachedMaxExpressionLength[key] = int.MaxValue; // prevent infinite recursion
    422444        long sumOfMaxTrees = 1 + (from argIndex in Enumerable.Range(0, GetMaximumSubtreeCount(symbol))
     
    429451        return cachedMaxExpressionLength[key];
    430452      }
    431       return temp;
    432453    }
    433454
     
    438459        return res;
    439460
    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;
     461      // value has to be calculated and cached make sure this is done in only one thread
     462      lock (cachedMinExpressionDepth) {
     463        // in case the value has been calculated on another thread in the meanwhile
     464        if (cachedMinExpressionDepth.TryGetValue(symbol.Name, out res)) return res;
     465
     466        res = GetMinimumExpressionDepthRec(symbol);
     467        foreach (var entry in cachedMinExpressionDepth.Where(e => e.Value >= int.MaxValue).ToList()) {
     468          if (entry.Key != symbol.Name) cachedMinExpressionDepth.Remove(entry.Key);
     469        }
     470        return res;
     471      }
    445472    }
    446473    private int GetMinimumExpressionDepthRec(ISymbol symbol) {
     
    462489    public int GetMaximumExpressionDepth(ISymbol symbol) {
    463490      int temp;
    464       if (!cachedMaxExpressionDepth.TryGetValue(symbol.Name, out temp)) {
     491      if (cachedMaxExpressionDepth.TryGetValue(symbol.Name, out temp)) return temp;
     492      // value has to be calculated and cached make sure this is done in only one thread
     493      lock (cachedMaxExpressionDepth) {
     494        // in case the value has been calculated on another thread in the meanwhile
     495        if (cachedMaxExpressionDepth.TryGetValue(symbol.Name, out temp)) return temp;
     496
    465497        cachedMaxExpressionDepth[symbol.Name] = int.MaxValue;
    466498        long maxDepth = 1 + (from argIndex in Enumerable.Range(0, GetMaximumSubtreeCount(symbol))
     
    472504        return cachedMaxExpressionDepth[symbol.Name];
    473505      }
    474       return temp;
    475506    }
    476507
Note: See TracChangeset for help on using the changeset viewer.