Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Manipulators/ReplaceBranchManipulation.cs @ 5593

Last change on this file since 5593 was 5569, checked in by gkronber, 14 years ago

#1418 fixed implementation of ReplaceBranchManipulation operator to make unit test succeed

File size: 6.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HeuristicLab.Parameters;
30
31namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
32  [StorableClass]
33  [Item("ReplaceBranchManipulation", "Selects a branch of the tree randomly and replaces it with a newly initialized branch (using PTC2).")]
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
54    [StorableConstructor]
55    private ReplaceBranchManipulation(bool deserializing) : base(deserializing) { }
56    private ReplaceBranchManipulation(ReplaceBranchManipulation original, Cloner cloner) : base(original, cloner) { }
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    }
62
63    public override IDeepCloneable Clone(Cloner cloner) {
64      return new ReplaceBranchManipulation(this, cloner);
65    }
66
67    protected override void Manipulate(IRandom random, ISymbolicExpressionTree symbolicExpressionTree) {
68      ReplaceRandomBranch(random, symbolicExpressionTree, MaximumSymbolicExpressionTreeLength.Value, MaximumSymbolicExpressionTreeDepth.Value);
69    }
70
71    public static void ReplaceRandomBranch(IRandom random, ISymbolicExpressionTree symbolicExpressionTree, int maxTreeLength, int maxTreeDepth) {
72      // select any node as parent (except the root node)
73      var manipulationPoints = (from parent in symbolicExpressionTree.Root.IterateNodesPrefix().Skip(1)
74                                from subtree in parent.SubTrees
75                                let subtreeIndex = parent.IndexOfSubTree(subtree)
76                                let maxLength = maxTreeLength - symbolicExpressionTree.Length + subtree.GetLength()
77                                let maxDepth = maxTreeDepth - symbolicExpressionTree.Depth + subtree.GetDepth()
78                                // find possible symbols for the node (also considering the existing branches below it)
79                                let allowedSymbols = (from symbol in parent.Grammar.GetAllowedSymbols(parent.Symbol, subtreeIndex)
80                                                      // do not replace symbol with the same symbol
81                                                      where symbol.Name != subtree.Symbol.Name
82                                                      where parent.Grammar.GetMinExpressionDepth(symbol) <= maxDepth
83                                                      where parent.Grammar.GetMinExpressionLength(symbol) <= maxLength
84                                                      select symbol)
85                                                      .ToList()
86                                where allowedSymbols.Count > 0
87                                select new {
88                                  Parent = parent,
89                                  Child = subtree,
90                                  Index = subtreeIndex,
91                                  AllowedSymbols = allowedSymbols,
92                                  MaxLength = maxLength,
93                                  MaxDepth = maxDepth
94                                })
95                               .ToList();
96
97      if (manipulationPoints.Count == 0) return;
98      var selectedManipulationPoint = manipulationPoints.SelectRandom(random);
99
100      var weights = selectedManipulationPoint.AllowedSymbols.Select(s => s.InitialFrequency).ToList();
101      var seedSymbol = selectedManipulationPoint.AllowedSymbols.SelectRandom(weights, random);
102      // replace the old node with the new node
103      var seedNode = seedSymbol.CreateTreeNode();
104      if (seedNode.HasLocalParameters)
105        seedNode.ResetLocalParameters(random);
106
107      selectedManipulationPoint.Parent.RemoveSubTree(selectedManipulationPoint.Index);
108      selectedManipulationPoint.Parent.InsertSubTree(selectedManipulationPoint.Index, seedNode);
109      seedNode = ProbabilisticTreeCreator.PTC2(random, seedNode, selectedManipulationPoint.MaxLength, selectedManipulationPoint.MaxDepth, 0, 0);
110    }
111  }
112}
Note: See TracBrowser for help on using the repository browser.