Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 6452 was 6443, checked in by mkommend, 14 years ago

#1559: Fixed memory leak in SymbolicExpressionGrammar.

File size: 14.2 KB
RevLine 
[5686]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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 {
38    #region properties for separation between implementation and persistence
[5695]39    [Storable(Name = "Symbols")]
40    private IEnumerable<ISymbol> StorableSymbols {
41      get { return symbols.Values.ToArray(); }
42      set { symbols = value.ToDictionary(sym => sym.Name); }
43    }
[5686]44
[5695]45    [Storable(Name = "SymbolSubtreeCount")]
46    private IEnumerable<KeyValuePair<ISymbol, Tuple<int, int>>> StorableSymbolSubtreeCount {
47      get { return symbolSubtreeCount.Select(x => new KeyValuePair<ISymbol, Tuple<int, int>>(GetSymbol(x.Key), x.Value)).ToArray(); }
48      set { symbolSubtreeCount = value.ToDictionary(x => x.Key.Name, x => x.Value); }
49    }
[5686]50
[5695]51    [Storable(Name = "AllowedChildSymbols")]
52    private IEnumerable<KeyValuePair<ISymbol, IEnumerable<ISymbol>>> StorableAllowedChildSymbols {
[5712]53      get { return allowedChildSymbols.Select(x => new KeyValuePair<ISymbol, IEnumerable<ISymbol>>(GetSymbol(x.Key), x.Value.Select(y => GetSymbol(y)).ToArray())).ToArray(); }
[5695]54      set { allowedChildSymbols = value.ToDictionary(x => x.Key.Name, x => x.Value.Select(y => y.Name).ToList()); }
55    }
56
57    [Storable(Name = "AllowedChildSymbolsPerIndex")]
58    private IEnumerable<KeyValuePair<Tuple<ISymbol, int>, IEnumerable<ISymbol>>> StorableAllowedChildSymbolsPerIndex {
[5712]59      get { return allowedChildSymbolsPerIndex.Select(x => new KeyValuePair<Tuple<ISymbol, int>, IEnumerable<ISymbol>>(Tuple.Create<ISymbol, int>(GetSymbol(x.Key.Item1), x.Key.Item2), x.Value.Select(y => GetSymbol(y)).ToArray())).ToArray(); }
[5695]60      set { allowedChildSymbolsPerIndex = value.ToDictionary(x => Tuple.Create(x.Key.Item1.Name, x.Key.Item2), x => x.Value.Select(y => y.Name).ToList()); }
61    }
[5686]62    #endregion
63
64    protected Dictionary<string, ISymbol> symbols;
[5695]65    protected Dictionary<string, Tuple<int, int>> symbolSubtreeCount;
[5686]66    protected Dictionary<string, List<string>> allowedChildSymbols;
67    protected Dictionary<Tuple<string, int>, List<string>> allowedChildSymbolsPerIndex;
68
[5688]69    public override bool CanChangeName {
70      get { return false; }
71    }
72    public override bool CanChangeDescription {
73      get { return false; }
74    }
75
[5686]76    [StorableConstructor]
77    protected SymbolicExpressionGrammarBase(bool deserializing)
78      : base(deserializing) {
79      cachedMinExpressionLength = new Dictionary<string, int>();
80      cachedMaxExpressionLength = new Dictionary<string, int>();
81      cachedMinExpressionDepth = new Dictionary<string, int>();
82    }
[6233]83
[5686]84    protected SymbolicExpressionGrammarBase(SymbolicExpressionGrammarBase original, Cloner cloner)
85      : base(original, cloner) {
86      cachedMinExpressionLength = new Dictionary<string, int>();
87      cachedMaxExpressionLength = new Dictionary<string, int>();
88      cachedMinExpressionDepth = new Dictionary<string, int>();
89
[6233]90      symbols = original.symbols.ToDictionary(x => x.Key, y => (ISymbol)cloner.Clone(y.Value));
[5695]91      symbolSubtreeCount = new Dictionary<string, Tuple<int, int>>(original.symbolSubtreeCount);
[5686]92
93      allowedChildSymbols = new Dictionary<string, List<string>>();
94      foreach (var element in original.allowedChildSymbols)
95        allowedChildSymbols.Add(element.Key, new List<string>(element.Value));
96
97      allowedChildSymbolsPerIndex = new Dictionary<Tuple<string, int>, List<string>>();
98      foreach (var element in original.allowedChildSymbolsPerIndex)
99        allowedChildSymbolsPerIndex.Add(element.Key, new List<string>(element.Value));
100    }
101
[5688]102    protected SymbolicExpressionGrammarBase(string name, string description)
103      : base(name, description) {
[5686]104      cachedMinExpressionLength = new Dictionary<string, int>();
105      cachedMaxExpressionLength = new Dictionary<string, int>();
106      cachedMinExpressionDepth = new Dictionary<string, int>();
107
108      symbols = new Dictionary<string, ISymbol>();
[5695]109      symbolSubtreeCount = new Dictionary<string, Tuple<int, int>>();
[5686]110      allowedChildSymbols = new Dictionary<string, List<string>>();
111      allowedChildSymbolsPerIndex = new Dictionary<Tuple<string, int>, List<string>>();
112    }
113
114    #region protected grammar manipulation methods
[6443]115    protected virtual void AddSymbol(ISymbol symbol) {
[5686]116      if (ContainsSymbol(symbol)) throw new ArgumentException("Symbol " + symbol + " is already defined.");
117      symbols.Add(symbol.Name, symbol);
[5695]118      symbolSubtreeCount.Add(symbol.Name, Tuple.Create(0, 0));
[5686]119      ClearCaches();
120    }
121
[6443]122    protected virtual void RemoveSymbol(ISymbol symbol) {
[5686]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));
[5695]127      symbolSubtreeCount.Remove(symbol.Name);
[5686]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);
137        }
138      }
139      ClearCaches();
140    }
141
142    public virtual ISymbol GetSymbol(string symbolName) {
143      ISymbol symbol;
144      if (symbols.TryGetValue(symbolName, out symbol)) return symbol;
145      return null;
146    }
147
148    protected void AddAllowedChildSymbol(ISymbol parent, ISymbol child) {
149      List<string> childSymbols;
150      if (!allowedChildSymbols.TryGetValue(parent.Name, out childSymbols)) {
151        childSymbols = new List<string>();
152        allowedChildSymbols.Add(parent.Name, childSymbols);
153      }
[5792]154      if (childSymbols.Contains(child.Name)) throw new ArgumentException();
[5686]155      childSymbols.Add(child.Name);
156      ClearCaches();
157    }
158
159    protected void AddAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) {
160      var key = Tuple.Create(parent.Name, argumentIndex);
161      List<string> childSymbols;
162      if (!allowedChildSymbolsPerIndex.TryGetValue(key, out childSymbols)) {
163        childSymbols = new List<string>();
164        allowedChildSymbolsPerIndex.Add(key, childSymbols);
165      }
166
[5792]167      if (IsAllowedChildSymbol(parent, child)) throw new ArgumentException();
168      if (childSymbols.Contains(child.Name)) throw new ArgumentException();
[5686]169      childSymbols.Add(child.Name);
170      ClearCaches();
171    }
172
173    protected void RemoveAllowedChildSymbol(ISymbol parent, ISymbol child) {
[5792]174      List<string> childSymbols;
175      if (allowedChildSymbols.TryGetValue(child.Name, out childSymbols)) {
176        if (allowedChildSymbols[parent.Name].Remove(child.Name))
177          ClearCaches();
178      }
[5686]179    }
180
181    protected void RemoveAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) {
182      var key = Tuple.Create(parent.Name, argumentIndex);
[5792]183      List<string> childSymbols;
184      if (allowedChildSymbolsPerIndex.TryGetValue(key, out childSymbols)) {
185        if (allowedChildSymbolsPerIndex[key].Remove(child.Name))
186          ClearCaches();
187      }
[5686]188    }
189
190    protected void SetSubtreeCount(ISymbol symbol, int minimumSubtreeCount, int maximumSubtreeCount) {
191      for (int i = GetMaximumSubtreeCount(symbol) - 1; i >= maximumSubtreeCount; i--) {
192        var key = Tuple.Create(symbol.Name, i);
193        allowedChildSymbolsPerIndex.Remove(key);
194      }
195
[5695]196      symbolSubtreeCount[symbol.Name] = Tuple.Create(minimumSubtreeCount, maximumSubtreeCount);
[5686]197      ClearCaches();
198    }
199    #endregion
200
201    #region ISymbolicExpressionGrammarBase Members
202    public virtual IEnumerable<ISymbol> Symbols {
203      get { return symbols.Values; }
204    }
205    public virtual IEnumerable<ISymbol> AllowedSymbols {
206      get { return Symbols.Where(s => !s.InitialFrequency.IsAlmost(0.0)); }
207    }
208    public virtual bool ContainsSymbol(ISymbol symbol) {
209      return symbols.ContainsKey(symbol.Name);
210    }
211
212    public virtual bool IsAllowedChildSymbol(ISymbol parent, ISymbol child) {
213      List<string> temp;
214      if (allowedChildSymbols.TryGetValue(parent.Name, out temp))
215        if (temp.Contains(child.Name)) return true;
216      return false;
217    }
218
219    public virtual bool IsAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) {
220      List<string> temp;
221      if (allowedChildSymbols.TryGetValue(parent.Name, out temp))
222        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);
227      return false;
228    }
229
230    public virtual IEnumerable<ISymbol> GetAllowedChildSymbols(ISymbol parent) {
[6009]231      return from s in AllowedSymbols where IsAllowedChildSymbol(parent, s) select s;
[5686]232    }
233
234    public virtual IEnumerable<ISymbol> GetAllowedChildSymbols(ISymbol parent, int argumentIndex) {
235      var result = Enumerable.Empty<string>();
236
237      List<string> temp;
238      if (allowedChildSymbols.TryGetValue(parent.Name, out temp))
239        result = result.Union(temp);
240      var key = Tuple.Create(parent.Name, argumentIndex);
241      if (allowedChildSymbolsPerIndex.TryGetValue(key, out temp))
242        result = result.Union(temp);
243
244      return result.Select(x => GetSymbol(x));
245    }
246
247    public virtual int GetMinimumSubtreeCount(ISymbol symbol) {
[5695]248      return symbolSubtreeCount[symbol.Name].Item1;
[5686]249    }
250    public virtual int GetMaximumSubtreeCount(ISymbol symbol) {
[5695]251      return symbolSubtreeCount[symbol.Name].Item2;
[5686]252    }
253
254
[6443]255    protected void ClearCaches() {
[5686]256      cachedMinExpressionLength.Clear();
257      cachedMaxExpressionLength.Clear();
258      cachedMinExpressionDepth.Clear();
259    }
260
261    private Dictionary<string, int> cachedMinExpressionLength;
262    public int GetMinimumExpressionLength(ISymbol symbol) {
263      int temp;
264      if (!cachedMinExpressionLength.TryGetValue(symbol.Name, out temp)) {
265        cachedMinExpressionLength[symbol.Name] = int.MaxValue; // prevent infinite recursion
266        long sumOfMinExpressionLengths = 1 + (from argIndex in Enumerable.Range(0, GetMinimumSubtreeCount(symbol))
[6009]267                                              let minForSlot = (long)(from s in AllowedSymbols
[5686]268                                                                      where IsAllowedChildSymbol(symbol, s, argIndex)
269                                                                      select GetMinimumExpressionLength(s)).DefaultIfEmpty(0).Min()
270                                              select minForSlot).DefaultIfEmpty(0).Sum();
271
272        cachedMinExpressionLength[symbol.Name] = (int)Math.Min(sumOfMinExpressionLengths, int.MaxValue);
273        return cachedMinExpressionLength[symbol.Name];
274      }
275      return temp;
276    }
277
278    private Dictionary<string, int> cachedMaxExpressionLength;
279    public int GetMaximumExpressionLength(ISymbol symbol) {
280      int temp;
281      if (!cachedMaxExpressionLength.TryGetValue(symbol.Name, out temp)) {
282        cachedMaxExpressionLength[symbol.Name] = int.MaxValue; // prevent infinite recursion
283        long sumOfMaxTrees = 1 + (from argIndex in Enumerable.Range(0, GetMaximumSubtreeCount(symbol))
[6009]284                                  let maxForSlot = (long)(from s in AllowedSymbols
[5686]285                                                          where IsAllowedChildSymbol(symbol, s, argIndex)
286                                                          select GetMaximumExpressionLength(s)).DefaultIfEmpty(0).Max()
287                                  select maxForSlot).DefaultIfEmpty(0).Sum();
[6009]288        cachedMaxExpressionLength[symbol.Name] = (int)Math.Min(sumOfMaxTrees, int.MaxValue);
[5686]289        return cachedMaxExpressionLength[symbol.Name];
290      }
291      return temp;
292    }
293
294    private Dictionary<string, int> cachedMinExpressionDepth;
295    public int GetMinimumExpressionDepth(ISymbol symbol) {
296      int temp;
297      if (!cachedMinExpressionDepth.TryGetValue(symbol.Name, out temp)) {
298        cachedMinExpressionDepth[symbol.Name] = int.MaxValue; // prevent infinite recursion
[6009]299        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)
302                                                     select GetMinimumExpressionDepth(s)).DefaultIfEmpty(0).Min()
303                             select minForSlot).DefaultIfEmpty(0).Max();
304        cachedMinExpressionDepth[symbol.Name] = (int)Math.Min(minDepth, int.MaxValue);
[5686]305        return cachedMinExpressionDepth[symbol.Name];
306      }
307      return temp;
308    }
309    #endregion
310  }
311}
Note: See TracBrowser for help on using the repository browser.