Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
09/20/11 11:12:10 (13 years ago)
Author:
mkommend
Message:

#1479: Merged grammar editor branch into trunk.

Location:
trunk/sources
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources

  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding

    • Property svn:ignore set to
      bin
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionGrammarBase.cs

    r6443 r6803  
    3636  [StorableClass]
    3737  public abstract class SymbolicExpressionGrammarBase : NamedItem, ISymbolicExpressionGrammarBase {
     38
    3839    #region properties for separation between implementation and persistence
    3940    [Storable(Name = "Symbols")]
     
    6263    #endregion
    6364
     65    private bool suppressEvents;
    6466    protected Dictionary<string, ISymbol> symbols;
    6567    protected Dictionary<string, Tuple<int, int>> symbolSubtreeCount;
     
    8082      cachedMaxExpressionLength = new Dictionary<string, int>();
    8183      cachedMinExpressionDepth = new Dictionary<string, int>();
     84
     85      suppressEvents = false;
    8286    }
    8387
     
    98102      foreach (var element in original.allowedChildSymbolsPerIndex)
    99103        allowedChildSymbolsPerIndex.Add(element.Key, new List<string>(element.Value));
     104
     105      suppressEvents = false;
    100106    }
    101107
     
    110116      allowedChildSymbols = new Dictionary<string, List<string>>();
    111117      allowedChildSymbolsPerIndex = new Dictionary<Tuple<string, int>, List<string>>();
     118
     119      suppressEvents = false;
    112120    }
    113121
     
    115123    protected virtual void AddSymbol(ISymbol symbol) {
    116124      if (ContainsSymbol(symbol)) throw new ArgumentException("Symbol " + symbol + " is already defined.");
    117       symbols.Add(symbol.Name, symbol);
    118       symbolSubtreeCount.Add(symbol.Name, Tuple.Create(0, 0));
     125      foreach (var s in symbol.Flatten()) {
     126        symbols.Add(s.Name, s);
     127        symbolSubtreeCount.Add(s.Name, Tuple.Create(s.MinimumArity, s.MaximumArity));
     128      }
    119129      ClearCaches();
    120130    }
    121131
    122132    protected virtual void RemoveSymbol(ISymbol symbol) {
    123       symbols.Remove(symbol.Name);
    124       allowedChildSymbols.Remove(symbol.Name);
    125       for (int i = 0; i < GetMaximumSubtreeCount(symbol); i++)
    126         allowedChildSymbolsPerIndex.Remove(Tuple.Create(symbol.Name, i));
    127       symbolSubtreeCount.Remove(symbol.Name);
    128 
    129       foreach (var parent in Symbols) {
    130         List<string> allowedChilds;
    131         if (allowedChildSymbols.TryGetValue(parent.Name, out allowedChilds))
    132           allowedChilds.Remove(symbol.Name);
    133 
    134         for (int i = 0; i < GetMaximumSubtreeCount(parent); i++) {
    135           if (allowedChildSymbolsPerIndex.TryGetValue(Tuple.Create(parent.Name, i), out allowedChilds))
    136             allowedChilds.Remove(symbol.Name);
     133      foreach (var s in symbol.Flatten()) {
     134        symbols.Remove(s.Name);
     135        allowedChildSymbols.Remove(s.Name);
     136        for (int i = 0; i < GetMaximumSubtreeCount(s); i++)
     137          allowedChildSymbolsPerIndex.Remove(Tuple.Create(s.Name, i));
     138        symbolSubtreeCount.Remove(s.Name);
     139
     140        foreach (var parent in Symbols) {
     141          List<string> allowedChilds;
     142          if (allowedChildSymbols.TryGetValue(parent.Name, out allowedChilds))
     143            allowedChilds.Remove(s.Name);
     144
     145          for (int i = 0; i < GetMaximumSubtreeCount(parent); i++) {
     146            if (allowedChildSymbolsPerIndex.TryGetValue(Tuple.Create(parent.Name, i), out allowedChilds))
     147              allowedChilds.Remove(s.Name);
     148          }
    137149        }
     150        suppressEvents = true;
     151        foreach (var groupSymbol in Symbols.OfType<GroupSymbol>())
     152          groupSymbol.SymbolsCollection.Remove(symbol);
     153        suppressEvents = false;
    138154      }
    139155      ClearCaches();
     
    147163
    148164    protected void AddAllowedChildSymbol(ISymbol parent, ISymbol child) {
     165      bool changed = false;
     166
     167      foreach (ISymbol p in parent.Flatten().Where(p => !(p is GroupSymbol)))
     168        changed |= AddAllowedChildSymbolToDictionaries(p, child);
     169
     170      if (changed) {
     171        ClearCaches();
     172        OnChanged();
     173      }
     174    }
     175
     176    private bool AddAllowedChildSymbolToDictionaries(ISymbol parent, ISymbol child) {
    149177      List<string> childSymbols;
    150178      if (!allowedChildSymbols.TryGetValue(parent.Name, out childSymbols)) {
     
    152180        allowedChildSymbols.Add(parent.Name, childSymbols);
    153181      }
    154       if (childSymbols.Contains(child.Name)) throw new ArgumentException();
     182      if (childSymbols.Contains(child.Name)) return false;
     183
     184      suppressEvents = true;
     185      for (int argumentIndex = 0; argumentIndex < GetMaximumSubtreeCount(parent); argumentIndex++)
     186        RemoveAllowedChildSymbol(parent, child, argumentIndex);
     187      suppressEvents = false;
     188
    155189      childSymbols.Add(child.Name);
    156       ClearCaches();
     190      return true;
    157191    }
    158192
    159193    protected void AddAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) {
     194      bool changed = false;
     195
     196      foreach (ISymbol p in parent.Flatten().Where(p => !(p is GroupSymbol)))
     197        changed |= AddAllowedChildSymbolToDictionaries(p, child, argumentIndex);
     198
     199      if (changed) {
     200        ClearCaches();
     201        OnChanged();
     202      }
     203    }
     204
     205
     206    private bool AddAllowedChildSymbolToDictionaries(ISymbol parent, ISymbol child, int argumentIndex) {
     207      List<string> childSymbols;
     208      if (!allowedChildSymbols.TryGetValue(parent.Name, out childSymbols)) {
     209        childSymbols = new List<string>();
     210        allowedChildSymbols.Add(parent.Name, childSymbols);
     211      }
     212      if (childSymbols.Contains(child.Name)) return false;
     213
     214
    160215      var key = Tuple.Create(parent.Name, argumentIndex);
    161       List<string> childSymbols;
    162216      if (!allowedChildSymbolsPerIndex.TryGetValue(key, out childSymbols)) {
    163217        childSymbols = new List<string>();
     
    165219      }
    166220
    167       if (IsAllowedChildSymbol(parent, child)) throw new ArgumentException();
    168       if (childSymbols.Contains(child.Name)) throw new ArgumentException();
     221      if (childSymbols.Contains(child.Name)) return false;
     222
    169223      childSymbols.Add(child.Name);
    170       ClearCaches();
     224      return true;
    171225    }
    172226
    173227    protected void RemoveAllowedChildSymbol(ISymbol parent, ISymbol child) {
     228      bool changed = false;
    174229      List<string> childSymbols;
    175230      if (allowedChildSymbols.TryGetValue(child.Name, out childSymbols)) {
    176         if (allowedChildSymbols[parent.Name].Remove(child.Name))
    177           ClearCaches();
     231        changed |= childSymbols.Remove(child.Name);
     232      }
     233
     234      for (int argumentIndex = 0; argumentIndex < GetMaximumSubtreeCount(parent); argumentIndex++) {
     235        var key = Tuple.Create(parent.Name, argumentIndex);
     236        if (allowedChildSymbolsPerIndex.TryGetValue(key, out childSymbols))
     237          changed |= childSymbols.Remove(child.Name);
     238      }
     239
     240      if (changed) {
     241        ClearCaches();
     242        OnChanged();
    178243      }
    179244    }
    180245
    181246    protected void RemoveAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) {
     247      bool changed = false;
     248
     249      suppressEvents = true;
     250      List<string> childSymbols;
     251      if (allowedChildSymbols.TryGetValue(parent.Name, out childSymbols)) {
     252        if (childSymbols.Remove(child.Name)) {
     253          for (int i = 0; i < GetMaximumSubtreeCount(parent); i++) {
     254            if (i != argumentIndex) AddAllowedChildSymbol(parent, child, i);
     255          }
     256          changed = true;
     257        }
     258      }
     259      suppressEvents = false;
     260
    182261      var key = Tuple.Create(parent.Name, argumentIndex);
    183       List<string> childSymbols;
    184       if (allowedChildSymbolsPerIndex.TryGetValue(key, out childSymbols)) {
    185         if (allowedChildSymbolsPerIndex[key].Remove(child.Name))
    186           ClearCaches();
     262      if (allowedChildSymbolsPerIndex.TryGetValue(key, out childSymbols))
     263        changed |= childSymbols.Remove(child.Name);
     264
     265      if (changed) {
     266        ClearCaches();
     267        OnChanged();
    187268      }
    188269    }
    189270
    190271    protected void SetSubtreeCount(ISymbol symbol, int minimumSubtreeCount, int maximumSubtreeCount) {
    191       for (int i = GetMaximumSubtreeCount(symbol) - 1; i >= maximumSubtreeCount; i--) {
     272      var symbols = symbol.Flatten().Where(s => !(s is GroupSymbol));
     273      if (symbols.Any(s => s.MinimumArity > minimumSubtreeCount)) throw new ArgumentException("Invalid minimum subtree count " + minimumSubtreeCount + " for " + symbol);
     274      if (symbols.Any(s => s.MaximumArity < maximumSubtreeCount)) throw new ArgumentException("Invalid maximum subtree count " + maximumSubtreeCount + " for " + symbol);
     275
     276      foreach (ISymbol s in symbols)
     277        SetSubTreeCountInDictionaries(s, minimumSubtreeCount, maximumSubtreeCount);
     278
     279      ClearCaches();
     280      OnChanged();
     281    }
     282
     283    private void SetSubTreeCountInDictionaries(ISymbol symbol, int minimumSubtreeCount, int maximumSubtreeCount) {
     284      for (int i = maximumSubtreeCount; i < GetMaximumSubtreeCount(symbol); i++) {
    192285        var key = Tuple.Create(symbol.Name, i);
    193286        allowedChildSymbolsPerIndex.Remove(key);
     
    195288
    196289      symbolSubtreeCount[symbol.Name] = Tuple.Create(minimumSubtreeCount, maximumSubtreeCount);
    197       ClearCaches();
    198290    }
    199291    #endregion
    200292
    201     #region ISymbolicExpressionGrammarBase Members
    202293    public virtual IEnumerable<ISymbol> Symbols {
    203294      get { return symbols.Values; }
    204295    }
    205296    public virtual IEnumerable<ISymbol> AllowedSymbols {
    206       get { return Symbols.Where(s => !s.InitialFrequency.IsAlmost(0.0)); }
     297      get { return Symbols.Where(s => s.Enabled); }
    207298    }
    208299    public virtual bool ContainsSymbol(ISymbol symbol) {
     
    211302
    212303    public virtual bool IsAllowedChildSymbol(ISymbol parent, ISymbol child) {
     304      if (!child.Enabled) return false;
     305
    213306      List<string> temp;
    214       if (allowedChildSymbols.TryGetValue(parent.Name, out temp))
     307      if (allowedChildSymbols.TryGetValue(parent.Name, out temp)) {
    215308        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      }
    216311      return false;
    217312    }
    218313
    219314    public virtual bool IsAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) {
     315      if (!child.Enabled) return false;
     316      if (IsAllowedChildSymbol(parent, child)) return true;
     317
    220318      List<string> temp;
    221       if (allowedChildSymbols.TryGetValue(parent.Name, out temp))
     319      var key = Tuple.Create(parent.Name, argumentIndex);
     320      if (allowedChildSymbolsPerIndex.TryGetValue(key, out temp)) {
    222321        if (temp.Contains(child.Name)) return true;
    223 
    224       var key = Tuple.Create(parent.Name, argumentIndex);
    225       if (allowedChildSymbolsPerIndex.TryGetValue(key, out temp))
    226         return temp.Contains(child.Name);
     322        if (temp.SelectMany(s => GetSymbol(s).Flatten().Select(n => n.Name)).Contains(child.Name)) return true;
     323      }
    227324      return false;
    228325    }
    229326
    230327    public virtual IEnumerable<ISymbol> GetAllowedChildSymbols(ISymbol parent) {
    231       return from s in AllowedSymbols where IsAllowedChildSymbol(parent, s) select s;
     328      List<string> childs;
     329      if (!allowedChildSymbols.TryGetValue(parent.Name, out childs))
     330        return Enumerable.Empty<ISymbol>();
     331
     332      return childs.Select(x => GetSymbol(x)).Where(s => s.Enabled);
    232333    }
    233334
     
    242343        result = result.Union(temp);
    243344
    244       return result.Select(x => GetSymbol(x));
     345      return result.Select(x => GetSymbol(x)).Where(s => s.Enabled);
    245346    }
    246347
     
    251352      return symbolSubtreeCount[symbol.Name].Item2;
    252353    }
    253 
    254354
    255355    protected void ClearCaches() {
     
    265365        cachedMinExpressionLength[symbol.Name] = int.MaxValue; // prevent infinite recursion
    266366        long sumOfMinExpressionLengths = 1 + (from argIndex in Enumerable.Range(0, GetMinimumSubtreeCount(symbol))
    267                                               let minForSlot = (long)(from s in AllowedSymbols
    268                                                                       where IsAllowedChildSymbol(symbol, s, argIndex)
     367                                              let minForSlot = (long)(from s in GetAllowedChildSymbols(symbol, argIndex)
    269368                                                                      select GetMinimumExpressionLength(s)).DefaultIfEmpty(0).Min()
    270369                                              select minForSlot).DefaultIfEmpty(0).Sum();
     
    282381        cachedMaxExpressionLength[symbol.Name] = int.MaxValue; // prevent infinite recursion
    283382        long sumOfMaxTrees = 1 + (from argIndex in Enumerable.Range(0, GetMaximumSubtreeCount(symbol))
    284                                   let maxForSlot = (long)(from s in AllowedSymbols
    285                                                           where IsAllowedChildSymbol(symbol, s, argIndex)
     383                                  let maxForSlot = (long)(from s in GetAllowedChildSymbols(symbol, argIndex)
    286384                                                          select GetMaximumExpressionLength(s)).DefaultIfEmpty(0).Max()
    287385                                  select maxForSlot).DefaultIfEmpty(0).Sum();
     
    298396        cachedMinExpressionDepth[symbol.Name] = int.MaxValue; // prevent infinite recursion
    299397        long minDepth = 1 + (from argIndex in Enumerable.Range(0, GetMinimumSubtreeCount(symbol))
    300                              let minForSlot = (long)(from s in AllowedSymbols
    301                                                      where IsAllowedChildSymbol(symbol, s, argIndex)
     398                             let minForSlot = (long)(from s in GetAllowedChildSymbols(symbol, argIndex)
    302399                                                     select GetMinimumExpressionDepth(s)).DefaultIfEmpty(0).Min()
    303400                             select minForSlot).DefaultIfEmpty(0).Max();
     
    307404      return temp;
    308405    }
    309     #endregion
     406
     407    public event EventHandler Changed;
     408    protected virtual void OnChanged() {
     409      if (suppressEvents) return;
     410      var handler = Changed;
     411      if (handler != null) Changed(this, EventArgs.Empty);
     412    }
    310413  }
    311414}
Note: See TracChangeset for help on using the changeset viewer.