Changeset 6814
- Timestamp:
- 09/21/11 15:51:43 (13 years ago)
- Location:
- trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4
- Files:
-
- 6 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; -
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTreeGrammar.cs
r6803 r6814 82 82 public override IEnumerable<ISymbol> GetAllowedChildSymbols(ISymbol parent) { 83 83 var symbols = grammar.GetAllowedChildSymbols(parent).Union(base.GetAllowedChildSymbols(parent)); 84 return symbols.SelectMany(s => s.Flatten()).Where(s => s.Enabled && !(s is GroupSymbol));84 return symbols.SelectMany(s => s.Flatten()).Where(s => s.Enabled).Distinct(); 85 85 } 86 86 public override IEnumerable<ISymbol> GetAllowedChildSymbols(ISymbol parent, int argumentIndex) { 87 87 var symbols = grammar.GetAllowedChildSymbols(parent, argumentIndex).Union(base.GetAllowedChildSymbols(parent, argumentIndex)); 88 return symbols.SelectMany(s => s.Flatten()).Where(s => s.Enabled && !(s is GroupSymbol));88 return symbols.SelectMany(s => s.Flatten()).Where(s => s.Enabled).Distinct(); 89 89 } 90 90 -
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Tests/Grammars.cs
r6803 r6814 21 21 22 22 using System.Collections.Generic; 23 using System.Linq; 23 24 using HeuristicLab.Common; 24 25 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 25 26 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 27 using HeuristicLab.Problems.DataAnalysis.Symbolic; 26 28 27 29 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._4.Tests { … … 212 214 213 215 public static ISymbolicExpressionGrammar CreateSimpleArithmeticGrammar() { 214 var g = new SimpleArithmeticGrammar(); 216 var g = new TypeCoherentExpressionGrammar(); 217 g.ConfigureAsDefaultRegressionGrammar(); 218 g.Symbols.OfType<Variable>().First().Enabled = false; 219 //var g = new SimpleArithmeticGrammar(); 215 220 g.MaximumFunctionArguments = 0; 216 221 g.MinimumFunctionArguments = 0; … … 221 226 222 227 public static ISymbolicExpressionGrammar CreateArithmeticAndAdfGrammar() { 223 var g = new SimpleArithmeticGrammar(); 228 var g = new TypeCoherentExpressionGrammar(); 229 g.ConfigureAsDefaultRegressionGrammar(); 230 g.Symbols.OfType<Variable>().First().Enabled = false; 224 231 g.MaximumFunctionArguments = 3; 225 232 g.MinimumFunctionArguments = 0; -
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Tests/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4.Tests.csproj
r5809 r6814 149 149 <Name>HeuristicLab.PluginInfrastructure-3.3</Name> 150 150 </ProjectReference> 151 <ProjectReference Include="..\..\..\HeuristicLab.Problems.DataAnalysis.Symbolic\3.4\HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.csproj"> 152 <Project>{3D28463F-EC96-4D82-AFEE-38BE91A0CA00}</Project> 153 <Name>HeuristicLab.Problems.DataAnalysis.Symbolic-3.4</Name> 154 </ProjectReference> 151 155 <ProjectReference Include="..\..\..\HeuristicLab.Random\3.3\HeuristicLab.Random-3.3.csproj"> 152 156 <Project>{F4539FB6-4708-40C9-BE64-0A1390AEA197}</Project> -
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Tests/ProbabilisticTreeCreaterTest.cs
r5809 r6814 72 72 Util.GetTerminalDistributionString(randomTrees) + Environment.NewLine 73 73 ); 74 Assert.IsTrue(Math.Round(1000.0 / (msPerRandomTreeCreation)) > 500); // must achieve more than 500 random trees / s74 Assert.IsTrue(Math.Round(1000.0 / (msPerRandomTreeCreation)) > 300); // must achieve more than 500 random trees / s 75 75 } 76 76 } -
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Tests/Util.cs
r6803 r6814 153 153 public static void IsValid(ISymbolicExpressionTreeGrammar grammar) { 154 154 Assert.IsTrue(grammar.Symbols.Count() == grammar.Symbols.Distinct().Count()); 155 foreach (ISymbol symbol in grammar. Symbols) {155 foreach (ISymbol symbol in grammar.AllowedSymbols) { 156 156 Assert.IsTrue(grammar.GetMinimumSubtreeCount(symbol) <= grammar.GetMaximumExpressionLength(symbol)); 157 157 Assert.IsTrue(grammar.GetAllowedChildSymbols(symbol).Count() == grammar.GetAllowedChildSymbols(symbol).Distinct().Count());
Note: See TracChangeset
for help on using the changeset viewer.