Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/DefaultSymbolicExpressionGrammar.cs @ 4055

Last change on this file since 4055 was 3993, checked in by mkommend, 14 years ago

changed symbols and grammars to be more efficient in respect to cloning, construction and deserialization (ticket #1073)

File size: 13.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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 System.Text;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using System.Xml;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
31
32namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
33  /// <summary>
34  /// The default symbolic expression grammar stores symbols and syntactic constraints for symbols.
35  /// Symbols are treated as equvivalent if they have the same name.
36  /// Syntactic constraints limit the number of allowed sub trees for a node with a symbol and which symbols are allowed
37  /// in the sub-trees of a symbol (can be specified for each sub-tree index separately).
38  /// </summary>
39  [StorableClass]
40  [Item("DefaultSymbolicExpressionGrammar", "Represents a grammar that defines the syntax of symbolic expression trees.")]
41  public class DefaultSymbolicExpressionGrammar : Item, ISymbolicExpressionGrammar {
42
43    #region properties for separation between implementation and persistence
44    [Storable]
45    private IEnumerable<KeyValuePair<string, int>> MinSubTreeCount {
46      get { return minSubTreeCount.AsEnumerable(); }
47      set { minSubTreeCount = value.ToDictionary(x => x.Key, x => x.Value); }
48    }
49
50    [Storable]
51    private IEnumerable<KeyValuePair<string, int>> MaxSubTreeCount {
52      get { return maxSubTreeCount.AsEnumerable(); }
53      set { maxSubTreeCount = value.ToDictionary(x => x.Key, x => x.Value); }
54    }
55
56    [Storable]
57    private IEnumerable<KeyValuePair<string, IEnumerable<IEnumerable<string>>>> AllowedChildSymbols {
58      get {
59        return (from parentEntry in allowedChildSymbols
60                let setEnumeration = parentEntry.Value.Select(set => set.AsEnumerable()).ToList()
61                select new KeyValuePair<string, IEnumerable<IEnumerable<string>>>(parentEntry.Key, setEnumeration))
62                .ToList();
63      }
64      set {
65        allowedChildSymbols = new Dictionary<string, List<List<string>>>();
66        foreach (var pair in value) {
67          allowedChildSymbols[pair.Key] = new List<List<string>>();
68          foreach (var entry in pair.Value) {
69            var hashSet = new List<string>();
70            foreach (string child in entry) {
71              hashSet.Add(child);
72            }
73            allowedChildSymbols[pair.Key].Add(hashSet);
74          }
75        }
76      }
77    }
78    [Storable]
79    private IEnumerable<KeyValuePair<string, Symbol>> AllSymbols {
80      get { return allSymbols.AsEnumerable(); }
81      set { allSymbols = value.ToDictionary(x => x.Key, x => x.Value); }
82    }
83    #endregion
84
85    private Dictionary<string, int> minSubTreeCount;
86    private Dictionary<string, int> maxSubTreeCount;
87    private Dictionary<string, List<List<string>>> allowedChildSymbols;
88    private Dictionary<string, Symbol> allSymbols;
89    [Storable]
90    private Symbol startSymbol;
91
92    public DefaultSymbolicExpressionGrammar()
93      : base() {
94      minSubTreeCount = new Dictionary<string, int>();
95      maxSubTreeCount = new Dictionary<string, int>();
96      allowedChildSymbols = new Dictionary<string, List<List<string>>>();
97      allSymbols = new Dictionary<string, Symbol>();
98
99      cachedMinExpressionLength = new Dictionary<string, int>();
100      cachedMaxExpressionLength = new Dictionary<string, int>();
101      cachedMinExpressionDepth = new Dictionary<string, int>();
102
103      startSymbol = new StartSymbol();
104      AddSymbol(startSymbol);
105      SetMinSubtreeCount(startSymbol, 1);
106      SetMaxSubtreeCount(startSymbol, 1);
107    }
108
109    //copy constructor for cloning
110    protected DefaultSymbolicExpressionGrammar(DefaultSymbolicExpressionGrammar copy) :base() {
111      this.minSubTreeCount = new Dictionary<string, int>(copy.minSubTreeCount);
112      this.maxSubTreeCount = new Dictionary<string, int>(copy.maxSubTreeCount);
113     
114      this.startSymbol = copy.startSymbol;
115      this.allowedChildSymbols = new Dictionary<string, List<List<string>>>();
116      foreach (var entry in copy.allowedChildSymbols) {
117        this.allowedChildSymbols[entry.Key] = new List<List<string>>(entry.Value.Count);
118        foreach (var set in entry.Value) {
119          this.allowedChildSymbols[entry.Key].Add(new List<string>(set));
120        }
121      }
122      this.allSymbols = new Dictionary<string, Symbol>(copy.allSymbols);
123
124      cachedMinExpressionLength = new Dictionary<string, int>();
125      cachedMaxExpressionLength = new Dictionary<string, int>();
126      cachedMinExpressionDepth = new Dictionary<string, int>();
127    }
128
129    [StorableConstructor]
130    protected DefaultSymbolicExpressionGrammar(bool deserializing)
131      : base(deserializing) {
132      cachedMinExpressionLength = new Dictionary<string, int>();
133      cachedMaxExpressionLength = new Dictionary<string, int>();
134      cachedMinExpressionDepth = new Dictionary<string, int>();
135    }
136
137    public void Clear() {
138      minSubTreeCount.Clear();
139      maxSubTreeCount.Clear();
140      allowedChildSymbols.Clear();
141      allSymbols.Clear();
142
143      cachedMaxExpressionLength.Clear();
144      cachedMinExpressionLength.Clear();
145      cachedMinExpressionDepth.Clear();
146
147      startSymbol = new StartSymbol();
148      AddSymbol(startSymbol);
149      SetMinSubtreeCount(startSymbol, 1);
150      SetMaxSubtreeCount(startSymbol, 1);
151    }
152
153    #region ISymbolicExpressionGrammar Members
154
155    public Symbol StartSymbol {
156      get { return startSymbol; }
157      set { startSymbol = value; }
158    }
159
160    public void AddSymbol(Symbol symbol) {
161      if (ContainsSymbol(symbol)) throw new ArgumentException("Symbol " + symbol + " is already defined.");
162      allSymbols.Add(symbol.Name, symbol);
163      allowedChildSymbols[symbol.Name] = new List<List<string>>();
164      ClearCaches();
165    }
166
167    public void RemoveSymbol(Symbol symbol) {
168      foreach (var parent in Symbols) {
169        for (int i = 0; i < GetMaxSubtreeCount(parent); i++)
170          if (IsAllowedChild(parent, symbol, i))
171            allowedChildSymbols[parent.Name][i].Remove(symbol.Name);
172      }
173      allSymbols.Remove(symbol.Name);
174      minSubTreeCount.Remove(symbol.Name);
175      maxSubTreeCount.Remove(symbol.Name);
176      allowedChildSymbols.Remove(symbol.Name);
177      ClearCaches();
178    }
179
180    public IEnumerable<Symbol> Symbols {
181      get { return allSymbols.Values.AsEnumerable(); }
182    }
183
184    public bool ContainsSymbol(Symbol symbol) {
185      return allSymbols.ContainsKey(symbol.Name);
186    }
187
188    public void SetAllowedChild(Symbol parent, Symbol child, int argumentIndex) {
189      if (!ContainsSymbol(parent)) throw new ArgumentException("Unknown symbol: " + parent, "parent");
190      if (!ContainsSymbol(child)) throw new ArgumentException("Unknown symbol: " + child, "child");
191      if (argumentIndex >= GetMaxSubtreeCount(parent)) throw new ArgumentException("Symbol " + parent + " can have only " + GetMaxSubtreeCount(parent) + " subtrees.");
192      allowedChildSymbols[parent.Name][argumentIndex].Add(child.Name);
193      ClearCaches();
194    }
195
196    public bool IsAllowedChild(Symbol parent, Symbol child, int argumentIndex) {
197      if (!ContainsSymbol(parent)) throw new ArgumentException("Unknown symbol: " + parent, "parent");
198      if (!ContainsSymbol(child)) throw new ArgumentException("Unknown symbol: " + child, "child");
199      if (argumentIndex >= GetMaxSubtreeCount(parent)) throw new ArgumentException("Symbol " + parent + " can have only " + GetMaxSubtreeCount(parent) + " subtrees.");
200      return allowedChildSymbols[parent.Name][argumentIndex].Contains(child.Name);
201    }
202
203    private Dictionary<string, int> cachedMinExpressionLength;
204    public int GetMinExpressionLength(Symbol symbol) {
205      if (!ContainsSymbol(symbol)) throw new ArgumentException("Unknown symbol: " + symbol);
206      if (!cachedMinExpressionLength.ContainsKey(symbol.Name)) {
207        cachedMinExpressionLength[symbol.Name] = int.MaxValue; // prevent infinite recursion
208        long sumOfMinExpressionLengths = 1 + (from argIndex in Enumerable.Range(0, GetMinSubtreeCount(symbol))
209                                              let minForSlot = (long)(from s in Symbols
210                                                                      where IsAllowedChild(symbol, s, argIndex)
211                                                                      select GetMinExpressionLength(s)).DefaultIfEmpty(0).Min()
212                                              select minForSlot).DefaultIfEmpty(0).Sum();
213
214        cachedMinExpressionLength[symbol.Name] = (int)Math.Min(sumOfMinExpressionLengths, int.MaxValue);
215      }
216      return cachedMinExpressionLength[symbol.Name];
217    }
218
219    private Dictionary<string, int> cachedMaxExpressionLength;
220    public int GetMaxExpressionLength(Symbol symbol) {
221      if (!ContainsSymbol(symbol)) throw new ArgumentException("Unknown symbol: " + symbol);
222      if (!cachedMaxExpressionLength.ContainsKey(symbol.Name)) {
223        cachedMaxExpressionLength[symbol.Name] = int.MaxValue; // prevent infinite recursion
224        long sumOfMaxTrees = 1 + (from argIndex in Enumerable.Range(0, GetMaxSubtreeCount(symbol))
225                                  let maxForSlot = (long)(from s in Symbols
226                                                          where IsAllowedChild(symbol, s, argIndex)
227                                                          select GetMaxExpressionLength(s)).DefaultIfEmpty(0).Max()
228                                  select maxForSlot).DefaultIfEmpty(0).Sum();
229        long limit = int.MaxValue;
230        cachedMaxExpressionLength[symbol.Name] = (int)Math.Min(sumOfMaxTrees, limit);
231      }
232      return cachedMaxExpressionLength[symbol.Name];
233    }
234
235    private Dictionary<string, int> cachedMinExpressionDepth;
236    public int GetMinExpressionDepth(Symbol symbol) {
237      if (!ContainsSymbol(symbol)) throw new ArgumentException("Unknown symbol: " + symbol);
238      if (!cachedMinExpressionDepth.ContainsKey(symbol.Name)) {
239        cachedMinExpressionDepth[symbol.Name] = int.MaxValue; // prevent infinite recursion
240        cachedMinExpressionDepth[symbol.Name] = 1 + (from argIndex in Enumerable.Range(0, GetMinSubtreeCount(symbol))
241                                                     let minForSlot = (from s in Symbols
242                                                                       where IsAllowedChild(symbol, s, argIndex)
243                                                                       select GetMinExpressionDepth(s)).DefaultIfEmpty(0).Min()
244                                                     select minForSlot).DefaultIfEmpty(0).Max();
245      }
246      return cachedMinExpressionDepth[symbol.Name];
247    }
248
249    public void SetMaxSubtreeCount(Symbol symbol, int nSubTrees) {
250      if (!ContainsSymbol(symbol)) throw new ArgumentException("Unknown symbol: " + symbol);
251      maxSubTreeCount[symbol.Name] = nSubTrees;
252      while (allowedChildSymbols[symbol.Name].Count <= nSubTrees)
253        allowedChildSymbols[symbol.Name].Add(new List<string>());
254      while (allowedChildSymbols[symbol.Name].Count > nSubTrees) {
255        allowedChildSymbols[symbol.Name].RemoveAt(allowedChildSymbols[symbol.Name].Count - 1);
256      }
257      ClearCaches();
258    }
259
260    public void SetMinSubtreeCount(Symbol symbol, int nSubTrees) {
261      if (!ContainsSymbol(symbol)) throw new ArgumentException("Unknown symbol: " + symbol);
262      minSubTreeCount[symbol.Name] = nSubTrees;
263      ClearCaches();
264    }
265
266    public int GetMinSubtreeCount(Symbol symbol) {
267      if (!ContainsSymbol(symbol)) throw new ArgumentException("Unknown symbol: " + symbol);
268      return minSubTreeCount[symbol.Name];
269    }
270
271    public int GetMaxSubtreeCount(Symbol symbol) {
272      if (!ContainsSymbol(symbol)) throw new ArgumentException("Unknown symbol: " + symbol);
273      return maxSubTreeCount[symbol.Name];
274    }
275
276    #endregion
277
278    private void ClearCaches() {
279      cachedMinExpressionLength.Clear();
280      cachedMaxExpressionLength.Clear();
281      cachedMinExpressionDepth.Clear();
282    }
283
284    public override IDeepCloneable Clone(Cloner cloner) {
285      DefaultSymbolicExpressionGrammar clone = new DefaultSymbolicExpressionGrammar(this);
286      cloner.RegisterClonedObject(this, clone);
287      return clone;
288    }
289  }
290}
Note: See TracBrowser for help on using the repository browser.