Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.TimeSeries/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Manipulators/ReplaceBranchManipulation.cs @ 7624

Last change on this file since 7624 was 7268, checked in by gkronber, 13 years ago

#1081: merged r7214:7266 from trunk into time series branch.

File size: 6.4 KB
RevLine 
[4189]1#region License Information
2/* HeuristicLab
[7268]3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[4189]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.Linq;
[4722]23using HeuristicLab.Common;
[4189]24using HeuristicLab.Core;
25using HeuristicLab.Data;
[5686]26using HeuristicLab.Parameters;
[4189]27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
[5499]29namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
[4189]30  [StorableClass]
31  [Item("ReplaceBranchManipulation", "Selects a branch of the tree randomly and replaces it with a newly initialized branch (using PTC2).")]
[5499]32  public sealed class ReplaceBranchManipulation : SymbolicExpressionTreeManipulator, ISymbolicExpressionTreeSizeConstraintOperator {
33    private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength";
34    private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth";
35    #region Parameter Properties
36    public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter {
37      get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; }
38    }
39    public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter {
40      get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; }
41    }
42    #endregion
43    #region Properties
44    public IntValue MaximumSymbolicExpressionTreeLength {
45      get { return MaximumSymbolicExpressionTreeLengthParameter.ActualValue; }
46    }
47    public IntValue MaximumSymbolicExpressionTreeDepth {
48      get { return MaximumSymbolicExpressionTreeDepthParameter.ActualValue; }
49    }
50    #endregion
51
[4722]52    [StorableConstructor]
53    private ReplaceBranchManipulation(bool deserializing) : base(deserializing) { }
54    private ReplaceBranchManipulation(ReplaceBranchManipulation original, Cloner cloner) : base(original, cloner) { }
[5499]55    public ReplaceBranchManipulation()
56      : base() {
57      Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, "The maximal length (number of nodes) of the symbolic expression tree."));
58      Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0)."));
59    }
[4189]60
[4722]61    public override IDeepCloneable Clone(Cloner cloner) {
62      return new ReplaceBranchManipulation(this, cloner);
[4189]63    }
64
[5499]65    protected override void Manipulate(IRandom random, ISymbolicExpressionTree symbolicExpressionTree) {
66      ReplaceRandomBranch(random, symbolicExpressionTree, MaximumSymbolicExpressionTreeLength.Value, MaximumSymbolicExpressionTreeDepth.Value);
[4189]67    }
68
[5499]69    public static void ReplaceRandomBranch(IRandom random, ISymbolicExpressionTree symbolicExpressionTree, int maxTreeLength, int maxTreeDepth) {
[4189]70      // select any node as parent (except the root node)
[5569]71      var manipulationPoints = (from parent in symbolicExpressionTree.Root.IterateNodesPrefix().Skip(1)
[5733]72                                from subtree in parent.Subtrees
73                                let subtreeIndex = parent.IndexOfSubtree(subtree)
[5569]74                                let maxLength = maxTreeLength - symbolicExpressionTree.Length + subtree.GetLength()
75                                let maxDepth = maxTreeDepth - symbolicExpressionTree.Depth + subtree.GetDepth()
76                                // find possible symbols for the node (also considering the existing branches below it)
[5686]77                                let allowedSymbols = (from symbol in parent.Grammar.GetAllowedChildSymbols(parent.Symbol, subtreeIndex)
[5569]78                                                      // do not replace symbol with the same symbol
79                                                      where symbol.Name != subtree.Symbol.Name
[5925]80                                                      where symbol.InitialFrequency > 0
[6817]81                                                      where parent.Grammar.GetMinimumExpressionDepth(symbol) + 1 <= maxDepth
[5686]82                                                      where parent.Grammar.GetMinimumExpressionLength(symbol) <= maxLength
[5569]83                                                      select symbol)
84                                                      .ToList()
85                                where allowedSymbols.Count > 0
86                                select new {
87                                  Parent = parent,
88                                  Child = subtree,
89                                  Index = subtreeIndex,
90                                  AllowedSymbols = allowedSymbols,
91                                  MaxLength = maxLength,
92                                  MaxDepth = maxDepth
93                                })
94                               .ToList();
[5014]95
[5569]96      if (manipulationPoints.Count == 0) return;
97      var selectedManipulationPoint = manipulationPoints.SelectRandom(random);
98
99      var weights = selectedManipulationPoint.AllowedSymbols.Select(s => s.InitialFrequency).ToList();
100      var seedSymbol = selectedManipulationPoint.AllowedSymbols.SelectRandom(weights, random);
[5499]101      // replace the old node with the new node
[4189]102      var seedNode = seedSymbol.CreateTreeNode();
103      if (seedNode.HasLocalParameters)
104        seedNode.ResetLocalParameters(random);
105
[5733]106      selectedManipulationPoint.Parent.RemoveSubtree(selectedManipulationPoint.Index);
107      selectedManipulationPoint.Parent.InsertSubtree(selectedManipulationPoint.Index, seedNode);
[5686]108      ProbabilisticTreeCreator.PTC2(random, seedNode, selectedManipulationPoint.MaxLength, selectedManipulationPoint.MaxDepth);
[4189]109    }
110  }
111}
Note: See TracBrowser for help on using the repository browser.