Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/DefaultSymbolicExpressionGrammar.cs @ 5551

Last change on this file since 5551 was 5529, checked in by gkronber, 14 years ago

#1418 Removed grammar specific members from ISymbolicExpressionTreeNode interface.

File size: 15.2 KB
Line 
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  [Item("DefaultSymbolicExpressionGrammar", "Represents a grammar that defines the syntax of symbolic expression trees.")]
38  public abstract class DefaultSymbolicExpressionGrammar : Item, ISymbolicExpressionTreeGrammar {
39
40    #region properties for separation between implementation and persistence
41    [Storable]
42    private IEnumerable<KeyValuePair<string, int>> MinSubTreeCount {
43      get { return minSubTreeCount.AsEnumerable(); }
44      set { minSubTreeCount = value.ToDictionary(x => x.Key, x => x.Value); }
45    }
46
47    [Storable]
48    private IEnumerable<KeyValuePair<string, int>> MaxSubTreeCount {
49      get { return maxSubTreeCount.AsEnumerable(); }
50      set { maxSubTreeCount = value.ToDictionary(x => x.Key, x => x.Value); }
51    }
52
53    [Storable]
54    private IEnumerable<KeyValuePair<string, IEnumerable<IEnumerable<string>>>> AllowedChildSymbols {
55      get {
56        return (from parentEntry in allowedChildSymbols
57                let setEnumeration = parentEntry.Value.Select(set => set.AsEnumerable()).ToList()
58                select new KeyValuePair<string, IEnumerable<IEnumerable<string>>>(parentEntry.Key, setEnumeration))
59                .ToList();
60      }
61      set {
62        allowedChildSymbols = new Dictionary<string, List<List<string>>>();
63        foreach (var pair in value) {
64          allowedChildSymbols[pair.Key] = new List<List<string>>();
65          foreach (var entry in pair.Value) {
66            var hashSet = new List<string>();
67            foreach (string child in entry) {
68              hashSet.Add(child);
69            }
70            allowedChildSymbols[pair.Key].Add(hashSet);
71          }
72        }
73      }
74    }
75    [Storable]
76    private IEnumerable<KeyValuePair<string, ISymbol>> AllSymbols {
77      get { return allSymbols.AsEnumerable(); }
78      set { allSymbols = value.ToDictionary(x => x.Key, x => x.Value); }
79    }
80    #endregion
81
82    private Dictionary<string, int> minSubTreeCount;
83    private Dictionary<string, int> maxSubTreeCount;
84    private Dictionary<string, List<List<string>>> allowedChildSymbols;
85    private Dictionary<string, ISymbol> allSymbols;
86    [Storable]
87    private ISymbol startSymbol;
88
89    [StorableConstructor]
90    protected DefaultSymbolicExpressionGrammar(bool deserializing)
91      : base(deserializing) {
92      cachedMinExpressionLength = new Dictionary<string, int>();
93      cachedMaxExpressionLength = new Dictionary<string, int>();
94      cachedMinExpressionDepth = new Dictionary<string, int>();
95    }
96    // cloning ctor
97    protected DefaultSymbolicExpressionGrammar(DefaultSymbolicExpressionGrammar original, Cloner cloner)
98      : base(original, cloner) {
99      this.cachedMinExpressionLength = new Dictionary<string, int>();
100      this.cachedMaxExpressionLength = new Dictionary<string, int>();
101      this.cachedMinExpressionDepth = new Dictionary<string, int>();
102      minSubTreeCount = new Dictionary<string, int>(original.minSubTreeCount);
103      maxSubTreeCount = new Dictionary<string, int>(original.maxSubTreeCount);
104
105      allSymbols = new Dictionary<string, ISymbol>();
106      foreach (ISymbol symbol in original.allSymbols.Values.Select(s => cloner.Clone(s)))
107        allSymbols.Add(symbol.Name, symbol);
108
109      startSymbol = cloner.Clone<ISymbol>(original.startSymbol);
110      allowedChildSymbols = new Dictionary<string, List<List<string>>>();
111      foreach (var entry in original.allowedChildSymbols) {
112        allowedChildSymbols[entry.Key] = new List<List<string>>(entry.Value.Count);
113        foreach (var set in entry.Value) {
114          allowedChildSymbols[entry.Key].Add(new List<string>(set));
115        }
116      }
117    }
118    protected DefaultSymbolicExpressionGrammar()
119      : base() {
120      this.minSubTreeCount = new Dictionary<string, int>();
121      this.maxSubTreeCount = new Dictionary<string, int>();
122      this.allowedChildSymbols = new Dictionary<string, List<List<string>>>();
123      this.allSymbols = new Dictionary<string, ISymbol>();
124      this.cachedMinExpressionLength = new Dictionary<string, int>();
125      this.cachedMaxExpressionLength = new Dictionary<string, int>();
126      this.cachedMinExpressionDepth = new Dictionary<string, int>();
127
128      this.startSymbol = new StartSymbol();
129      this.AddSymbol(startSymbol);
130      this.SetMinSubtreeCount(startSymbol, 1);
131      this.SetMaxSubtreeCount(startSymbol, 1);
132    }
133
134    protected DefaultSymbolicExpressionGrammar(ISymbolicExpressionTreeGrammar grammar)
135      : base() {
136      Cloner cloner = new Cloner();
137      this.cachedMinExpressionLength = new Dictionary<string, int>();
138      this.cachedMaxExpressionLength = new Dictionary<string, int>();
139      this.cachedMinExpressionDepth = new Dictionary<string, int>();
140
141      this.minSubTreeCount = new Dictionary<string, int>();
142      this.maxSubTreeCount = new Dictionary<string, int>();
143      this.allowedChildSymbols = new Dictionary<string, List<List<string>>>();
144      this.allSymbols = new Dictionary<string, ISymbol>();
145
146      this.StartSymbol = (ISymbol)cloner.Clone(grammar.StartSymbol);
147
148      foreach (ISymbol symbol in grammar.Symbols) {
149        ISymbol clonedSymbol = (ISymbol)cloner.Clone(symbol);
150        this.AddSymbol(clonedSymbol);
151        this.SetMinSubtreeCount(clonedSymbol, grammar.GetMinSubtreeCount(symbol));
152        this.SetMaxSubtreeCount(clonedSymbol, grammar.GetMaxSubtreeCount(symbol));
153      }
154
155      foreach (ISymbol parent in grammar.Symbols) {
156        for (int i = 0; i < grammar.GetMaxSubtreeCount(parent); i++) {
157          foreach (ISymbol child in grammar.Symbols) {
158            if (grammar.IsAllowedChild(parent, child, i)) {
159              this.SetAllowedChild((ISymbol)cloner.Clone(parent), (ISymbol)cloner.Clone(child), i);
160            }
161          }
162        }
163      }
164    }
165
166    public void Clear() {
167      minSubTreeCount.Clear();
168      maxSubTreeCount.Clear();
169      allowedChildSymbols.Clear();
170      allSymbols.Clear();
171
172      cachedMaxExpressionLength.Clear();
173      cachedMinExpressionLength.Clear();
174      cachedMinExpressionDepth.Clear();
175
176      startSymbol = new StartSymbol();
177      AddSymbol(startSymbol);
178      SetMinSubtreeCount(startSymbol, 1);
179      SetMaxSubtreeCount(startSymbol, 1);
180    }
181
182    #region ISymbolicExpressionGrammar Members
183    public ISymbol StartSymbol {
184      get { return startSymbol; }
185      set { startSymbol = value; }
186    }
187
188    public void AddSymbol(ISymbol symbol) {
189      if (ContainsSymbol(symbol)) throw new ArgumentException("Symbol " + symbol + " is already defined.");
190      allSymbols.Add(symbol.Name, symbol);
191      allowedChildSymbols[symbol.Name] = new List<List<string>>();
192      ClearCaches();
193    }
194
195    public void RemoveSymbol(ISymbol symbol) {
196      foreach (var parent in Symbols) {
197        for (int i = 0; i < GetMaxSubtreeCount(parent); i++)
198          if (IsAllowedChild(parent, symbol, i))
199            allowedChildSymbols[parent.Name][i].Remove(symbol.Name);
200      }
201      allSymbols.Remove(symbol.Name);
202      minSubTreeCount.Remove(symbol.Name);
203      maxSubTreeCount.Remove(symbol.Name);
204      allowedChildSymbols.Remove(symbol.Name);
205      ClearCaches();
206    }
207
208    public IEnumerable<ISymbol> Symbols {
209      get { return allSymbols.Values.AsEnumerable(); }
210    }
211
212    public bool ContainsSymbol(ISymbol symbol) {
213      return allSymbols.ContainsKey(symbol.Name);
214    }
215
216    public void SetAllowedChild(ISymbol parent, ISymbol child, int argumentIndex) {
217      if (!ContainsSymbol(parent)) throw new ArgumentException("Unknown symbol: " + parent, "parent");
218      if (!ContainsSymbol(child)) throw new ArgumentException("Unknown symbol: " + child, "child");
219      if (argumentIndex >= GetMaxSubtreeCount(parent)) throw new ArgumentException("Symbol " + parent + " can have only " + GetMaxSubtreeCount(parent) + " subtrees.");
220      allowedChildSymbols[parent.Name][argumentIndex].Add(child.Name);
221      ClearCaches();
222    }
223
224    public bool IsAllowedChild(ISymbol parent, ISymbol child, int argumentIndex) {
225      if (!ContainsSymbol(parent)) throw new ArgumentException("Unknown symbol: " + parent, "parent");
226      if (!ContainsSymbol(child)) throw new ArgumentException("Unknown symbol: " + child, "child");
227      if (argumentIndex >= GetMaxSubtreeCount(parent)) throw new ArgumentException("Symbol " + parent + " can have only " + GetMaxSubtreeCount(parent) + " subtrees.");
228      return allowedChildSymbols[parent.Name][argumentIndex].Contains(child.Name);
229    }
230
231    public IEnumerable<ISymbol> GetAllowedSymbols(ISymbol parent, int argumentIndex) {
232      return allowedChildSymbols[parent.Name][argumentIndex].Select(x => allSymbols[x]);
233    }
234
235    private Dictionary<string, int> cachedMinExpressionLength;
236    public int GetMinExpressionLength(ISymbol symbol) {
237      if (!ContainsSymbol(symbol)) throw new ArgumentException("Unknown symbol: " + symbol);
238      if (!cachedMinExpressionLength.ContainsKey(symbol.Name)) {
239        cachedMinExpressionLength[symbol.Name] = int.MaxValue; // prevent infinite recursion
240        long sumOfMinExpressionLengths = 1 + (from argIndex in Enumerable.Range(0, GetMinSubtreeCount(symbol))
241                                              let minForSlot = (long)(from s in Symbols
242                                                                      where IsAllowedChild(symbol, s, argIndex)
243                                                                      select GetMinExpressionLength(s)).DefaultIfEmpty(0).Min()
244                                              select minForSlot).DefaultIfEmpty(0).Sum();
245
246        cachedMinExpressionLength[symbol.Name] = (int)Math.Min(sumOfMinExpressionLengths, int.MaxValue);
247      }
248      return cachedMinExpressionLength[symbol.Name];
249    }
250
251    private Dictionary<string, int> cachedMaxExpressionLength;
252    public int GetMaxExpressionLength(ISymbol symbol) {
253      if (!ContainsSymbol(symbol)) throw new ArgumentException("Unknown symbol: " + symbol);
254      if (!cachedMaxExpressionLength.ContainsKey(symbol.Name)) {
255        cachedMaxExpressionLength[symbol.Name] = int.MaxValue; // prevent infinite recursion
256        long sumOfMaxTrees = 1 + (from argIndex in Enumerable.Range(0, GetMaxSubtreeCount(symbol))
257                                  let maxForSlot = (long)(from s in Symbols
258                                                          where IsAllowedChild(symbol, s, argIndex)
259                                                          select GetMaxExpressionLength(s)).DefaultIfEmpty(0).Max()
260                                  select maxForSlot).DefaultIfEmpty(0).Sum();
261        long limit = int.MaxValue;
262        cachedMaxExpressionLength[symbol.Name] = (int)Math.Min(sumOfMaxTrees, limit);
263      }
264      return cachedMaxExpressionLength[symbol.Name];
265    }
266
267    private Dictionary<string, int> cachedMinExpressionDepth;
268    public int GetMinExpressionDepth(ISymbol symbol) {
269      if (!ContainsSymbol(symbol)) throw new ArgumentException("Unknown symbol: " + symbol);
270      if (!cachedMinExpressionDepth.ContainsKey(symbol.Name)) {
271        cachedMinExpressionDepth[symbol.Name] = int.MaxValue; // prevent infinite recursion
272        cachedMinExpressionDepth[symbol.Name] = 1 + (from argIndex in Enumerable.Range(0, GetMinSubtreeCount(symbol))
273                                                     let minForSlot = (from s in Symbols
274                                                                       where IsAllowedChild(symbol, s, argIndex)
275                                                                       select GetMinExpressionDepth(s)).DefaultIfEmpty(0).Min()
276                                                     select minForSlot).DefaultIfEmpty(0).Max();
277      }
278      return cachedMinExpressionDepth[symbol.Name];
279    }
280
281    public void SetMaxSubtreeCount(ISymbol symbol, int nSubTrees) {
282      if (!ContainsSymbol(symbol)) throw new ArgumentException("Unknown symbol: " + symbol);
283      maxSubTreeCount[symbol.Name] = nSubTrees;
284      while (allowedChildSymbols[symbol.Name].Count <= nSubTrees)
285        allowedChildSymbols[symbol.Name].Add(new List<string>());
286      while (allowedChildSymbols[symbol.Name].Count > nSubTrees) {
287        allowedChildSymbols[symbol.Name].RemoveAt(allowedChildSymbols[symbol.Name].Count - 1);
288      }
289      ClearCaches();
290    }
291
292    public void SetMinSubtreeCount(ISymbol symbol, int nSubTrees) {
293      if (!ContainsSymbol(symbol)) throw new ArgumentException("Unknown symbol: " + symbol);
294      minSubTreeCount[symbol.Name] = nSubTrees;
295      ClearCaches();
296    }
297
298    public int GetMinSubtreeCount(ISymbol symbol) {
299      if (!ContainsSymbol(symbol)) throw new ArgumentException("Unknown symbol: " + symbol);
300      return minSubTreeCount[symbol.Name];
301    }
302
303    public int GetMaxSubtreeCount(ISymbol symbol) {
304      if (!ContainsSymbol(symbol)) throw new ArgumentException("Unknown symbol: " + symbol);
305      return maxSubTreeCount[symbol.Name];
306    }
307    #endregion
308
309    private void ClearCaches() {
310      cachedMinExpressionLength.Clear();
311      cachedMaxExpressionLength.Clear();
312      cachedMinExpressionDepth.Clear();
313    }
314
315    protected void InitializeShallowClone(DefaultSymbolicExpressionGrammar original) {
316      minSubTreeCount = new Dictionary<string, int>(original.minSubTreeCount);
317      maxSubTreeCount = new Dictionary<string, int>(original.maxSubTreeCount);
318
319      allSymbols = new Dictionary<string, ISymbol>(original.allSymbols);
320      startSymbol = original.startSymbol;
321      allowedChildSymbols = new Dictionary<string, List<List<string>>>(original.allowedChildSymbols.Count);
322      foreach (var entry in original.allowedChildSymbols) {
323        allowedChildSymbols[entry.Key] = new List<List<string>>(entry.Value.Count);
324        foreach (var set in entry.Value) {
325          allowedChildSymbols[entry.Key].Add(new List<string>(set));
326        }
327      }
328    }
329  }
330}
Note: See TracBrowser for help on using the repository browser.