Changeset 6803 for trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionGrammarBase.cs
- Timestamp:
- 09/20/11 11:12:10 (13 years ago)
- Location:
- trunk/sources
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources
- Property svn:ignore
-
old new 13 13 *.vsp 14 14 *.docstates 15 bin 16 HeuristicLab 3.3.6.0.ReSharper.user
-
- Property svn:mergeinfo changed
/branches/GP.Grammar.Editor (added) merged: 6284-6285,6296,6299,6335,6337,6377,6379,6387,6403,6409,6415,6493-6494,6497,6618,6620,6622,6626,6647,6675,6782,6784,6786,6795
- Property svn:ignore
-
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding
-
Property
svn:ignore
set to
bin
-
Property
svn:ignore
set to
-
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionGrammarBase.cs
r6443 r6803 36 36 [StorableClass] 37 37 public abstract class SymbolicExpressionGrammarBase : NamedItem, ISymbolicExpressionGrammarBase { 38 38 39 #region properties for separation between implementation and persistence 39 40 [Storable(Name = "Symbols")] … … 62 63 #endregion 63 64 65 private bool suppressEvents; 64 66 protected Dictionary<string, ISymbol> symbols; 65 67 protected Dictionary<string, Tuple<int, int>> symbolSubtreeCount; … … 80 82 cachedMaxExpressionLength = new Dictionary<string, int>(); 81 83 cachedMinExpressionDepth = new Dictionary<string, int>(); 84 85 suppressEvents = false; 82 86 } 83 87 … … 98 102 foreach (var element in original.allowedChildSymbolsPerIndex) 99 103 allowedChildSymbolsPerIndex.Add(element.Key, new List<string>(element.Value)); 104 105 suppressEvents = false; 100 106 } 101 107 … … 110 116 allowedChildSymbols = new Dictionary<string, List<string>>(); 111 117 allowedChildSymbolsPerIndex = new Dictionary<Tuple<string, int>, List<string>>(); 118 119 suppressEvents = false; 112 120 } 113 121 … … 115 123 protected virtual void AddSymbol(ISymbol symbol) { 116 124 if (ContainsSymbol(symbol)) throw new ArgumentException("Symbol " + symbol + " is already defined."); 117 symbols.Add(symbol.Name, symbol); 118 symbolSubtreeCount.Add(symbol.Name, Tuple.Create(0, 0)); 125 foreach (var s in symbol.Flatten()) { 126 symbols.Add(s.Name, s); 127 symbolSubtreeCount.Add(s.Name, Tuple.Create(s.MinimumArity, s.MaximumArity)); 128 } 119 129 ClearCaches(); 120 130 } 121 131 122 132 protected virtual void RemoveSymbol(ISymbol symbol) { 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)); 127 symbolSubtreeCount.Remove(symbol.Name); 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); 133 foreach (var s in symbol.Flatten()) { 134 symbols.Remove(s.Name); 135 allowedChildSymbols.Remove(s.Name); 136 for (int i = 0; i < GetMaximumSubtreeCount(s); i++) 137 allowedChildSymbolsPerIndex.Remove(Tuple.Create(s.Name, i)); 138 symbolSubtreeCount.Remove(s.Name); 139 140 foreach (var parent in Symbols) { 141 List<string> allowedChilds; 142 if (allowedChildSymbols.TryGetValue(parent.Name, out allowedChilds)) 143 allowedChilds.Remove(s.Name); 144 145 for (int i = 0; i < GetMaximumSubtreeCount(parent); i++) { 146 if (allowedChildSymbolsPerIndex.TryGetValue(Tuple.Create(parent.Name, i), out allowedChilds)) 147 allowedChilds.Remove(s.Name); 148 } 137 149 } 150 suppressEvents = true; 151 foreach (var groupSymbol in Symbols.OfType<GroupSymbol>()) 152 groupSymbol.SymbolsCollection.Remove(symbol); 153 suppressEvents = false; 138 154 } 139 155 ClearCaches(); … … 147 163 148 164 protected void AddAllowedChildSymbol(ISymbol parent, ISymbol child) { 165 bool changed = false; 166 167 foreach (ISymbol p in parent.Flatten().Where(p => !(p is GroupSymbol))) 168 changed |= AddAllowedChildSymbolToDictionaries(p, child); 169 170 if (changed) { 171 ClearCaches(); 172 OnChanged(); 173 } 174 } 175 176 private bool AddAllowedChildSymbolToDictionaries(ISymbol parent, ISymbol child) { 149 177 List<string> childSymbols; 150 178 if (!allowedChildSymbols.TryGetValue(parent.Name, out childSymbols)) { … … 152 180 allowedChildSymbols.Add(parent.Name, childSymbols); 153 181 } 154 if (childSymbols.Contains(child.Name)) throw new ArgumentException(); 182 if (childSymbols.Contains(child.Name)) return false; 183 184 suppressEvents = true; 185 for (int argumentIndex = 0; argumentIndex < GetMaximumSubtreeCount(parent); argumentIndex++) 186 RemoveAllowedChildSymbol(parent, child, argumentIndex); 187 suppressEvents = false; 188 155 189 childSymbols.Add(child.Name); 156 ClearCaches();190 return true; 157 191 } 158 192 159 193 protected void AddAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) { 194 bool changed = false; 195 196 foreach (ISymbol p in parent.Flatten().Where(p => !(p is GroupSymbol))) 197 changed |= AddAllowedChildSymbolToDictionaries(p, child, argumentIndex); 198 199 if (changed) { 200 ClearCaches(); 201 OnChanged(); 202 } 203 } 204 205 206 private bool AddAllowedChildSymbolToDictionaries(ISymbol parent, ISymbol child, int argumentIndex) { 207 List<string> childSymbols; 208 if (!allowedChildSymbols.TryGetValue(parent.Name, out childSymbols)) { 209 childSymbols = new List<string>(); 210 allowedChildSymbols.Add(parent.Name, childSymbols); 211 } 212 if (childSymbols.Contains(child.Name)) return false; 213 214 160 215 var key = Tuple.Create(parent.Name, argumentIndex); 161 List<string> childSymbols;162 216 if (!allowedChildSymbolsPerIndex.TryGetValue(key, out childSymbols)) { 163 217 childSymbols = new List<string>(); … … 165 219 } 166 220 167 if ( IsAllowedChildSymbol(parent, child)) throw new ArgumentException();168 if (childSymbols.Contains(child.Name)) throw new ArgumentException(); 221 if (childSymbols.Contains(child.Name)) return false; 222 169 223 childSymbols.Add(child.Name); 170 ClearCaches();224 return true; 171 225 } 172 226 173 227 protected void RemoveAllowedChildSymbol(ISymbol parent, ISymbol child) { 228 bool changed = false; 174 229 List<string> childSymbols; 175 230 if (allowedChildSymbols.TryGetValue(child.Name, out childSymbols)) { 176 if (allowedChildSymbols[parent.Name].Remove(child.Name)) 177 ClearCaches(); 231 changed |= childSymbols.Remove(child.Name); 232 } 233 234 for (int argumentIndex = 0; argumentIndex < GetMaximumSubtreeCount(parent); argumentIndex++) { 235 var key = Tuple.Create(parent.Name, argumentIndex); 236 if (allowedChildSymbolsPerIndex.TryGetValue(key, out childSymbols)) 237 changed |= childSymbols.Remove(child.Name); 238 } 239 240 if (changed) { 241 ClearCaches(); 242 OnChanged(); 178 243 } 179 244 } 180 245 181 246 protected void RemoveAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) { 247 bool changed = false; 248 249 suppressEvents = true; 250 List<string> childSymbols; 251 if (allowedChildSymbols.TryGetValue(parent.Name, out childSymbols)) { 252 if (childSymbols.Remove(child.Name)) { 253 for (int i = 0; i < GetMaximumSubtreeCount(parent); i++) { 254 if (i != argumentIndex) AddAllowedChildSymbol(parent, child, i); 255 } 256 changed = true; 257 } 258 } 259 suppressEvents = false; 260 182 261 var key = Tuple.Create(parent.Name, argumentIndex); 183 List<string> childSymbols; 184 if (allowedChildSymbolsPerIndex.TryGetValue(key, out childSymbols)) { 185 if (allowedChildSymbolsPerIndex[key].Remove(child.Name)) 186 ClearCaches(); 262 if (allowedChildSymbolsPerIndex.TryGetValue(key, out childSymbols)) 263 changed |= childSymbols.Remove(child.Name); 264 265 if (changed) { 266 ClearCaches(); 267 OnChanged(); 187 268 } 188 269 } 189 270 190 271 protected void SetSubtreeCount(ISymbol symbol, int minimumSubtreeCount, int maximumSubtreeCount) { 191 for (int i = GetMaximumSubtreeCount(symbol) - 1; i >= maximumSubtreeCount; i--) { 272 var symbols = symbol.Flatten().Where(s => !(s is GroupSymbol)); 273 if (symbols.Any(s => s.MinimumArity > minimumSubtreeCount)) throw new ArgumentException("Invalid minimum subtree count " + minimumSubtreeCount + " for " + symbol); 274 if (symbols.Any(s => s.MaximumArity < maximumSubtreeCount)) throw new ArgumentException("Invalid maximum subtree count " + maximumSubtreeCount + " for " + symbol); 275 276 foreach (ISymbol s in symbols) 277 SetSubTreeCountInDictionaries(s, minimumSubtreeCount, maximumSubtreeCount); 278 279 ClearCaches(); 280 OnChanged(); 281 } 282 283 private void SetSubTreeCountInDictionaries(ISymbol symbol, int minimumSubtreeCount, int maximumSubtreeCount) { 284 for (int i = maximumSubtreeCount; i < GetMaximumSubtreeCount(symbol); i++) { 192 285 var key = Tuple.Create(symbol.Name, i); 193 286 allowedChildSymbolsPerIndex.Remove(key); … … 195 288 196 289 symbolSubtreeCount[symbol.Name] = Tuple.Create(minimumSubtreeCount, maximumSubtreeCount); 197 ClearCaches();198 290 } 199 291 #endregion 200 292 201 #region ISymbolicExpressionGrammarBase Members202 293 public virtual IEnumerable<ISymbol> Symbols { 203 294 get { return symbols.Values; } 204 295 } 205 296 public virtual IEnumerable<ISymbol> AllowedSymbols { 206 get { return Symbols.Where(s => !s.InitialFrequency.IsAlmost(0.0)); }297 get { return Symbols.Where(s => s.Enabled); } 207 298 } 208 299 public virtual bool ContainsSymbol(ISymbol symbol) { … … 211 302 212 303 public virtual bool IsAllowedChildSymbol(ISymbol parent, ISymbol child) { 304 if (!child.Enabled) return false; 305 213 306 List<string> temp; 214 if (allowedChildSymbols.TryGetValue(parent.Name, out temp)) 307 if (allowedChildSymbols.TryGetValue(parent.Name, out temp)) { 215 308 if (temp.Contains(child.Name)) return true; 309 if (temp.SelectMany(s => GetSymbol(s).Flatten().Select(n => n.Name)).Contains(child.Name)) return true; 310 } 216 311 return false; 217 312 } 218 313 219 314 public virtual bool IsAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) { 315 if (!child.Enabled) return false; 316 if (IsAllowedChildSymbol(parent, child)) return true; 317 220 318 List<string> temp; 221 if (allowedChildSymbols.TryGetValue(parent.Name, out temp)) 319 var key = Tuple.Create(parent.Name, argumentIndex); 320 if (allowedChildSymbolsPerIndex.TryGetValue(key, out temp)) { 222 321 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); 322 if (temp.SelectMany(s => GetSymbol(s).Flatten().Select(n => n.Name)).Contains(child.Name)) return true; 323 } 227 324 return false; 228 325 } 229 326 230 327 public virtual IEnumerable<ISymbol> GetAllowedChildSymbols(ISymbol parent) { 231 return from s in AllowedSymbols where IsAllowedChildSymbol(parent, s) select s; 328 List<string> childs; 329 if (!allowedChildSymbols.TryGetValue(parent.Name, out childs)) 330 return Enumerable.Empty<ISymbol>(); 331 332 return childs.Select(x => GetSymbol(x)).Where(s => s.Enabled); 232 333 } 233 334 … … 242 343 result = result.Union(temp); 243 344 244 return result.Select(x => GetSymbol(x)) ;345 return result.Select(x => GetSymbol(x)).Where(s => s.Enabled); 245 346 } 246 347 … … 251 352 return symbolSubtreeCount[symbol.Name].Item2; 252 353 } 253 254 354 255 355 protected void ClearCaches() { … … 265 365 cachedMinExpressionLength[symbol.Name] = int.MaxValue; // prevent infinite recursion 266 366 long sumOfMinExpressionLengths = 1 + (from argIndex in Enumerable.Range(0, GetMinimumSubtreeCount(symbol)) 267 let minForSlot = (long)(from s in AllowedSymbols 268 where IsAllowedChildSymbol(symbol, s, argIndex) 367 let minForSlot = (long)(from s in GetAllowedChildSymbols(symbol, argIndex) 269 368 select GetMinimumExpressionLength(s)).DefaultIfEmpty(0).Min() 270 369 select minForSlot).DefaultIfEmpty(0).Sum(); … … 282 381 cachedMaxExpressionLength[symbol.Name] = int.MaxValue; // prevent infinite recursion 283 382 long sumOfMaxTrees = 1 + (from argIndex in Enumerable.Range(0, GetMaximumSubtreeCount(symbol)) 284 let maxForSlot = (long)(from s in AllowedSymbols 285 where IsAllowedChildSymbol(symbol, s, argIndex) 383 let maxForSlot = (long)(from s in GetAllowedChildSymbols(symbol, argIndex) 286 384 select GetMaximumExpressionLength(s)).DefaultIfEmpty(0).Max() 287 385 select maxForSlot).DefaultIfEmpty(0).Sum(); … … 298 396 cachedMinExpressionDepth[symbol.Name] = int.MaxValue; // prevent infinite recursion 299 397 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) 398 let minForSlot = (long)(from s in GetAllowedChildSymbols(symbol, argIndex) 302 399 select GetMinimumExpressionDepth(s)).DefaultIfEmpty(0).Min() 303 400 select minForSlot).DefaultIfEmpty(0).Max(); … … 307 404 return temp; 308 405 } 309 #endregion 406 407 public event EventHandler Changed; 408 protected virtual void OnChanged() { 409 if (suppressEvents) return; 410 var handler = Changed; 411 if (handler != null) Changed(this, EventArgs.Empty); 412 } 310 413 } 311 414 }
Note: See TracChangeset
for help on using the changeset viewer.