Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5510 was 5510, checked in by gkronber, 13 years ago

#1418 Fixed compiler errors in symbolic expression tree encoding

File size: 15.0 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    private Dictionary<string, int> cachedMinExpressionLength;
232    public int GetMinExpressionLength(ISymbol symbol) {
233      if (!ContainsSymbol(symbol)) throw new ArgumentException("Unknown symbol: " + symbol);
234      if (!cachedMinExpressionLength.ContainsKey(symbol.Name)) {
235        cachedMinExpressionLength[symbol.Name] = int.MaxValue; // prevent infinite recursion
236        long sumOfMinExpressionLengths = 1 + (from argIndex in Enumerable.Range(0, GetMinSubtreeCount(symbol))
237                                              let minForSlot = (long)(from s in Symbols
238                                                                      where IsAllowedChild(symbol, s, argIndex)
239                                                                      select GetMinExpressionLength(s)).DefaultIfEmpty(0).Min()
240                                              select minForSlot).DefaultIfEmpty(0).Sum();
241
242        cachedMinExpressionLength[symbol.Name] = (int)Math.Min(sumOfMinExpressionLengths, int.MaxValue);
243      }
244      return cachedMinExpressionLength[symbol.Name];
245    }
246
247    private Dictionary<string, int> cachedMaxExpressionLength;
248    public int GetMaxExpressionLength(ISymbol symbol) {
249      if (!ContainsSymbol(symbol)) throw new ArgumentException("Unknown symbol: " + symbol);
250      if (!cachedMaxExpressionLength.ContainsKey(symbol.Name)) {
251        cachedMaxExpressionLength[symbol.Name] = int.MaxValue; // prevent infinite recursion
252        long sumOfMaxTrees = 1 + (from argIndex in Enumerable.Range(0, GetMaxSubtreeCount(symbol))
253                                  let maxForSlot = (long)(from s in Symbols
254                                                          where IsAllowedChild(symbol, s, argIndex)
255                                                          select GetMaxExpressionLength(s)).DefaultIfEmpty(0).Max()
256                                  select maxForSlot).DefaultIfEmpty(0).Sum();
257        long limit = int.MaxValue;
258        cachedMaxExpressionLength[symbol.Name] = (int)Math.Min(sumOfMaxTrees, limit);
259      }
260      return cachedMaxExpressionLength[symbol.Name];
261    }
262
263    private Dictionary<string, int> cachedMinExpressionDepth;
264    public int GetMinExpressionDepth(ISymbol symbol) {
265      if (!ContainsSymbol(symbol)) throw new ArgumentException("Unknown symbol: " + symbol);
266      if (!cachedMinExpressionDepth.ContainsKey(symbol.Name)) {
267        cachedMinExpressionDepth[symbol.Name] = int.MaxValue; // prevent infinite recursion
268        cachedMinExpressionDepth[symbol.Name] = 1 + (from argIndex in Enumerable.Range(0, GetMinSubtreeCount(symbol))
269                                                     let minForSlot = (from s in Symbols
270                                                                       where IsAllowedChild(symbol, s, argIndex)
271                                                                       select GetMinExpressionDepth(s)).DefaultIfEmpty(0).Min()
272                                                     select minForSlot).DefaultIfEmpty(0).Max();
273      }
274      return cachedMinExpressionDepth[symbol.Name];
275    }
276
277    public void SetMaxSubtreeCount(ISymbol symbol, int nSubTrees) {
278      if (!ContainsSymbol(symbol)) throw new ArgumentException("Unknown symbol: " + symbol);
279      maxSubTreeCount[symbol.Name] = nSubTrees;
280      while (allowedChildSymbols[symbol.Name].Count <= nSubTrees)
281        allowedChildSymbols[symbol.Name].Add(new List<string>());
282      while (allowedChildSymbols[symbol.Name].Count > nSubTrees) {
283        allowedChildSymbols[symbol.Name].RemoveAt(allowedChildSymbols[symbol.Name].Count - 1);
284      }
285      ClearCaches();
286    }
287
288    public void SetMinSubtreeCount(ISymbol symbol, int nSubTrees) {
289      if (!ContainsSymbol(symbol)) throw new ArgumentException("Unknown symbol: " + symbol);
290      minSubTreeCount[symbol.Name] = nSubTrees;
291      ClearCaches();
292    }
293
294    public int GetMinSubtreeCount(ISymbol symbol) {
295      if (!ContainsSymbol(symbol)) throw new ArgumentException("Unknown symbol: " + symbol);
296      return minSubTreeCount[symbol.Name];
297    }
298
299    public int GetMaxSubtreeCount(ISymbol symbol) {
300      if (!ContainsSymbol(symbol)) throw new ArgumentException("Unknown symbol: " + symbol);
301      return maxSubTreeCount[symbol.Name];
302    }
303    #endregion
304
305    private void ClearCaches() {
306      cachedMinExpressionLength.Clear();
307      cachedMaxExpressionLength.Clear();
308      cachedMinExpressionDepth.Clear();
309    }
310
311    protected void InitializeShallowClone(DefaultSymbolicExpressionGrammar original) {
312      minSubTreeCount = new Dictionary<string, int>(original.minSubTreeCount);
313      maxSubTreeCount = new Dictionary<string, int>(original.maxSubTreeCount);
314
315      allSymbols = new Dictionary<string, ISymbol>(original.allSymbols);
316      startSymbol = original.startSymbol;
317      allowedChildSymbols = new Dictionary<string, List<List<string>>>(original.allowedChildSymbols.Count);
318      foreach (var entry in original.allowedChildSymbols) {
319        allowedChildSymbols[entry.Key] = new List<List<string>>(entry.Value.Count);
320        foreach (var set in entry.Value) {
321          allowedChildSymbols[entry.Key].Add(new List<string>(set));
322        }
323      }
324    }
325  }
326}
Note: See TracBrowser for help on using the repository browser.