Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
09/21/11 15:51:43 (13 years ago)
Author:
mkommend
Message:

#1479: Adapted symbolic expression encoding unit tests.

File:
1 edited

Legend:

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

    r6803 r6814  
    5252    [Storable(Name = "AllowedChildSymbols")]
    5353    private IEnumerable<KeyValuePair<ISymbol, IEnumerable<ISymbol>>> StorableAllowedChildSymbols {
    54       get { return allowedChildSymbols.Select(x => new KeyValuePair<ISymbol, IEnumerable<ISymbol>>(GetSymbol(x.Key), x.Value.Select(y => GetSymbol(y)).ToArray())).ToArray(); }
     54      get { return allowedChildSymbols.Select(x => new KeyValuePair<ISymbol, IEnumerable<ISymbol>>(GetSymbol(x.Key), x.Value.Select(GetSymbol).ToArray())).ToArray(); }
    5555      set { allowedChildSymbols = value.ToDictionary(x => x.Key.Name, x => x.Value.Select(y => y.Name).ToList()); }
    5656    }
     
    8383      cachedMinExpressionDepth = new Dictionary<string, int>();
    8484
     85      cachedIsAllowedChildSymbol = new Dictionary<Tuple<string, string>, bool>();
     86      cachedIsAllowedChildSymbolIndex = new Dictionary<Tuple<string, string, int>, bool>();
     87
    8588      suppressEvents = false;
    8689    }
     
    9295      cachedMinExpressionDepth = new Dictionary<string, int>();
    9396
    94       symbols = original.symbols.ToDictionary(x => x.Key, y => (ISymbol)cloner.Clone(y.Value));
     97      cachedIsAllowedChildSymbol = new Dictionary<Tuple<string, string>, bool>();
     98      cachedIsAllowedChildSymbolIndex = new Dictionary<Tuple<string, string, int>, bool>();
     99
     100      symbols = original.symbols.ToDictionary(x => x.Key, y => cloner.Clone(y.Value));
    95101      symbolSubtreeCount = new Dictionary<string, Tuple<int, int>>(original.symbolSubtreeCount);
    96102
     
    111117      cachedMaxExpressionLength = new Dictionary<string, int>();
    112118      cachedMinExpressionDepth = new Dictionary<string, int>();
     119
     120      cachedIsAllowedChildSymbol = new Dictionary<Tuple<string, string>, bool>();
     121      cachedIsAllowedChildSymbolIndex = new Dictionary<Tuple<string, string, int>, bool>();
    113122
    114123      symbols = new Dictionary<string, ISymbol>();
     
    301310    }
    302311
     312    private readonly Dictionary<Tuple<string, string>, bool> cachedIsAllowedChildSymbol;
    303313    public virtual bool IsAllowedChildSymbol(ISymbol parent, ISymbol child) {
    304314      if (!child.Enabled) return false;
    305315
     316      bool result;
     317      if (cachedIsAllowedChildSymbol.TryGetValue(Tuple.Create(parent.Name, child.Name), out result)) return result;
    306318      List<string> temp;
    307319      if (allowedChildSymbols.TryGetValue(parent.Name, out temp)) {
    308         if (temp.Contains(child.Name)) return true;
    309         if (temp.SelectMany(s => GetSymbol(s).Flatten().Select(n => n.Name)).Contains(child.Name)) return true;
    310       }
     320        //if (temp.Contains(child.Name)) return true;
     321        if (temp.SelectMany(s => GetSymbol(s).Flatten()).Where(s => s.Name == child.Name).Any()) {
     322          cachedIsAllowedChildSymbol.Add(Tuple.Create(parent.Name, child.Name), true);
     323          return true;
     324        }
     325      }
     326      cachedIsAllowedChildSymbol.Add(Tuple.Create(parent.Name, child.Name), false);
    311327      return false;
    312328    }
    313329
     330    private readonly Dictionary<Tuple<string, string, int>, bool> cachedIsAllowedChildSymbolIndex;
    314331    public virtual bool IsAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) {
    315332      if (!child.Enabled) return false;
    316333      if (IsAllowedChildSymbol(parent, child)) return true;
    317334
     335      bool result;
     336      if (cachedIsAllowedChildSymbolIndex.TryGetValue(Tuple.Create(parent.Name, child.Name, argumentIndex), out result)) return result;
    318337      List<string> temp;
    319338      var key = Tuple.Create(parent.Name, argumentIndex);
    320339      if (allowedChildSymbolsPerIndex.TryGetValue(key, out temp)) {
    321         if (temp.Contains(child.Name)) return true;
    322         if (temp.SelectMany(s => GetSymbol(s).Flatten().Select(n => n.Name)).Contains(child.Name)) return true;
    323       }
     340        //if (temp.Contains(child.Name)) return true;
     341        if (temp.SelectMany(s => GetSymbol(s).Flatten()).Where(s => s.Name == child.Name).Any()) {
     342          cachedIsAllowedChildSymbolIndex.Add(Tuple.Create(parent.Name, child.Name, argumentIndex), true);
     343          return true;
     344        }
     345      }
     346      cachedIsAllowedChildSymbolIndex.Add(Tuple.Create(parent.Name, child.Name, argumentIndex), false);
    324347      return false;
    325348    }
     
    330353        return Enumerable.Empty<ISymbol>();
    331354
    332       return childs.Select(x => GetSymbol(x)).Where(s => s.Enabled);
     355      return childs.Select(GetSymbol).Where(s => s.Enabled);
    333356    }
    334357
     
    343366        result = result.Union(temp);
    344367
    345       return result.Select(x => GetSymbol(x)).Where(s => s.Enabled);
     368      return result.Select(GetSymbol).Where(s => s.Enabled);
    346369    }
    347370
     
    357380      cachedMaxExpressionLength.Clear();
    358381      cachedMinExpressionDepth.Clear();
    359     }
    360 
    361     private Dictionary<string, int> cachedMinExpressionLength;
     382
     383      cachedIsAllowedChildSymbol.Clear();
     384      cachedIsAllowedChildSymbolIndex.Clear();
     385    }
     386
     387    private readonly Dictionary<string, int> cachedMinExpressionLength;
    362388    public int GetMinimumExpressionLength(ISymbol symbol) {
    363389      int temp;
     
    375401    }
    376402
    377     private Dictionary<string, int> cachedMaxExpressionLength;
     403    private readonly Dictionary<string, int> cachedMaxExpressionLength;
    378404    public int GetMaximumExpressionLength(ISymbol symbol) {
    379405      int temp;
     
    390416    }
    391417
    392     private Dictionary<string, int> cachedMinExpressionDepth;
     418    private readonly Dictionary<string, int> cachedMinExpressionDepth;
    393419    public int GetMinimumExpressionDepth(ISymbol symbol) {
    394420      int temp;
Note: See TracChangeset for help on using the changeset viewer.