Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Grammars/SymbolicExpressionGrammarBase.cs @ 14342

Last change on this file since 14342 was 14342, checked in by mkommend, 8 years ago

#2685: Refactored calculation of minimum expression length and depth into an separate class. Removed separate initialization code for the grammar caches in the ctor.

File size: 20.3 KB
RevLine 
[5686]1#region License Information
2/* HeuristicLab
[14185]3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[5686]4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
30  /// <summary>
31  /// The default symbolic expression grammar stores symbols and syntactic constraints for symbols.
32  /// Symbols are treated as equvivalent if they have the same name.
33  /// Syntactic constraints limit the number of allowed sub trees for a node with a symbol and which symbols are allowed
34  /// in the sub-trees of a symbol (can be specified for each sub-tree index separately).
35  /// </summary>
36  [StorableClass]
37  public abstract class SymbolicExpressionGrammarBase : NamedItem, ISymbolicExpressionGrammarBase {
[6803]38
[5686]39    #region properties for separation between implementation and persistence
[5695]40    [Storable(Name = "Symbols")]
41    private IEnumerable<ISymbol> StorableSymbols {
42      get { return symbols.Values.ToArray(); }
[12422]43      set { foreach (var s in value) symbols.Add(s.Name, s); }
[5695]44    }
[5686]45
[5695]46    [Storable(Name = "SymbolSubtreeCount")]
47    private IEnumerable<KeyValuePair<ISymbol, Tuple<int, int>>> StorableSymbolSubtreeCount {
48      get { return symbolSubtreeCount.Select(x => new KeyValuePair<ISymbol, Tuple<int, int>>(GetSymbol(x.Key), x.Value)).ToArray(); }
[12422]49      set { foreach (var pair in value) symbolSubtreeCount.Add(pair.Key.Name, pair.Value); }
[5695]50    }
[5686]51
[5695]52    [Storable(Name = "AllowedChildSymbols")]
53    private IEnumerable<KeyValuePair<ISymbol, IEnumerable<ISymbol>>> StorableAllowedChildSymbols {
[6814]54      get { return allowedChildSymbols.Select(x => new KeyValuePair<ISymbol, IEnumerable<ISymbol>>(GetSymbol(x.Key), x.Value.Select(GetSymbol).ToArray())).ToArray(); }
[12422]55      set { foreach (var pair in value) allowedChildSymbols.Add(pair.Key.Name, pair.Value.Select(y => y.Name).ToList()); }
[5695]56    }
57
58    [Storable(Name = "AllowedChildSymbolsPerIndex")]
59    private IEnumerable<KeyValuePair<Tuple<ISymbol, int>, IEnumerable<ISymbol>>> StorableAllowedChildSymbolsPerIndex {
[12422]60      get { return allowedChildSymbolsPerIndex.Select(x => new KeyValuePair<Tuple<ISymbol, int>, IEnumerable<ISymbol>>(Tuple.Create(GetSymbol(x.Key.Item1), x.Key.Item2), x.Value.Select(GetSymbol).ToArray())).ToArray(); }
61      set {
62        foreach (var pair in value)
63          allowedChildSymbolsPerIndex.Add(Tuple.Create(pair.Key.Item1.Name, pair.Key.Item2), pair.Value.Select(y => y.Name).ToList());
64      }
[5695]65    }
[5686]66    #endregion
67
[6803]68    private bool suppressEvents;
[12422]69    protected readonly Dictionary<string, ISymbol> symbols;
70    protected readonly Dictionary<string, Tuple<int, int>> symbolSubtreeCount;
71    protected readonly Dictionary<string, List<string>> allowedChildSymbols;
72    protected readonly Dictionary<Tuple<string, int>, List<string>> allowedChildSymbolsPerIndex;
[5686]73
[5688]74    public override bool CanChangeName {
75      get { return false; }
76    }
77    public override bool CanChangeDescription {
78      get { return false; }
79    }
80
[5686]81    [StorableConstructor]
82    protected SymbolicExpressionGrammarBase(bool deserializing)
83      : base(deserializing) {
[6803]84
[12422]85      symbols = new Dictionary<string, ISymbol>();
86      symbolSubtreeCount = new Dictionary<string, Tuple<int, int>>();
87      allowedChildSymbols = new Dictionary<string, List<string>>();
88      allowedChildSymbolsPerIndex = new Dictionary<Tuple<string, int>, List<string>>();
89
[6803]90      suppressEvents = false;
[5686]91    }
[6233]92
[5686]93    protected SymbolicExpressionGrammarBase(SymbolicExpressionGrammarBase original, Cloner cloner)
94      : base(original, cloner) {
95
[6814]96      symbols = original.symbols.ToDictionary(x => x.Key, y => cloner.Clone(y.Value));
[5695]97      symbolSubtreeCount = new Dictionary<string, Tuple<int, int>>(original.symbolSubtreeCount);
[5686]98
99      allowedChildSymbols = new Dictionary<string, List<string>>();
100      foreach (var element in original.allowedChildSymbols)
101        allowedChildSymbols.Add(element.Key, new List<string>(element.Value));
102
103      allowedChildSymbolsPerIndex = new Dictionary<Tuple<string, int>, List<string>>();
104      foreach (var element in original.allowedChildSymbolsPerIndex)
105        allowedChildSymbolsPerIndex.Add(element.Key, new List<string>(element.Value));
[6803]106
107      suppressEvents = false;
[5686]108    }
109
[5688]110    protected SymbolicExpressionGrammarBase(string name, string description)
111      : base(name, description) {
[5686]112      symbols = new Dictionary<string, ISymbol>();
[5695]113      symbolSubtreeCount = new Dictionary<string, Tuple<int, int>>();
[5686]114      allowedChildSymbols = new Dictionary<string, List<string>>();
115      allowedChildSymbolsPerIndex = new Dictionary<Tuple<string, int>, List<string>>();
[6803]116
117      suppressEvents = false;
[5686]118    }
119
120    #region protected grammar manipulation methods
[12422]121    public virtual void AddSymbol(ISymbol symbol) {
[5686]122      if (ContainsSymbol(symbol)) throw new ArgumentException("Symbol " + symbol + " is already defined.");
[6803]123      foreach (var s in symbol.Flatten()) {
124        symbols.Add(s.Name, s);
[7001]125        int maxSubTreeCount = Math.Min(s.MinimumArity + 1, s.MaximumArity);
126        symbolSubtreeCount.Add(s.Name, Tuple.Create(s.MinimumArity, maxSubTreeCount));
[6803]127      }
[5686]128      ClearCaches();
129    }
130
[12422]131    public virtual void RemoveSymbol(ISymbol symbol) {
[6803]132      foreach (var s in symbol.Flatten()) {
133        symbols.Remove(s.Name);
134        allowedChildSymbols.Remove(s.Name);
135        for (int i = 0; i < GetMaximumSubtreeCount(s); i++)
136          allowedChildSymbolsPerIndex.Remove(Tuple.Create(s.Name, i));
137        symbolSubtreeCount.Remove(s.Name);
[5686]138
[6803]139        foreach (var parent in Symbols) {
140          List<string> allowedChilds;
141          if (allowedChildSymbols.TryGetValue(parent.Name, out allowedChilds))
142            allowedChilds.Remove(s.Name);
[5686]143
[6803]144          for (int i = 0; i < GetMaximumSubtreeCount(parent); i++) {
145            if (allowedChildSymbolsPerIndex.TryGetValue(Tuple.Create(parent.Name, i), out allowedChilds))
146              allowedChilds.Remove(s.Name);
147          }
[5686]148        }
[6803]149        suppressEvents = true;
150        foreach (var groupSymbol in Symbols.OfType<GroupSymbol>())
151          groupSymbol.SymbolsCollection.Remove(symbol);
152        suppressEvents = false;
[5686]153      }
154      ClearCaches();
155    }
156
157    public virtual ISymbol GetSymbol(string symbolName) {
158      ISymbol symbol;
159      if (symbols.TryGetValue(symbolName, out symbol)) return symbol;
160      return null;
161    }
162
[12422]163    public virtual void AddAllowedChildSymbol(ISymbol parent, ISymbol child) {
[6803]164      bool changed = false;
165
166      foreach (ISymbol p in parent.Flatten().Where(p => !(p is GroupSymbol)))
167        changed |= AddAllowedChildSymbolToDictionaries(p, child);
168
169      if (changed) {
170        ClearCaches();
171        OnChanged();
172      }
173    }
174
175    private bool AddAllowedChildSymbolToDictionaries(ISymbol parent, ISymbol child) {
[5686]176      List<string> childSymbols;
177      if (!allowedChildSymbols.TryGetValue(parent.Name, out childSymbols)) {
178        childSymbols = new List<string>();
179        allowedChildSymbols.Add(parent.Name, childSymbols);
180      }
[6803]181      if (childSymbols.Contains(child.Name)) return false;
182
183      suppressEvents = true;
184      for (int argumentIndex = 0; argumentIndex < GetMaximumSubtreeCount(parent); argumentIndex++)
185        RemoveAllowedChildSymbol(parent, child, argumentIndex);
186      suppressEvents = false;
187
[5686]188      childSymbols.Add(child.Name);
[6803]189      return true;
[5686]190    }
191
[12422]192    public virtual void AddAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) {
[6803]193      bool changed = false;
194
195      foreach (ISymbol p in parent.Flatten().Where(p => !(p is GroupSymbol)))
196        changed |= AddAllowedChildSymbolToDictionaries(p, child, argumentIndex);
197
198      if (changed) {
199        ClearCaches();
200        OnChanged();
201      }
202    }
203
204
205    private bool AddAllowedChildSymbolToDictionaries(ISymbol parent, ISymbol child, int argumentIndex) {
206      List<string> childSymbols;
207      if (!allowedChildSymbols.TryGetValue(parent.Name, out childSymbols)) {
208        childSymbols = new List<string>();
209        allowedChildSymbols.Add(parent.Name, childSymbols);
210      }
211      if (childSymbols.Contains(child.Name)) return false;
212
213
[5686]214      var key = Tuple.Create(parent.Name, argumentIndex);
215      if (!allowedChildSymbolsPerIndex.TryGetValue(key, out childSymbols)) {
216        childSymbols = new List<string>();
217        allowedChildSymbolsPerIndex.Add(key, childSymbols);
218      }
219
[6803]220      if (childSymbols.Contains(child.Name)) return false;
221
[5686]222      childSymbols.Add(child.Name);
[6803]223      return true;
[5686]224    }
225
[12422]226    public virtual void RemoveAllowedChildSymbol(ISymbol parent, ISymbol child) {
[6803]227      bool changed = false;
[5792]228      List<string> childSymbols;
229      if (allowedChildSymbols.TryGetValue(child.Name, out childSymbols)) {
[6803]230        changed |= childSymbols.Remove(child.Name);
[5792]231      }
[6803]232
233      for (int argumentIndex = 0; argumentIndex < GetMaximumSubtreeCount(parent); argumentIndex++) {
234        var key = Tuple.Create(parent.Name, argumentIndex);
235        if (allowedChildSymbolsPerIndex.TryGetValue(key, out childSymbols))
236          changed |= childSymbols.Remove(child.Name);
237      }
238
239      if (changed) {
240        ClearCaches();
241        OnChanged();
242      }
[5686]243    }
244
[12422]245    public virtual void RemoveAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) {
[6803]246      bool changed = false;
247
248      suppressEvents = true;
[5792]249      List<string> childSymbols;
[6803]250      if (allowedChildSymbols.TryGetValue(parent.Name, out childSymbols)) {
251        if (childSymbols.Remove(child.Name)) {
252          for (int i = 0; i < GetMaximumSubtreeCount(parent); i++) {
253            if (i != argumentIndex) AddAllowedChildSymbol(parent, child, i);
254          }
255          changed = true;
256        }
[5792]257      }
[6803]258      suppressEvents = false;
259
260      var key = Tuple.Create(parent.Name, argumentIndex);
261      if (allowedChildSymbolsPerIndex.TryGetValue(key, out childSymbols))
262        changed |= childSymbols.Remove(child.Name);
263
264      if (changed) {
265        ClearCaches();
266        OnChanged();
267      }
[5686]268    }
269
[12422]270    public virtual void SetSubtreeCount(ISymbol symbol, int minimumSubtreeCount, int maximumSubtreeCount) {
[6803]271      var symbols = symbol.Flatten().Where(s => !(s is GroupSymbol));
272      if (symbols.Any(s => s.MinimumArity > minimumSubtreeCount)) throw new ArgumentException("Invalid minimum subtree count " + minimumSubtreeCount + " for " + symbol);
273      if (symbols.Any(s => s.MaximumArity < maximumSubtreeCount)) throw new ArgumentException("Invalid maximum subtree count " + maximumSubtreeCount + " for " + symbol);
274
275      foreach (ISymbol s in symbols)
276        SetSubTreeCountInDictionaries(s, minimumSubtreeCount, maximumSubtreeCount);
277
278      ClearCaches();
279      OnChanged();
280    }
281
282    private void SetSubTreeCountInDictionaries(ISymbol symbol, int minimumSubtreeCount, int maximumSubtreeCount) {
283      for (int i = maximumSubtreeCount; i < GetMaximumSubtreeCount(symbol); i++) {
[5686]284        var key = Tuple.Create(symbol.Name, i);
285        allowedChildSymbolsPerIndex.Remove(key);
286      }
287
[5695]288      symbolSubtreeCount[symbol.Name] = Tuple.Create(minimumSubtreeCount, maximumSubtreeCount);
[5686]289    }
290    #endregion
291
292    public virtual IEnumerable<ISymbol> Symbols {
293      get { return symbols.Values; }
294    }
295    public virtual IEnumerable<ISymbol> AllowedSymbols {
[12422]296      get { return Symbols.Where(s => s.Enabled); }
[5686]297    }
298    public virtual bool ContainsSymbol(ISymbol symbol) {
299      return symbols.ContainsKey(symbol.Name);
300    }
301
[14342]302    private readonly Dictionary<Tuple<string, string>, bool> cachedIsAllowedChildSymbol = new Dictionary<Tuple<string, string>, bool>();
[5686]303    public virtual bool IsAllowedChildSymbol(ISymbol parent, ISymbol child) {
[7656]304      if (allowedChildSymbols.Count == 0) return false;
[6803]305      if (!child.Enabled) return false;
306
[6814]307      bool result;
[7656]308      var key = Tuple.Create(parent.Name, child.Name);
309      if (cachedIsAllowedChildSymbol.TryGetValue(key, out result)) return result;
310
[10985]311      // value has to be calculated and cached make sure this is done in only one thread
312      lock (cachedIsAllowedChildSymbol) {
313        // in case the value has been calculated on another thread in the meanwhile
314        if (cachedIsAllowedChildSymbol.TryGetValue(key, out result)) return result;
315
316        List<string> temp;
317        if (allowedChildSymbols.TryGetValue(parent.Name, out temp)) {
[12509]318          for (int i = 0; i < temp.Count; i++) {
319            var symbol = GetSymbol(temp[i]);
320            foreach (var s in symbol.Flatten())
321              if (s.Name == child.Name) {
322                cachedIsAllowedChildSymbol.Add(key, true);
323                return true;
324              }
[10985]325          }
[6814]326        }
[10985]327        cachedIsAllowedChildSymbol.Add(key, false);
328        return false;
[6803]329      }
[5686]330    }
331
[14342]332    private readonly Dictionary<Tuple<string, string, int>, bool> cachedIsAllowedChildSymbolIndex = new Dictionary<Tuple<string, string, int>, bool>();
[5686]333    public virtual bool IsAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) {
[6803]334      if (!child.Enabled) return false;
[7660]335      if (IsAllowedChildSymbol(parent, child)) return true;
[7656]336      if (allowedChildSymbolsPerIndex.Count == 0) return false;
[6803]337
[6814]338      bool result;
[7656]339      var key = Tuple.Create(parent.Name, child.Name, argumentIndex);
340      if (cachedIsAllowedChildSymbolIndex.TryGetValue(key, out result)) return result;
341
[10985]342      // value has to be calculated and cached make sure this is done in only one thread
343      lock (cachedIsAllowedChildSymbolIndex) {
344        // in case the value has been calculated on another thread in the meanwhile
345        if (cachedIsAllowedChildSymbolIndex.TryGetValue(key, out result)) return result;
346
347        List<string> temp;
348        if (allowedChildSymbolsPerIndex.TryGetValue(Tuple.Create(parent.Name, argumentIndex), out temp)) {
[12509]349          for (int i = 0; i < temp.Count; i++) {
350            var symbol = GetSymbol(temp[i]);
351            foreach (var s in symbol.Flatten())
352              if (s.Name == child.Name) {
353                cachedIsAllowedChildSymbolIndex.Add(key, true);
354                return true;
355              }
[10985]356          }
[6814]357        }
[10985]358        cachedIsAllowedChildSymbolIndex.Add(key, false);
359        return false;
[6803]360      }
[5686]361    }
362
[6911]363    public IEnumerable<ISymbol> GetAllowedChildSymbols(ISymbol parent) {
[7656]364      foreach (ISymbol child in AllowedSymbols) {
365        if (IsAllowedChildSymbol(parent, child)) yield return child;
366      }
[5686]367    }
368
[6911]369    public IEnumerable<ISymbol> GetAllowedChildSymbols(ISymbol parent, int argumentIndex) {
[7656]370      foreach (ISymbol child in AllowedSymbols) {
371        if (IsAllowedChildSymbol(parent, child, argumentIndex)) yield return child;
372      }
[5686]373    }
374
375    public virtual int GetMinimumSubtreeCount(ISymbol symbol) {
[5695]376      return symbolSubtreeCount[symbol.Name].Item1;
[5686]377    }
378    public virtual int GetMaximumSubtreeCount(ISymbol symbol) {
[5695]379      return symbolSubtreeCount[symbol.Name].Item2;
[5686]380    }
381
[6443]382    protected void ClearCaches() {
[5686]383      cachedMinExpressionLength.Clear();
384      cachedMaxExpressionLength.Clear();
385      cachedMinExpressionDepth.Clear();
[7076]386      cachedMaxExpressionDepth.Clear();
[6814]387
388      cachedIsAllowedChildSymbol.Clear();
389      cachedIsAllowedChildSymbolIndex.Clear();
[5686]390    }
391
[14342]392    private readonly Dictionary<string, int> cachedMinExpressionLength = new Dictionary<string, int>();
[5686]393    public int GetMinimumExpressionLength(ISymbol symbol) {
[9402]394      int res;
395      if (cachedMinExpressionLength.TryGetValue(symbol.Name, out res))
396        return res;
397
[10985]398      // value has to be calculated and cached make sure this is done in only one thread
399      lock (cachedMinExpressionLength) {
400        // in case the value has been calculated on another thread in the meanwhile
401        if (cachedMinExpressionLength.TryGetValue(symbol.Name, out res)) return res;
402
[14342]403        GrammarUtils.CalculateMinimumExpressionLengths(this, cachedMinExpressionLength);
404        return cachedMinExpressionLength[symbol.Name];
[9402]405      }
406    }
407
[5686]408
[14342]409    private readonly Dictionary<Tuple<string, int>, int> cachedMaxExpressionLength = new Dictionary<Tuple<string, int>, int>();
[6911]410    public int GetMaximumExpressionLength(ISymbol symbol, int maxDepth) {
[5686]411      int temp;
[6911]412      var key = Tuple.Create(symbol.Name, maxDepth);
[10985]413      if (cachedMaxExpressionLength.TryGetValue(key, out temp)) return temp;
414      // value has to be calculated and cached make sure this is done in only one thread
415      lock (cachedMaxExpressionLength) {
416        // in case the value has been calculated on another thread in the meanwhile
417        if (cachedMaxExpressionLength.TryGetValue(key, out temp)) return temp;
418
[6911]419        cachedMaxExpressionLength[key] = int.MaxValue; // prevent infinite recursion
[5686]420        long sumOfMaxTrees = 1 + (from argIndex in Enumerable.Range(0, GetMaximumSubtreeCount(symbol))
[6803]421                                  let maxForSlot = (long)(from s in GetAllowedChildSymbols(symbol, argIndex)
[6911]422                                                          where s.InitialFrequency > 0.0
423                                                          where GetMinimumExpressionDepth(s) < maxDepth
424                                                          select GetMaximumExpressionLength(s, maxDepth - 1)).DefaultIfEmpty(0).Max()
[5686]425                                  select maxForSlot).DefaultIfEmpty(0).Sum();
[6911]426        cachedMaxExpressionLength[key] = (int)Math.Min(sumOfMaxTrees, int.MaxValue);
427        return cachedMaxExpressionLength[key];
[5686]428      }
429    }
430
[14342]431    private readonly Dictionary<string, int> cachedMinExpressionDepth = new Dictionary<string, int>();
[5686]432    public int GetMinimumExpressionDepth(ISymbol symbol) {
[9402]433      int res;
434      if (cachedMinExpressionDepth.TryGetValue(symbol.Name, out res))
435        return res;
436
[10985]437      // value has to be calculated and cached make sure this is done in only one thread
438      lock (cachedMinExpressionDepth) {
439        // in case the value has been calculated on another thread in the meanwhile
440        if (cachedMinExpressionDepth.TryGetValue(symbol.Name, out res)) return res;
441
[14342]442        GrammarUtils.CalculateMinimumExpressionDepth(this, cachedMinExpressionDepth);
[5686]443        return cachedMinExpressionDepth[symbol.Name];
444      }
445    }
[6803]446
[14342]447    private readonly Dictionary<string, int> cachedMaxExpressionDepth = new Dictionary<string, int>();
[7076]448    public int GetMaximumExpressionDepth(ISymbol symbol) {
449      int temp;
[10985]450      if (cachedMaxExpressionDepth.TryGetValue(symbol.Name, out temp)) return temp;
451      // value has to be calculated and cached make sure this is done in only one thread
452      lock (cachedMaxExpressionDepth) {
453        // in case the value has been calculated on another thread in the meanwhile
454        if (cachedMaxExpressionDepth.TryGetValue(symbol.Name, out temp)) return temp;
455
[7076]456        cachedMaxExpressionDepth[symbol.Name] = int.MaxValue;
457        long maxDepth = 1 + (from argIndex in Enumerable.Range(0, GetMaximumSubtreeCount(symbol))
458                             let maxForSlot = (long)(from s in GetAllowedChildSymbols(symbol, argIndex)
459                                                     where s.InitialFrequency > 0.0
460                                                     select GetMaximumExpressionDepth(s)).DefaultIfEmpty(0).Max()
461                             select maxForSlot).DefaultIfEmpty(0).Max();
462        cachedMaxExpressionDepth[symbol.Name] = (int)Math.Min(maxDepth, int.MaxValue);
463        return cachedMaxExpressionDepth[symbol.Name];
464      }
465    }
466
[6803]467    public event EventHandler Changed;
468    protected virtual void OnChanged() {
469      if (suppressEvents) return;
470      var handler = Changed;
[12422]471      if (handler != null) handler(this, EventArgs.Empty);
[6803]472    }
[5686]473  }
474}
Note: See TracBrowser for help on using the repository browser.