- Timestamp:
- 06/11/14 14:03:06 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionGrammarBase.cs
r9456 r10985 323 323 if (cachedIsAllowedChildSymbol.TryGetValue(key, out result)) return result; 324 324 325 List<string> temp; 326 if (allowedChildSymbols.TryGetValue(parent.Name, out temp)) { 327 //if (temp.Contains(child.Name)) return true; 328 if (temp.SelectMany(s => GetSymbol(s).Flatten()).Any(s => s.Name == child.Name)) { 329 cachedIsAllowedChildSymbol.Add(key, true); 330 return true; 325 // value has to be calculated and cached make sure this is done in only one thread 326 lock (cachedIsAllowedChildSymbol) { 327 // in case the value has been calculated on another thread in the meanwhile 328 if (cachedIsAllowedChildSymbol.TryGetValue(key, out result)) return result; 329 330 List<string> temp; 331 if (allowedChildSymbols.TryGetValue(parent.Name, out temp)) { 332 if (temp.SelectMany(s => GetSymbol(s).Flatten()).Any(s => s.Name == child.Name)) { 333 cachedIsAllowedChildSymbol.Add(key, true); 334 return true; 335 } 331 336 } 332 }333 cachedIsAllowedChildSymbol.Add(key, false);334 return false;337 cachedIsAllowedChildSymbol.Add(key, false); 338 return false; 339 } 335 340 } 336 341 … … 345 350 if (cachedIsAllowedChildSymbolIndex.TryGetValue(key, out result)) return result; 346 351 347 List<string> temp; 348 if (allowedChildSymbolsPerIndex.TryGetValue(Tuple.Create(parent.Name, argumentIndex), out temp)) { 349 if (temp.SelectMany(s => GetSymbol(s).Flatten()).Any(s => s.Name == child.Name)) { 350 cachedIsAllowedChildSymbolIndex.Add(key, true); 351 return true; 352 // value has to be calculated and cached make sure this is done in only one thread 353 lock (cachedIsAllowedChildSymbolIndex) { 354 // in case the value has been calculated on another thread in the meanwhile 355 if (cachedIsAllowedChildSymbolIndex.TryGetValue(key, out result)) return result; 356 357 List<string> temp; 358 if (allowedChildSymbolsPerIndex.TryGetValue(Tuple.Create(parent.Name, argumentIndex), out temp)) { 359 if (temp.SelectMany(s => GetSymbol(s).Flatten()).Any(s => s.Name == child.Name)) { 360 cachedIsAllowedChildSymbolIndex.Add(key, true); 361 return true; 362 } 352 363 } 353 }354 cachedIsAllowedChildSymbolIndex.Add(key, false);355 return false;364 cachedIsAllowedChildSymbolIndex.Add(key, false); 365 return false; 366 } 356 367 } 357 368 … … 391 402 return res; 392 403 393 res = GetMinimumExpressionLengthRec(symbol); 394 foreach (var entry in cachedMinExpressionLength.Where(e => e.Value >= int.MaxValue).ToList()) { 395 if (entry.Key != symbol.Name) cachedMinExpressionLength.Remove(entry.Key); 396 } 397 return res; 404 // value has to be calculated and cached make sure this is done in only one thread 405 lock (cachedMinExpressionLength) { 406 // in case the value has been calculated on another thread in the meanwhile 407 if (cachedMinExpressionLength.TryGetValue(symbol.Name, out res)) return res; 408 409 res = GetMinimumExpressionLengthRec(symbol); 410 foreach (var entry in cachedMinExpressionLength.Where(e => e.Value >= int.MaxValue).ToList()) { 411 if (entry.Key != symbol.Name) cachedMinExpressionLength.Remove(entry.Key); 412 } 413 return res; 414 } 398 415 } 399 416 … … 418 435 int temp; 419 436 var key = Tuple.Create(symbol.Name, maxDepth); 420 if (!cachedMaxExpressionLength.TryGetValue(key, out temp)) { 437 if (cachedMaxExpressionLength.TryGetValue(key, out temp)) return temp; 438 // value has to be calculated and cached make sure this is done in only one thread 439 lock (cachedMaxExpressionLength) { 440 // in case the value has been calculated on another thread in the meanwhile 441 if (cachedMaxExpressionLength.TryGetValue(key, out temp)) return temp; 442 421 443 cachedMaxExpressionLength[key] = int.MaxValue; // prevent infinite recursion 422 444 long sumOfMaxTrees = 1 + (from argIndex in Enumerable.Range(0, GetMaximumSubtreeCount(symbol)) … … 429 451 return cachedMaxExpressionLength[key]; 430 452 } 431 return temp;432 453 } 433 454 … … 438 459 return res; 439 460 440 res = GetMinimumExpressionDepthRec(symbol); 441 foreach (var entry in cachedMinExpressionDepth.Where(e => e.Value >= int.MaxValue).ToList()) { 442 if (entry.Key != symbol.Name) cachedMinExpressionDepth.Remove(entry.Key); 443 } 444 return res; 461 // value has to be calculated and cached make sure this is done in only one thread 462 lock (cachedMinExpressionDepth) { 463 // in case the value has been calculated on another thread in the meanwhile 464 if (cachedMinExpressionDepth.TryGetValue(symbol.Name, out res)) return res; 465 466 res = GetMinimumExpressionDepthRec(symbol); 467 foreach (var entry in cachedMinExpressionDepth.Where(e => e.Value >= int.MaxValue).ToList()) { 468 if (entry.Key != symbol.Name) cachedMinExpressionDepth.Remove(entry.Key); 469 } 470 return res; 471 } 445 472 } 446 473 private int GetMinimumExpressionDepthRec(ISymbol symbol) { … … 462 489 public int GetMaximumExpressionDepth(ISymbol symbol) { 463 490 int temp; 464 if (!cachedMaxExpressionDepth.TryGetValue(symbol.Name, out temp)) { 491 if (cachedMaxExpressionDepth.TryGetValue(symbol.Name, out temp)) return temp; 492 // value has to be calculated and cached make sure this is done in only one thread 493 lock (cachedMaxExpressionDepth) { 494 // in case the value has been calculated on another thread in the meanwhile 495 if (cachedMaxExpressionDepth.TryGetValue(symbol.Name, out temp)) return temp; 496 465 497 cachedMaxExpressionDepth[symbol.Name] = int.MaxValue; 466 498 long maxDepth = 1 + (from argIndex in Enumerable.Range(0, GetMaximumSubtreeCount(symbol)) … … 472 504 return cachedMaxExpressionDepth[symbol.Name]; 473 505 } 474 return temp;475 506 } 476 507
Note: See TracChangeset
for help on using the changeset viewer.