[4189] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[5445] | 3 | * Copyright (C) 2002-2011 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 |
|
---|
| 22 | using System.Linq;
|
---|
[4722] | 23 | using HeuristicLab.Common;
|
---|
[4189] | 24 | using HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.Data;
|
---|
[5686] | 26 | using HeuristicLab.Parameters;
|
---|
[4189] | 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 28 |
|
---|
[5499] | 29 | namespace 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
|
---|
[5686] | 80 | where parent.Grammar.GetMinimumExpressionDepth(symbol) <= maxDepth
|
---|
| 81 | where parent.Grammar.GetMinimumExpressionLength(symbol) <= maxLength
|
---|
[5569] | 82 | select symbol)
|
---|
| 83 | .ToList()
|
---|
| 84 | where allowedSymbols.Count > 0
|
---|
| 85 | select new {
|
---|
| 86 | Parent = parent,
|
---|
| 87 | Child = subtree,
|
---|
| 88 | Index = subtreeIndex,
|
---|
| 89 | AllowedSymbols = allowedSymbols,
|
---|
| 90 | MaxLength = maxLength,
|
---|
| 91 | MaxDepth = maxDepth
|
---|
| 92 | })
|
---|
| 93 | .ToList();
|
---|
[5014] | 94 |
|
---|
[5569] | 95 | if (manipulationPoints.Count == 0) return;
|
---|
| 96 | var selectedManipulationPoint = manipulationPoints.SelectRandom(random);
|
---|
| 97 |
|
---|
| 98 | var weights = selectedManipulationPoint.AllowedSymbols.Select(s => s.InitialFrequency).ToList();
|
---|
| 99 | var seedSymbol = selectedManipulationPoint.AllowedSymbols.SelectRandom(weights, random);
|
---|
[5499] | 100 | // replace the old node with the new node
|
---|
[4189] | 101 | var seedNode = seedSymbol.CreateTreeNode();
|
---|
| 102 | if (seedNode.HasLocalParameters)
|
---|
| 103 | seedNode.ResetLocalParameters(random);
|
---|
| 104 |
|
---|
[5733] | 105 | selectedManipulationPoint.Parent.RemoveSubtree(selectedManipulationPoint.Index);
|
---|
| 106 | selectedManipulationPoint.Parent.InsertSubtree(selectedManipulationPoint.Index, seedNode);
|
---|
[5686] | 107 | ProbabilisticTreeCreator.PTC2(random, seedNode, selectedManipulationPoint.MaxLength, selectedManipulationPoint.MaxDepth);
|
---|
[4189] | 108 | }
|
---|
| 109 | }
|
---|
| 110 | }
|
---|