Changeset 6814 for trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionGrammarBase.cs
- Timestamp:
- 09/21/11 15:51:43 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionGrammarBase.cs
r6803 r6814 52 52 [Storable(Name = "AllowedChildSymbols")] 53 53 private IEnumerable<KeyValuePair<ISymbol, IEnumerable<ISymbol>>> StorableAllowedChildSymbols { 54 get { return allowedChildSymbols.Select(x => new KeyValuePair<ISymbol, IEnumerable<ISymbol>>(GetSymbol(x.Key), x.Value.Select( y => GetSymbol(y)).ToArray())).ToArray(); }54 get { return allowedChildSymbols.Select(x => new KeyValuePair<ISymbol, IEnumerable<ISymbol>>(GetSymbol(x.Key), x.Value.Select(GetSymbol).ToArray())).ToArray(); } 55 55 set { allowedChildSymbols = value.ToDictionary(x => x.Key.Name, x => x.Value.Select(y => y.Name).ToList()); } 56 56 } … … 83 83 cachedMinExpressionDepth = new Dictionary<string, int>(); 84 84 85 cachedIsAllowedChildSymbol = new Dictionary<Tuple<string, string>, bool>(); 86 cachedIsAllowedChildSymbolIndex = new Dictionary<Tuple<string, string, int>, bool>(); 87 85 88 suppressEvents = false; 86 89 } … … 92 95 cachedMinExpressionDepth = new Dictionary<string, int>(); 93 96 94 symbols = original.symbols.ToDictionary(x => x.Key, y => (ISymbol)cloner.Clone(y.Value)); 97 cachedIsAllowedChildSymbol = new Dictionary<Tuple<string, string>, bool>(); 98 cachedIsAllowedChildSymbolIndex = new Dictionary<Tuple<string, string, int>, bool>(); 99 100 symbols = original.symbols.ToDictionary(x => x.Key, y => cloner.Clone(y.Value)); 95 101 symbolSubtreeCount = new Dictionary<string, Tuple<int, int>>(original.symbolSubtreeCount); 96 102 … … 111 117 cachedMaxExpressionLength = new Dictionary<string, int>(); 112 118 cachedMinExpressionDepth = new Dictionary<string, int>(); 119 120 cachedIsAllowedChildSymbol = new Dictionary<Tuple<string, string>, bool>(); 121 cachedIsAllowedChildSymbolIndex = new Dictionary<Tuple<string, string, int>, bool>(); 113 122 114 123 symbols = new Dictionary<string, ISymbol>(); … … 301 310 } 302 311 312 private readonly Dictionary<Tuple<string, string>, bool> cachedIsAllowedChildSymbol; 303 313 public virtual bool IsAllowedChildSymbol(ISymbol parent, ISymbol child) { 304 314 if (!child.Enabled) return false; 305 315 316 bool result; 317 if (cachedIsAllowedChildSymbol.TryGetValue(Tuple.Create(parent.Name, child.Name), out result)) return result; 306 318 List<string> temp; 307 319 if (allowedChildSymbols.TryGetValue(parent.Name, out temp)) { 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 } 320 //if (temp.Contains(child.Name)) return true; 321 if (temp.SelectMany(s => GetSymbol(s).Flatten()).Where(s => s.Name == child.Name).Any()) { 322 cachedIsAllowedChildSymbol.Add(Tuple.Create(parent.Name, child.Name), true); 323 return true; 324 } 325 } 326 cachedIsAllowedChildSymbol.Add(Tuple.Create(parent.Name, child.Name), false); 311 327 return false; 312 328 } 313 329 330 private readonly Dictionary<Tuple<string, string, int>, bool> cachedIsAllowedChildSymbolIndex; 314 331 public virtual bool IsAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) { 315 332 if (!child.Enabled) return false; 316 333 if (IsAllowedChildSymbol(parent, child)) return true; 317 334 335 bool result; 336 if (cachedIsAllowedChildSymbolIndex.TryGetValue(Tuple.Create(parent.Name, child.Name, argumentIndex), out result)) return result; 318 337 List<string> temp; 319 338 var key = Tuple.Create(parent.Name, argumentIndex); 320 339 if (allowedChildSymbolsPerIndex.TryGetValue(key, out temp)) { 321 if (temp.Contains(child.Name)) return true; 322 if (temp.SelectMany(s => GetSymbol(s).Flatten().Select(n => n.Name)).Contains(child.Name)) return true; 323 } 340 //if (temp.Contains(child.Name)) return true; 341 if (temp.SelectMany(s => GetSymbol(s).Flatten()).Where(s => s.Name == child.Name).Any()) { 342 cachedIsAllowedChildSymbolIndex.Add(Tuple.Create(parent.Name, child.Name, argumentIndex), true); 343 return true; 344 } 345 } 346 cachedIsAllowedChildSymbolIndex.Add(Tuple.Create(parent.Name, child.Name, argumentIndex), false); 324 347 return false; 325 348 } … … 330 353 return Enumerable.Empty<ISymbol>(); 331 354 332 return childs.Select( x => GetSymbol(x)).Where(s => s.Enabled);355 return childs.Select(GetSymbol).Where(s => s.Enabled); 333 356 } 334 357 … … 343 366 result = result.Union(temp); 344 367 345 return result.Select( x => GetSymbol(x)).Where(s => s.Enabled);368 return result.Select(GetSymbol).Where(s => s.Enabled); 346 369 } 347 370 … … 357 380 cachedMaxExpressionLength.Clear(); 358 381 cachedMinExpressionDepth.Clear(); 359 } 360 361 private Dictionary<string, int> cachedMinExpressionLength; 382 383 cachedIsAllowedChildSymbol.Clear(); 384 cachedIsAllowedChildSymbolIndex.Clear(); 385 } 386 387 private readonly Dictionary<string, int> cachedMinExpressionLength; 362 388 public int GetMinimumExpressionLength(ISymbol symbol) { 363 389 int temp; … … 375 401 } 376 402 377 private Dictionary<string, int> cachedMaxExpressionLength;403 private readonly Dictionary<string, int> cachedMaxExpressionLength; 378 404 public int GetMaximumExpressionLength(ISymbol symbol) { 379 405 int temp; … … 390 416 } 391 417 392 private Dictionary<string, int> cachedMinExpressionDepth;418 private readonly Dictionary<string, int> cachedMinExpressionDepth; 393 419 public int GetMinimumExpressionDepth(ISymbol symbol) { 394 420 int temp;
Note: See TracChangeset
for help on using the changeset viewer.