Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/16/11 19:01:00 (13 years ago)
Author:
gkronber
Message:

#1418 changes in symbolic expression tree encoding.

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

Legend:

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

    • Property svn:ignore
      •  

        old new  
        22obj
        33HeuristicLabEncodingsSymbolicExpressionTreeEncodingPlugin.cs
         4*.user
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Manipulators/ChangeNodeTypeManipulation.cs

    r5445 r5499  
    2626using HeuristicLab.Core;
    2727using HeuristicLab.Data;
    28 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
    2928using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    3029
    31 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Manipulators {
     30namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
    3231  [StorableClass]
    3332  [Item("ChangeNodeTypeManipulation", "Selects a random tree node and changes the symbol.")]
    3433  public sealed class ChangeNodeTypeManipulation : SymbolicExpressionTreeManipulator {
    35 
    3634    [StorableConstructor]
    3735    private ChangeNodeTypeManipulation(bool deserializing) : base(deserializing) { }
     
    4341    }
    4442
    45     protected override void Manipulate(IRandom random, SymbolicExpressionTree symbolicExpressionTree, ISymbolicExpressionGrammar grammar, IntValue maxTreeSize, IntValue maxTreeHeight, out bool success) {
    46       ChangeNodeType(random, symbolicExpressionTree, grammar, maxTreeSize.Value, maxTreeHeight.Value, out success);
     43    protected override void Manipulate(IRandom random, SymbolicExpressionTree symbolicExpressionTree) {
     44      ChangeNodeType(random, symbolicExpressionTree);
    4745    }
    4846
    49     public static void ChangeNodeType(IRandom random, SymbolicExpressionTree symbolicExpressionTree, ISymbolicExpressionGrammar grammar, int maxTreeSize, int maxTreeHeight, out bool success) {
     47    public static void ChangeNodeType(IRandom random, SymbolicExpressionTree symbolicExpressionTree) {
    5048
    5149      // select any node as parent (except the root node)
    5250      var manipulationPoint = (from parent in symbolicExpressionTree.Root.IterateNodesPrefix().Skip(1)
    5351                               from subtree in parent.SubTrees
    54                                select new { Parent = parent, Node = subtree, Index = parent.SubTrees.IndexOf(subtree) }).SelectRandom(random);
     52                               select new { Parent = parent, Node = subtree, Index = parent.IndexOfSubTree(subtree) })
     53                               .SelectRandom(random);
     54      int subTreeCount = manipulationPoint.Node.SubTrees.Count();
    5555      // find possible symbols for the node (also considering the existing branches below it)
    5656      var allowedSymbols = from symbol in manipulationPoint.Parent.GetAllowedSymbols(manipulationPoint.Index)
    57                            where manipulationPoint.Node.SubTrees.Count <= manipulationPoint.Node.Grammar.GetMaxSubtreeCount(symbol)
    58                            where manipulationPoint.Node.SubTrees.Count >= manipulationPoint.Node.Grammar.GetMinSubtreeCount(symbol)
     57                           where subTreeCount <= manipulationPoint.Node.Grammar.GetMaxSubtreeCount(symbol)
     58                           where subTreeCount >= manipulationPoint.Node.Grammar.GetMinSubtreeCount(symbol)
    5959                           select symbol;
    6060
    6161      if (allowedSymbols.Count() == 0) {
    62         success = false;
    6362        return;
    6463      }
    6564      var node = manipulationPoint.Node;
    6665      // keep only symbols that are still possible considering the existing sub-trees
    67       var constrainedSymbols = from symbol in allowedSymbols
    68                                let disallowedSubtrees =
    69                                      from subtree in node.SubTrees
    70                                      where !node.Grammar.IsAllowedChild(symbol, subtree.Symbol, node.SubTrees.IndexOf(subtree))
    71                                      select subtree
    72                                where disallowedSubtrees.Count() == 0
    73                                select symbol;
     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();
    7474      if (constrainedSymbols.Count() == 0) {
    75         success = false;
    7675        return;
    7776      }
    78       var newSymbol = SelectRandomSymbol(random, constrainedSymbols);
     77      var weights = constrainedSymbols.Select(s => s.InitialFrequency).ToList();
     78      var newSymbol =  constrainedSymbols.SelectRandom(weights, random);
    7979
    8080      // replace the old node with the new node
     
    8686      manipulationPoint.Parent.RemoveSubTree(manipulationPoint.Index);
    8787      manipulationPoint.Parent.InsertSubTree(manipulationPoint.Index, newNode);
    88       success = true;
    89     }
    90 
    91     private static Symbol SelectRandomSymbol(IRandom random, IEnumerable<Symbol> symbols) {
    92       var symbolList = symbols.ToList();
    93       var ticketsSum = symbolList.Select(x => x.InitialFrequency).Sum();
    94       if (ticketsSum == 0.0) throw new ArgumentException("The initial frequency of all allowed symbols is zero.");
    95       var r = random.NextDouble() * ticketsSum;
    96       double aggregatedTickets = 0;
    97       for (int i = 0; i < symbolList.Count; i++) {
    98         aggregatedTickets += symbolList[i].InitialFrequency;
    99         if (aggregatedTickets > r) {
    100           return symbolList[i];
    101         }
    102       }
    103       // this should never happen
    104       throw new ArgumentException("There is a problem with the initial frequency setting of allowed symbols.");
    10588    }
    10689  }
Note: See TracChangeset for help on using the changeset viewer.