Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/16/11 19:01:00 (14 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/ReplaceBranchManipulation.cs

    r5445 r5499  
    2626using HeuristicLab.Core;
    2727using HeuristicLab.Data;
    28 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Creators;
    29 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
    3028using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     29using HeuristicLab.Parameters;
    3130
    32 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Manipulators {
     31namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
    3332  [StorableClass]
    3433  [Item("ReplaceBranchManipulation", "Selects a branch of the tree randomly and replaces it with a newly initialized branch (using PTC2).")]
    35   public sealed class ReplaceBranchManipulation : SymbolicExpressionTreeManipulator {
     34  public sealed class ReplaceBranchManipulation : SymbolicExpressionTreeManipulator, ISymbolicExpressionTreeSizeConstraintOperator {
     35    private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength";
     36    private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth";
     37    #region Parameter Properties
     38    public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter {
     39      get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; }
     40    }
     41    public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter {
     42      get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; }
     43    }
     44    #endregion
     45    #region Properties
     46    public IntValue MaximumSymbolicExpressionTreeLength {
     47      get { return MaximumSymbolicExpressionTreeLengthParameter.ActualValue; }
     48    }
     49    public IntValue MaximumSymbolicExpressionTreeDepth {
     50      get { return MaximumSymbolicExpressionTreeDepthParameter.ActualValue; }
     51    }
     52    #endregion
     53
    3654    [StorableConstructor]
    3755    private ReplaceBranchManipulation(bool deserializing) : base(deserializing) { }
    3856    private ReplaceBranchManipulation(ReplaceBranchManipulation original, Cloner cloner) : base(original, cloner) { }
    39     public ReplaceBranchManipulation() : base() { }
     57    public ReplaceBranchManipulation()
     58      : base() {
     59      Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, "The maximal length (number of nodes) of the symbolic expression tree."));
     60      Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0)."));
     61    }
    4062
    4163    public override IDeepCloneable Clone(Cloner cloner) {
     
    4365    }
    4466
    45     protected override void Manipulate(IRandom random, SymbolicExpressionTree symbolicExpressionTree, ISymbolicExpressionGrammar grammar, IntValue maxTreeSize, IntValue maxTreeHeight, out bool success) {
    46       ReplaceRandomBranch(random, symbolicExpressionTree, grammar, maxTreeSize.Value, maxTreeHeight.Value, out success);
     67    protected override void Manipulate(IRandom random, ISymbolicExpressionTree symbolicExpressionTree) {
     68      ReplaceRandomBranch(random, symbolicExpressionTree, MaximumSymbolicExpressionTreeLength.Value, MaximumSymbolicExpressionTreeDepth.Value);
    4769    }
    4870
    49     public static void ReplaceRandomBranch(IRandom random, SymbolicExpressionTree symbolicExpressionTree, ISymbolicExpressionGrammar grammar, int maxTreeSize, int maxTreeHeight, out bool success) {
    50       success = false;
     71    public static void ReplaceRandomBranch(IRandom random, ISymbolicExpressionTree symbolicExpressionTree, int maxTreeLength, int maxTreeDepth) {
    5172      // select any node as parent (except the root node)
    5273      var manipulationPoint = (from parent in symbolicExpressionTree.Root.IterateNodesPrefix().Skip(1)
    5374                               from subtree in parent.SubTrees
    54                                select new { Parent = parent, Node = subtree, Index = parent.SubTrees.IndexOf(subtree) }).SelectRandom(random);
     75                               select new { Parent = parent, Node = subtree, Index = parent.IndexOfSubTree(subtree) })
     76                               .SelectRandom(random);
    5577
    56       int maxSize = maxTreeSize - symbolicExpressionTree.Size + manipulationPoint.Node.GetSize();
    57       int maxHeight = maxTreeHeight - symbolicExpressionTree.Height + manipulationPoint.Node.GetHeight();
     78      int maxLength = maxTreeLength - symbolicExpressionTree.Size + manipulationPoint.Node.GetSize();
     79      int maxDepth = maxTreeDepth - symbolicExpressionTree.Height + manipulationPoint.Node.GetHeight();
    5880      // find possible symbols for the node (also considering the existing branches below it)
    59       var allowedSymbols = from symbol in manipulationPoint.Parent.GetAllowedSymbols(manipulationPoint.Index)
    60                            where manipulationPoint.Node.Grammar.GetMinExpressionDepth(symbol) <= maxHeight
    61                            where manipulationPoint.Node.Grammar.GetMinExpressionLength(symbol) <= maxSize
    62                            select symbol;
     81      var allowedSymbols = (from symbol in manipulationPoint.Parent.GetAllowedSymbols(manipulationPoint.Index)
     82                            where manipulationPoint.Node.Grammar.GetMinExpressionDepth(symbol) <= maxDepth
     83                            where manipulationPoint.Node.Grammar.GetMinExpressionLength(symbol) <= maxLength
     84                            select symbol).ToList();
    6385      if (allowedSymbols.Count() == 0) return;
    64 
    65       var seedSymbol = SelectRandomSymbol(random, allowedSymbols);  // replace the old node with the new node
     86      var weights = allowedSymbols.Select(s => s.InitialFrequency).ToList();
     87      var seedSymbol = allowedSymbols.SelectRandom(weights, random);
     88      // replace the old node with the new node
    6689      var seedNode = seedSymbol.CreateTreeNode();
    6790      if (seedNode.HasLocalParameters)
     
    7093      manipulationPoint.Parent.RemoveSubTree(manipulationPoint.Index);
    7194      manipulationPoint.Parent.InsertSubTree(manipulationPoint.Index, seedNode);
    72       seedNode = ProbabilisticTreeCreator.PTC2(random, seedNode, maxSize, maxHeight, 0, 0);
    73       success = true;
    74     }
    75 
    76     private static Symbol SelectRandomSymbol(IRandom random, IEnumerable<Symbol> symbols) {
    77       var symbolList = symbols.ToList();
    78       var ticketsSum = symbolList.Select(x => x.InitialFrequency).Sum();
    79       if (ticketsSum == 0.0) throw new ArgumentException("The initial frequency of all allowed symbols is zero.");
    80       var r = random.NextDouble() * ticketsSum;
    81       double aggregatedTickets = 0;
    82       for (int i = 0; i < symbolList.Count; i++) {
    83         aggregatedTickets += symbolList[i].InitialFrequency;
    84         if (aggregatedTickets > r) {
    85           return symbolList[i];
    86         }
    87       }
    88       // this should never happen
    89       throw new ArgumentException("There is a problem with the initial frequency setting of allowed symbols.");
     95      seedNode = ProbabilisticTreeCreator.PTC2(random, seedNode, maxLength, maxDepth, 0, 0);
    9096    }
    9197  }
Note: See TracChangeset for help on using the changeset viewer.