Changeset 5549
- Timestamp:
- 02/22/11 19:04:54 (14 years ago)
- Location:
- branches/DataAnalysis Refactoring
- Files:
-
- 31 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.4/SymbolicExpressionTreeChart.cs
r5513 r5549 124 124 graphics.Clear(backgroundColor); 125 125 if (tree != null) { 126 int height = this.Height / tree. Height;126 int height = this.Height / tree.Depth; 127 127 DrawFunctionTree(tree, graphics, 0, 0, this.Width, height); 128 128 } … … 284 284 xBoundaries[0] = x; 285 285 for (int i = 0; i < node.SubTrees.Count(); i++) { 286 xBoundaries[i + 1] = (int)(xBoundaries[i] + (width * (double)node.SubTrees.ElementAt(i).Get Size()) / (node.GetSize() - 1));286 xBoundaries[i + 1] = (int)(xBoundaries[i] + (width * (double)node.SubTrees.ElementAt(i).GetLength()) / (node.GetLength() - 1)); 287 287 DrawFunctionTree(node.SubTrees.ElementAt(i), graphics, xBoundaries[i], y + height, 288 288 xBoundaries[i + 1] - xBoundaries[i], height, connectFrom); … … 305 305 Image image = new Bitmap(Width, Height); 306 306 using (Graphics g = Graphics.FromImage(image)) { 307 int height = this.Height / tree. Height;307 int height = this.Height / tree.Depth; 308 308 DrawFunctionTree(tree, g, 0, 0, Width, height); 309 309 } … … 316 316 using (Metafile file = new Metafile(filename, g.GetHdc())) { 317 317 using (Graphics emfFile = Graphics.FromImage(file)) { 318 int height = this.Height / tree. Height;318 int height = this.Height / tree.Depth; 319 319 DrawFunctionTree(tree, emfFile, 0, 0, Width, height); 320 320 } -
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Analyzers/MinAverageMaxSymbolicExpressionTreeLengthAnalyzer.cs
r5510 r5549 108 108 private void AfterDeserialization() { 109 109 SymbolicExpressionTreeParameter.DepthChanged += new EventHandler(SymbolicExpressionTreeParameter_DepthChanged); 110 SymbolicExpressionTreeLengthParameter.DepthChanged += new EventHandler(SymbolicExpressionTree SizeParameter_DepthChanged);110 SymbolicExpressionTreeLengthParameter.DepthChanged += new EventHandler(SymbolicExpressionTreeLengthParameter_DepthChanged); 111 111 } 112 112 … … 119 119 } 120 120 121 private void SymbolicExpressionTree SizeParameter_DepthChanged(object sender, EventArgs e) {121 private void SymbolicExpressionTreeLengthParameter_DepthChanged(object sender, EventArgs e) { 122 122 OnDepthParameterChanged(); 123 123 } -
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Analyzers/SymbolicExpressionTreeLengthCalculator.cs
r5499 r5549 66 66 67 67 public override IOperation Apply() { 68 SymbolicExpressionTreeLength = new DoubleValue(SymbolicExpressionTree. Size);68 SymbolicExpressionTreeLength = new DoubleValue(SymbolicExpressionTree.Length); 69 69 return base.Apply(); 70 70 } -
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/ArchitectureManipulators/ArgumentCreater.cs
r5529 r5549 101 101 ArgumentTreeNode newArgumentNode = MakeArgumentNode(newArgumentIndex); 102 102 103 // this operation potentially creates very big trees so the access to the sizeproperty might throw overflow exception103 // this operation potentially creates very big trees so the access to the length property might throw overflow exception 104 104 try { 105 if (CreateNewArgumentForDefun(random, clonedTree, selectedDefunBranch, newArgumentNode) && clonedTree. Size <= maxTreeLength && clonedTree.Height<= maxTreeDepth) {105 if (CreateNewArgumentForDefun(random, clonedTree, selectedDefunBranch, newArgumentNode) && clonedTree.Length <= maxTreeLength && clonedTree.Depth <= maxTreeDepth) { 106 106 107 107 // size constraints are fulfilled -
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/ArchitectureManipulators/SubroutineCreater.cs
r5510 r5549 86 86 // allowed maximum number of ADF reached => abort 87 87 return false; 88 if (symbolicExpressionTree. Size+ 4 > maxTreeLength)89 // defining a new function causes an size increase by 4 nodes (max) if the max tree sizeis reached => abort88 if (symbolicExpressionTree.Length + 4 > maxTreeLength) 89 // defining a new function causes an length increase by 4 nodes (max) if the max tree length is reached => abort 90 90 return false; 91 91 string formatString = new StringBuilder().Append('0', (int)Math.Log10(maxFunctionDefinitions * 10 - 1)).ToString(); // >= 100 functions => ### … … 95 95 // select a random body (either the result producing branch or an ADF branch) 96 96 var bodies = from node in symbolicExpressionTree.Root.SubTrees 97 select new { Tree = node, Size = node.GetSize() };98 var totalNumberOfBodyNodes = bodies.Select(x => x. Size).Sum();97 select new { Tree = node, Length = node.GetLength() }; 98 var totalNumberOfBodyNodes = bodies.Select(x => x.Length).Sum(); 99 99 int r = random.Next(totalNumberOfBodyNodes); 100 100 int aggregatedNumberOfBodyNodes = 0; 101 101 ISymbolicExpressionTreeNode selectedBody = null; 102 102 foreach (var body in bodies) { 103 aggregatedNumberOfBodyNodes += body. Size;103 aggregatedNumberOfBodyNodes += body.Length; 104 104 if (aggregatedNumberOfBodyNodes > r) 105 105 selectedBody = body.Tree; -
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/ArchitectureManipulators/SubroutineDeleter.cs
r5529 r5549 95 95 var selectedSymbol = allowedSymbolsList.SelectRandom(weights, random); 96 96 97 int minPossible Size= invocationCutPoint.Parent.Grammar.GetMinExpressionLength(selectedSymbol);98 int max Size = Math.Max(minPossibleSize, invocationCutPoint.ReplacedChild.GetSize());99 int minPossible Height= invocationCutPoint.Parent.Grammar.GetMinExpressionDepth(selectedSymbol);100 int max Height = Math.Max(minPossibleHeight, invocationCutPoint.ReplacedChild.GetHeight());97 int minPossibleLength = invocationCutPoint.Parent.Grammar.GetMinExpressionLength(selectedSymbol); 98 int maxLength = Math.Max(minPossibleLength, invocationCutPoint.ReplacedChild.GetLength()); 99 int minPossibleDepth = invocationCutPoint.Parent.Grammar.GetMinExpressionDepth(selectedSymbol); 100 int maxDepth = Math.Max(minPossibleDepth, invocationCutPoint.ReplacedChild.GetDepth()); 101 101 replacementTree = selectedSymbol.CreateTreeNode(); 102 102 if (replacementTree.HasLocalParameters) … … 105 105 invocationCutPoint.Parent.InsertSubTree(invocationCutPoint.ReplacedChildIndex, replacementTree); 106 106 107 ProbabilisticTreeCreator.PTC2(random, replacementTree, max Size, maxHeight, 0, 0);107 ProbabilisticTreeCreator.PTC2(random, replacementTree, maxLength, maxDepth, 0, 0); 108 108 109 109 invocationCutPoint = (from node in symbolicExpressionTree.IterateNodesPrefix() -
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Creators/ProbabilisticTreeCreator.cs
r5529 r5549 32 32 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding { 33 33 [StorableClass] 34 [Item("ProbabilisticTreeCreator", "An operator that creates new symbolic expression trees with uniformly distributed size")]34 [Item("ProbabilisticTreeCreator", "An operator that creates new symbolic expression trees with uniformly distributed length")] 35 35 public sealed class ProbabilisticTreeCreator : SymbolicExpressionTreeCreator, 36 36 ISymbolicExpressionTreeSizeConstraintOperator, ISymbolicExpressionTreeGrammarBasedOperator, ISymbolicExpressionTreeArchitectureAlteringOperator { … … 98 98 99 99 public static ISymbolicExpressionTree Create(IRandom random, ISymbolicExpressionTreeGrammar grammar, 100 int maxTree Size, int maxTreeHeight,100 int maxTreeLength, int maxTreeDepth, 101 101 int maxFunctionDefinitions, int maxFunctionArguments 102 102 ) { … … 105 105 if (rootNode.HasLocalParameters) rootNode.ResetLocalParameters(random); 106 106 rootNode.SetGrammar(new SymbolicExpressionTreeGrammar(grammar)); 107 tree.Root = PTC2(random, rootNode, maxTree Size, maxTreeHeight, maxFunctionDefinitions, maxFunctionArguments);107 tree.Root = PTC2(random, rootNode, maxTreeLength, maxTreeDepth, maxFunctionDefinitions, maxFunctionArguments); 108 108 return tree; 109 109 } … … 116 116 117 117 public static ISymbolicExpressionTreeNode PTC2(IRandom random, ISymbolicExpressionTreeNode seedNode, 118 int max TreeSize, int maxDepth, int maxFunctionDefinitions, int maxFunctionArguments) {119 // tree sizeis limited by the grammar and by the explicit size constraints120 int allowedMin Size= seedNode.Grammar.GetMinExpressionLength(seedNode.Symbol);121 int allowedMax Size = Math.Min(maxTreeSize, seedNode.Grammar.GetMaxExpressionLength(seedNode.Symbol));118 int maxLength, int maxDepth, int maxFunctionDefinitions, int maxFunctionArguments) { 119 // tree length is limited by the grammar and by the explicit size constraints 120 int allowedMinLength = seedNode.Grammar.GetMinExpressionLength(seedNode.Symbol); 121 int allowedMaxLength = Math.Min(maxLength, seedNode.Grammar.GetMaxExpressionLength(seedNode.Symbol)); 122 122 int tries = 0; 123 123 while (tries++ < MAX_TRIES) { 124 // select a target tree sizeuniformly in the possible range (as determined by explicit limits and limits of the grammar)125 int t reeSize;126 t reeSize = random.Next(allowedMinSize, allowedMaxSize+ 1);127 if (t reeSize<= 1 || maxDepth <= 1) return seedNode;128 129 bool success = CreateFullTreeFromSeed(random, seedNode, seedNode.Grammar, t reeSize, maxDepth, maxFunctionDefinitions, maxFunctionArguments);124 // select a target tree length uniformly in the possible range (as determined by explicit limits and limits of the grammar) 125 int targetTreeLength; 126 targetTreeLength = random.Next(allowedMinLength, allowedMaxLength + 1); 127 if (targetTreeLength <= 1 || maxDepth <= 1) return seedNode; 128 129 bool success = CreateFullTreeFromSeed(random, seedNode, seedNode.Grammar, targetTreeLength, maxDepth, maxFunctionDefinitions, maxFunctionArguments); 130 130 131 131 // if successful => check constraints and return the tree if everything looks ok 132 if (success && seedNode.Get Size() <= maxTreeSize && seedNode.GetHeight() <= maxDepth) {132 if (success && seedNode.GetLength() <= maxLength && seedNode.GetDepth() <= maxDepth) { 133 133 return seedNode; 134 134 } else { … … 136 136 while (seedNode.SubTrees.Count() > 0) seedNode.RemoveSubTree(0); 137 137 } 138 // try a different sizeMAX_TRIES times138 // try a different length MAX_TRIES times 139 139 } 140 140 throw new ArgumentException("Couldn't create a random valid tree."); … … 142 142 143 143 private static bool CreateFullTreeFromSeed(IRandom random, ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeGrammar globalGrammar, 144 int size, int maxDepth, int maxFunctionDefinitions, int maxFunctionArguments) {144 int targetLength, int maxDepth, int maxFunctionDefinitions, int maxFunctionArguments) { 145 145 try { 146 TryCreateFullTreeFromSeed(random, root, globalGrammar, size, maxDepth, maxFunctionDefinitions, maxFunctionArguments);146 TryCreateFullTreeFromSeed(random, root, globalGrammar, targetLength, maxDepth, maxFunctionDefinitions, maxFunctionArguments); 147 147 return true; 148 148 } … … 151 151 152 152 private static void TryCreateFullTreeFromSeed(IRandom random, ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeGrammar globalGrammar, 153 int size, int maxDepth, int maxFunctionDefinitions, int maxFunctionArguments) {153 int targetLength, int maxDepth, int maxFunctionDefinitions, int maxFunctionArguments) { 154 154 List<TreeExtensionPoint> extensionPoints = new List<TreeExtensionPoint>(); 155 int current Size= 1;156 int totalListMin Size= globalGrammar.GetMinExpressionLength(root.Symbol) - 1;157 int actualArity = SampleArity(random, root, size);155 int currentLength = 1; 156 int totalListMinLength = globalGrammar.GetMinExpressionLength(root.Symbol) - 1; 157 int actualArity = SampleArity(random, root, targetLength); 158 158 for (int i = 0; i < actualArity; i++) { 159 159 // insert a dummy sub-tree and add the pending extension to the list … … 163 163 } 164 164 // while there are pending extension points and we have not reached the limit of adding new extension points 165 while (extensionPoints.Count > 0 && totalListMin Size + currentSize < size) {165 while (extensionPoints.Count > 0 && totalListMinLength + currentLength < targetLength) { 166 166 int randomIndex = random.Next(extensionPoints.Count); 167 167 TreeExtensionPoint nextExtension = extensionPoints[randomIndex]; … … 176 176 where parent.Grammar.IsAllowedChild(parent.Symbol, s, argumentIndex) 177 177 where parent.Grammar.GetMinExpressionDepth(s) + extensionDepth - 1 < maxDepth 178 where parent.Grammar.GetMaxExpressionLength(s) > size - totalListMinSize - currentSize178 where parent.Grammar.GetMaxExpressionLength(s) > targetLength - totalListMinLength - currentLength 179 179 select s) 180 180 .ToList(); … … 188 188 InitializeNewTreeNode(random, root, newTree, maxFunctionDefinitions, maxFunctionArguments); 189 189 190 current Size++;191 totalListMin Size--;192 193 actualArity = SampleArity(random, newTree, size - currentSize);190 currentLength++; 191 totalListMinLength--; 192 193 actualArity = SampleArity(random, newTree, targetLength - currentLength); 194 194 for (int i = 0; i < actualArity; i++) { 195 195 // insert a dummy sub-tree and add the pending extension to the list … … 198 198 extensionPoints.Add(new TreeExtensionPoint { Parent = newTree, ChildIndex = i, ExtensionPointDepth = extensionDepth + 1 }); 199 199 } 200 totalListMin Size+= newTree.Grammar.GetMinExpressionLength(newTree.Symbol);200 totalListMinLength += newTree.Grammar.GetMinExpressionLength(newTree.Symbol); 201 201 } 202 202 } … … 283 283 } 284 284 285 private static int SampleArity(IRandom random, ISymbolicExpressionTreeNode node, int target Size) {285 private static int SampleArity(IRandom random, ISymbolicExpressionTreeNode node, int targetLength) { 286 286 // select actualArity randomly with the constraint that the sub-trees in the minimal arity can become large enough 287 287 int minArity = node.Grammar.GetMinSubtreeCount(node.Symbol); 288 288 int maxArity = node.Grammar.GetMaxSubtreeCount(node.Symbol); 289 if (maxArity > target Size) {290 maxArity = target Size;291 } 292 // the min number of sub-trees has to be set to a value that is large enough so that the largest possible tree is at least tree size293 // if 1..3 trees are possible and the largest possible first sub-tree is smaller larger than the target sizethen minArity should be at least 2289 if (maxArity > targetLength) { 290 maxArity = targetLength; 291 } 292 // the min number of sub-trees has to be set to a value that is large enough so that the largest possible tree is at least tree length 293 // if 1..3 trees are possible and the largest possible first sub-tree is smaller larger than the target length then minArity should be at least 2 294 294 long aggregatedLongestExpressionLength = 0; 295 295 for (int i = 0; i < maxArity; i++) { 296 296 aggregatedLongestExpressionLength += (from s in node.Grammar.GetAllowedSymbols(node.Symbol, i) 297 297 select node.Grammar.GetMaxExpressionLength(s)).Max(); 298 if (aggregatedLongestExpressionLength < target Size) minArity = i;298 if (aggregatedLongestExpressionLength < targetLength) minArity = i; 299 299 else break; 300 300 } 301 301 302 // the max number of sub-trees has to be set to a value that is small enough so that the smallest possible tree is at most tree size303 // if 1..3 trees are possible and the smallest possible first sub-tree is already larger than the target sizethen maxArity should be at most 0302 // the max number of sub-trees has to be set to a value that is small enough so that the smallest possible tree is at most tree length 303 // if 1..3 trees are possible and the smallest possible first sub-tree is already larger than the target length then maxArity should be at most 0 304 304 long aggregatedShortestExpressionLength = 0; 305 305 for (int i = 0; i < maxArity; i++) { 306 306 aggregatedShortestExpressionLength += (from s in node.Grammar.GetAllowedSymbols(node.Symbol, i) 307 307 select node.Grammar.GetMinExpressionLength(s)).Min(); 308 if (aggregatedShortestExpressionLength > target Size) {308 if (aggregatedShortestExpressionLength > targetLength) { 309 309 maxArity = i; 310 310 break; -
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Crossovers/SubtreeCrossover.cs
r5510 r5549 86 86 public static ISymbolicExpressionTree Cross(IRandom random, 87 87 ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1, 88 double internalCrossoverPointProbability, int maxTree Size, int maxTreeHeight) {88 double internalCrossoverPointProbability, int maxTreeLength, int maxTreeDepth) { 89 89 // select a random crossover point in the first parent 90 90 ISymbolicExpressionTreeNode crossoverPoint0; 91 91 int replacedSubtreeIndex; 92 SelectCrossoverPoint(random, parent0, internalCrossoverPointProbability, maxTree Size, maxTreeHeight, out crossoverPoint0, out replacedSubtreeIndex);93 94 // calculate the max size and heightthat the inserted branch can have95 int maxInsertedBranch Size = maxTreeSize - (parent0.Size - crossoverPoint0.GetSubTree(replacedSubtreeIndex).GetSize());96 int maxInsertedBranch Height = maxTreeHeight- GetBranchLevel(parent0.Root, crossoverPoint0);92 SelectCrossoverPoint(random, parent0, internalCrossoverPointProbability, maxTreeLength, maxTreeDepth, out crossoverPoint0, out replacedSubtreeIndex); 93 94 // calculate the max length and depth that the inserted branch can have 95 int maxInsertedBranchLength = maxTreeLength - (parent0.Length - crossoverPoint0.GetSubTree(replacedSubtreeIndex).GetLength()); 96 int maxInsertedBranchDepth = maxTreeDepth - GetBranchLevel(parent0.Root, crossoverPoint0); 97 97 98 98 List<ISymbolicExpressionTreeNode> allowedBranches = new List<ISymbolicExpressionTreeNode>(); 99 99 parent1.Root.ForEachNodePostfix((n) => { 100 if (n.Get Size() <= maxInsertedBranchSize&&101 n.Get Height() <= maxInsertedBranchHeight&&100 if (n.GetLength() <= maxInsertedBranchLength && 101 n.GetDepth() <= maxInsertedBranchDepth && 102 102 IsMatchingPointType(crossoverPoint0, replacedSubtreeIndex, n)) 103 103 allowedBranches.Add(n); … … 134 134 } 135 135 136 private static void SelectCrossoverPoint(IRandom random, ISymbolicExpressionTree parent0, double internalNodeProbability, int maxBranch Size, int maxBranchHeight, out ISymbolicExpressionTreeNode crossoverPoint, out int subtreeIndex) {136 private static void SelectCrossoverPoint(IRandom random, ISymbolicExpressionTree parent0, double internalNodeProbability, int maxBranchLength, int maxBranchDepth, out ISymbolicExpressionTreeNode crossoverPoint, out int subtreeIndex) { 137 137 if (internalNodeProbability < 0.0 || internalNodeProbability > 1.0) throw new ArgumentException("internalNodeProbability"); 138 138 List<CrossoverPoint> internalCrossoverPoints = new List<CrossoverPoint>(); … … 141 141 if (n.SubTrees.Count() > 0 && n != parent0.Root) { 142 142 foreach (var child in n.SubTrees) { 143 if (child.Get Size() <= maxBranchSize&&144 child.Get Height() <= maxBranchHeight) {143 if (child.GetLength() <= maxBranchLength && 144 child.GetDepth() <= maxBranchDepth) { 145 145 if (child.SubTrees.Count() > 0) 146 146 internalCrossoverPoints.Add(new CrossoverPoint(n, child)); -
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Interfaces/ISymbolicExpressionTree.cs
r5510 r5549 26 26 public interface ISymbolicExpressionTree : IItem { 27 27 ISymbolicExpressionTreeNode Root { get; set; } 28 int Size{ get; }29 int Height{ get; }28 int Length { get; } 29 int Depth { get; } 30 30 31 31 IEnumerable<ISymbolicExpressionTreeNode> IterateNodesPrefix(); -
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Interfaces/ISymbolicExpressionTreeNode.cs
r5529 r5549 30 30 bool HasLocalParameters { get; } 31 31 32 int Get Height();33 int Get Size();32 int GetDepth(); 33 int GetLength(); 34 34 35 35 IEnumerable<ISymbolicExpressionTreeNode> IterateNodesPostfix(); -
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Manipulators/ReplaceBranchManipulation.cs
r5529 r5549 76 76 .SelectRandom(random); 77 77 78 int maxLength = maxTreeLength - symbolicExpressionTree. Size + manipulationPoint.Node.GetSize();79 int maxDepth = maxTreeDepth - symbolicExpressionTree. Height + manipulationPoint.Node.GetHeight();78 int maxLength = maxTreeLength - symbolicExpressionTree.Length + manipulationPoint.Node.GetLength(); 79 int maxDepth = maxTreeDepth - symbolicExpressionTree.Depth + manipulationPoint.Node.GetDepth(); 80 80 // find possible symbols for the node (also considering the existing branches below it) 81 81 var allowedSymbols = (from symbol in manipulationPoint.Parent.Grammar.GetAllowedSymbols(manipulationPoint.Parent.Symbol, manipulationPoint.Index) -
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTree.cs
r5532 r5549 47 47 } 48 48 49 public int Size{49 public int Length { 50 50 get { 51 51 if (root == null) 52 52 return 0; 53 return root.Get Size();53 return root.GetLength(); 54 54 } 55 55 } 56 56 57 public int Height{57 public int Depth { 58 58 get { 59 59 if (root == null) 60 60 return 0; 61 return root.Get Height();61 return root.GetDepth(); 62 62 } 63 63 } -
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTreeNode.cs
r5529 r5549 36 36 37 37 // cached values to prevent unnecessary tree iterations 38 private ushort size;39 private ushort height;38 private ushort length; 39 private ushort depth; 40 40 41 41 public ISymbol Symbol { … … 99 99 } 100 100 101 public int Get Size() {102 if ( size > 0) return size;101 public int GetLength() { 102 if (length > 0) return length; 103 103 else { 104 size= 1;104 length = 1; 105 105 if (subTrees != null) { 106 106 for (int i = 0; i < subTrees.Count; i++) { 107 checked { size += (ushort)subTrees[i].GetSize(); }107 checked { length += (ushort)subTrees[i].GetLength(); } 108 108 } 109 109 } 110 return size;110 return length; 111 111 } 112 112 } 113 113 114 public int Get Height() {115 if ( height > 0) return height;114 public int GetDepth() { 115 if (depth > 0) return depth; 116 116 else { 117 117 if (subTrees != null) { 118 for (int i = 0; i < subTrees.Count; i++) height = Math.Max(height, (ushort)subTrees[i].GetHeight());118 for (int i = 0; i < subTrees.Count; i++) depth = Math.Max(depth, (ushort)subTrees[i].GetDepth()); 119 119 } 120 height++;121 return height;120 depth++; 121 return depth; 122 122 } 123 123 } … … 183 183 184 184 private void ResetCachedValues() { 185 size = 0; height= 0;185 length = 0; depth = 0; 186 186 SymbolicExpressionTreeNode parentNode = parent as SymbolicExpressionTreeNode; 187 187 if (parentNode != null) parentNode.ResetCachedValues(); -
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Tests/AllArchitectureAlteringOperatorsTest.cs
r5445 r5549 25 25 using HeuristicLab.Data; 26 26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 27 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ArchitectureManipulators;28 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Creators;29 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Crossovers;30 27 using HeuristicLab.Random; 31 28 using Microsoft.VisualStudio.TestTools.UnitTesting; 32 29 33 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._ 3.Tests {30 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._4.Tests { 34 31 [TestClass] 35 32 public class AllArchitectureAlteringOperatorsTest { 36 33 private const int POPULATION_SIZE = 1000; 37 34 private const int N_ITERATIONS = 20; 38 private const int MAX_TREE_ SIZE= 100;39 private const int MAX_TREE_ HEIGHT= 10;35 private const int MAX_TREE_LENGTH = 100; 36 private const int MAX_TREE_DEPTH = 10; 40 37 private TestContext testContextInstance; 41 38 … … 55 52 [TestMethod()] 56 53 public void AllArchitectureAlteringOperatorsDistributionTest() { 57 var trees = new List< SymbolicExpressionTree>();58 var newTrees = new List< SymbolicExpressionTree>();54 var trees = new List<ISymbolicExpressionTree>(); 55 var newTrees = new List<ISymbolicExpressionTree>(); 59 56 var grammar = Grammars.CreateArithmeticAndAdfGrammar(); 60 57 var random = new MersenneTwister(31415); 61 IntValue maxTreeSize = new IntValue(MAX_TREE_ SIZE);62 IntValue maxTreeHeigth = new IntValue(MAX_TREE_ HEIGHT);58 IntValue maxTreeSize = new IntValue(MAX_TREE_LENGTH); 59 IntValue maxTreeHeigth = new IntValue(MAX_TREE_DEPTH); 63 60 IntValue maxDefuns = new IntValue(3); 64 61 IntValue maxArgs = new IntValue(3); 65 62 for (int i = 0; i < POPULATION_SIZE; i++) { 66 var tree = ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_ SIZE, MAX_TREE_HEIGHT, 3, 3);63 var tree = ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_LENGTH, MAX_TREE_DEPTH, 3, 3); 67 64 Util.IsValid(tree); 68 65 trees.Add(tree); … … 70 67 Stopwatch stopwatch = new Stopwatch(); 71 68 stopwatch.Start(); 72 var combinedAAOperator = new MultiSymbolicExpressionTreeArchitectureManipulator();73 69 int failedEvents = 0; 74 70 for (int g = 0; g < N_ITERATIONS; g++) { … … 76 72 if (random.NextDouble() < 0.5) { 77 73 // manipulate 78 var selectedTree = (SymbolicExpressionTree)trees.SelectRandom(random).Clone(); 79 var op = combinedAAOperator.Operators.SelectRandom(random); 74 var selectedTree = (ISymbolicExpressionTree)trees.SelectRandom(random).Clone(); 80 75 bool success = false; 81 op.ModifyArchitecture(random, selectedTree, grammar, maxTreeSize, maxTreeHeigth, maxDefuns, maxArgs, out success); 82 if (!success) failedEvents++; // architecture manipulation might fail 76 switch (random.Next(6)) { 77 case 0: success = ArgumentCreater.CreateNewArgument(random, selectedTree, MAX_TREE_LENGTH, MAX_TREE_DEPTH, 3, 3); break; 78 case 1: success = ArgumentDeleter.DeleteArgument(random, selectedTree, 3, 3); break; 79 case 2: success = ArgumentDuplicater.DuplicateArgument(random, selectedTree, 3, 3); break; 80 case 3: success = SubroutineCreater.CreateSubroutine(random, selectedTree, MAX_TREE_LENGTH, MAX_TREE_DEPTH, 3, 3); break; 81 case 4: success = SubroutineDuplicater.DuplicateSubroutine(random, selectedTree, 3, 3); break; 82 case 5: success = SubroutineDeleter.DeleteSubroutine(random, selectedTree, 3, 3); break; 83 } 84 if (!success) failedEvents++; 83 85 Util.IsValid(selectedTree); 84 86 newTrees.Add(selectedTree); … … 90 92 par0 = (SymbolicExpressionTree)trees.SelectRandom(random).Clone(); 91 93 par1 = (SymbolicExpressionTree)trees.SelectRandom(random).Clone(); 92 } while (par0.Size > MAX_TREE_SIZE || par1.Size > MAX_TREE_SIZE); 93 bool success; 94 newTrees.Add(SubtreeCrossover.Cross(random, par0, par1, 0.9, MAX_TREE_SIZE, MAX_TREE_HEIGHT, out success)); 95 Assert.IsTrue(success); // crossover must succeed 94 } while (par0.Length > MAX_TREE_LENGTH || par1.Length > MAX_TREE_LENGTH); 95 newTrees.Add(SubtreeCrossover.Cross(random, par0, par1, 0.9, MAX_TREE_LENGTH, MAX_TREE_DEPTH)); 96 96 } 97 97 } -
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Tests/ArgumentCreaterTest.cs
r5445 r5549 24 24 using System.Collections.Generic; 25 25 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ArchitectureManipulators;27 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Creators;28 26 using HeuristicLab.Random; 29 27 using Microsoft.VisualStudio.TestTools.UnitTesting; 30 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;31 28 32 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._ 3.Tests {29 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._4.Tests { 33 30 [TestClass] 34 31 public class ArgumentCreaterTest { 35 32 private const int POPULATION_SIZE = 1000; 36 private const int MAX_TREE_ SIZE= 100;37 private const int MAX_TREE_ HEIGHT= 10;33 private const int MAX_TREE_LENGTH = 100; 34 private const int MAX_TREE_DEPTH = 10; 38 35 private TestContext testContextInstance; 39 36 … … 53 50 [TestMethod()] 54 51 public void ArgumentCreaterDistributionsTest() { 55 var trees = new List< SymbolicExpressionTree>();52 var trees = new List<ISymbolicExpressionTree>(); 56 53 var grammar = Grammars.CreateArithmeticAndAdfGrammar(); 57 54 var random = new MersenneTwister(31415); 58 55 int failedOps = 0; 59 56 for (int i = 0; i < POPULATION_SIZE; i++) { 60 SymbolicExpressionTree tree;57 ISymbolicExpressionTree tree; 61 58 do { 62 tree = ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_ SIZE, MAX_TREE_HEIGHT, 3, 3);59 tree = ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_LENGTH, MAX_TREE_DEPTH, 3, 3); 63 60 } while (!TreeHasAdfWithParameter(tree, 3)); 64 var success = ArgumentCreater.CreateNewArgument(random, tree, grammar,60000, 100, 3, 3);61 var success = ArgumentCreater.CreateNewArgument(random, tree, 60000, 100, 3, 3); 65 62 if (!success) failedOps++; 66 63 Util.IsValid(tree); … … 79 76 } 80 77 81 private bool TreeHasAdfWithParameter(SymbolicExpressionTree tree, int maxParameters) { 82 return tree.Root.SubTrees.Count == 2 && 83 tree.Root.SubTrees[1].GetAllowedSymbols(0).Where(x => x is Argument).Count() < maxParameters; 78 private bool TreeHasAdfWithParameter(ISymbolicExpressionTree tree, int maxParameters) { 79 if(tree.Root.SubTrees.Count() != 2 ) return false; 80 var firstAdf = tree.Root.GetSubTree(1); 81 return firstAdf.Grammar.GetAllowedSymbols(firstAdf.Symbol, 0).Where(x => x is Argument).Count() < maxParameters; 84 82 } 85 83 } -
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Tests/ArgumentDeleterTest.cs
r5445 r5549 24 24 using System.Collections.Generic; 25 25 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ArchitectureManipulators;27 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Creators;28 26 using HeuristicLab.Random; 29 27 using Microsoft.VisualStudio.TestTools.UnitTesting; 30 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;31 28 32 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._ 3.Tests {29 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._4.Tests { 33 30 [TestClass] 34 31 public class ArgumentDeleterTest { 35 32 private const int POPULATION_SIZE = 1000; 36 private const int MAX_TREE_ SIZE= 100;37 private const int MAX_TREE_ HEIGHT= 10;33 private const int MAX_TREE_LENGTH = 100; 34 private const int MAX_TREE_DEPTH = 10; 38 35 private TestContext testContextInstance; 39 36 … … 53 50 [TestMethod()] 54 51 public void ArgumentDeleterDistributionsTest() { 55 var trees = new List< SymbolicExpressionTree>();52 var trees = new List<ISymbolicExpressionTree>(); 56 53 var grammar = Grammars.CreateArithmeticAndAdfGrammar(); 57 54 var random = new MersenneTwister(31415); 58 55 for (int i = 0; i < POPULATION_SIZE; i++) { 59 SymbolicExpressionTree tree = null;56 ISymbolicExpressionTree tree = null; 60 57 do { 61 tree = ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_ SIZE, MAX_TREE_HEIGHT, 3, 3);58 tree = ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_LENGTH, MAX_TREE_DEPTH, 3, 3); 62 59 } while (!TreeHasAdfWithArguments(tree)); 63 var success = ArgumentDeleter.DeleteArgument(random, tree, grammar, MAX_TREE_SIZE, MAX_TREE_HEIGHT,3, 3);60 var success = ArgumentDeleter.DeleteArgument(random, tree, 3, 3); 64 61 Assert.IsTrue(success); 65 62 Util.IsValid(tree); … … 73 70 ); 74 71 } 75 private bool TreeHasAdfWithArguments(SymbolicExpressionTree tree) { 76 return tree.Root.SubTrees.Count == 2 && 77 tree.Root.SubTrees[1].GetAllowedSymbols(0).Where(x => x is Argument).Count() >= 2; 72 private bool TreeHasAdfWithArguments(ISymbolicExpressionTree tree) { 73 if (tree.Root.SubTrees.Count() != 2) return false; 74 var firstAdf = tree.Root.GetSubTree(1); 75 return firstAdf.Grammar.GetAllowedSymbols(firstAdf.Symbol, 0).Where(x => x is Argument).Count() >= 2; 78 76 } 79 77 } -
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Tests/ArgumentDuplicaterTest.cs
r5445 r5549 24 24 using System.Collections.Generic; 25 25 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ArchitectureManipulators;27 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Creators;28 26 using HeuristicLab.Random; 29 27 using Microsoft.VisualStudio.TestTools.UnitTesting; 30 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;31 28 32 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._ 3.Tests {29 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._4.Tests { 33 30 [TestClass] 34 31 public class ArgumentDuplicaterTest { 35 32 private const int POPULATION_SIZE = 1000; 36 private const int MAX_TREE_ SIZE= 100;37 private const int MAX_TREE_ HEIGHT= 10;33 private const int MAX_TREE_LENGTH = 100; 34 private const int MAX_TREE_DEPTH = 10; 38 35 private TestContext testContextInstance; 39 36 … … 53 50 [TestMethod()] 54 51 public void ArgumentDuplicaterDistributionsTest() { 55 var trees = new List< SymbolicExpressionTree>();52 var trees = new List<ISymbolicExpressionTree>(); 56 53 var grammar = Grammars.CreateArithmeticAndAdfGrammar(); 57 54 var random = new MersenneTwister(31415); 58 55 for (int i = 0; i < POPULATION_SIZE; i++) { 59 SymbolicExpressionTree tree = null;56 ISymbolicExpressionTree tree = null; 60 57 do { 61 tree = ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_ SIZE, MAX_TREE_HEIGHT, 3, 3);58 tree = ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_LENGTH, MAX_TREE_DEPTH, 3, 3); 62 59 } while(!HasAdfWithArguments(tree)); 63 var success = ArgumentDuplicater.DuplicateArgument(random, tree, grammar, MAX_TREE_SIZE, MAX_TREE_HEIGHT,3, 3);60 var success = ArgumentDuplicater.DuplicateArgument(random, tree, 3, 3); 64 61 Assert.IsTrue(success); 65 62 Util.IsValid(tree); … … 74 71 } 75 72 76 private bool HasAdfWithArguments(SymbolicExpressionTree tree) { 77 return tree.Root.SubTrees.Count == 2 && 78 tree.Root.SubTrees[1].GetAllowedSymbols(0).Where(x => x is Argument).Count() == 1; 73 private bool HasAdfWithArguments(ISymbolicExpressionTree tree) { 74 if (tree.Root.SubTrees.Count() != 2) return false; 75 var firstAdf = tree.Root.GetSubTree(1); 76 return firstAdf.Grammar.GetAllowedSymbols(firstAdf.Symbol, 0).Where(x => x is Argument).Count() == 1; 79 77 } 80 78 } -
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Tests/ChangeNodeTypeManipulationTest.cs
r5445 r5549 23 23 using System.Collections.Generic; 24 24 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 25 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Creators;26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Manipulators;27 25 using HeuristicLab.Random; 28 26 using Microsoft.VisualStudio.TestTools.UnitTesting; 29 27 30 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._ 3.Tests {28 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._4.Tests { 31 29 [TestClass] 32 30 public class ChangeNodeTypeManipulationTest { 33 31 private const int POPULATION_SIZE = 1000; 34 private const int MAX_TREE_ SIZE= 100;35 private const int MAX_TREE_ HEIGHT= 10;32 private const int MAX_TREE_LENGTH = 100; 33 private const int MAX_TREE_DEPTH = 10; 36 34 private TestContext testContextInstance; 37 35 … … 51 49 [TestMethod()] 52 50 public void ChangeNodeTypeManipulationDistributionsTest() { 53 var trees = new List<SymbolicExpressionTree>(); 51 SymbolicExpressionTreeStringFormatter formatter = new SymbolicExpressionTreeStringFormatter(); 52 var trees = new List<ISymbolicExpressionTree>(); 54 53 var grammar = Grammars.CreateArithmeticAndAdfGrammar(); 55 54 var random = new MersenneTwister(31415); 56 55 for (int i = 0; i < POPULATION_SIZE; i++) { 57 var tree = ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_SIZE, MAX_TREE_HEIGHT, 3, 3); 58 bool success; 59 ChangeNodeTypeManipulation.ChangeNodeType(random, tree, grammar, MAX_TREE_SIZE, MAX_TREE_HEIGHT, out success); 60 Assert.IsTrue(success); 56 var tree = ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_LENGTH, MAX_TREE_DEPTH, 3, 3); 57 string originalTree = formatter.Format(tree); 58 ChangeNodeTypeManipulation.ChangeNodeType(random, tree); 59 string manipulatedTree = formatter.Format(tree); 60 Assert.IsFalse(originalTree == manipulatedTree); 61 61 Util.IsValid(tree); 62 62 trees.Add(tree); -
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Tests/Grammars.cs
r5445 r5549 23 23 using HeuristicLab.Common; 24 24 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 25 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;26 25 27 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._ 3.Tests {26 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._4.Tests { 28 27 public static class Grammars { 29 28 private class Addition : Symbol { … … 109 108 } 110 109 111 public static ISymbolicExpression Grammar CreateSimpleArithmeticGrammar() {110 public static ISymbolicExpressionTreeGrammar CreateSimpleArithmeticGrammar() { 112 111 var g = new GlobalSymbolicExpressionGrammar(new SimpleArithmeticGrammar()); 113 112 g.MaxFunctionArguments = 0; … … 118 117 } 119 118 120 public static ISymbolicExpression Grammar CreateArithmeticAndAdfGrammar() {119 public static ISymbolicExpressionTreeGrammar CreateArithmeticAndAdfGrammar() { 121 120 var g = new GlobalSymbolicExpressionGrammar(new SimpleArithmeticGrammar()); 122 121 g.MaxFunctionArguments = 3; -
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Tests/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4.Tests.csproj
r5477 r5549 9 9 <OutputType>Library</OutputType> 10 10 <AppDesignerFolder>Properties</AppDesignerFolder> 11 <RootNamespace>HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._ 3.Tests</RootNamespace>11 <RootNamespace>HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._4.Tests</RootNamespace> 12 12 <AssemblyName>HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4.Tests</AssemblyName> 13 13 <TargetFrameworkVersion>v4.0</TargetFrameworkVersion> … … 153 153 <Name>HeuristicLab.Random-3.3</Name> 154 154 </ProjectReference> 155 <ProjectReference Include="..\ ..\3.3\HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.3.csproj">156 <Project>{ 125D3006-67F5-48CB-913E-73C0548F17FA}</Project>157 <Name>HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3. 3</Name>155 <ProjectReference Include="..\HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4.csproj"> 156 <Project>{06D4A186-9319-48A0-BADE-A2058D462EEA}</Project> 157 <Name>HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4</Name> 158 158 </ProjectReference> 159 159 </ItemGroup> -
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Tests/ProbabilisticTreeCreaterTest.cs
r5445 r5549 23 23 using System.Collections.Generic; 24 24 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 25 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Creators;26 25 using HeuristicLab.Random; 27 26 using Microsoft.VisualStudio.TestTools.UnitTesting; 27 using System.Diagnostics; 28 28 29 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._ 3.Tests {29 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._4.Tests { 30 30 [TestClass] 31 31 public class ProbabilisticTreeCreaterTest { 32 private const int POPULATION_SIZE = 1000 ;33 private const int MAX_TREE_ SIZE= 100;34 private const int MAX_TREE_ HEIGHT= 10;32 private const int POPULATION_SIZE = 10000; 33 private const int MAX_TREE_LENGTH = 100; 34 private const int MAX_TREE_DEPTH = 10; 35 35 private TestContext testContextInstance; 36 36 … … 50 50 [TestMethod()] 51 51 public void ProbabilisticTreeCreaterDistributionsTest() { 52 var randomTrees = new List< SymbolicExpressionTree>();52 var randomTrees = new List<ISymbolicExpressionTree>(); 53 53 var grammar = Grammars.CreateSimpleArithmeticGrammar(); 54 54 var random = new MersenneTwister(31415); 55 var stopwatch = new Stopwatch(); 56 stopwatch.Start(); 55 57 for (int i = 0; i < POPULATION_SIZE; i++) { 56 randomTrees.Add(ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_ SIZE, MAX_TREE_HEIGHT, 0, 0));58 randomTrees.Add(ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_LENGTH, MAX_TREE_DEPTH, 0, 0)); 57 59 } 60 stopwatch.Stop(); 58 61 59 62 foreach (var tree in randomTrees) { 60 63 Util.IsValid(tree); 61 64 } 65 double msPerRandomTreeCreation = stopwatch.ElapsedMilliseconds / (double)POPULATION_SIZE; 66 62 67 Console.WriteLine("ProbabilisticTreeCreator: " + Environment.NewLine + 68 msPerRandomTreeCreation + " ms per random tree (~" + Math.Round(1000.0 / (msPerRandomTreeCreation)) + "random trees / s)" + Environment.NewLine + 63 69 Util.GetSizeDistributionString(randomTrees, 105, 5) + Environment.NewLine + 64 70 Util.GetFunctionDistributionString(randomTrees) + Environment.NewLine + … … 66 72 Util.GetTerminalDistributionString(randomTrees) + Environment.NewLine 67 73 ); 74 Assert.IsTrue(Math.Round(1000.0 / (msPerRandomTreeCreation)) > 2000); // must achieve more than 2000 random trees / s 68 75 } 69 76 … … 71 78 [TestMethod()] 72 79 public void ProbabilisticTreeCreaterWithAdfDistributionsTest() { 73 var randomTrees = new List< SymbolicExpressionTree>();80 var randomTrees = new List<ISymbolicExpressionTree>(); 74 81 var grammar = Grammars.CreateArithmeticAndAdfGrammar(); 75 82 var random = new MersenneTwister(31415); 83 var stopwatch = new Stopwatch(); 84 stopwatch.Start(); 76 85 for (int i = 0; i < POPULATION_SIZE; i++) { 77 var tree = ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_SIZE, MAX_TREE_HEIGHT, 3, 3); 78 Util.IsValid(tree); 86 var tree = ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_LENGTH, MAX_TREE_DEPTH, 3, 3); 79 87 randomTrees.Add(tree); 80 88 } 89 stopwatch.Stop(); 90 foreach (var tree in randomTrees) 91 Util.IsValid(tree); 92 93 double msPerRandomTreeCreation = stopwatch.ElapsedMilliseconds / (double)POPULATION_SIZE; 94 81 95 Console.WriteLine("ProbabilisticTreeCreator: " + Environment.NewLine + 96 msPerRandomTreeCreation + " ms per random tree (~" + Math.Round(1000.0 / (msPerRandomTreeCreation)) + "random trees / s)" + Environment.NewLine + 82 97 Util.GetSizeDistributionString(randomTrees, 105, 5) + Environment.NewLine + 83 98 Util.GetFunctionDistributionString(randomTrees) + Environment.NewLine + … … 85 100 Util.GetTerminalDistributionString(randomTrees) + Environment.NewLine 86 101 ); 102 103 Assert.IsTrue(Math.Round(1000.0 / (msPerRandomTreeCreation)) > 2000); // must achieve more than 2000 random trees / s 87 104 } 88 105 } -
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Tests/ReplaceBranchManipulationTest.cs
r5445 r5549 23 23 using System.Collections.Generic; 24 24 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 25 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Creators;26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Manipulators;27 25 using HeuristicLab.Random; 28 26 using Microsoft.VisualStudio.TestTools.UnitTesting; 29 27 30 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._ 3.Tests {28 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._4.Tests { 31 29 [TestClass] 32 30 public class ReplaceBranchManipulationTest { 33 31 private const int POPULATION_SIZE = 1000; 34 private const int MAX_TREE_ SIZE= 100;35 private const int MAX_TREE_ HEIGHT= 10;32 private const int MAX_TREE_LENGTH = 100; 33 private const int MAX_TREE_DEPTH = 10; 36 34 private TestContext testContextInstance; 37 35 … … 51 49 [TestMethod()] 52 50 public void ReplaceBranchManipulationDistributionsTest() { 53 var trees = new List<SymbolicExpressionTree>(); 51 SymbolicExpressionTreeStringFormatter formatter = new SymbolicExpressionTreeStringFormatter(); 52 var trees = new List<ISymbolicExpressionTree>(); 54 53 var grammar = Grammars.CreateArithmeticAndAdfGrammar(); 55 54 var random = new MersenneTwister(31415); 56 55 for (int i = 0; i < POPULATION_SIZE; i++) { 57 var tree = ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_SIZE, MAX_TREE_HEIGHT, 3, 3); 58 bool success; 59 ReplaceBranchManipulation.ReplaceRandomBranch(random, tree, grammar, MAX_TREE_SIZE, MAX_TREE_HEIGHT, out success); 60 Assert.IsTrue(success); 56 var tree = ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_LENGTH, MAX_TREE_DEPTH, 3, 3); 57 string originalTree = formatter.Format(tree); 58 ReplaceBranchManipulation.ReplaceRandomBranch(random, tree, MAX_TREE_LENGTH, MAX_TREE_DEPTH); 59 string manipulatedTree = formatter.Format(tree); 60 Assert.IsFalse(originalTree == manipulatedTree); 61 61 Util.IsValid(tree); 62 62 trees.Add(tree); -
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Tests/SubroutineCreaterTest.cs
r5445 r5549 21 21 22 22 using System; 23 using System.Linq; 23 24 using System.Collections.Generic; 24 25 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 25 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ArchitectureManipulators;26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Creators;27 26 using HeuristicLab.Random; 28 27 using Microsoft.VisualStudio.TestTools.UnitTesting; 29 28 30 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._ 3.Tests {29 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._4.Tests { 31 30 [TestClass] 32 31 public class SubroutineCreaterTest { 33 32 private const int POPULATION_SIZE = 1000; 34 private const int MAX_TREE_ SIZE= 100;35 private const int MAX_TREE_ HEIGHT= 10;33 private const int MAX_TREE_LENGTH = 100; 34 private const int MAX_TREE_DEPTH = 10; 36 35 private TestContext testContextInstance; 37 36 … … 51 50 [TestMethod()] 52 51 public void SubroutineCreaterDistributionsTest() { 53 var trees = new List< SymbolicExpressionTree>();52 var trees = new List<ISymbolicExpressionTree>(); 54 53 var grammar = Grammars.CreateArithmeticAndAdfGrammar(); 55 54 var random = new MersenneTwister(31415); 56 55 for (int i = 0; i < POPULATION_SIZE; i++) { 57 SymbolicExpressionTree tree = null;56 ISymbolicExpressionTree tree = null; 58 57 do { 59 tree = ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_ SIZE, MAX_TREE_HEIGHT, 3, 3);58 tree = ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_LENGTH, MAX_TREE_DEPTH, 3, 3); 60 59 } while ( !OneMoreAdfAllowed(tree)); 61 var success = SubroutineCreater.CreateSubroutine(random, tree, grammar, MAX_TREE_SIZE, MAX_TREE_HEIGHT, 3, 3);60 var success = SubroutineCreater.CreateSubroutine(random, tree, MAX_TREE_LENGTH, MAX_TREE_DEPTH, 3, 3); 62 61 Assert.IsTrue(success); 63 62 Util.IsValid(tree); … … 72 71 } 73 72 74 private bool OneMoreAdfAllowed( SymbolicExpressionTree tree) {75 return tree. Size < 80 && tree.Root.SubTrees.Count< 4;73 private bool OneMoreAdfAllowed(ISymbolicExpressionTree tree) { 74 return tree.Length < 80 && tree.Root.SubTrees.Count() < 4; 76 75 } 77 76 } -
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Tests/SubroutineDeleterTest.cs
r5445 r5549 21 21 22 22 using System; 23 using System.Linq; 23 24 using System.Collections.Generic; 24 25 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 25 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ArchitectureManipulators;26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Creators;27 26 using HeuristicLab.Random; 28 27 using Microsoft.VisualStudio.TestTools.UnitTesting; 29 28 30 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._ 3.Tests {29 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._4.Tests { 31 30 [TestClass] 32 31 public class SubroutineDeleterTest { 33 32 private const int POPULATION_SIZE = 1000; 34 private const int MAX_TREE_ SIZE= 100;35 private const int MAX_TREE_ HEIGHT= 10;33 private const int MAX_TREE_LENGTH = 100; 34 private const int MAX_TREE_DEPTH = 10; 36 35 private TestContext testContextInstance; 37 36 … … 51 50 [TestMethod()] 52 51 public void SubroutineDeleterDistributionsTest() { 53 var trees = new List< SymbolicExpressionTree>();52 var trees = new List<ISymbolicExpressionTree>(); 54 53 var grammar = Grammars.CreateArithmeticAndAdfGrammar(); 55 54 var random = new MersenneTwister(31415); 56 55 for (int i = 0; i < POPULATION_SIZE; i++) { 57 SymbolicExpressionTree tree = null;56 ISymbolicExpressionTree tree = null; 58 57 do { 59 tree = ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_ SIZE, MAX_TREE_HEIGHT, 3, 3);58 tree = ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_LENGTH, MAX_TREE_DEPTH, 3, 3); 60 59 } while (!HasAtLeastOneAdf(tree)); 61 var success = SubroutineDeleter.DeleteSubroutine(random, tree, grammar, MAX_TREE_SIZE, MAX_TREE_HEIGHT,3, 3);60 var success = SubroutineDeleter.DeleteSubroutine(random, tree, 3, 3); 62 61 Assert.IsTrue(success); 63 62 Util.IsValid(tree); … … 72 71 } 73 72 74 private bool HasAtLeastOneAdf( SymbolicExpressionTree tree) {75 return tree.Root.SubTrees.Count > 1;73 private bool HasAtLeastOneAdf(ISymbolicExpressionTree tree) { 74 return tree.Root.SubTrees.Count() > 1; 76 75 } 77 76 } -
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Tests/SubroutineDuplicaterTest.cs
r5445 r5549 21 21 22 22 using System; 23 using System.Linq; 23 24 using System.Collections.Generic; 24 25 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 25 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ArchitectureManipulators;26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Creators;27 26 using HeuristicLab.Random; 28 27 using Microsoft.VisualStudio.TestTools.UnitTesting; 29 28 30 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._ 3.Tests {29 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._4.Tests { 31 30 [TestClass] 32 31 public class SubroutineDuplicaterTest { 33 32 private const int POPULATION_SIZE = 1000; 34 private const int MAX_TREE_ SIZE= 100;35 private const int MAX_TREE_ HEIGHT= 10;33 private const int MAX_TREE_LENGTH = 100; 34 private const int MAX_TREE_DEPTH = 10; 36 35 private TestContext testContextInstance; 37 36 … … 51 50 [TestMethod()] 52 51 public void SubroutineDuplicaterDistributionsTest() { 53 var trees = new List< SymbolicExpressionTree>();52 var trees = new List<ISymbolicExpressionTree>(); 54 53 var grammar = Grammars.CreateArithmeticAndAdfGrammar(); 55 54 var random = new MersenneTwister(); 56 55 for (int i = 0; i < POPULATION_SIZE; i++) { 57 SymbolicExpressionTree tree = null;56 ISymbolicExpressionTree tree = null; 58 57 do { 59 tree = ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_ SIZE, MAX_TREE_HEIGHT, 3, 3);58 tree = ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_LENGTH, MAX_TREE_DEPTH, 3, 3); 60 59 } while (!HasOneAdf(tree)); 61 var success = SubroutineDuplicater.DuplicateSubroutine(random, tree, grammar, MAX_TREE_SIZE, MAX_TREE_HEIGHT,3, 3);60 var success = SubroutineDuplicater.DuplicateSubroutine(random, tree, 3, 3); 62 61 Assert.IsTrue(success); 63 62 Util.IsValid(tree); … … 72 71 } 73 72 74 private bool HasOneAdf( SymbolicExpressionTree tree) {75 return tree.Root.SubTrees.Count == 2;73 private bool HasOneAdf(ISymbolicExpressionTree tree) { 74 return tree.Root.SubTrees.Count() == 2; 76 75 } 77 76 } -
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Tests/SubtreeCrossoverTest.cs
r5445 r5549 24 24 using System.Diagnostics; 25 25 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Creators;27 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Crossovers;28 26 using HeuristicLab.Random; 29 27 using Microsoft.VisualStudio.TestTools.UnitTesting; 30 28 31 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._ 3.Tests {29 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._4.Tests { 32 30 [TestClass] 33 31 public class SubtreeCrossoverTest { … … 51 49 public void SubtreeCrossoverDistributionsTest() { 52 50 int generations = 5; 53 var trees = new List< SymbolicExpressionTree>();51 var trees = new List<ISymbolicExpressionTree>(); 54 52 var grammar = Grammars.CreateArithmeticAndAdfGrammar(); 55 53 var random = new MersenneTwister(31415); 56 List< SymbolicExpressionTree> crossoverTrees;54 List<ISymbolicExpressionTree> crossoverTrees; 57 55 double msPerCrossoverEvent; 58 56 … … 63 61 stopwatch.Start(); 64 62 for (int gCount = 0; gCount < generations; gCount++) { 65 var newPopulation = new List< SymbolicExpressionTree>();63 var newPopulation = new List<ISymbolicExpressionTree>(); 66 64 for (int i = 0; i < POPULATION_SIZE; i++) { 67 65 var par0 = (SymbolicExpressionTree)trees.SelectRandom(random).Clone(); 68 66 var par1 = (SymbolicExpressionTree)trees.SelectRandom(random).Clone(); 69 bool success; 70 newPopulation.Add(SubtreeCrossover.Cross(random, par0, par1, 0.9, 100, 10, out success)); 71 Assert.IsTrue(success); 67 newPopulation.Add(SubtreeCrossover.Cross(random, par0, par1, 0.9, 100, 10)); 72 68 } 73 69 crossoverTrees = newPopulation; -
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Tests/Util.cs
r5445 r5549 25 25 using System.Text; 26 26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 27 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;28 27 using Microsoft.VisualStudio.TestTools.UnitTesting; 29 28 30 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._ 3.Tests {29 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._4.Tests { 31 30 public static class Util { 32 public static string GetSizeDistributionString(IList< SymbolicExpressionTree> trees, int maxTreeSize, int binSize) {33 int[] histogram = new int[maxTree Size/ binSize];31 public static string GetSizeDistributionString(IList<ISymbolicExpressionTree> trees, int maxTreeLength, int binSize) { 32 int[] histogram = new int[maxTreeLength / binSize]; 34 33 for (int i = 0; i < trees.Count; i++) { 35 int binIndex = Math.Min(histogram.Length - 1, trees[i]. Size/ binSize);34 int binIndex = Math.Min(histogram.Length - 1, trees[i].Length / binSize); 36 35 histogram[binIndex]++; 37 36 } … … 49 48 } 50 49 51 public static string GetFunctionDistributionString(IList< SymbolicExpressionTree> trees) {50 public static string GetFunctionDistributionString(IList<ISymbolicExpressionTree> trees) { 52 51 Dictionary<string, int> occurances = new Dictionary<string, int>(); 53 52 double n = 0.0; 54 53 for (int i = 0; i < trees.Count; i++) { 55 54 foreach (var node in trees[i].IterateNodesPrefix()) { 56 if (node.SubTrees.Count > 0) {55 if (node.SubTrees.Count() > 0) { 57 56 if (!occurances.ContainsKey(node.Symbol.Name)) 58 57 occurances[node.Symbol.Name] = 0; … … 71 70 } 72 71 73 public static string GetNumberOfSubTreesDistributionString(IList< SymbolicExpressionTree> trees) {72 public static string GetNumberOfSubTreesDistributionString(IList<ISymbolicExpressionTree> trees) { 74 73 Dictionary<int, int> occurances = new Dictionary<int, int>(); 75 74 double n = 0.0; 76 75 for (int i = 0; i < trees.Count; i++) { 77 76 foreach (var node in trees[i].IterateNodesPrefix()) { 78 if (!occurances.ContainsKey(node.SubTrees.Count ))79 occurances[node.SubTrees.Count ] = 0;80 occurances[node.SubTrees.Count ]++;77 if (!occurances.ContainsKey(node.SubTrees.Count())) 78 occurances[node.SubTrees.Count()] = 0; 79 occurances[node.SubTrees.Count()]++; 81 80 n++; 82 81 } … … 92 91 93 92 94 public static string GetTerminalDistributionString(IList< SymbolicExpressionTree> trees) {93 public static string GetTerminalDistributionString(IList<ISymbolicExpressionTree> trees) { 95 94 Dictionary<string, int> occurances = new Dictionary<string, int>(); 96 95 double n = 0.0; 97 96 for (int i = 0; i < trees.Count; i++) { 98 97 foreach (var node in trees[i].IterateNodesPrefix()) { 99 if (node.SubTrees.Count == 0) {98 if (node.SubTrees.Count() == 0) { 100 99 if (!occurances.ContainsKey(node.Symbol.Name)) 101 100 occurances[node.Symbol.Name] = 0; … … 114 113 } 115 114 116 public static void IsValid( SymbolicExpressionTree tree) {117 int reportedSize = tree. Size;115 public static void IsValid(ISymbolicExpressionTree tree) { 116 int reportedSize = tree.Length; 118 117 int actualSize = tree.IterateNodesPostfix().Count(); 119 118 Assert.AreEqual(actualSize, reportedSize); … … 135 134 } 136 135 137 public static void IsValid( SymbolicExpressionTreeNode treeNode) {136 public static void IsValid(ISymbolicExpressionTreeNode treeNode) { 138 137 var matchingSymbol = (from symb in treeNode.Grammar.Symbols 139 138 where symb.Name == treeNode.Symbol.Name 140 139 select symb).SingleOrDefault(); 141 Assert.IsTrue(treeNode.SubTrees.Count >= treeNode.Grammar.GetMinSubtreeCount(matchingSymbol));142 Assert.IsTrue(treeNode.SubTrees.Count <= treeNode.Grammar.GetMaxSubtreeCount(matchingSymbol));140 Assert.IsTrue(treeNode.SubTrees.Count() >= treeNode.Grammar.GetMinSubtreeCount(matchingSymbol)); 141 Assert.IsTrue(treeNode.SubTrees.Count() <= treeNode.Grammar.GetMaxSubtreeCount(matchingSymbol)); 143 142 Assert.AreNotEqual(0.0, matchingSymbol.InitialFrequency); // check that no deactivated symbols occur in the tree 144 for (int i = 0; i < treeNode.SubTrees.Count ; i++) {145 Assert.IsTrue(treeNode.G etAllowedSymbols(i).Select(x => x.Name).Contains(treeNode.SubTrees[i].Symbol.Name));146 IsValid(treeNode. SubTrees[i]);143 for (int i = 0; i < treeNode.SubTrees.Count(); i++) { 144 Assert.IsTrue(treeNode.Grammar.GetAllowedSymbols(treeNode.Symbol, i).Select(x => x.Name).Contains(treeNode.GetSubTree(i).Symbol.Name)); 145 IsValid(treeNode.GetSubTree(i)); 147 146 } 148 147 } -
branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/MultiObjective/SymbolicClassificationMultiObjectiveMeanSquaredErrorTreeSizeEvaluator.cs
r5547 r5549 56 56 IEnumerable<double> boundedEstimationValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit); 57 57 double mse = OnlineMeanSquaredErrorEvaluator.Calculate(originalValues, boundedEstimationValues); 58 return new double[2] { mse, solution. Size};58 return new double[2] { mse, solution.Length }; 59 59 } 60 60 } -
branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/MultiObjective/SymbolicClassificationMultiObjectivePearsonRSquaredTreeSizeEvaluator.cs
r5547 r5549 35 35 IEnumerable<double> boundedEstimationValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit); 36 36 double r2 = OnlinePearsonsRSquaredEvaluator.Calculate(originalValues, boundedEstimationValues); 37 return new double[2] { r2, solution. Size};37 return new double[2] { r2, solution.Length }; 38 38 } 39 39 } -
branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/MultiObjective/SymbolicRegressionMultiObjectiveMeanSquaredErrorTreeSizeEvaluator.cs
r5548 r5549 56 56 IEnumerable<double> boundedEstimationValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit); 57 57 double mse = OnlineMeanSquaredErrorEvaluator.Calculate(originalValues, boundedEstimationValues); 58 return new double[2] { mse, solution. Size};58 return new double[2] { mse, solution.Length }; 59 59 } 60 60 } -
branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/MultiObjective/SymbolicRegressionMultiObjectivePearsonRSquaredTreeSizeEvaluator.cs
r5548 r5549 56 56 IEnumerable<double> boundedEstimationValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit); 57 57 double r2 = OnlinePearsonsRSquaredEvaluator.Calculate(originalValues, boundedEstimationValues); 58 return new double[2] { r2, solution. Size};58 return new double[2] { r2, solution.Length }; 59 59 } 60 60 }
Note: See TracChangeset
for help on using the changeset viewer.