Free cookie consent management tool by TermsFeed Policy Generator

Changeset 7656


Ignore:
Timestamp:
03/22/12 18:05:15 (12 years ago)
Author:
mkommend
Message:

#1806: improved memory usage of symbolic expression tree encoding

Location:
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding

    • Property svn:mergeinfo set to (toggle deleted branches)
      /branches/HeuristicLab.Crossovers/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding7343-7503
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionGrammarBase.cs

    r7259 r7656  
    308308    }
    309309    public virtual IEnumerable<ISymbol> AllowedSymbols {
    310       get { return Symbols.Where(s => s.Enabled); }
     310      get { foreach (var s in Symbols) if (s.Enabled) yield return s; }
    311311    }
    312312    public virtual bool ContainsSymbol(ISymbol symbol) {
     
    316316    private readonly Dictionary<Tuple<string, string>, bool> cachedIsAllowedChildSymbol;
    317317    public virtual bool IsAllowedChildSymbol(ISymbol parent, ISymbol child) {
     318      if (allowedChildSymbols.Count == 0) return false;
    318319      if (!child.Enabled) return false;
    319320
    320321      bool result;
    321       if (cachedIsAllowedChildSymbol.TryGetValue(Tuple.Create(parent.Name, child.Name), out result)) return result;
     322      var key = Tuple.Create(parent.Name, child.Name);
     323      if (cachedIsAllowedChildSymbol.TryGetValue(key, out result)) return result;
     324
    322325      List<string> temp;
    323326      if (allowedChildSymbols.TryGetValue(parent.Name, out temp)) {
    324327        //if (temp.Contains(child.Name)) return true;
    325         if (temp.SelectMany(s => GetSymbol(s).Flatten()).Where(s => s.Name == child.Name).Any()) {
    326           cachedIsAllowedChildSymbol.Add(Tuple.Create(parent.Name, child.Name), true);
     328        if (temp.SelectMany(s => GetSymbol(s).Flatten()).Any(s => s.Name == child.Name)) {
     329          cachedIsAllowedChildSymbol.Add(key, true);
    327330          return true;
    328331        }
    329332      }
    330       cachedIsAllowedChildSymbol.Add(Tuple.Create(parent.Name, child.Name), false);
     333      cachedIsAllowedChildSymbol.Add(key, false);
    331334      return false;
    332335    }
     
    335338    public virtual bool IsAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) {
    336339      if (!child.Enabled) return false;
     340      if (allowedChildSymbolsPerIndex.Count == 0) return false;
    337341      if (IsAllowedChildSymbol(parent, child)) return true;
    338342
    339343      bool result;
    340       if (cachedIsAllowedChildSymbolIndex.TryGetValue(Tuple.Create(parent.Name, child.Name, argumentIndex), out result)) return result;
     344      var key = Tuple.Create(parent.Name, child.Name, argumentIndex);
     345      if (cachedIsAllowedChildSymbolIndex.TryGetValue(key, out result)) return result;
     346
    341347      List<string> temp;
    342       var key = Tuple.Create(parent.Name, argumentIndex);
    343       if (allowedChildSymbolsPerIndex.TryGetValue(key, out temp)) {
    344         //if (temp.Contains(child.Name)) return true;
    345         if (temp.SelectMany(s => GetSymbol(s).Flatten()).Where(s => s.Name == child.Name).Any()) {
    346           cachedIsAllowedChildSymbolIndex.Add(Tuple.Create(parent.Name, child.Name, argumentIndex), true);
     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);
    347351          return true;
    348352        }
    349353      }
    350       cachedIsAllowedChildSymbolIndex.Add(Tuple.Create(parent.Name, child.Name, argumentIndex), false);
     354      cachedIsAllowedChildSymbolIndex.Add(key, false);
    351355      return false;
    352356    }
    353357
    354358    public IEnumerable<ISymbol> GetAllowedChildSymbols(ISymbol parent) {
    355       return from child in AllowedSymbols
    356              where IsAllowedChildSymbol(parent, child)
    357              select child;
     359      foreach (ISymbol child in AllowedSymbols) {
     360        if (IsAllowedChildSymbol(parent, child)) yield return child;
     361      }
    358362    }
    359363
    360364    public IEnumerable<ISymbol> GetAllowedChildSymbols(ISymbol parent, int argumentIndex) {
    361       return from child in AllowedSymbols
    362              where IsAllowedChildSymbol(parent, child, argumentIndex)
    363              select child;
     365      foreach (ISymbol child in AllowedSymbols) {
     366        if (IsAllowedChildSymbol(parent, child, argumentIndex)) yield return child;
     367      }
    364368    }
    365369
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTreeGrammar.cs

    r7259 r7656  
    2222using System;
    2323using System.Collections.Generic;
    24 using System.Linq;
    2524using HeuristicLab.Common;
    2625using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     
    5150
    5251    public override IEnumerable<ISymbol> Symbols {
    53       get { return grammar.Symbols.Union(base.Symbols); }
     52      get {
     53        foreach (var s in base.symbols.Values) yield return s;
     54        foreach (var s in grammar.Symbols) yield return s;
     55      }
    5456    }
    5557    public override IEnumerable<ISymbol> AllowedSymbols {
Note: See TracChangeset for help on using the changeset viewer.