Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/28/11 17:11:56 (14 years ago)
Author:
gkronber
Message:

#1418 improved ChangeNodeTypeManipulation operator to make unit test succeed.

Location:
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Manipulators/ChangeNodeTypeManipulation.cs

    r5529 r5567  
    4646
    4747    public static void ChangeNodeType(IRandom random, ISymbolicExpressionTree symbolicExpressionTree) {
     48      // select any node as parent (except the root node)
     49      var manipulationPoints = (from parent in symbolicExpressionTree.Root.IterateNodesPrefix().Skip(1)
     50                                let subtreeCount = parent.SubTrees.Count()
     51                                from subtreeIndex in Enumerable.Range(0, subtreeCount)
     52                                let subtree = parent.GetSubTree(subtreeIndex)
     53                                let existingSubtreeCount = subtree.SubTrees.Count()
     54                                // find possible symbols for the node (also considering the existing branches below it)
     55                                let allowedSymbols = (from symbol in parent.Grammar.GetAllowedSymbols(parent.Symbol, subtreeIndex)
     56                                                      // do not replace the existing symbol with itself
     57                                                      where symbol.Name != subtree.Symbol.Name
     58                                                      where existingSubtreeCount <= parent.Grammar.GetMaxSubtreeCount(symbol)
     59                                                      where existingSubtreeCount >= parent.Grammar.GetMinSubtreeCount(symbol)
     60                                                      // keep only symbols that are still possible considering the existing sub-trees
     61                                                      where (from existingSubtreeIndex in Enumerable.Range(0, existingSubtreeCount)
     62                                                             let existingSubtree = subtree.GetSubTree(existingSubtreeIndex)
     63                                                             select parent.Grammar.IsAllowedChild(symbol, existingSubtree.Symbol, existingSubtreeIndex))
     64                                                             .All(x => x == true)
     65                                                      select symbol)
     66                                                      .ToList()
     67                                where allowedSymbols.Count() > 0
     68                                select new { Parent = parent, Child = subtree, Index = subtreeIndex, AllowedSymbols = allowedSymbols })
     69                               .ToList();
     70      if (manipulationPoints.Count == 0) { return; }
     71      var selectedManipulationPoint = manipulationPoints.SelectRandom(random);
    4872
    49       // select any node as parent (except the root node)
    50       var manipulationPoint = (from parent in symbolicExpressionTree.Root.IterateNodesPrefix().Skip(1)
    51                                from subtree in parent.SubTrees
    52                                select new { Parent = parent, Node = subtree, Index = parent.IndexOfSubTree(subtree) })
    53                                .SelectRandom(random);
    54       int subTreeCount = manipulationPoint.Node.SubTrees.Count();
    55       // find possible symbols for the node (also considering the existing branches below it)
    56       var allowedSymbols = from symbol in manipulationPoint.Parent.Grammar.GetAllowedSymbols(manipulationPoint.Parent.Symbol, manipulationPoint.Index)
    57                            where subTreeCount <= manipulationPoint.Node.Grammar.GetMaxSubtreeCount(symbol)
    58                            where subTreeCount >= manipulationPoint.Node.Grammar.GetMinSubtreeCount(symbol)
    59                            select symbol;
    60 
    61       if (allowedSymbols.Count() == 0) {
    62         return;
    63       }
    64       var node = manipulationPoint.Node;
    65       // keep only symbols that are still possible considering the existing sub-trees
    66       var constrainedSymbols = (from symbol in allowedSymbols
    67                                 let disallowedSubtrees =
    68                                       from subtree in node.SubTrees
    69                                       where !node.Grammar.IsAllowedChild(symbol, subtree.Symbol, node.IndexOfSubTree(subtree))
    70                                       select subtree
    71                                 where disallowedSubtrees.Count() == 0
    72                                 select symbol)
    73                                .ToList();
    74       if (constrainedSymbols.Count() == 0) {
    75         return;
    76       }
    77       var weights = constrainedSymbols.Select(s => s.InitialFrequency).ToList();
    78       var newSymbol =  constrainedSymbols.SelectRandom(weights, random);
     73      var weights = selectedManipulationPoint.AllowedSymbols.Select(s => s.InitialFrequency).ToList();
     74      var newSymbol = selectedManipulationPoint.AllowedSymbols.SelectRandom(weights, random);
    7975
    8076      // replace the old node with the new node
     
    8278      if (newNode.HasLocalParameters)
    8379        newNode.ResetLocalParameters(random);
    84       foreach (var subtree in node.SubTrees)
     80      foreach (var subtree in selectedManipulationPoint.Child.SubTrees)
    8581        newNode.AddSubTree(subtree);
    86       manipulationPoint.Parent.RemoveSubTree(manipulationPoint.Index);
    87       manipulationPoint.Parent.InsertSubTree(manipulationPoint.Index, newNode);
     82      selectedManipulationPoint.Parent.RemoveSubTree(selectedManipulationPoint.Index);
     83      selectedManipulationPoint.Parent.InsertSubTree(selectedManipulationPoint.Index, newNode);
    8884    }
    8985  }
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Tests/ChangeNodeTypeManipulationTest.cs

    r5549 r5567  
    5353      var grammar = Grammars.CreateArithmeticAndAdfGrammar();
    5454      var random = new MersenneTwister(31415);
     55      int failedEvents = 0;
    5556      for (int i = 0; i < POPULATION_SIZE; i++) {
    5657        var tree = ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_LENGTH, MAX_TREE_DEPTH, 3, 3);
     
    5859        ChangeNodeTypeManipulation.ChangeNodeType(random, tree);
    5960        string manipulatedTree = formatter.Format(tree);
    60         Assert.IsFalse(originalTree == manipulatedTree);
     61        if (originalTree == manipulatedTree) failedEvents++;
    6162        Util.IsValid(tree);
    6263        trees.Add(tree);
    6364      }
    6465      Console.WriteLine("ChangeNodeTypeManipulation: " + Environment.NewLine +
     66        "Failed events: " + failedEvents * 100.0 / POPULATION_SIZE + " %" + Environment.NewLine +
    6567        Util.GetSizeDistributionString(trees, 105, 5) + Environment.NewLine +
    6668        Util.GetFunctionDistributionString(trees) + Environment.NewLine +
     
    6870        Util.GetTerminalDistributionString(trees) + Environment.NewLine
    6971        );
     72      Assert.IsTrue(failedEvents * 100.0 / POPULATION_SIZE < 5.0); // only max 5% failed mutations are allowed
    7073    }
    7174  }
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Tests/Grammars.cs

    r5549 r5567  
    2323using HeuristicLab.Common;
    2424using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
     25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2526
    2627namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._4.Tests {
     
    5960      public override IDeepCloneable Clone(Cloner cloner) {
    6061        return new Terminal(this, cloner);
     62      }
     63
     64      public override ISymbolicExpressionTreeNode CreateTreeNode() {
     65        return new TerminalNode(this);
     66      }
     67    }
     68
     69    private class TerminalNode : SymbolicExpressionTreeTerminalNode {
     70      public override bool HasLocalParameters { get { return true; } }
     71      private double value;
     72      protected TerminalNode(TerminalNode original, Cloner cloner)
     73        : base(original, cloner) {
     74        this.value = original.value;
     75      }
     76      [StorableConstructor]
     77      protected TerminalNode(bool deserializing) : base(deserializing) { }
     78      public TerminalNode(Terminal symbol) : base(symbol) { }
     79
     80      public override IDeepCloneable Clone(Cloner cloner) {
     81        return new TerminalNode(this, cloner);
     82      }
     83      public override void ResetLocalParameters(Core.IRandom random) {
     84        base.ResetLocalParameters(random);
     85        value = random.NextDouble();
     86      }
     87      public override void ShakeLocalParameters(Core.IRandom random, double shakingFactor) {
     88        base.ShakeLocalParameters(random, shakingFactor);
     89        value = random.NextDouble();
     90      }
     91      public override string ToString() {
     92        return value.ToString("E4");
    6193      }
    6294    }
Note: See TracChangeset for help on using the changeset viewer.