[4189] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[16565] | 3 | * Copyright (C) 2002-2019 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 |
|
---|
[12422] | 22 | using System.Collections.Generic;
|
---|
[4189] | 23 | using System.Linq;
|
---|
[4722] | 24 | using HeuristicLab.Common;
|
---|
[4189] | 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
[5686] | 27 | using HeuristicLab.Parameters;
|
---|
[16565] | 28 | using HEAL.Attic;
|
---|
[4189] | 29 |
|
---|
[5499] | 30 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
|
---|
[16565] | 31 | [StorableType("92286134-0520-4A1E-B8EE-01AFAD55C3AE")]
|
---|
[4189] | 32 | [Item("ReplaceBranchManipulation", "Selects a branch of the tree randomly and replaces it with a newly initialized branch (using PTC2).")]
|
---|
[5499] | 33 | public sealed class ReplaceBranchManipulation : SymbolicExpressionTreeManipulator, ISymbolicExpressionTreeSizeConstraintOperator {
|
---|
[7662] | 34 | private const int MAX_TRIES = 100;
|
---|
[5499] | 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 |
|
---|
[4722] | 54 | [StorableConstructor]
|
---|
[16565] | 55 | private ReplaceBranchManipulation(StorableConstructorFlag _) : base(_) { }
|
---|
[4722] | 56 | private ReplaceBranchManipulation(ReplaceBranchManipulation original, Cloner cloner) : base(original, cloner) { }
|
---|
[5499] | 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 | }
|
---|
[4189] | 62 |
|
---|
[4722] | 63 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 64 | return new ReplaceBranchManipulation(this, cloner);
|
---|
[4189] | 65 | }
|
---|
| 66 |
|
---|
[5499] | 67 | protected override void Manipulate(IRandom random, ISymbolicExpressionTree symbolicExpressionTree) {
|
---|
| 68 | ReplaceRandomBranch(random, symbolicExpressionTree, MaximumSymbolicExpressionTreeLength.Value, MaximumSymbolicExpressionTreeDepth.Value);
|
---|
[4189] | 69 | }
|
---|
| 70 |
|
---|
[5499] | 71 | public static void ReplaceRandomBranch(IRandom random, ISymbolicExpressionTree symbolicExpressionTree, int maxTreeLength, int maxTreeDepth) {
|
---|
[7662] | 72 | var allowedSymbols = new List<ISymbol>();
|
---|
| 73 | ISymbolicExpressionTreeNode parent;
|
---|
| 74 | int childIndex;
|
---|
| 75 | int maxLength;
|
---|
| 76 | int maxDepth;
|
---|
| 77 | // repeat until a fitting parent and child are found (MAX_TRIES times)
|
---|
| 78 | int tries = 0;
|
---|
| 79 | do {
|
---|
[12422] | 80 | #pragma warning disable 612, 618
|
---|
[7662] | 81 | parent = symbolicExpressionTree.Root.IterateNodesPrefix().Skip(1).Where(n => n.SubtreeCount > 0).SelectRandom(random);
|
---|
[12422] | 82 | #pragma warning restore 612, 618
|
---|
| 83 |
|
---|
[7662] | 84 | childIndex = random.Next(parent.SubtreeCount);
|
---|
| 85 | var child = parent.GetSubtree(childIndex);
|
---|
| 86 | maxLength = maxTreeLength - symbolicExpressionTree.Length + child.GetLength();
|
---|
| 87 | maxDepth = maxTreeDepth - symbolicExpressionTree.Depth + child.GetDepth();
|
---|
[5014] | 88 |
|
---|
[7662] | 89 | allowedSymbols.Clear();
|
---|
| 90 | foreach (var symbol in parent.Grammar.GetAllowedChildSymbols(parent.Symbol, childIndex)) {
|
---|
| 91 | // check basic properties that the new symbol must have
|
---|
| 92 | if (symbol.Name != child.Symbol.Name &&
|
---|
| 93 | symbol.InitialFrequency > 0 &&
|
---|
| 94 | parent.Grammar.GetMinimumExpressionDepth(symbol) + 1 <= maxDepth &&
|
---|
| 95 | parent.Grammar.GetMinimumExpressionLength(symbol) <= maxLength) {
|
---|
| 96 | allowedSymbols.Add(symbol);
|
---|
| 97 | }
|
---|
| 98 | }
|
---|
| 99 | tries++;
|
---|
| 100 | } while (tries < MAX_TRIES && allowedSymbols.Count == 0);
|
---|
[5569] | 101 |
|
---|
[7662] | 102 | if (tries < MAX_TRIES) {
|
---|
| 103 | var weights = allowedSymbols.Select(s => s.InitialFrequency).ToList();
|
---|
[12422] | 104 | #pragma warning disable 612, 618
|
---|
[7662] | 105 | var seedSymbol = allowedSymbols.SelectRandom(weights, random);
|
---|
[12422] | 106 | #pragma warning restore 612, 618
|
---|
| 107 |
|
---|
[7662] | 108 | // replace the old node with the new node
|
---|
| 109 | var seedNode = seedSymbol.CreateTreeNode();
|
---|
| 110 | if (seedNode.HasLocalParameters)
|
---|
| 111 | seedNode.ResetLocalParameters(random);
|
---|
[4189] | 112 |
|
---|
[7662] | 113 | parent.RemoveSubtree(childIndex);
|
---|
| 114 | parent.InsertSubtree(childIndex, seedNode);
|
---|
| 115 | ProbabilisticTreeCreator.PTC2(random, seedNode, maxLength, maxDepth);
|
---|
| 116 | }
|
---|
[4189] | 117 | }
|
---|
| 118 | }
|
---|
| 119 | }
|
---|