Changeset 13227


Ignore:
Timestamp:
11/17/15 17:22:58 (4 years ago)
Author:
mkommend
Message:

#2510: Added method for creating trees with a specific length to the PTC2 and adapted the according unit test.

Location:
trunk/sources
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Creators/ProbabilisticTreeCreator.cs

    r12422 r13227  
    7474    }
    7575
     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
    7696    private class TreeExtensionPoint {
    7797      public ISymbolicExpressionTreeNode Parent { get; set; }
     
    107127        } else {
    108128          // clean seedNode
    109           while (seedNode.Subtrees.Count() > 0) seedNode.RemoveSubtree(0);
     129          while (seedNode.Subtrees.Any()) seedNode.RemoveSubtree(0);
    110130        }
    111131        // try a different length MAX_TRIES times
  • trunk/sources/HeuristicLab.Tests/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4/ProbabilisticTreeCreaterTest.cs

    r12012 r13227  
    2323using System.Collections.Generic;
    2424using System.Diagnostics;
    25 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2625using HeuristicLab.Random;
    2726using Microsoft.VisualStudio.TestTools.UnitTesting;
     
    7574    [TestProperty("Time", "short")]
    7675    public void ProbabilisticTreeCreatorSpecificTreeSizesTest() {
    77       var trees = new List<ISymbolicExpressionTree>();
    7876      var grammar = Grammars.CreateSimpleArithmeticGrammar();
    7977      var random = new MersenneTwister(31415);
    80       var treeGrammarType = SymbolicExpressionTreeGrammar_Accessor.ShadowedType.ReferencedType;
    8178
    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);
    9782      }
    9883    }
Note: See TracChangeset for help on using the changeset viewer.