Changeset 6409 for branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4
- Timestamp:
- 06/14/11 10:57:27 (13 years ago)
- Location:
- branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Creators/ProbabilisticTreeCreator.cs
r6377 r6409 270 270 where s.InitialFrequency > 0.0 271 271 select node.Grammar.GetMaximumExpressionLength(s)).Max(); 272 if ( aggregatedLongestExpressionLength < targetLength) minArity = i + 1;272 if (i > minArity && aggregatedLongestExpressionLength < targetLength) minArity = i + 1; 273 273 else break; 274 274 } -
branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Interfaces/ISymbol.cs
r6387 r6409 21 21 22 22 using System; 23 using System.Collections.Generic; 23 24 using HeuristicLab.Core; 24 25 … … 33 34 int MaximumArity { get; } 34 35 36 IEnumerable<ISymbol> Flatten(); 37 35 38 event EventHandler Changed; 36 39 } -
branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionGrammarBase.cs
r6403 r6409 131 131 #region protected grammar manipulation methods 132 132 protected void AddSymbol(ISymbol symbol) { 133 AddSymbolToDictionaries(symbol); 134 var groupSymbol = symbol as GroupSymbol; 135 if (groupSymbol != null) { 136 foreach (ISymbol s in groupSymbol.Flatten()) 137 AddSymbolToDictionaries(s); 138 } 133 foreach (ISymbol s in symbol.Flatten()) 134 AddSymbolToDictionaries(s); 139 135 140 136 ClearCaches(); … … 148 144 149 145 protected void RemoveSymbol(ISymbol symbol) { 150 RemoveSymbolFromDictionaries(symbol); 151 var groupSymbol = symbol as GroupSymbol; 152 if (groupSymbol != null) { 153 foreach (ISymbol s in groupSymbol.Flatten()) 154 RemoveSymbolFromDictionaries(s); 155 } 146 foreach (ISymbol s in symbol.Flatten()) 147 RemoveSymbolFromDictionaries(s); 156 148 157 149 foreach (GroupSymbol group in symbols.Values.OfType<GroupSymbol>()) … … 188 180 189 181 protected void AddAllowedChildSymbol(ISymbol parent, ISymbol child) { 190 List<ISymbol> parents;191 List<ISymbol> childs;192 182 bool changed = false; 193 183 194 var parentGroup = parent as GroupSymbol; 195 if (parentGroup != null) parents = parentGroup.Flatten().Where(p => !(p is GroupSymbol)).ToList(); 196 else parents = new List<ISymbol>() { parent }; 197 var childGroup = child as GroupSymbol; 198 if (childGroup != null) childs = childGroup.Flatten().Where(c => !(c is GroupSymbol)).ToList(); 199 else childs = new List<ISymbol>() { child }; 200 201 foreach (ISymbol p in parents) { 202 foreach (ISymbol c in childs) { 203 changed |= AddAllowedChildSymbolToDictionaries(p, c); 204 } 205 } 184 foreach (ISymbol p in parent.Flatten().Where(p => !(p is GroupSymbol))) 185 changed |= AddAllowedChildSymbolToDictionaries(p, child); 206 186 207 187 if (changed) { … … 229 209 230 210 protected void AddAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) { 231 List<ISymbol> parents;232 List<ISymbol> childs;233 211 bool changed = false; 234 212 235 var parentGroup = parent as GroupSymbol; 236 if (parentGroup != null) parents = parentGroup.Flatten().Where(p => !(p is GroupSymbol)).ToList(); 237 else parents = new List<ISymbol>() { parent }; 238 var childGroup = child as GroupSymbol; 239 if (childGroup != null) childs = childGroup.Flatten().Where(c => !(c is GroupSymbol)).ToList(); 240 else childs = new List<ISymbol>() { child }; 241 242 foreach (ISymbol p in parents) { 243 foreach (ISymbol c in childs) { 244 changed |= AddAllowedChildSymbolToDictionaries(p, c, argumentIndex); 245 } 246 } 213 foreach (ISymbol p in parent.Flatten().Where(p => !(p is GroupSymbol))) 214 changed |= AddAllowedChildSymbolToDictionaries(p, child, argumentIndex); 247 215 248 216 if (changed) { … … 255 223 private bool AddAllowedChildSymbolToDictionaries(ISymbol parent, ISymbol child, int argumentIndex) { 256 224 List<string> childSymbols; 257 if (allowedChildSymbols.TryGetValue(parent.Name, out childSymbols)) 258 if (childSymbols.Contains(child.Name)) return false; 225 if (!allowedChildSymbols.TryGetValue(parent.Name, out childSymbols)) { 226 childSymbols = new List<string>(); 227 allowedChildSymbols.Add(parent.Name, childSymbols); 228 } 229 if (childSymbols.Contains(child.Name)) return false; 230 259 231 260 232 var key = Tuple.Create(parent.Name, argumentIndex); … … 263 235 allowedChildSymbolsPerIndex.Add(key, childSymbols); 264 236 } 237 265 238 if (childSymbols.Contains(child.Name)) return false; 266 239 … … 314 287 315 288 protected void SetSubtreeCount(ISymbol symbol, int minimumSubtreeCount, int maximumSubtreeCount) { 316 var groupSymbol = symbol as GroupSymbol; 317 if (groupSymbol != null) { 318 var symbols = groupSymbol.Flatten().Where(s => !(s is GroupSymbol)); 319 if (symbols.Any(s => s.MinimumArity > minimumSubtreeCount)) throw new ArgumentException("Invalid minimum subtree count " + minimumSubtreeCount + " for " + symbol); 320 if (symbols.Any(s => s.MaximumArity < maximumSubtreeCount)) throw new ArgumentException("Invalid minimum subtree count " + minimumSubtreeCount + " for " + symbol); 321 foreach (ISymbol s in symbols) 322 SetSubTreeCountInDictionaries(s, minimumSubtreeCount, maximumSubtreeCount); 323 } else { 324 if (symbol.MinimumArity > minimumSubtreeCount) throw new ArgumentException("Invalid minimum subtree count " + minimumSubtreeCount + " for " + symbol); 325 if (symbol.MaximumArity < maximumSubtreeCount) throw new ArgumentException("Invalid minimum subtree count " + minimumSubtreeCount + " for " + symbol); 326 SetSubTreeCountInDictionaries(symbol, minimumSubtreeCount, maximumSubtreeCount); 327 } 289 var symbols = symbol.Flatten().Where(s => !(s is GroupSymbol)); 290 if (symbols.Any(s => s.MinimumArity > minimumSubtreeCount)) throw new ArgumentException("Invalid minimum subtree count " + minimumSubtreeCount + " for " + symbol); 291 if (symbols.Any(s => s.MaximumArity < maximumSubtreeCount)) throw new ArgumentException("Invalid minimum subtree count " + minimumSubtreeCount + " for " + symbol); 292 293 foreach (ISymbol s in symbols) 294 SetSubTreeCountInDictionaries(s, minimumSubtreeCount, maximumSubtreeCount); 328 295 329 296 ClearCaches(); … … 355 322 356 323 List<string> temp; 357 if (allowedChildSymbols.TryGetValue(parent.Name, out temp)) 358 return temp.Contains(child.Name); 324 if (allowedChildSymbols.TryGetValue(parent.Name, out temp)) { 325 if (temp.Contains(child.Name)) return true; 326 if (temp.SelectMany(s => GetSymbol(s).Flatten().Select(n => n.Name)).Contains(child.Name)) return true; 327 } 359 328 return false; 360 329 } … … 362 331 public virtual bool IsAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) { 363 332 if (!child.Enabled) return false; 333 if (IsAllowedChildSymbol(parent, child)) return true; 364 334 365 335 List<string> temp; 366 if (allowedChildSymbols.TryGetValue(parent.Name, out temp)) 336 var key = Tuple.Create(parent.Name, argumentIndex); 337 if (allowedChildSymbolsPerIndex.TryGetValue(key, out temp)) { 367 338 if (temp.Contains(child.Name)) return true; 368 369 var key = Tuple.Create(parent.Name, argumentIndex); 370 if (allowedChildSymbolsPerIndex.TryGetValue(key, out temp)) 371 return temp.Contains(child.Name); 339 if (temp.SelectMany(s => GetSymbol(s).Flatten().Select(n => n.Name)).Contains(child.Name)) return true; 340 } 372 341 return false; 373 342 } … … 375 344 public virtual IEnumerable<ISymbol> GetAllowedChildSymbols(ISymbol parent) { 376 345 List<string> childs; 377 if (allowedChildSymbols.TryGetValue(parent.Name, out childs)) 378 return childs.Select(s => GetSymbol(s)).Where(s => s.Enabled); 379 return Enumerable.Empty<ISymbol>(); 346 if (!allowedChildSymbols.TryGetValue(parent.Name, out childs)) 347 return Enumerable.Empty<ISymbol>(); 348 349 return childs.Select(x => GetSymbol(x)).Where(s => s.Enabled); 380 350 } 381 351 … … 452 422 } 453 423 454 455 424 #region events 456 425 private void RegisterSymbolEvents(ISymbol symbol) { -
branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTreeGrammar.cs
r6379 r6409 81 81 } 82 82 public override IEnumerable<ISymbol> GetAllowedChildSymbols(ISymbol parent) { 83 return grammar.GetAllowedChildSymbols(parent).Union(base.GetAllowedChildSymbols(parent)); 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 85 } 85 86 public override IEnumerable<ISymbol> GetAllowedChildSymbols(ISymbol parent, int argumentIndex) { 86 return grammar.GetAllowedChildSymbols(parent, argumentIndex).Union(base.GetAllowedChildSymbols(parent, argumentIndex)); 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)); 87 89 } 88 90 -
branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Symbols/GroupSymbol.cs
r6387 r6409 27 27 28 28 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding { 29 [StorableClass] 29 30 public sealed class GroupSymbol : Symbol { 30 31 private const int minimumArity = 0; … … 45 46 public IEnumerable<ISymbol> Symbols { 46 47 get { return symbols; } 48 private set { symbols = new ObservableSet<ISymbol>(value); } 47 49 } 48 50 … … 75 77 } 76 78 77 public IEnumerable<ISymbol> Flatten() {78 return symbols.Union(symbols.OfType<GroupSymbol>().SelectMany(g => g.Flatten()));79 public override IEnumerable<ISymbol> Flatten() { 80 return base.Flatten().Union(symbols.SelectMany(s => s.Flatten())); 79 81 } 80 82 } -
branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Symbols/Symbol.cs
r6387 r6409 21 21 22 22 using System; 23 using System.Collections.Generic; 23 24 using HeuristicLab.Common; 24 25 using HeuristicLab.Core; … … 96 97 [StorableHook(HookType.AfterDeserialization)] 97 98 private void AfterDeserialization() { 98 if (initialFrequency.IsAlmost(0.0)) enabled = false; 99 // BackwardsCompatibility3.3 100 #region Backwards compatible code, remove with 3.4 101 if (initialFrequency.IsAlmost(0.0) && !(this is GroupSymbol)) enabled = false; 102 #endregion 103 99 104 } 100 105 101 106 public virtual ISymbolicExpressionTreeNode CreateTreeNode() { 102 107 return new SymbolicExpressionTreeNode(this); 108 } 109 110 public virtual IEnumerable<ISymbol> Flatten() { 111 return new List<ISymbol>() { this }; 103 112 } 104 113
Note: See TracChangeset
for help on using the changeset viewer.