- Timestamp:
- 09/20/11 11:12:10 (13 years ago)
- Location:
- trunk/sources
- Files:
-
- 1 added
- 23 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources
- Property svn:ignore
-
old new 13 13 *.vsp 14 14 *.docstates 15 bin 16 HeuristicLab 3.3.6.0.ReSharper.user
-
- Property svn:mergeinfo changed
/branches/GP.Grammar.Editor (added) merged: 6284-6285,6296,6299,6335,6337,6377,6379,6387,6403,6409,6415,6493-6494,6497,6618,6620,6622,6626,6647,6675,6782,6784,6786,6795
- Property svn:ignore
-
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding
-
Property
svn:ignore
set to
bin
-
Property
svn:ignore
set to
-
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Analyzers/SymbolicExpressionSymbolFrequencyAnalyzer.cs
r6709 r6803 127 127 string symbolName; 128 128 if (aggregateDifferentNumberOfSubtrees) symbolName = node.Symbol.Name; 129 else symbolName = node.Symbol.Name + "-" + node.Subtree sCount;129 else symbolName = node.Symbol.Name + "-" + node.SubtreeCount; 130 130 if (symbolFrequencies.ContainsKey(symbolName)) symbolFrequencies[symbolName] += 1; 131 131 else symbolFrequencies.Add(symbolName, 1); -
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Compiler/SymbolicExpressionTreeCompiler.cs
r5809 r6803 62 62 foreach (var node in branch.IterateNodesPrefix()) { 63 63 Instruction instr = new Instruction(); 64 int subtreesCount = node.Subtree sCount;64 int subtreesCount = node.SubtreeCount; 65 65 if (subtreesCount > 255) throw new ArgumentException("Number of subtrees is too big (>255)"); 66 66 instr.nArguments = (byte)subtreesCount; -
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Creators/ProbabilisticTreeCreator.cs
r6233 r6803 179 179 ReplaceWithMinimalTree(random, root, parent, argumentIndex); 180 180 } else { 181 var allowedSymbols = (from s in parent.Grammar. Symbols181 var allowedSymbols = (from s in parent.Grammar.GetAllowedChildSymbols(parent.Symbol, argumentIndex) 182 182 where s.InitialFrequency > 0.0 183 where parent.Grammar.IsAllowedChildSymbol(parent.Symbol, s, argumentIndex)184 183 where parent.Grammar.GetMinimumExpressionDepth(s) < maxDepth - extensionDepth + 1 185 184 where parent.Grammar.GetMaximumExpressionLength(s) > targetLength - totalListMinLength - currentLength … … 271 270 where s.InitialFrequency > 0.0 272 271 select node.Grammar.GetMaximumExpressionLength(s)).Max(); 273 if ( aggregatedLongestExpressionLength < targetLength) minArity = i + 1;272 if (i > minArity && aggregatedLongestExpressionLength < targetLength) minArity = i + 1; 274 273 else break; 275 274 } -
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Crossovers/SubtreeCrossover.cs
r5916 r6803 96 96 int maxInsertedBranchDepth = maxTreeDepth - GetBranchLevel(parent0.Root, crossoverPoint0.Parent); 97 97 98 99 98 List<ISymbolicExpressionTreeNode> allowedBranches = new List<ISymbolicExpressionTreeNode>(); 100 99 parent1.Root.ForEachNodePostfix((n) => { 101 100 if (n.GetLength() <= maxInsertedBranchLength && 102 n.GetDepth() <= maxInsertedBranchDepth &&103 IsMatchingPointType(crossoverPoint0, n))101 n.GetDepth() <= maxInsertedBranchDepth && 102 IsMatchingPointType(crossoverPoint0, n)) 104 103 allowedBranches.Add(n); 105 104 }); … … 133 132 if (newChild == null) { 134 133 // make sure that one subtree can be removed and that only the last subtree is removed 135 return parent.Grammar.GetMinimumSubtreeCount(parent.Symbol) < parent.Subtree sCount &&136 cutPoint.ChildIndex == parent.Subtree sCount - 1;134 return parent.Grammar.GetMinimumSubtreeCount(parent.Symbol) < parent.SubtreeCount && 135 cutPoint.ChildIndex == parent.SubtreeCount - 1; 137 136 } else { 138 137 // check syntax constraints of direct parent - child relation … … 146 145 result && 147 146 parent.Grammar.ContainsSymbol(n.Symbol) && 148 n.Subtree sCount >= parent.Grammar.GetMinimumSubtreeCount(n.Symbol) &&149 n.Subtree sCount <= parent.Grammar.GetMaximumSubtreeCount(n.Symbol);147 n.SubtreeCount >= parent.Grammar.GetMinimumSubtreeCount(n.Symbol) && 148 n.SubtreeCount <= parent.Grammar.GetMaximumSubtreeCount(n.Symbol); 150 149 }); 151 150 return result; … … 169 168 } 170 169 // add one additional extension point if the number of sub trees for the symbol is not full 171 if (n.Subtree sCount < n.Grammar.GetMaximumSubtreeCount(n.Symbol)) {170 if (n.SubtreeCount < n.Grammar.GetMaximumSubtreeCount(n.Symbol)) { 172 171 // empty extension point 173 internalCrossoverPoints.Add(new CutPoint(n, n.Subtree sCount));172 internalCrossoverPoints.Add(new CutPoint(n, n.SubtreeCount)); 174 173 } 175 174 } -
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4.csproj
r5809 r6803 167 167 <Compile Include="Symbols\Argument.cs" /> 168 168 <Compile Include="Symbols\ArgumentTreeNode.cs" /> 169 <Compile Include="Symbols\GroupSymbol.cs" /> 169 170 <Compile Include="Symbols\StartSymbol.cs" /> 170 171 <Compile Include="Symbols\InvokeFunction.cs" /> -
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Interfaces/ISymbol.cs
r5809 r6803 21 21 22 22 using System; 23 using System.Collections.Generic; 23 24 using HeuristicLab.Core; 24 25 … … 27 28 ISymbolicExpressionTreeNode CreateTreeNode(); 28 29 double InitialFrequency { get; set; } 30 bool Enabled { get; set; } 31 32 int MinimumArity { get; } 33 int MaximumArity { get; } 34 35 IEnumerable<ISymbol> Flatten(); 29 36 30 37 event EventHandler Changed; -
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Interfaces/ISymbolicExpressionGrammar.cs
r6233 r6803 35 35 bool ReadOnly { get; set; } 36 36 event EventHandler ReadOnlyChanged; 37 38 void AddSymbol(ISymbol symbol); 39 void RemoveSymbol(ISymbol symbol); 40 41 void AddAllowedChildSymbol(ISymbol parent, ISymbol child); 42 void AddAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex); 43 void RemoveAllowedChildSymbol(ISymbol parent, ISymbol child); 44 void RemoveAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex); 45 46 void SetSubtreeCount(ISymbol symbol, int minimumSubtreeCount, int maximumSubtreeCount); 47 48 void StartGrammarManipulation(); 49 void FinishedGrammarManipulation(); 37 50 } 38 51 } -
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Interfaces/ISymbolicExpressionGrammarBase.cs
r5809 r6803 20 20 #endregion 21 21 22 using System; 22 23 using System.Collections.Generic; 23 24 using HeuristicLab.Core; … … 41 42 int GetMaximumExpressionLength(ISymbol start); 42 43 int GetMinimumExpressionDepth(ISymbol start); 44 45 event EventHandler Changed; 43 46 } 44 47 } -
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Interfaces/ISymbolicExpressionTreeNode.cs
r5809 r6803 39 39 40 40 IEnumerable<ISymbolicExpressionTreeNode> Subtrees { get; } 41 int Subtree sCount { get; }41 int SubtreeCount { get; } 42 42 ISymbolicExpressionTreeNode GetSubtree(int index); 43 43 int IndexOfSubtree(ISymbolicExpressionTreeNode tree); -
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionGrammar.cs
r6532 r6803 21 21 22 22 using System; 23 using System.Collections.Generic; 23 24 using System.Linq; 25 using HeuristicLab.Collections; 24 26 using HeuristicLab.Common; 25 27 using HeuristicLab.Core; 26 28 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 27 using System.Collections.Generic;28 29 29 30 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding { … … 120 121 protected SymbolicExpressionGrammar(SymbolicExpressionGrammar original, Cloner cloner) 121 122 : base(original, cloner) { 122 foreach (ISymbol symbol in Symbols)123 foreach (ISymbol symbol in symbols.Values) 123 124 RegisterSymbolEvents(symbol); 124 125 … … 161 162 } 162 163 163 protected override void AddSymbol(ISymbol symbol) {164 protected override sealed void AddSymbol(ISymbol symbol) { 164 165 base.AddSymbol(symbol); 165 166 RegisterSymbolEvents(symbol); 166 } 167 protected override void RemoveSymbol(ISymbol symbol) { 167 OnChanged(); 168 } 169 protected override sealed void RemoveSymbol(ISymbol symbol) { 168 170 DeregisterSymbolEvents(symbol); 169 171 base.RemoveSymbol(symbol); 172 OnChanged(); 170 173 } 171 174 … … 177 180 } 178 181 179 #region IStatefulItem 182 #region IStatefulItem methods 180 183 void IStatefulItem.InitializeState() { } 181 184 void IStatefulItem.ClearState() { … … 184 187 #endregion 185 188 189 #region ISymbolicExpressionGrammar methods 190 void ISymbolicExpressionGrammar.AddSymbol(ISymbol symbol) { 191 if (ReadOnly) throw new InvalidOperationException(); 192 AddSymbol(symbol); 193 } 194 void ISymbolicExpressionGrammar.RemoveSymbol(ISymbol symbol) { 195 if (ReadOnly) throw new InvalidOperationException(); 196 RemoveSymbol(symbol); 197 } 198 199 void ISymbolicExpressionGrammar.AddAllowedChildSymbol(ISymbol parent, ISymbol child) { 200 if (ReadOnly) throw new InvalidOperationException(); 201 base.AddAllowedChildSymbol(parent, child); 202 } 203 void ISymbolicExpressionGrammar.AddAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) { 204 if (ReadOnly) throw new InvalidOperationException(); 205 base.AddAllowedChildSymbol(parent, child, argumentIndex); 206 } 207 void ISymbolicExpressionGrammar.RemoveAllowedChildSymbol(ISymbol parent, ISymbol child) { 208 if (ReadOnly) throw new InvalidOperationException(); 209 base.RemoveAllowedChildSymbol(parent, child); 210 } 211 void ISymbolicExpressionGrammar.RemoveAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) { 212 if (ReadOnly) throw new InvalidOperationException(); 213 base.RemoveAllowedChildSymbol(parent, child, argumentIndex); 214 } 215 216 void ISymbolicExpressionGrammar.SetSubtreeCount(ISymbol symbol, int minimumSubtreeCount, int maximumSubtreeCount) { 217 if (ReadOnly) throw new InvalidOperationException(); 218 base.SetSubtreeCount(symbol, minimumSubtreeCount, maximumSubtreeCount); 219 } 220 221 private bool suppressEvents = false; 222 void ISymbolicExpressionGrammar.StartGrammarManipulation() { 223 suppressEvents = true; 224 } 225 void ISymbolicExpressionGrammar.FinishedGrammarManipulation() { 226 suppressEvents = false; 227 OnChanged(); 228 } 229 230 protected override void OnChanged() { 231 if (suppressEvents) return; 232 base.OnChanged(); 233 } 234 #endregion 235 186 236 #region symbol events 187 protected virtual void RegisterSymbolEvents(ISymbol symbol) { 188 symbol.NameChanging += new EventHandler<CancelEventArgs<string>>(Symbol_NameChanging); 189 symbol.NameChanged += new EventHandler(Symbol_NameChanged); 190 } 191 protected virtual void DeregisterSymbolEvents(ISymbol symbol) { 192 symbol.NameChanging -= new EventHandler<CancelEventArgs<string>>(Symbol_NameChanging); 193 symbol.NameChanged -= new EventHandler(Symbol_NameChanged); 237 private void RegisterSymbolEvents(ISymbol symbol) { 238 foreach (var s in symbol.Flatten()) { 239 s.NameChanging += new EventHandler<CancelEventArgs<string>>(Symbol_NameChanging); 240 s.NameChanged += new EventHandler(Symbol_NameChanged); 241 242 var groupSymbol = s as GroupSymbol; 243 if (groupSymbol != null) RegisterGroupSymbolEvents(groupSymbol); 244 else symbol.Changed += new EventHandler(Symbol_Changed); 245 } 246 } 247 private void DeregisterSymbolEvents(ISymbol symbol) { 248 foreach (var s in symbol.Flatten()) { 249 s.NameChanging -= new EventHandler<CancelEventArgs<string>>(Symbol_NameChanging); 250 s.NameChanged -= new EventHandler(Symbol_NameChanged); 251 252 var groupSymbol = s as GroupSymbol; 253 if (groupSymbol != null) DeregisterGroupSymbolEvents(groupSymbol); 254 else symbol.Changed -= new EventHandler(Symbol_Changed); 255 } 256 } 257 258 private void RegisterGroupSymbolEvents(GroupSymbol groupSymbol) { 259 groupSymbol.Changed += new EventHandler(GroupSymbol_Changed); 260 groupSymbol.SymbolsCollection.ItemsAdded += new Collections.CollectionItemsChangedEventHandler<ISymbol>(GroupSymbol_ItemsAdded); 261 groupSymbol.SymbolsCollection.ItemsRemoved += new Collections.CollectionItemsChangedEventHandler<ISymbol>(GroupSymbol_ItemsRemoved); 262 groupSymbol.SymbolsCollection.CollectionReset += new Collections.CollectionItemsChangedEventHandler<ISymbol>(GroupSymbol_CollectionReset); 263 } 264 private void DeregisterGroupSymbolEvents(GroupSymbol groupSymbol) { 265 groupSymbol.Changed -= new EventHandler(GroupSymbol_Changed); 266 groupSymbol.SymbolsCollection.ItemsAdded -= new Collections.CollectionItemsChangedEventHandler<ISymbol>(GroupSymbol_ItemsAdded); 267 groupSymbol.SymbolsCollection.ItemsRemoved -= new Collections.CollectionItemsChangedEventHandler<ISymbol>(GroupSymbol_ItemsRemoved); 268 groupSymbol.SymbolsCollection.CollectionReset -= new Collections.CollectionItemsChangedEventHandler<ISymbol>(GroupSymbol_CollectionReset); 269 } 270 271 private void Symbol_Changed(object sender, EventArgs e) { 272 ClearCaches(); 273 OnChanged(); 274 } 275 276 private void GroupSymbol_Changed(object sender, EventArgs e) { 277 GroupSymbol groupSymbol = (GroupSymbol)sender; 278 foreach (ISymbol symbol in groupSymbol.Flatten()) 279 symbol.Enabled = groupSymbol.Enabled; 280 281 ClearCaches(); 282 OnChanged(); 194 283 } 195 284 … … 234 323 235 324 ClearCaches(); 325 OnChanged(); 326 } 327 328 private void GroupSymbol_ItemsAdded(object sender, CollectionItemsChangedEventArgs<ISymbol> e) { 329 foreach (ISymbol symbol in e.Items) 330 if (!ContainsSymbol(symbol)) 331 AddSymbol(symbol); 332 OnChanged(); 333 } 334 private void GroupSymbol_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<ISymbol> e) { 335 foreach (ISymbol symbol in e.Items) 336 if (ContainsSymbol(symbol)) 337 RemoveSymbol(symbol); 338 OnChanged(); 339 } 340 private void GroupSymbol_CollectionReset(object sender, CollectionItemsChangedEventArgs<ISymbol> e) { 341 foreach (ISymbol symbol in e.Items) 342 if (!ContainsSymbol(symbol)) 343 AddSymbol(symbol); 344 foreach (ISymbol symbol in e.OldItems) 345 if (ContainsSymbol(symbol)) 346 RemoveSymbol(symbol); 347 OnChanged(); 236 348 } 237 349 #endregion -
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionGrammarBase.cs
r6443 r6803 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 private 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 … … 98 102 foreach (var element in original.allowedChildSymbolsPerIndex) 99 103 allowedChildSymbolsPerIndex.Add(element.Key, new List<string>(element.Value)); 104 105 suppressEvents = false; 100 106 } 101 107 … … 110 116 allowedChildSymbols = new Dictionary<string, List<string>>(); 111 117 allowedChildSymbolsPerIndex = new Dictionary<Tuple<string, int>, List<string>>(); 118 119 suppressEvents = false; 112 120 } 113 121 … … 115 123 protected virtual void AddSymbol(ISymbol symbol) { 116 124 if (ContainsSymbol(symbol)) throw new ArgumentException("Symbol " + symbol + " is already defined."); 117 symbols.Add(symbol.Name, symbol); 118 symbolSubtreeCount.Add(symbol.Name, Tuple.Create(0, 0)); 125 foreach (var s in symbol.Flatten()) { 126 symbols.Add(s.Name, s); 127 symbolSubtreeCount.Add(s.Name, Tuple.Create(s.MinimumArity, s.MaximumArity)); 128 } 119 129 ClearCaches(); 120 130 } 121 131 122 132 protected virtual void RemoveSymbol(ISymbol symbol) { 123 symbols.Remove(symbol.Name); 124 allowedChildSymbols.Remove(symbol.Name); 125 for (int i = 0; i < GetMaximumSubtreeCount(symbol); i++) 126 allowedChildSymbolsPerIndex.Remove(Tuple.Create(symbol.Name, i)); 127 symbolSubtreeCount.Remove(symbol.Name); 128 129 foreach (var parent in Symbols) { 130 List<string> allowedChilds; 131 if (allowedChildSymbols.TryGetValue(parent.Name, out allowedChilds)) 132 allowedChilds.Remove(symbol.Name); 133 134 for (int i = 0; i < GetMaximumSubtreeCount(parent); i++) { 135 if (allowedChildSymbolsPerIndex.TryGetValue(Tuple.Create(parent.Name, i), out allowedChilds)) 136 allowedChilds.Remove(symbol.Name); 133 foreach (var s in symbol.Flatten()) { 134 symbols.Remove(s.Name); 135 allowedChildSymbols.Remove(s.Name); 136 for (int i = 0; i < GetMaximumSubtreeCount(s); i++) 137 allowedChildSymbolsPerIndex.Remove(Tuple.Create(s.Name, i)); 138 symbolSubtreeCount.Remove(s.Name); 139 140 foreach (var parent in Symbols) { 141 List<string> allowedChilds; 142 if (allowedChildSymbols.TryGetValue(parent.Name, out allowedChilds)) 143 allowedChilds.Remove(s.Name); 144 145 for (int i = 0; i < GetMaximumSubtreeCount(parent); i++) { 146 if (allowedChildSymbolsPerIndex.TryGetValue(Tuple.Create(parent.Name, i), out allowedChilds)) 147 allowedChilds.Remove(s.Name); 148 } 137 149 } 150 suppressEvents = true; 151 foreach (var groupSymbol in Symbols.OfType<GroupSymbol>()) 152 groupSymbol.SymbolsCollection.Remove(symbol); 153 suppressEvents = false; 138 154 } 139 155 ClearCaches(); … … 147 163 148 164 protected void AddAllowedChildSymbol(ISymbol parent, ISymbol child) { 165 bool changed = false; 166 167 foreach (ISymbol p in parent.Flatten().Where(p => !(p is GroupSymbol))) 168 changed |= AddAllowedChildSymbolToDictionaries(p, child); 169 170 if (changed) { 171 ClearCaches(); 172 OnChanged(); 173 } 174 } 175 176 private bool AddAllowedChildSymbolToDictionaries(ISymbol parent, ISymbol child) { 149 177 List<string> childSymbols; 150 178 if (!allowedChildSymbols.TryGetValue(parent.Name, out childSymbols)) { … … 152 180 allowedChildSymbols.Add(parent.Name, childSymbols); 153 181 } 154 if (childSymbols.Contains(child.Name)) throw new ArgumentException(); 182 if (childSymbols.Contains(child.Name)) return false; 183 184 suppressEvents = true; 185 for (int argumentIndex = 0; argumentIndex < GetMaximumSubtreeCount(parent); argumentIndex++) 186 RemoveAllowedChildSymbol(parent, child, argumentIndex); 187 suppressEvents = false; 188 155 189 childSymbols.Add(child.Name); 156 ClearCaches();190 return true; 157 191 } 158 192 159 193 protected void AddAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) { 194 bool changed = false; 195 196 foreach (ISymbol p in parent.Flatten().Where(p => !(p is GroupSymbol))) 197 changed |= AddAllowedChildSymbolToDictionaries(p, child, argumentIndex); 198 199 if (changed) { 200 ClearCaches(); 201 OnChanged(); 202 } 203 } 204 205 206 private bool AddAllowedChildSymbolToDictionaries(ISymbol parent, ISymbol child, int argumentIndex) { 207 List<string> childSymbols; 208 if (!allowedChildSymbols.TryGetValue(parent.Name, out childSymbols)) { 209 childSymbols = new List<string>(); 210 allowedChildSymbols.Add(parent.Name, childSymbols); 211 } 212 if (childSymbols.Contains(child.Name)) return false; 213 214 160 215 var key = Tuple.Create(parent.Name, argumentIndex); 161 List<string> childSymbols;162 216 if (!allowedChildSymbolsPerIndex.TryGetValue(key, out childSymbols)) { 163 217 childSymbols = new List<string>(); … … 165 219 } 166 220 167 if ( IsAllowedChildSymbol(parent, child)) throw new ArgumentException();168 if (childSymbols.Contains(child.Name)) throw new ArgumentException(); 221 if (childSymbols.Contains(child.Name)) return false; 222 169 223 childSymbols.Add(child.Name); 170 ClearCaches();224 return true; 171 225 } 172 226 173 227 protected void RemoveAllowedChildSymbol(ISymbol parent, ISymbol child) { 228 bool changed = false; 174 229 List<string> childSymbols; 175 230 if (allowedChildSymbols.TryGetValue(child.Name, out childSymbols)) { 176 if (allowedChildSymbols[parent.Name].Remove(child.Name)) 177 ClearCaches(); 231 changed |= childSymbols.Remove(child.Name); 232 } 233 234 for (int argumentIndex = 0; argumentIndex < GetMaximumSubtreeCount(parent); argumentIndex++) { 235 var key = Tuple.Create(parent.Name, argumentIndex); 236 if (allowedChildSymbolsPerIndex.TryGetValue(key, out childSymbols)) 237 changed |= childSymbols.Remove(child.Name); 238 } 239 240 if (changed) { 241 ClearCaches(); 242 OnChanged(); 178 243 } 179 244 } 180 245 181 246 protected void RemoveAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) { 247 bool changed = false; 248 249 suppressEvents = true; 250 List<string> childSymbols; 251 if (allowedChildSymbols.TryGetValue(parent.Name, out childSymbols)) { 252 if (childSymbols.Remove(child.Name)) { 253 for (int i = 0; i < GetMaximumSubtreeCount(parent); i++) { 254 if (i != argumentIndex) AddAllowedChildSymbol(parent, child, i); 255 } 256 changed = true; 257 } 258 } 259 suppressEvents = false; 260 182 261 var key = Tuple.Create(parent.Name, argumentIndex); 183 List<string> childSymbols; 184 if (allowedChildSymbolsPerIndex.TryGetValue(key, out childSymbols)) { 185 if (allowedChildSymbolsPerIndex[key].Remove(child.Name)) 186 ClearCaches(); 262 if (allowedChildSymbolsPerIndex.TryGetValue(key, out childSymbols)) 263 changed |= childSymbols.Remove(child.Name); 264 265 if (changed) { 266 ClearCaches(); 267 OnChanged(); 187 268 } 188 269 } 189 270 190 271 protected void SetSubtreeCount(ISymbol symbol, int minimumSubtreeCount, int maximumSubtreeCount) { 191 for (int i = GetMaximumSubtreeCount(symbol) - 1; i >= maximumSubtreeCount; i--) { 272 var symbols = symbol.Flatten().Where(s => !(s is GroupSymbol)); 273 if (symbols.Any(s => s.MinimumArity > minimumSubtreeCount)) throw new ArgumentException("Invalid minimum subtree count " + minimumSubtreeCount + " for " + symbol); 274 if (symbols.Any(s => s.MaximumArity < maximumSubtreeCount)) throw new ArgumentException("Invalid maximum subtree count " + maximumSubtreeCount + " for " + symbol); 275 276 foreach (ISymbol s in symbols) 277 SetSubTreeCountInDictionaries(s, minimumSubtreeCount, maximumSubtreeCount); 278 279 ClearCaches(); 280 OnChanged(); 281 } 282 283 private void SetSubTreeCountInDictionaries(ISymbol symbol, int minimumSubtreeCount, int maximumSubtreeCount) { 284 for (int i = maximumSubtreeCount; i < GetMaximumSubtreeCount(symbol); i++) { 192 285 var key = Tuple.Create(symbol.Name, i); 193 286 allowedChildSymbolsPerIndex.Remove(key); … … 195 288 196 289 symbolSubtreeCount[symbol.Name] = Tuple.Create(minimumSubtreeCount, maximumSubtreeCount); 197 ClearCaches();198 290 } 199 291 #endregion 200 292 201 #region ISymbolicExpressionGrammarBase Members202 293 public virtual IEnumerable<ISymbol> Symbols { 203 294 get { return symbols.Values; } 204 295 } 205 296 public virtual IEnumerable<ISymbol> AllowedSymbols { 206 get { return Symbols.Where(s => !s.InitialFrequency.IsAlmost(0.0)); }297 get { return Symbols.Where(s => s.Enabled); } 207 298 } 208 299 public virtual bool ContainsSymbol(ISymbol symbol) { … … 211 302 212 303 public virtual bool IsAllowedChildSymbol(ISymbol parent, ISymbol child) { 304 if (!child.Enabled) return false; 305 213 306 List<string> temp; 214 if (allowedChildSymbols.TryGetValue(parent.Name, out temp)) 307 if (allowedChildSymbols.TryGetValue(parent.Name, out temp)) { 215 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 } 216 311 return false; 217 312 } 218 313 219 314 public virtual bool IsAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) { 315 if (!child.Enabled) return false; 316 if (IsAllowedChildSymbol(parent, child)) return true; 317 220 318 List<string> temp; 221 if (allowedChildSymbols.TryGetValue(parent.Name, out temp)) 319 var key = Tuple.Create(parent.Name, argumentIndex); 320 if (allowedChildSymbolsPerIndex.TryGetValue(key, out temp)) { 222 321 if (temp.Contains(child.Name)) return true; 223 224 var key = Tuple.Create(parent.Name, argumentIndex); 225 if (allowedChildSymbolsPerIndex.TryGetValue(key, out temp)) 226 return temp.Contains(child.Name); 322 if (temp.SelectMany(s => GetSymbol(s).Flatten().Select(n => n.Name)).Contains(child.Name)) return true; 323 } 227 324 return false; 228 325 } 229 326 230 327 public virtual IEnumerable<ISymbol> GetAllowedChildSymbols(ISymbol parent) { 231 return from s in AllowedSymbols where IsAllowedChildSymbol(parent, s) select s; 328 List<string> childs; 329 if (!allowedChildSymbols.TryGetValue(parent.Name, out childs)) 330 return Enumerable.Empty<ISymbol>(); 331 332 return childs.Select(x => GetSymbol(x)).Where(s => s.Enabled); 232 333 } 233 334 … … 242 343 result = result.Union(temp); 243 344 244 return result.Select(x => GetSymbol(x)) ;345 return result.Select(x => GetSymbol(x)).Where(s => s.Enabled); 245 346 } 246 347 … … 251 352 return symbolSubtreeCount[symbol.Name].Item2; 252 353 } 253 254 354 255 355 protected void ClearCaches() { … … 265 365 cachedMinExpressionLength[symbol.Name] = int.MaxValue; // prevent infinite recursion 266 366 long sumOfMinExpressionLengths = 1 + (from argIndex in Enumerable.Range(0, GetMinimumSubtreeCount(symbol)) 267 let minForSlot = (long)(from s in AllowedSymbols 268 where IsAllowedChildSymbol(symbol, s, argIndex) 367 let minForSlot = (long)(from s in GetAllowedChildSymbols(symbol, argIndex) 269 368 select GetMinimumExpressionLength(s)).DefaultIfEmpty(0).Min() 270 369 select minForSlot).DefaultIfEmpty(0).Sum(); … … 282 381 cachedMaxExpressionLength[symbol.Name] = int.MaxValue; // prevent infinite recursion 283 382 long sumOfMaxTrees = 1 + (from argIndex in Enumerable.Range(0, GetMaximumSubtreeCount(symbol)) 284 let maxForSlot = (long)(from s in AllowedSymbols 285 where IsAllowedChildSymbol(symbol, s, argIndex) 383 let maxForSlot = (long)(from s in GetAllowedChildSymbols(symbol, argIndex) 286 384 select GetMaximumExpressionLength(s)).DefaultIfEmpty(0).Max() 287 385 select maxForSlot).DefaultIfEmpty(0).Sum(); … … 298 396 cachedMinExpressionDepth[symbol.Name] = int.MaxValue; // prevent infinite recursion 299 397 long minDepth = 1 + (from argIndex in Enumerable.Range(0, GetMinimumSubtreeCount(symbol)) 300 let minForSlot = (long)(from s in AllowedSymbols 301 where IsAllowedChildSymbol(symbol, s, argIndex) 398 let minForSlot = (long)(from s in GetAllowedChildSymbols(symbol, argIndex) 302 399 select GetMinimumExpressionDepth(s)).DefaultIfEmpty(0).Min() 303 400 select minForSlot).DefaultIfEmpty(0).Max(); … … 307 404 return temp; 308 405 } 309 #endregion 406 407 public event EventHandler Changed; 408 protected virtual void OnChanged() { 409 if (suppressEvents) return; 410 var handler = Changed; 411 if (handler != null) Changed(this, EventArgs.Empty); 412 } 310 413 } 311 414 } -
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTreeGrammar.cs
r6378 r6803 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 -
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTreeNode.cs
r6684 r6803 128 128 public virtual void ShakeLocalParameters(IRandom random, double shakingFactor) { } 129 129 130 public int Subtree sCount {130 public int SubtreeCount { 131 131 get { 132 132 if (subtrees == null) return 0; -
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Symbols/Argument.cs
r6233 r6803 32 32 public const string ArgumentName = "Argument"; 33 33 public const string ArgumentDescription = "Symbol that represents a function argument."; 34 private const int minimumArity = 0; 35 private const int maximumArity = 0; 36 37 public override int MinimumArity { 38 get { return minimumArity; } 39 } 40 public override int MaximumArity { 41 get { return maximumArity; } 42 } 43 34 44 [Storable] 35 45 private int argumentIndex; -
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Symbols/Defun.cs
r5809 r6803 32 32 public const string DefunName = "Defun"; 33 33 public const string DefunDescription = "Symbol that represents a function defining node."; 34 private const int minimumArity = 1; 35 private const int maximumArity = 1; 36 37 public override int MinimumArity { 38 get { return minimumArity; } 39 } 40 public override int MaximumArity { 41 get { return maximumArity; } 42 } 34 43 35 44 [StorableConstructor] -
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Symbols/InvokeFunction.cs
r6233 r6803 32 32 public const string InvokeFunctionName = "InvokeFunction"; 33 33 public const string InvokeFunctionDescription = "Symbol that the invocation of another function."; 34 private const int minimumArity = 0; 35 private const int maximumArity = byte.MaxValue; 36 37 public override int MinimumArity { 38 get { return minimumArity; } 39 } 40 public override int MaximumArity { 41 get { return maximumArity; } 42 } 34 43 35 44 [Storable] -
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Symbols/ProgramRootSymbol.cs
r5809 r6803 29 29 public const string ProgramRootSymbolName = "ProgramRootSymbol"; 30 30 public const string ProgramRootSymbolDescription = "Special symbol that represents the program root node of a symbolic expression tree."; 31 private const int minimumArity = 1; 32 private const int maximumArity = byte.MaxValue; 33 34 public override int MinimumArity { 35 get { return minimumArity; } 36 } 37 public override int MaximumArity { 38 get { return maximumArity; } 39 } 31 40 32 41 [StorableConstructor] -
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Symbols/StartSymbol.cs
r5809 r6803 29 29 public const string StartSymbolName = "StartSymbol"; 30 30 public const string StartSymbolDescription = "Special symbol that represents the starting node of the result producing branch of a symbolic expression tree."; 31 private const int minimumArity = 1; 32 private const int maximumArity = 1; 33 34 public override int MinimumArity { 35 get { return minimumArity; } 36 } 37 public override int MaximumArity { 38 get { return maximumArity; } 39 } 31 40 32 41 [StorableConstructor] 33 private StartSymbol(bool deserializing) : base(deserializing) { } 42 private StartSymbol(bool deserializing) 43 : base(deserializing) { 44 } 34 45 private StartSymbol(StartSymbol original, Cloner cloner) : base(original, cloner) { } 35 46 public StartSymbol() : base(StartSymbol.StartSymbolName, StartSymbol.StartSymbolDescription) { } -
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Symbols/Symbol.cs
r6233 r6803 21 21 22 22 using System; 23 using System.Collections.Generic; 23 24 using HeuristicLab.Common; 24 25 using HeuristicLab.Core; … … 42 43 } 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; 53 OnChanged(EventArgs.Empty); 54 } 55 } 56 } 57 58 [Storable(DefaultValue = false)] 59 private bool @fixed; 60 public bool Fixed { 61 get { return @fixed; } 62 set { 63 if (value != @fixed) { 64 @fixed = value; 65 OnChanged(EventArgs.Empty); 66 } 67 } 68 } 69 44 70 public override bool CanChangeName { 45 71 get { return !(this is IReadOnlySymbol); } … … 48 74 get { return false; } 49 75 } 76 77 public abstract int MinimumArity { get; } 78 public abstract int MaximumArity { get; } 50 79 #endregion 51 80 … … 55 84 : base(original, cloner) { 56 85 initialFrequency = original.initialFrequency; 86 enabled = original.enabled; 87 @fixed = original.@fixed; 57 88 } 58 89 … … 60 91 : base(name, description) { 61 92 initialFrequency = 1.0; 93 enabled = true; 94 @fixed = false; 95 } 96 97 [StorableHook(HookType.AfterDeserialization)] 98 private void AfterDeserialization() { 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 62 104 } 63 105 … … 66 108 } 67 109 68 #region events 110 public virtual IEnumerable<ISymbol> Flatten() { 111 yield return this; 112 } 113 69 114 public event EventHandler Changed; 70 protected v oid OnChanged(EventArgs e) {115 protected virtual void OnChanged(EventArgs e) { 71 116 EventHandler handlers = Changed; 72 117 if (handlers != null) 73 118 handlers(this, e); 74 119 } 75 #endregion76 120 } 77 121 } -
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Tests/Grammars.cs
r6375 r6803 27 27 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._4.Tests { 28 28 public static class Grammars { 29 30 29 [StorableClass] 31 30 private class Addition : Symbol { 31 private const int minimumArity = 1; 32 private const int maximumArity = byte.MaxValue; 33 34 public override int MinimumArity { 35 get { return minimumArity; } 36 } 37 public override int MaximumArity { 38 get { return maximumArity; } 39 } 40 32 41 [StorableConstructor] 33 42 protected Addition(bool deserializing) : base(deserializing) { } … … 41 50 [StorableClass] 42 51 private class Subtraction : Symbol { 52 private const int minimumArity = 1; 53 private const int maximumArity = byte.MaxValue; 54 55 public override int MinimumArity { 56 get { return minimumArity; } 57 } 58 public override int MaximumArity { 59 get { return maximumArity; } 60 } 61 43 62 [StorableConstructor] 44 63 protected Subtraction(bool deserializing) : base(deserializing) { } … … 52 71 [StorableClass] 53 72 private class Multiplication : Symbol { 73 private const int minimumArity = 1; 74 private const int maximumArity = byte.MaxValue; 75 76 public override int MinimumArity { 77 get { return minimumArity; } 78 } 79 public override int MaximumArity { 80 get { return maximumArity; } 81 } 82 54 83 [StorableConstructor] 55 84 protected Multiplication(bool deserializing) : base(deserializing) { } … … 63 92 [StorableClass] 64 93 private class Division : Symbol { 94 private const int minimumArity = 1; 95 private const int maximumArity = byte.MaxValue; 96 97 public override int MinimumArity { 98 get { return minimumArity; } 99 } 100 public override int MaximumArity { 101 get { return maximumArity; } 102 } 103 65 104 [StorableConstructor] 66 105 protected Division(bool deserializing) : base(deserializing) { } … … 74 113 [StorableClass] 75 114 private class Terminal : Symbol { 115 private const int minimumArity = 0; 116 private const int maximumArity = 0; 117 118 public override int MinimumArity { 119 get { return minimumArity; } 120 } 121 public override int MaximumArity { 122 get { return maximumArity; } 123 } 124 76 125 [StorableConstructor] 77 126 protected Terminal(bool deserializing) : base(deserializing) { } -
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Tests/Util.cs
r5809 r6803 122 122 123 123 foreach (var argTreenode in defunTreeNode.IterateNodesPrefix().OfType<ArgumentTreeNode>()) { 124 Assert.IsTrue(argTreenode.Subtree sCount == 0);124 Assert.IsTrue(argTreenode.SubtreeCount == 0); 125 125 Assert.IsTrue(((Argument)argTreenode.Symbol).ArgumentIndex < arity); 126 126 }
Note: See TracChangeset
for help on using the changeset viewer.