Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Scheduling/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Manipulators/ReplaceBranchManipulation.cs @ 6479

Last change on this file since 6479 was 5925, checked in by gkronber, 14 years ago

#1418 fixed bugs in symbolic expression tree operators.

File size: 6.4 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.Linq;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Parameters;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
30  [StorableClass]
31  [Item("ReplaceBranchManipulation", "Selects a branch of the tree randomly and replaces it with a newly initialized branch (using PTC2).")]
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
52    [StorableConstructor]
53    private ReplaceBranchManipulation(bool deserializing) : base(deserializing) { }
54    private ReplaceBranchManipulation(ReplaceBranchManipulation original, Cloner cloner) : base(original, cloner) { }
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    }
60
61    public override IDeepCloneable Clone(Cloner cloner) {
62      return new ReplaceBranchManipulation(this, cloner);
63    }
64
65    protected override void Manipulate(IRandom random, ISymbolicExpressionTree symbolicExpressionTree) {
66      ReplaceRandomBranch(random, symbolicExpressionTree, MaximumSymbolicExpressionTreeLength.Value, MaximumSymbolicExpressionTreeDepth.Value);
67    }
68
69    public static void ReplaceRandomBranch(IRandom random, ISymbolicExpressionTree symbolicExpressionTree, int maxTreeLength, int maxTreeDepth) {
70      // select any node as parent (except the root node)
71      var manipulationPoints = (from parent in symbolicExpressionTree.Root.IterateNodesPrefix().Skip(1)
72                                from subtree in parent.Subtrees
73                                let subtreeIndex = parent.IndexOfSubtree(subtree)
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)
77                                let allowedSymbols = (from symbol in parent.Grammar.GetAllowedChildSymbols(parent.Symbol, subtreeIndex)
78                                                      // do not replace symbol with the same symbol
79                                                      where symbol.Name != subtree.Symbol.Name
80                                                      where symbol.InitialFrequency > 0
81                                                      where parent.Grammar.GetMinimumExpressionDepth(symbol) <= maxDepth
82                                                      where parent.Grammar.GetMinimumExpressionLength(symbol) <= maxLength
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();
95
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);
101      // replace the old node with the new node
102      var seedNode = seedSymbol.CreateTreeNode();
103      if (seedNode.HasLocalParameters)
104        seedNode.ResetLocalParameters(random);
105
106      selectedManipulationPoint.Parent.RemoveSubtree(selectedManipulationPoint.Index);
107      selectedManipulationPoint.Parent.InsertSubtree(selectedManipulationPoint.Index, seedNode);
108      ProbabilisticTreeCreator.PTC2(random, seedNode, selectedManipulationPoint.MaxLength, selectedManipulationPoint.MaxDepth);
109    }
110  }
111}
Note: See TracBrowser for help on using the repository browser.