[3294] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using System.Text;
|
---|
[3376] | 26 | using HeuristicLab.Common;
|
---|
[3294] | 27 | using HeuristicLab.Core;
|
---|
| 28 | using System.Xml;
|
---|
| 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[3462] | 30 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
|
---|
[3294] | 31 |
|
---|
| 32 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
|
---|
[3369] | 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>
|
---|
[3294] | 39 | [StorableClass]
|
---|
| 40 | [Item("DefaultSymbolicExpressionGrammar", "Represents a grammar that defines the syntax of symbolic expression trees.")]
|
---|
| 41 | public class DefaultSymbolicExpressionGrammar : Item, ISymbolicExpressionGrammar {
|
---|
[3541] | 42 |
|
---|
| 43 | #region properties for separation between implementation and persistence
|
---|
[3294] | 44 | [Storable]
|
---|
[3541] | 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<HashSet<string>>>();
|
---|
| 66 | foreach (var pair in value) {
|
---|
| 67 | allowedChildSymbols[pair.Key] = new List<HashSet<string>>();
|
---|
| 68 | foreach (var entry in pair.Value) {
|
---|
| 69 | var hashSet = new HashSet<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 |
|
---|
[3294] | 85 | private Dictionary<string, int> minSubTreeCount;
|
---|
| 86 | private Dictionary<string, int> maxSubTreeCount;
|
---|
[3338] | 87 | private Dictionary<string, List<HashSet<string>>> allowedChildSymbols;
|
---|
[3541] | 88 | private Dictionary<string, Symbol> allSymbols;
|
---|
[3294] | 89 | [Storable]
|
---|
[3541] | 90 | private Symbol startSymbol;
|
---|
[3294] | 91 |
|
---|
[3338] | 92 | public DefaultSymbolicExpressionGrammar()
|
---|
[3294] | 93 | : base() {
|
---|
[3541] | 94 | Clear();
|
---|
[3294] | 95 | }
|
---|
| 96 |
|
---|
[3541] | 97 | public void Clear() {
|
---|
| 98 | minSubTreeCount = new Dictionary<string, int>();
|
---|
| 99 | maxSubTreeCount = new Dictionary<string, int>();
|
---|
| 100 | allowedChildSymbols = new Dictionary<string, List<HashSet<string>>>();
|
---|
| 101 | allSymbols = new Dictionary<string, Symbol>();
|
---|
| 102 | cachedMinExpressionLength = new Dictionary<string, int>();
|
---|
| 103 | cachedMaxExpressionLength = new Dictionary<string, int>();
|
---|
| 104 | cachedMinExpressionDepth = new Dictionary<string, int>();
|
---|
| 105 |
|
---|
[3294] | 106 | startSymbol = new StartSymbol();
|
---|
[3338] | 107 | AddSymbol(startSymbol);
|
---|
| 108 | SetMinSubtreeCount(startSymbol, 1);
|
---|
| 109 | SetMaxSubtreeCount(startSymbol, 1);
|
---|
| 110 | }
|
---|
[3294] | 111 |
|
---|
[3338] | 112 | #region ISymbolicExpressionGrammar Members
|
---|
| 113 |
|
---|
| 114 | public Symbol StartSymbol {
|
---|
| 115 | get { return startSymbol; }
|
---|
| 116 | set { startSymbol = value; }
|
---|
[3294] | 117 | }
|
---|
| 118 |
|
---|
| 119 |
|
---|
[3338] | 120 | public void AddSymbol(Symbol symbol) {
|
---|
[3369] | 121 | if (ContainsSymbol(symbol)) throw new ArgumentException("Symbol " + symbol + " is already defined.");
|
---|
| 122 | allSymbols.Add(symbol.Name, symbol);
|
---|
[3338] | 123 | allowedChildSymbols[symbol.Name] = new List<HashSet<string>>();
|
---|
[3294] | 124 | ClearCaches();
|
---|
| 125 | }
|
---|
| 126 |
|
---|
[3338] | 127 | public void RemoveSymbol(Symbol symbol) {
|
---|
[3360] | 128 | foreach (var parent in Symbols) {
|
---|
| 129 | for (int i = 0; i < GetMaxSubtreeCount(parent); i++)
|
---|
| 130 | if (IsAllowedChild(parent, symbol, i))
|
---|
| 131 | allowedChildSymbols[parent.Name][i].Remove(symbol.Name);
|
---|
| 132 | }
|
---|
[3369] | 133 | allSymbols.Remove(symbol.Name);
|
---|
[3338] | 134 | minSubTreeCount.Remove(symbol.Name);
|
---|
| 135 | maxSubTreeCount.Remove(symbol.Name);
|
---|
| 136 | allowedChildSymbols.Remove(symbol.Name);
|
---|
[3294] | 137 | ClearCaches();
|
---|
| 138 | }
|
---|
| 139 |
|
---|
[3338] | 140 | public IEnumerable<Symbol> Symbols {
|
---|
[3369] | 141 | get { return allSymbols.Values.AsEnumerable(); }
|
---|
[3294] | 142 | }
|
---|
| 143 |
|
---|
[3369] | 144 | public bool ContainsSymbol(Symbol symbol) {
|
---|
| 145 | return allSymbols.ContainsKey(symbol.Name);
|
---|
| 146 | }
|
---|
| 147 |
|
---|
[3338] | 148 | public void SetAllowedChild(Symbol parent, Symbol child, int argumentIndex) {
|
---|
[3369] | 149 | if (!ContainsSymbol(parent)) throw new ArgumentException("Unknown symbol: " + parent, "parent");
|
---|
| 150 | if (!ContainsSymbol(child)) throw new ArgumentException("Unknown symbol: " + child, "child");
|
---|
[3338] | 151 | if (argumentIndex >= GetMaxSubtreeCount(parent)) throw new ArgumentException("Symbol " + parent + " can have only " + GetMaxSubtreeCount(parent) + " subtrees.");
|
---|
| 152 | allowedChildSymbols[parent.Name][argumentIndex].Add(child.Name);
|
---|
| 153 | ClearCaches();
|
---|
[3294] | 154 | }
|
---|
| 155 |
|
---|
[3338] | 156 | public bool IsAllowedChild(Symbol parent, Symbol child, int argumentIndex) {
|
---|
[3369] | 157 | if (!ContainsSymbol(parent)) throw new ArgumentException("Unknown symbol: " + parent, "parent");
|
---|
| 158 | if (!ContainsSymbol(child)) throw new ArgumentException("Unknown symbol: " + child, "child");
|
---|
[3338] | 159 | if (argumentIndex >= GetMaxSubtreeCount(parent)) throw new ArgumentException("Symbol " + parent + " can have only " + GetMaxSubtreeCount(parent) + " subtrees.");
|
---|
[3369] | 160 | return allowedChildSymbols[parent.Name][argumentIndex].Contains(child.Name);
|
---|
[3294] | 161 | }
|
---|
| 162 |
|
---|
[3338] | 163 | private Dictionary<string, int> cachedMinExpressionLength;
|
---|
| 164 | public int GetMinExpressionLength(Symbol symbol) {
|
---|
[3369] | 165 | if (!ContainsSymbol(symbol)) throw new ArgumentException("Unknown symbol: " + symbol);
|
---|
[3338] | 166 | if (!cachedMinExpressionLength.ContainsKey(symbol.Name)) {
|
---|
| 167 | cachedMinExpressionLength[symbol.Name] = int.MaxValue; // prevent infinite recursion
|
---|
| 168 | long sumOfMinExpressionLengths = 1 + (from argIndex in Enumerable.Range(0, GetMinSubtreeCount(symbol))
|
---|
[3369] | 169 | let minForSlot = (long)(from s in Symbols
|
---|
[3338] | 170 | where IsAllowedChild(symbol, s, argIndex)
|
---|
| 171 | select GetMinExpressionLength(s)).DefaultIfEmpty(0).Min()
|
---|
| 172 | select minForSlot).DefaultIfEmpty(0).Sum();
|
---|
[3294] | 173 |
|
---|
[3338] | 174 | cachedMinExpressionLength[symbol.Name] = (int)Math.Min(sumOfMinExpressionLengths, int.MaxValue);
|
---|
[3294] | 175 | }
|
---|
[3338] | 176 | return cachedMinExpressionLength[symbol.Name];
|
---|
[3294] | 177 | }
|
---|
| 178 |
|
---|
[3338] | 179 | private Dictionary<string, int> cachedMaxExpressionLength;
|
---|
| 180 | public int GetMaxExpressionLength(Symbol symbol) {
|
---|
[3369] | 181 | if (!ContainsSymbol(symbol)) throw new ArgumentException("Unknown symbol: " + symbol);
|
---|
[3338] | 182 | if (!cachedMaxExpressionLength.ContainsKey(symbol.Name)) {
|
---|
| 183 | cachedMaxExpressionLength[symbol.Name] = int.MaxValue; // prevent infinite recursion
|
---|
| 184 | long sumOfMaxTrees = 1 + (from argIndex in Enumerable.Range(0, GetMaxSubtreeCount(symbol))
|
---|
[3369] | 185 | let maxForSlot = (long)(from s in Symbols
|
---|
[3338] | 186 | where IsAllowedChild(symbol, s, argIndex)
|
---|
| 187 | select GetMaxExpressionLength(s)).DefaultIfEmpty(0).Max()
|
---|
[3294] | 188 | select maxForSlot).DefaultIfEmpty(0).Sum();
|
---|
| 189 | long limit = int.MaxValue;
|
---|
[3338] | 190 | cachedMaxExpressionLength[symbol.Name] = (int)Math.Min(sumOfMaxTrees, limit);
|
---|
[3294] | 191 | }
|
---|
[3338] | 192 | return cachedMaxExpressionLength[symbol.Name];
|
---|
[3294] | 193 | }
|
---|
| 194 |
|
---|
[3338] | 195 | private Dictionary<string, int> cachedMinExpressionDepth;
|
---|
| 196 | public int GetMinExpressionDepth(Symbol symbol) {
|
---|
[3369] | 197 | if (!ContainsSymbol(symbol)) throw new ArgumentException("Unknown symbol: " + symbol);
|
---|
[3338] | 198 | if (!cachedMinExpressionDepth.ContainsKey(symbol.Name)) {
|
---|
| 199 | cachedMinExpressionDepth[symbol.Name] = int.MaxValue; // prevent infinite recursion
|
---|
| 200 | cachedMinExpressionDepth[symbol.Name] = 1 + (from argIndex in Enumerable.Range(0, GetMinSubtreeCount(symbol))
|
---|
[3369] | 201 | let minForSlot = (from s in Symbols
|
---|
[3338] | 202 | where IsAllowedChild(symbol, s, argIndex)
|
---|
| 203 | select GetMinExpressionDepth(s)).DefaultIfEmpty(0).Min()
|
---|
| 204 | select minForSlot).DefaultIfEmpty(0).Max();
|
---|
[3294] | 205 | }
|
---|
[3338] | 206 | return cachedMinExpressionDepth[symbol.Name];
|
---|
[3294] | 207 | }
|
---|
| 208 |
|
---|
[3338] | 209 | public void SetMaxSubtreeCount(Symbol symbol, int nSubTrees) {
|
---|
[3369] | 210 | if (!ContainsSymbol(symbol)) throw new ArgumentException("Unknown symbol: " + symbol);
|
---|
[3338] | 211 | maxSubTreeCount[symbol.Name] = nSubTrees;
|
---|
| 212 | while (allowedChildSymbols[symbol.Name].Count <= nSubTrees)
|
---|
| 213 | allowedChildSymbols[symbol.Name].Add(new HashSet<string>());
|
---|
| 214 | while (allowedChildSymbols[symbol.Name].Count > nSubTrees) {
|
---|
| 215 | allowedChildSymbols[symbol.Name].RemoveAt(allowedChildSymbols[symbol.Name].Count - 1);
|
---|
| 216 | }
|
---|
| 217 | ClearCaches();
|
---|
[3294] | 218 | }
|
---|
| 219 |
|
---|
[3338] | 220 | public void SetMinSubtreeCount(Symbol symbol, int nSubTrees) {
|
---|
[3369] | 221 | if (!ContainsSymbol(symbol)) throw new ArgumentException("Unknown symbol: " + symbol);
|
---|
[3338] | 222 | minSubTreeCount[symbol.Name] = nSubTrees;
|
---|
| 223 | ClearCaches();
|
---|
[3294] | 224 | }
|
---|
| 225 |
|
---|
[3338] | 226 | public int GetMinSubtreeCount(Symbol symbol) {
|
---|
[3369] | 227 | if (!ContainsSymbol(symbol)) throw new ArgumentException("Unknown symbol: " + symbol);
|
---|
[3338] | 228 | return minSubTreeCount[symbol.Name];
|
---|
[3294] | 229 | }
|
---|
| 230 |
|
---|
[3338] | 231 | public int GetMaxSubtreeCount(Symbol symbol) {
|
---|
[3369] | 232 | if (!ContainsSymbol(symbol)) throw new ArgumentException("Unknown symbol: " + symbol);
|
---|
[3338] | 233 | return maxSubTreeCount[symbol.Name];
|
---|
[3294] | 234 | }
|
---|
| 235 |
|
---|
[3338] | 236 | #endregion
|
---|
| 237 |
|
---|
| 238 | private void ClearCaches() {
|
---|
| 239 | cachedMinExpressionLength.Clear();
|
---|
| 240 | cachedMaxExpressionLength.Clear();
|
---|
| 241 | cachedMinExpressionDepth.Clear();
|
---|
[3294] | 242 | }
|
---|
| 243 |
|
---|
[3338] | 244 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 245 | DefaultSymbolicExpressionGrammar clone = (DefaultSymbolicExpressionGrammar)base.Clone(cloner);
|
---|
| 246 | clone.maxSubTreeCount = new Dictionary<string, int>(maxSubTreeCount);
|
---|
| 247 | clone.minSubTreeCount = new Dictionary<string, int>(minSubTreeCount);
|
---|
| 248 | clone.startSymbol = startSymbol;
|
---|
[3360] | 249 | clone.allowedChildSymbols = new Dictionary<string, List<HashSet<string>>>();
|
---|
| 250 | foreach (var entry in allowedChildSymbols) {
|
---|
| 251 | clone.allowedChildSymbols[entry.Key] = new List<HashSet<string>>();
|
---|
| 252 | foreach (var set in entry.Value) {
|
---|
| 253 | clone.allowedChildSymbols[entry.Key].Add(new HashSet<string>(set));
|
---|
| 254 | }
|
---|
| 255 | }
|
---|
[3369] | 256 | clone.allSymbols = new Dictionary<string, Symbol>(allSymbols);
|
---|
[3338] | 257 | return clone;
|
---|
[3294] | 258 | }
|
---|
| 259 | }
|
---|
| 260 | }
|
---|