- Timestamp:
- 07/26/10 16:18:45 (14 years ago)
- Location:
- trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3
- Files:
-
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureManipulators/ArgumentCreater.cs
r4068 r4106 83 83 selectedCutPoint.Parent.RemoveSubTree(selectedCutPoint.ReplacedChildIndex); 84 84 selectedCutPoint.Parent.InsertSubTree(selectedCutPoint.ReplacedChildIndex, newArgNode); 85 85 86 // find all invocations of the selected ADF and attach a cloned version of the replaced branch (with all argument-nodes expanded) 86 87 var invocationNodes = from node in symbolicExpressionTree.IterateNodesPrefix().OfType<InvokeFunctionTreeNode>() 87 88 where node.Symbol.FunctionName == selectedDefunBranch.FunctionName 89 where node.SubTrees.Count == selectedDefunBranch.NumberOfArguments 88 90 select node; 89 foreach (var invocationNode in invocationNodes) { 90 // append a new argument branch after expanding all argument nodes 91 var clonedBranch = (SymbolicExpressionTreeNode)replacedBranch.Clone(); 92 clonedBranch = ReplaceArgumentsInBranch(clonedBranch, invocationNode.SubTrees); 93 invocationNode.InsertSubTree(newArgumentIndex, clonedBranch); 91 // do this repeatedly until no matching invocations are found 92 while (invocationNodes.Count() > 0) { 93 List<SymbolicExpressionTreeNode> newlyAddedBranches = new List<SymbolicExpressionTreeNode>(); 94 foreach (var invocationNode in invocationNodes) { 95 // append a new argument branch after expanding all argument nodes 96 var clonedBranch = (SymbolicExpressionTreeNode)replacedBranch.Clone(); 97 clonedBranch = ReplaceArgumentsInBranch(clonedBranch, invocationNode.SubTrees); 98 invocationNode.InsertSubTree(newArgumentIndex, clonedBranch); 99 newlyAddedBranches.Add(clonedBranch); 100 } 101 invocationNodes = from newlyAddedBranch in newlyAddedBranches 102 from node in newlyAddedBranch.IterateNodesPrefix().OfType<InvokeFunctionTreeNode>() 103 where node.Symbol.FunctionName == selectedDefunBranch.FunctionName 104 where node.SubTrees.Count == selectedDefunBranch.NumberOfArguments 105 select node; 94 106 } 95 107 // increase expected number of arguments of function defining branch -
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureManipulators/ArgumentDuplicater.cs
r4068 r4106 25 25 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols; 26 26 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 27 using System.Collections.Generic; 27 28 28 29 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ArchitectureManipulators { … … 80 81 var invocationNodes = from node in symbolicExpressionTree.IterateNodesPrefix().OfType<InvokeFunctionTreeNode>() 81 82 where node.Symbol.FunctionName == selectedDefunBranch.FunctionName 83 where node.SubTrees.Count == selectedDefunBranch.NumberOfArguments 82 84 select node; 83 foreach (var invokeNode in invocationNodes) { 84 var argumentBranch = invokeNode.SubTrees[selectedArgumentSymbol.ArgumentIndex]; 85 var clonedArgumentBranch = (SymbolicExpressionTreeNode)argumentBranch.Clone(); 86 invokeNode.InsertSubTree(newArgumentIndex, clonedArgumentBranch); 85 // do this repeatedly until no matching invocations are found 86 while (invocationNodes.Count() > 0) { 87 List<SymbolicExpressionTreeNode> newlyAddedBranches = new List<SymbolicExpressionTreeNode>(); 88 foreach (var invokeNode in invocationNodes) { 89 var argumentBranch = invokeNode.SubTrees[selectedArgumentSymbol.ArgumentIndex]; 90 var clonedArgumentBranch = (SymbolicExpressionTreeNode)argumentBranch.Clone(); 91 invokeNode.InsertSubTree(newArgumentIndex, clonedArgumentBranch); 92 newlyAddedBranches.Add(clonedArgumentBranch); 93 } 94 invocationNodes = from newlyAddedBranch in newlyAddedBranches 95 from node in newlyAddedBranch.IterateNodesPrefix().OfType<InvokeFunctionTreeNode>() 96 where node.Symbol.FunctionName == selectedDefunBranch.FunctionName 97 where node.SubTrees.Count == selectedDefunBranch.NumberOfArguments 98 select node; 87 99 } 88 100 // register the new argument symbol and increase the number of arguments of the ADF -
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Crossovers/SubtreeCrossover.cs
r4068 r4106 90 90 private static bool IsMatchingPointType(SymbolicExpressionTreeNode parent, int replacedSubtreeIndex, SymbolicExpressionTreeNode branch) { 91 91 // check syntax constraints of direct parent - child relation 92 if (!parent.Grammar.IsAllowedChild(parent.Symbol, branch.Symbol, replacedSubtreeIndex)) return false; 92 if (!parent.Grammar.ContainsSymbol(branch.Symbol) || 93 !parent.Grammar.IsAllowedChild(parent.Symbol, branch.Symbol, replacedSubtreeIndex)) return false; 93 94 94 95 bool result = true; … … 97 98 result = 98 99 result && 100 parent.Grammar.ContainsSymbol(n.Symbol) && 99 101 n.SubTrees.Count >= parent.Grammar.GetMinSubtreeCount(n.Symbol) && 100 n.SubTrees.Count <= parent.Grammar.GetMaxSubtreeCount(n.Symbol) && 101 parent.Grammar.ContainsSymbol(n.Symbol); 102 n.SubTrees.Count <= parent.Grammar.GetMaxSubtreeCount(n.Symbol); 102 103 }); 103 104 return result; -
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/SymbolicExpressionTreeNode.cs
r4068 r4106 51 51 } 52 52 53 publicSymbolicExpressionTreeNode() {53 internal SymbolicExpressionTreeNode() { 54 54 // don't allocate subtrees list here! 55 55 // because we don't want to allocate it in terminal nodes … … 85 85 } 86 86 87 internalvirtual ISymbolicExpressionGrammar Grammar {87 public virtual ISymbolicExpressionGrammar Grammar { 88 88 get { return parent.Grammar; } 89 89 set { throw new NotSupportedException("Grammar can be set only for SymbolicExpressionTreeTopLevelNodes."); } … … 94 94 else { 95 95 size = 1; 96 for (int i = 0; i < SubTrees.Count; i++) size += (short)SubTrees[i].GetSize(); 96 if (SubTrees != null) { 97 for (int i = 0; i < SubTrees.Count; i++) size += (short)SubTrees[i].GetSize(); 98 } 97 99 return size; 98 100 } … … 102 104 if (height > 0) return height; 103 105 else { 104 for (int i = 0; i < SubTrees.Count; i++) height = Math.Max(height, (short)SubTrees[i].GetHeight()); 106 if (SubTrees != null) { 107 for (int i = 0; i < SubTrees.Count; i++) height = Math.Max(height, (short)SubTrees[i].GetHeight()); 108 } 105 109 height++; 106 110 return height; … … 112 116 113 117 public virtual void AddSubTree(SymbolicExpressionTreeNode tree) { 114 subTrees.Add(tree);118 SubTrees.Add(tree); 115 119 tree.Parent = this; 116 120 ResetCachedValues(); … … 118 122 119 123 public virtual void InsertSubTree(int index, SymbolicExpressionTreeNode tree) { 120 subTrees.Insert(index, tree);124 SubTrees.Insert(index, tree); 121 125 tree.Parent = this; 122 126 ResetCachedValues(); … … 124 128 125 129 public virtual void RemoveSubTree(int index) { 126 subTrees[index].Parent = null;127 subTrees.RemoveAt(index);130 SubTrees[index].Parent = null; 131 SubTrees.RemoveAt(index); 128 132 ResetCachedValues(); 129 133 } … … 137 141 public void ForEachNodePrefix(Action<SymbolicExpressionTreeNode> a) { 138 142 a(this); 139 for (int i = 0; i < SubTrees.Count; i++) { 140 SubTrees[i].ForEachNodePrefix(a); 143 if (SubTrees != null) { 144 for (int i = 0; i < SubTrees.Count; i++) { 145 SubTrees[i].ForEachNodePrefix(a); 146 } 141 147 } 142 148 } … … 149 155 150 156 public void ForEachNodePostfix(Action<SymbolicExpressionTreeNode> a) { 151 for (int i = 0; i < SubTrees.Count; i++) { 152 SubTrees[i].ForEachNodePrefix(a); 157 if (SubTrees != null) { 158 for (int i = 0; i < SubTrees.Count; i++) { 159 SubTrees[i].ForEachNodePostfix(a); 160 } 153 161 } 154 162 a(this); -
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/SymbolicExpressionTreeTopLevelNode.cs
r4068 r4106 37 37 [Storable] 38 38 private ISymbolicExpressionGrammar grammar; 39 internaloverride ISymbolicExpressionGrammar Grammar {39 public override ISymbolicExpressionGrammar Grammar { 40 40 get { return grammar; } 41 41 set { grammar = value; } -
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Symbols/InvokeFunction.cs
r4068 r4106 32 32 public sealed class InvokeFunction : ReadOnlySymbol { 33 33 public const string InvokeFunctionName = "InvokeFunction"; 34 public const string InvokeFunctionDescription = "Symbol that the invo kation of another function.";34 public const string InvokeFunctionDescription = "Symbol that the invocation of another function."; 35 35 public override bool CanChangeName { 36 36 get { -
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/Grammars.cs
r4068 r4106 26 26 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._3.Tests { 27 27 public static class Grammars { 28 private class Addition : Symbol { }29 private class Subtraction : Symbol { }30 private class Multiplication : Symbol { }31 private class Division : Symbol { }32 private class Terminal : Symbol { }28 private class Addition : Symbol { public Addition() : base("Addition", "") { } } 29 private class Subtraction : Symbol { public Subtraction() : base("Subtraction", "") { } } 30 private class Multiplication : Symbol { public Multiplication() : base("Multiplication", "") { } } 31 private class Division : Symbol { public Division() : base("Division", "") { } } 32 private class Terminal : Symbol { public Terminal() : base("Terminal", "") { } } 33 33 34 34 private class SimpleArithmeticGrammar : DefaultSymbolicExpressionGrammar { … … 89 89 return g; 90 90 } 91 92 public static void HasValidAdfGrammars(SymbolicExpressionTree tree) {93 //Assert.AreEqual(tree.Root.Grammar.Symbols.Count(), 8);94 //Assert.AreEqual(tree.Root.GetAllowedSymbols(0).Count(), 1); // only the start symbol is allowed95 //// we allow 3 ADF branches96 //Assert.AreEqual(tree.Root.GetAllowedSymbols(1).Count(), 1); // only the defun branch is allowed97 //Assert.AreEqual(tree.Root.GetAllowedSymbols(2).Count(), 1); // only the defun symbol is allowed98 //Assert.AreEqual(tree.Root.GetAllowedSymbols(3).Count(), 1); // only the defun symbol is allowed99 //foreach (var subtree in tree.Root.SubTrees) {100 // // check consistency of each sub-tree grammar independently101 // var allowedSymbols = subtree.GetAllowedSymbols(0);102 // int numberOfAllowedSymbols = allowedSymbols.Count();103 // foreach (var parent in allowedSymbols) {104 // for (int argIndex = 0; argIndex < subtree.Grammar.GetMaxSubtreeCount(parent); argIndex++) {105 // var allowedChildren = from child in subtree.Grammar.Symbols106 // where subtree.Grammar.IsAllowedChild(parent, child, argIndex)107 // select child;108 // Assert.AreEqual(numberOfAllowedSymbols, allowedChildren.Count());109 // }110 // }111 //}112 }113 114 91 } 115 92 } -
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/ProbabilisticTreeCreaterTest.cs
r4068 r4106 76 76 for (int i = 0; i < POPULATION_SIZE; i++) { 77 77 var tree = ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_SIZE, MAX_TREE_HEIGHT, 3, 3); 78 Grammars.HasValidAdfGrammars(tree);79 78 Util.IsValid(tree); 80 79 randomTrees.Add(tree); -
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/Util.cs
r4068 r4106 26 26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 27 27 using Microsoft.VisualStudio.TestTools.UnitTesting; 28 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols; 28 29 29 30 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._3.Tests { … … 114 115 115 116 public static void IsValid(SymbolicExpressionTree tree) { 116 Grammars.HasValidAdfGrammars(tree); 117 foreach (var defunTreeNode in tree.Root.SubTrees.OfType<DefunTreeNode>()) { 118 int arity = defunTreeNode.NumberOfArguments; 119 var invoke = new InvokeFunction(defunTreeNode.FunctionName); 120 foreach (var otherRootNode in tree.Root.SubTrees) { 121 if (otherRootNode.Grammar.ContainsSymbol(invoke)) { 122 Assert.IsTrue(otherRootNode.Grammar.GetMinSubtreeCount(invoke) == arity); 123 Assert.IsTrue(otherRootNode.Grammar.GetMaxSubtreeCount(invoke) == arity); 124 } 125 } 126 } 117 127 //Assert.AreEqual(tree.Root.Symbol, tree.Root.Grammar.StartSymbol); 118 128 //foreach (var subtree in tree.Root.SubTrees) … … 122 132 123 133 public static void IsValid(SymbolicExpressionTreeNode treeNode) { 124 //var matchingSymbol = (from symb in treeNode.Grammar.Symbols125 //where symb.Name == treeNode.Symbol.Name126 //select symb).SingleOrDefault();127 //Assert.IsTrue(treeNode.SubTrees.Count >= treeNode.Grammar.GetMinSubtreeCount(matchingSymbol));128 //Assert.IsTrue(treeNode.SubTrees.Count <= treeNode.Grammar.GetMaxSubtreeCount(matchingSymbol));134 var matchingSymbol = (from symb in treeNode.Grammar.Symbols 135 where symb.Name == treeNode.Symbol.Name 136 select symb).SingleOrDefault(); 137 Assert.IsTrue(treeNode.SubTrees.Count >= treeNode.Grammar.GetMinSubtreeCount(matchingSymbol)); 138 Assert.IsTrue(treeNode.SubTrees.Count <= treeNode.Grammar.GetMaxSubtreeCount(matchingSymbol)); 129 139 for (int i = 0; i < treeNode.SubTrees.Count; i++) { 130 140 Assert.IsTrue(treeNode.GetAllowedSymbols(i).Select(x => x.Name).Contains(treeNode.SubTrees[i].Symbol.Name));
Note: See TracChangeset
for help on using the changeset viewer.