Changeset 13227
- Timestamp:
- 11/17/15 17:22:58 (9 years ago)
- Location:
- trunk/sources
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Creators/ProbabilisticTreeCreator.cs
r12422 r13227 74 74 } 75 75 76 public static ISymbolicExpressionTree CreateExpressionTree(IRandom random, ISymbolicExpressionGrammar grammar, int targetLength, 77 int maxTreeDepth) { 78 SymbolicExpressionTree tree = new SymbolicExpressionTree(); 79 var rootNode = (SymbolicExpressionTreeTopLevelNode)grammar.ProgramRootSymbol.CreateTreeNode(); 80 if (rootNode.HasLocalParameters) rootNode.ResetLocalParameters(random); 81 rootNode.SetGrammar(grammar.CreateExpressionTreeGrammar()); 82 83 var startNode = (SymbolicExpressionTreeTopLevelNode)grammar.StartSymbol.CreateTreeNode(); 84 if (startNode.HasLocalParameters) startNode.ResetLocalParameters(random); 85 startNode.SetGrammar(grammar.CreateExpressionTreeGrammar()); 86 87 rootNode.AddSubtree(startNode); 88 bool success = TryCreateFullTreeFromSeed(random, startNode, targetLength - 2, maxTreeDepth - 1); 89 if (!success) throw new InvalidOperationException(string.Format("Could not create a tree with target length {0} and max depth {1}", targetLength, maxTreeDepth)); 90 91 tree.Root = rootNode; 92 return tree; 93 94 } 95 76 96 private class TreeExtensionPoint { 77 97 public ISymbolicExpressionTreeNode Parent { get; set; } … … 107 127 } else { 108 128 // clean seedNode 109 while (seedNode.Subtrees. Count() > 0) seedNode.RemoveSubtree(0);129 while (seedNode.Subtrees.Any()) seedNode.RemoveSubtree(0); 110 130 } 111 131 // try a different length MAX_TRIES times -
trunk/sources/HeuristicLab.Tests/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4/ProbabilisticTreeCreaterTest.cs
r12012 r13227 23 23 using System.Collections.Generic; 24 24 using System.Diagnostics; 25 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;26 25 using HeuristicLab.Random; 27 26 using Microsoft.VisualStudio.TestTools.UnitTesting; … … 75 74 [TestProperty("Time", "short")] 76 75 public void ProbabilisticTreeCreatorSpecificTreeSizesTest() { 77 var trees = new List<ISymbolicExpressionTree>();78 76 var grammar = Grammars.CreateSimpleArithmeticGrammar(); 79 77 var random = new MersenneTwister(31415); 80 var treeGrammarType = SymbolicExpressionTreeGrammar_Accessor.ShadowedType.ReferencedType;81 78 82 83 for (int targetTreeSize = 1; targetTreeSize <= 100; targetTreeSize++) { 84 var tree = new SymbolicExpressionTree(); 85 var rootNode = (SymbolicExpressionTreeTopLevelNode)grammar.ProgramRootSymbol.CreateTreeNode(); 86 rootNode.SetGrammar((ISymbolicExpressionTreeGrammar)Activator.CreateInstance(treeGrammarType, grammar)); 87 if (rootNode.HasLocalParameters) rootNode.ResetLocalParameters(random); 88 var startNode = (SymbolicExpressionTreeTopLevelNode)grammar.StartSymbol.CreateTreeNode(); 89 startNode.SetGrammar((ISymbolicExpressionTreeGrammar)Activator.CreateInstance(treeGrammarType, grammar)); 90 if (startNode.HasLocalParameters) startNode.ResetLocalParameters(random); 91 rootNode.AddSubtree(startNode); 92 93 ProbabilisticTreeCreator_Accessor.TryCreateFullTreeFromSeed(random, startNode, targetTreeSize, ((int)Math.Log(targetTreeSize, 2)) + 1); 94 tree.Root = rootNode; 95 //mkommend: commented due to performance issues on the builder 96 // Assert.AreEqual(targetTreeSize + 2, tree.Length); //the root and start node must be additionally added 79 for (int targetTreeSize = 3; targetTreeSize <= 100; targetTreeSize++) { 80 var tree = ProbabilisticTreeCreator.CreateExpressionTree(random, grammar, targetTreeSize, ((int)Math.Log(targetTreeSize, 2)) + 2); 81 Assert.AreEqual(targetTreeSize, tree.Length); 97 82 } 98 83 }
Note: See TracChangeset
for help on using the changeset viewer.