- Timestamp:
- 05/26/11 12:28:43 (14 years ago)
- Location:
- branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Interfaces/ISymbol.cs
r6284 r6296 27 27 ISymbolicExpressionTreeNode CreateTreeNode(); 28 28 double InitialFrequency { get; set; } 29 bool Enabled { get; set; } 29 30 bool Fixed { get; set; } 30 31 -
branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionGrammarBase.cs
r6284 r6296 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 protected 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 [StorableHook(HookType.AfterDeserialization)] … … 106 110 foreach (ISymbol symbol in symbols.Values) 107 111 RegisterSymbolEvents(symbol); 112 113 suppressEvents = false; 108 114 } 109 115 … … 118 124 allowedChildSymbols = new Dictionary<string, List<string>>(); 119 125 allowedChildSymbolsPerIndex = new Dictionary<Tuple<string, int>, List<string>>(); 126 127 suppressEvents = false; 120 128 } 121 129 122 130 #region protected grammar manipulation methods 123 131 protected void AddSymbol(ISymbol symbol) { 124 if (ContainsSymbol(symbol)) throw new ArgumentException("Symbol " + symbol + " is already defined.");125 RegisterSymbolEvents(symbol);126 132 symbols.Add(symbol.Name, symbol); 127 133 symbolSubtreeCount.Add(symbol.Name, Tuple.Create(0, 0)); 134 RegisterSymbolEvents(symbol); 135 136 var groupSymbol = symbol as GroupSymbol; 137 if (groupSymbol != null) AddSymbol(groupSymbol); 138 128 139 ClearCaches(); 129 140 OnChanged(); 130 141 } 131 142 132 private void RegisterSymbolEvents(ISymbol symbol) { 133 symbol.NameChanging += new EventHandler<CancelEventArgs<string>>(Symbol_NameChanging); 134 symbol.NameChanged += new EventHandler(Symbol_NameChanged); 135 symbol.Changed += new EventHandler(Symbol_Changed); 136 } 137 private void DeregisterSymbolEvents(ISymbol symbol) { 138 symbol.NameChanging -= new EventHandler<CancelEventArgs<string>>(Symbol_NameChanging); 139 symbol.NameChanged -= new EventHandler(Symbol_NameChanged); 140 symbol.Changed -= new EventHandler(Symbol_Changed); 141 } 142 143 private void Symbol_NameChanging(object sender, CancelEventArgs<string> e) { 144 if (symbols.ContainsKey(e.Value)) e.Cancel = true; 145 } 146 private void Symbol_NameChanged(object sender, EventArgs e) { 147 ISymbol symbol = (ISymbol)sender; 148 string oldName = symbols.Where(x => x.Value == symbol).First().Key; 149 string newName = symbol.Name; 150 151 symbols.Remove(oldName); 152 symbols.Add(newName, symbol); 153 154 var subtreeCount = symbolSubtreeCount[oldName]; 155 symbolSubtreeCount.Remove(oldName); 156 symbolSubtreeCount.Add(newName, subtreeCount); 157 158 List<string> allowedChilds; 159 if (allowedChildSymbols.TryGetValue(oldName, out allowedChilds)) { 160 allowedChildSymbols.Remove(oldName); 161 allowedChildSymbols.Add(newName, allowedChilds); 162 } 163 164 for (int i = 0; i < GetMaximumSubtreeCount(symbol); i++) { 165 if (allowedChildSymbolsPerIndex.TryGetValue(Tuple.Create(oldName, i), out allowedChilds)) { 166 allowedChildSymbolsPerIndex.Remove(Tuple.Create(oldName, i)); 167 allowedChildSymbolsPerIndex.Add(Tuple.Create(newName, i), allowedChilds); 168 } 169 } 170 171 foreach (var parent in Symbols) { 172 if (allowedChildSymbols.TryGetValue(parent.Name, out allowedChilds)) 173 if (allowedChilds.Remove(oldName)) 174 allowedChilds.Add(newName); 175 176 for (int i = 0; i < GetMaximumSubtreeCount(parent); i++) { 177 if (allowedChildSymbolsPerIndex.TryGetValue(Tuple.Create(parent.Name, i), out allowedChilds)) 178 if (allowedChilds.Remove(oldName)) allowedChilds.Add(newName); 179 } 180 } 181 182 ClearCaches(); 183 OnChanged(); 184 } 185 186 private void Symbol_Changed(object sende, EventArgs e) { 187 ClearCaches(); 188 OnChanged(); 143 private void AddSymbol(GroupSymbol groupSymbol) { 144 foreach (ISymbol symbol in groupSymbol.Symbols) { 145 symbols.Add(symbol.Name, symbol); 146 symbolSubtreeCount.Add(symbol.Name, Tuple.Create(0, 0)); 147 RegisterSymbolEvents(symbol); 148 149 var childGroup = symbol as GroupSymbol; 150 if (childGroup != null) AddSymbol(childGroup); 151 } 189 152 } 190 153 … … 218 181 219 182 protected void AddAllowedChildSymbol(ISymbol parent, ISymbol child) { 183 suppressEvents = true; 184 for (int argumentIndex = 0; argumentIndex < GetMaximumSubtreeCount(parent); argumentIndex++) 185 RemoveAllowedChildSymbol(parent, child, argumentIndex); 186 suppressEvents = false; 187 220 188 List<string> childSymbols; 221 189 if (!allowedChildSymbols.TryGetValue(parent.Name, out childSymbols)) { … … 223 191 allowedChildSymbols.Add(parent.Name, childSymbols); 224 192 } 225 if (childSymbols.Contains(child.Name)) throw new ArgumentException(); 226 childSymbols.Add(child.Name); 227 ClearCaches(); 228 OnChanged(); 193 194 if (!childSymbols.Contains(child.Name)) { 195 childSymbols.Add(child.Name); 196 ClearCaches(); 197 OnChanged(); 198 } 229 199 } 230 200 231 201 protected void AddAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) { 202 List<string> childSymbols; 203 if (allowedChildSymbols.TryGetValue(parent.Name, out childSymbols)) 204 if (childSymbols.Contains(child.Name)) return; 205 232 206 var key = Tuple.Create(parent.Name, argumentIndex); 233 List<string> childSymbols;234 207 if (!allowedChildSymbolsPerIndex.TryGetValue(key, out childSymbols)) { 235 208 childSymbols = new List<string>(); … … 237 210 } 238 211 239 if ( IsAllowedChildSymbol(parent, child)) throw new ArgumentException();240 if (childSymbols.Contains(child.Name)) throw new ArgumentException();241 childSymbols.Add(child.Name);242 ClearCaches();243 OnChanged();212 if (!childSymbols.Contains(child.Name)) { 213 childSymbols.Add(child.Name); 214 ClearCaches(); 215 OnChanged(); 216 } 244 217 } 245 218 246 219 protected void RemoveAllowedChildSymbol(ISymbol parent, ISymbol child) { 220 bool changed = false; 247 221 List<string> childSymbols; 248 222 if (allowedChildSymbols.TryGetValue(child.Name, out childSymbols)) { 249 if (allowedChildSymbols[parent.Name].Remove(child.Name)) { 250 ClearCaches(); 251 OnChanged(); 223 if (allowedChildSymbols[parent.Name].Remove(child.Name)) changed = true; 224 } 225 226 for (int argumentIndex = 0; argumentIndex < GetMaximumSubtreeCount(parent); argumentIndex++) { 227 var key = Tuple.Create(parent.Name, argumentIndex); 228 if (allowedChildSymbolsPerIndex.TryGetValue(key, out childSymbols)) { 229 if (allowedChildSymbolsPerIndex[key].Remove(child.Name)) changed = true; 252 230 } 253 231 } 232 233 if (changed) { 234 ClearCaches(); 235 OnChanged(); 236 } 254 237 } 255 238 256 239 protected void RemoveAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) { 240 bool changed = false; 241 242 List<string> childSymbols; 243 if (allowedChildSymbols.TryGetValue(parent.Name, out childSymbols)) { 244 if (childSymbols.Remove(child.Name)) { 245 suppressEvents = true; 246 for (int i = 0; i < GetMaximumSubtreeCount(parent); i++) { 247 if (i != argumentIndex) AddAllowedChildSymbol(parent, child, i); 248 } 249 suppressEvents = false; 250 changed = true; 251 } 252 } 253 257 254 var key = Tuple.Create(parent.Name, argumentIndex); 258 List<string> childSymbols;259 255 if (allowedChildSymbolsPerIndex.TryGetValue(key, out childSymbols)) { 260 if (allowedChildSymbolsPerIndex[key].Remove(child.Name)) { 261 ClearCaches(); 262 OnChanged(); 263 } 256 if (allowedChildSymbolsPerIndex[key].Remove(child.Name)) 257 changed = true; 258 } 259 260 if (changed) { 261 ClearCaches(); 262 OnChanged(); 264 263 } 265 264 } … … 277 276 #endregion 278 277 279 #region ISymbolicExpressionGrammarBase Members280 278 public virtual IEnumerable<ISymbol> Symbols { 281 279 get { return symbols.Values; } 282 280 } 283 281 public virtual IEnumerable<ISymbol> AllowedSymbols { 284 get { return Symbols.Where(s => !s.InitialFrequency.IsAlmost(0.0)); }282 get { return Symbols.Where(s => s.Enabled); } 285 283 } 286 284 public virtual bool ContainsSymbol(ISymbol symbol) { … … 289 287 290 288 public virtual bool IsAllowedChildSymbol(ISymbol parent, ISymbol child) { 289 if (!child.Enabled) return false; 290 291 291 List<string> temp; 292 292 if (allowedChildSymbols.TryGetValue(parent.Name, out temp)) … … 296 296 297 297 public virtual bool IsAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) { 298 if (!child.Enabled) return false; 299 298 300 List<string> temp; 299 301 if (allowedChildSymbols.TryGetValue(parent.Name, out temp)) … … 307 309 308 310 public virtual IEnumerable<ISymbol> GetAllowedChildSymbols(ISymbol parent) { 309 return from s in AllowedSymbols where IsAllowedChildSymbol(parent, s) select s; 311 List<string> childs; 312 if (allowedChildSymbols.TryGetValue(parent.Name, out childs)) 313 return childs.Select(s => GetSymbol(s)).Where(s => s.Enabled); 314 return Enumerable.Empty<ISymbol>(); 310 315 } 311 316 … … 320 325 result = result.Union(temp); 321 326 322 return result.Select(x => GetSymbol(x)).Where(s => !s.InitialFrequency.IsAlmost(0.0));327 return result.Select(x => GetSymbol(x)).Where(s => s.Enabled); 323 328 } 324 329 … … 342 347 cachedMinExpressionLength[symbol.Name] = int.MaxValue; // prevent infinite recursion 343 348 long sumOfMinExpressionLengths = 1 + (from argIndex in Enumerable.Range(0, GetMinimumSubtreeCount(symbol)) 344 let minForSlot = (long)(from s in AllowedSymbols 345 where IsAllowedChildSymbol(symbol, s, argIndex) 349 let minForSlot = (long)(from s in GetAllowedChildSymbols(symbol, argIndex) 346 350 select GetMinimumExpressionLength(s)).DefaultIfEmpty(0).Min() 347 351 select minForSlot).DefaultIfEmpty(0).Sum(); … … 359 363 cachedMaxExpressionLength[symbol.Name] = int.MaxValue; // prevent infinite recursion 360 364 long sumOfMaxTrees = 1 + (from argIndex in Enumerable.Range(0, GetMaximumSubtreeCount(symbol)) 361 let maxForSlot = (long)(from s in AllowedSymbols 362 where IsAllowedChildSymbol(symbol, s, argIndex) 365 let maxForSlot = (long)(from s in GetAllowedChildSymbols(symbol, argIndex) 363 366 select GetMaximumExpressionLength(s)).DefaultIfEmpty(0).Max() 364 367 select maxForSlot).DefaultIfEmpty(0).Sum(); … … 375 378 cachedMinExpressionDepth[symbol.Name] = int.MaxValue; // prevent infinite recursion 376 379 long minDepth = 1 + (from argIndex in Enumerable.Range(0, GetMinimumSubtreeCount(symbol)) 377 let minForSlot = (long)(from s in AllowedSymbols 378 where IsAllowedChildSymbol(symbol, s, argIndex) 380 let minForSlot = (long)(from s in GetAllowedChildSymbols(symbol, argIndex) 379 381 select GetMinimumExpressionDepth(s)).DefaultIfEmpty(0).Min() 380 382 select minForSlot).DefaultIfEmpty(0).Max(); … … 385 387 } 386 388 389 390 #region events 391 private void RegisterSymbolEvents(ISymbol symbol) { 392 symbol.NameChanging += new EventHandler<CancelEventArgs<string>>(Symbol_NameChanging); 393 symbol.NameChanged += new EventHandler(Symbol_NameChanged); 394 symbol.Changed += new EventHandler(Symbol_Changed); 395 } 396 private void DeregisterSymbolEvents(ISymbol symbol) { 397 symbol.NameChanging -= new EventHandler<CancelEventArgs<string>>(Symbol_NameChanging); 398 symbol.NameChanged -= new EventHandler(Symbol_NameChanged); 399 symbol.Changed -= new EventHandler(Symbol_Changed); 400 } 401 402 private void Symbol_NameChanging(object sender, CancelEventArgs<string> e) { 403 if (symbols.ContainsKey(e.Value)) e.Cancel = true; 404 } 405 private void Symbol_NameChanged(object sender, EventArgs e) { 406 ISymbol symbol = (ISymbol)sender; 407 string oldName = symbols.Where(x => x.Value == symbol).First().Key; 408 string newName = symbol.Name; 409 410 symbols.Remove(oldName); 411 symbols.Add(newName, symbol); 412 413 var subtreeCount = symbolSubtreeCount[oldName]; 414 symbolSubtreeCount.Remove(oldName); 415 symbolSubtreeCount.Add(newName, subtreeCount); 416 417 List<string> allowedChilds; 418 if (allowedChildSymbols.TryGetValue(oldName, out allowedChilds)) { 419 allowedChildSymbols.Remove(oldName); 420 allowedChildSymbols.Add(newName, allowedChilds); 421 } 422 423 for (int i = 0; i < GetMaximumSubtreeCount(symbol); i++) { 424 if (allowedChildSymbolsPerIndex.TryGetValue(Tuple.Create(oldName, i), out allowedChilds)) { 425 allowedChildSymbolsPerIndex.Remove(Tuple.Create(oldName, i)); 426 allowedChildSymbolsPerIndex.Add(Tuple.Create(newName, i), allowedChilds); 427 } 428 } 429 430 foreach (var parent in Symbols) { 431 if (allowedChildSymbols.TryGetValue(parent.Name, out allowedChilds)) 432 if (allowedChilds.Remove(oldName)) 433 allowedChilds.Add(newName); 434 435 for (int i = 0; i < GetMaximumSubtreeCount(parent); i++) { 436 if (allowedChildSymbolsPerIndex.TryGetValue(Tuple.Create(parent.Name, i), out allowedChilds)) 437 if (allowedChilds.Remove(oldName)) allowedChilds.Add(newName); 438 } 439 } 440 441 ClearCaches(); 442 OnChanged(); 443 } 444 445 private void Symbol_Changed(object sende, EventArgs e) { 446 ClearCaches(); 447 OnChanged(); 448 } 449 387 450 public event EventHandler Changed; 388 451 protected virtual void OnChanged() { 452 if (suppressEvents) return; 389 453 var handler = Changed; 390 454 if (handler != null) Changed(this, EventArgs.Empty); -
branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Symbols/GroupSymbol.cs
r6285 r6296 32 32 public IEnumerable<ISymbol> Symbols { 33 33 get { return symbols; } 34 private set { symbols = new List<ISymbol>(value); } 34 } 35 36 public override bool Enabled { 37 get { return base.Enabled; } 38 set { 39 if (value != Enabled) { 40 base.Enabled = value; 41 symbols.ForEach(s => s.Enabled = value); 42 } 43 } 35 44 } 36 45 … … 39 48 private GroupSymbol(GroupSymbol original, Cloner cloner) 40 49 : base(original, cloner) { 50 symbols = original.Symbols.Select(s => cloner.Clone(s)).ToList(); 41 51 } 42 52 public override IDeepCloneable Clone(Cloner cloner) { … … 44 54 } 45 55 46 public GroupSymbol(string name) : this(name, "A symbol to group other symbols.") { } 47 public GroupSymbol(string name, string description) : this(name, description, Enumerable.Empty<ISymbol>()) { } 48 public GroupSymbol(string name, string description, IEnumerable<ISymbol> symbols) 49 : base(name, description) { 56 public GroupSymbol(string name, IEnumerable<ISymbol> symbols) 57 : base(name, "A symbol which groups other symbols") { 50 58 this.symbols = new List<ISymbol>(symbols); 59 InitialFrequency = 0.0; 51 60 } 52 61 } -
branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Symbols/Symbol.cs
r6284 r6296 38 38 if (value != initialFrequency) { 39 39 initialFrequency = value; 40 if (initialFrequency.IsAlmost(0.0)) enabled = false; 41 OnChanged(EventArgs.Empty); 42 } 43 } 44 } 45 46 [Storable(DefaultValue = true)] 47 private bool enabled; 48 public virtual bool Enabled { 49 get { return enabled; } 50 set { 51 if (value != enabled) { 52 enabled = value; 40 53 OnChanged(EventArgs.Empty); 41 54 } … … 68 81 : base(original, cloner) { 69 82 initialFrequency = original.initialFrequency; 83 enabled = original.enabled; 70 84 @fixed = original.@fixed; 71 85 } … … 74 88 : base(name, description) { 75 89 initialFrequency = 1.0; 90 enabled = true; 76 91 @fixed = false; 92 } 93 94 [StorableHook(HookType.AfterDeserialization)] 95 private void AfterDeserialization() { 96 if (initialFrequency.IsAlmost(0.0)) enabled = false; 77 97 } 78 98 … … 81 101 } 82 102 83 #region events84 103 public event EventHandler Changed; 85 104 protected virtual void OnChanged(EventArgs e) { … … 88 107 handlers(this, e); 89 108 } 90 #endregion91 109 } 92 110 }
Note: See TracChangeset
for help on using the changeset viewer.