[645] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[5445] | 3 | * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[645] | 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 |
|
---|
[4068] | 22 | using System;
|
---|
[645] | 23 | using System.Collections.Generic;
|
---|
[4068] | 24 | using System.Linq;
|
---|
[4722] | 25 | using HeuristicLab.Common;
|
---|
[645] | 26 | using HeuristicLab.Core;
|
---|
[3237] | 27 | using HeuristicLab.Data;
|
---|
| 28 | using HeuristicLab.Parameters;
|
---|
[4068] | 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[645] | 30 |
|
---|
[5499] | 31 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
|
---|
[3237] | 32 | /// <summary>
|
---|
| 33 | /// Takes two parent individuals P0 and P1 each. Selects a random node N0 of P0 and a random node N1 of P1.
|
---|
| 34 | /// And replaces the branch with root0 N0 in P0 with N1 from P1 if the tree-size limits are not violated.
|
---|
| 35 | /// When recombination with N0 and N1 would create a tree that is too large or invalid the operator randomly selects new N0 and N1
|
---|
| 36 | /// until a valid configuration is found.
|
---|
| 37 | /// </summary>
|
---|
| 38 | [Item("SubtreeCrossover", "An operator which performs subtree swapping crossover.")]
|
---|
| 39 | [StorableClass]
|
---|
[5499] | 40 | public sealed class SubtreeCrossover : SymbolicExpressionTreeCrossover, ISymbolicExpressionTreeSizeConstraintOperator {
|
---|
| 41 | private const string InternalCrossoverPointProbabilityParameterName = "InternalCrossoverPointProbability";
|
---|
| 42 | private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength";
|
---|
| 43 | private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth";
|
---|
| 44 | #region Parameter Properties
|
---|
[3237] | 45 | public IValueLookupParameter<PercentValue> InternalCrossoverPointProbabilityParameter {
|
---|
[5499] | 46 | get { return (IValueLookupParameter<PercentValue>)Parameters[InternalCrossoverPointProbabilityParameterName]; }
|
---|
[645] | 47 | }
|
---|
[5499] | 48 | public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter {
|
---|
| 49 | get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; }
|
---|
| 50 | }
|
---|
| 51 | public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter {
|
---|
| 52 | get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; }
|
---|
| 53 | }
|
---|
| 54 | #endregion
|
---|
| 55 | #region Properties
|
---|
| 56 | public PercentValue InternalCrossoverPointProbability {
|
---|
| 57 | get { return InternalCrossoverPointProbabilityParameter.ActualValue; }
|
---|
| 58 | }
|
---|
| 59 | public IntValue MaximumSymbolicExpressionTreeLength {
|
---|
| 60 | get { return MaximumSymbolicExpressionTreeLengthParameter.ActualValue; }
|
---|
| 61 | }
|
---|
| 62 | public IntValue MaximumSymbolicExpressionTreeDepth {
|
---|
| 63 | get { return MaximumSymbolicExpressionTreeDepthParameter.ActualValue; }
|
---|
| 64 | }
|
---|
| 65 | #endregion
|
---|
[4722] | 66 | [StorableConstructor]
|
---|
| 67 | private SubtreeCrossover(bool deserializing) : base(deserializing) { }
|
---|
| 68 | private SubtreeCrossover(SubtreeCrossover original, Cloner cloner) : base(original, cloner) { }
|
---|
[3237] | 69 | public SubtreeCrossover()
|
---|
| 70 | : base() {
|
---|
[5499] | 71 | Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, "The maximal length (number of nodes) of the symbolic expression tree."));
|
---|
| 72 | Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0)."));
|
---|
| 73 | Parameters.Add(new ValueLookupParameter<PercentValue>(InternalCrossoverPointProbabilityParameterName, "The probability to select an internal crossover point (instead of a leaf node).", new PercentValue(0.9)));
|
---|
[3237] | 74 | }
|
---|
| 75 |
|
---|
[4722] | 76 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 77 | return new SubtreeCrossover(this, cloner);
|
---|
| 78 | }
|
---|
| 79 |
|
---|
[5510] | 80 | protected override ISymbolicExpressionTree Cross(IRandom random,
|
---|
| 81 | ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1) {
|
---|
[5499] | 82 | return Cross(random, parent0, parent1, InternalCrossoverPointProbability.Value,
|
---|
| 83 | MaximumSymbolicExpressionTreeLength.Value, MaximumSymbolicExpressionTreeDepth.Value);
|
---|
[3237] | 84 | }
|
---|
| 85 |
|
---|
[5510] | 86 | public static ISymbolicExpressionTree Cross(IRandom random,
|
---|
| 87 | ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1,
|
---|
[5499] | 88 | double internalCrossoverPointProbability, int maxTreeSize, int maxTreeHeight) {
|
---|
[3294] | 89 | // select a random crossover point in the first parent
|
---|
[5510] | 90 | ISymbolicExpressionTreeNode crossoverPoint0;
|
---|
[3294] | 91 | int replacedSubtreeIndex;
|
---|
[5014] | 92 | SelectCrossoverPoint(random, parent0, internalCrossoverPointProbability, maxTreeSize, maxTreeHeight, out crossoverPoint0, out replacedSubtreeIndex);
|
---|
[645] | 93 |
|
---|
[3294] | 94 | // calculate the max size and height that the inserted branch can have
|
---|
[5510] | 95 | int maxInsertedBranchSize = maxTreeSize - (parent0.Size - crossoverPoint0.GetSubTree(replacedSubtreeIndex).GetSize());
|
---|
[3294] | 96 | int maxInsertedBranchHeight = maxTreeHeight - GetBranchLevel(parent0.Root, crossoverPoint0);
|
---|
[645] | 97 |
|
---|
[5510] | 98 | List<ISymbolicExpressionTreeNode> allowedBranches = new List<ISymbolicExpressionTreeNode>();
|
---|
[3997] | 99 | parent1.Root.ForEachNodePostfix((n) => {
|
---|
[5367] | 100 | if (n.GetSize() <= maxInsertedBranchSize &&
|
---|
| 101 | n.GetHeight() <= maxInsertedBranchHeight &&
|
---|
[3997] | 102 | IsMatchingPointType(crossoverPoint0, replacedSubtreeIndex, n))
|
---|
| 103 | allowedBranches.Add(n);
|
---|
| 104 | });
|
---|
[645] | 105 |
|
---|
[3997] | 106 | if (allowedBranches.Count == 0) {
|
---|
[3297] | 107 | return parent0;
|
---|
| 108 | } else {
|
---|
[3294] | 109 | var selectedBranch = SelectRandomBranch(random, allowedBranches, internalCrossoverPointProbability);
|
---|
[645] | 110 |
|
---|
[3294] | 111 | // manipulate the tree of parent0 in place
|
---|
| 112 | // replace the branch in tree0 with the selected branch from tree1
|
---|
| 113 | crossoverPoint0.RemoveSubTree(replacedSubtreeIndex);
|
---|
| 114 | crossoverPoint0.InsertSubTree(replacedSubtreeIndex, selectedBranch);
|
---|
| 115 | return parent0;
|
---|
[645] | 116 | }
|
---|
| 117 | }
|
---|
| 118 |
|
---|
[5510] | 119 | private static bool IsMatchingPointType(ISymbolicExpressionTreeNode parent, int replacedSubtreeIndex, ISymbolicExpressionTreeNode branch) {
|
---|
[3338] | 120 | // check syntax constraints of direct parent - child relation
|
---|
[4106] | 121 | if (!parent.Grammar.ContainsSymbol(branch.Symbol) ||
|
---|
| 122 | !parent.Grammar.IsAllowedChild(parent.Symbol, branch.Symbol, replacedSubtreeIndex)) return false;
|
---|
[3338] | 123 |
|
---|
[3997] | 124 | bool result = true;
|
---|
| 125 | // check point type for the whole branch
|
---|
| 126 | branch.ForEachNodePostfix((n) => {
|
---|
[3998] | 127 | result =
|
---|
| 128 | result &&
|
---|
[4106] | 129 | parent.Grammar.ContainsSymbol(n.Symbol) &&
|
---|
[5510] | 130 | n.SubTrees.Count() >= parent.Grammar.GetMinSubtreeCount(n.Symbol) &&
|
---|
| 131 | n.SubTrees.Count() <= parent.Grammar.GetMaxSubtreeCount(n.Symbol);
|
---|
[3997] | 132 | });
|
---|
| 133 | return result;
|
---|
[3294] | 134 | }
|
---|
| 135 |
|
---|
[5510] | 136 | private static void SelectCrossoverPoint(IRandom random, ISymbolicExpressionTree parent0, double internalNodeProbability, int maxBranchSize, int maxBranchHeight, out ISymbolicExpressionTreeNode crossoverPoint, out int subtreeIndex) {
|
---|
[3997] | 137 | if (internalNodeProbability < 0.0 || internalNodeProbability > 1.0) throw new ArgumentException("internalNodeProbability");
|
---|
| 138 | List<CrossoverPoint> internalCrossoverPoints = new List<CrossoverPoint>();
|
---|
| 139 | List<CrossoverPoint> leafCrossoverPoints = new List<CrossoverPoint>();
|
---|
| 140 | parent0.Root.ForEachNodePostfix((n) => {
|
---|
[5510] | 141 | if (n.SubTrees.Count() > 0 && n != parent0.Root) {
|
---|
[3997] | 142 | foreach (var child in n.SubTrees) {
|
---|
[5367] | 143 | if (child.GetSize() <= maxBranchSize &&
|
---|
| 144 | child.GetHeight() <= maxBranchHeight) {
|
---|
[5510] | 145 | if (child.SubTrees.Count() > 0)
|
---|
[5367] | 146 | internalCrossoverPoints.Add(new CrossoverPoint(n, child));
|
---|
| 147 | else
|
---|
| 148 | leafCrossoverPoints.Add(new CrossoverPoint(n, child));
|
---|
| 149 | }
|
---|
[3997] | 150 | }
|
---|
| 151 | }
|
---|
| 152 | });
|
---|
[5367] | 153 |
|
---|
[3997] | 154 | if (random.NextDouble() < internalNodeProbability) {
|
---|
| 155 | // select from internal node if possible
|
---|
| 156 | if (internalCrossoverPoints.Count > 0) {
|
---|
| 157 | // select internal crossover point or leaf
|
---|
| 158 | var selectedCrossoverPoint = internalCrossoverPoints[random.Next(internalCrossoverPoints.Count)];
|
---|
| 159 | crossoverPoint = selectedCrossoverPoint.Parent;
|
---|
| 160 | subtreeIndex = selectedCrossoverPoint.SubtreeIndex;
|
---|
| 161 | } else {
|
---|
| 162 | // otherwise select external node
|
---|
| 163 | var selectedCrossoverPoint = leafCrossoverPoints[random.Next(leafCrossoverPoints.Count)];
|
---|
| 164 | crossoverPoint = selectedCrossoverPoint.Parent;
|
---|
| 165 | subtreeIndex = selectedCrossoverPoint.SubtreeIndex;
|
---|
| 166 | }
|
---|
| 167 | } else if (leafCrossoverPoints.Count > 0) {
|
---|
| 168 | // select from leaf crossover point if possible
|
---|
[3294] | 169 | var selectedCrossoverPoint = leafCrossoverPoints[random.Next(leafCrossoverPoints.Count)];
|
---|
[3997] | 170 | crossoverPoint = selectedCrossoverPoint.Parent;
|
---|
[3294] | 171 | subtreeIndex = selectedCrossoverPoint.SubtreeIndex;
|
---|
[3997] | 172 | } else {
|
---|
| 173 | // otherwise select internal crossover point
|
---|
[3237] | 174 | var selectedCrossoverPoint = internalCrossoverPoints[random.Next(internalCrossoverPoints.Count)];
|
---|
[3997] | 175 | crossoverPoint = selectedCrossoverPoint.Parent;
|
---|
[3237] | 176 | subtreeIndex = selectedCrossoverPoint.SubtreeIndex;
|
---|
[645] | 177 | }
|
---|
| 178 | }
|
---|
[3237] | 179 |
|
---|
[5510] | 180 | private static ISymbolicExpressionTreeNode SelectRandomBranch(IRandom random, IEnumerable<ISymbolicExpressionTreeNode> branches, double internalNodeProbability) {
|
---|
[3237] | 181 | if (internalNodeProbability < 0.0 || internalNodeProbability > 1.0) throw new ArgumentException("internalNodeProbability");
|
---|
[5510] | 182 | List<ISymbolicExpressionTreeNode> allowedInternalBranches;
|
---|
| 183 | List<ISymbolicExpressionTreeNode> allowedLeafBranches;
|
---|
[3997] | 184 | if (random.NextDouble() < internalNodeProbability) {
|
---|
| 185 | // select internal node if possible
|
---|
| 186 | allowedInternalBranches = (from branch in branches
|
---|
[5510] | 187 | where branch.SubTrees.Count() > 0
|
---|
[3997] | 188 | select branch).ToList();
|
---|
| 189 | if (allowedInternalBranches.Count > 0) {
|
---|
| 190 | return allowedInternalBranches.SelectRandom(random);
|
---|
| 191 | } else {
|
---|
| 192 | // no internal nodes allowed => select leaf nodes
|
---|
| 193 | allowedLeafBranches = (from branch in branches
|
---|
[5510] | 194 | where branch.SubTrees.Count() == 0
|
---|
[3989] | 195 | select branch).ToList();
|
---|
[3997] | 196 | return allowedLeafBranches.SelectRandom(random);
|
---|
| 197 | }
|
---|
[3237] | 198 | } else {
|
---|
[3997] | 199 | // select leaf node if possible
|
---|
| 200 | allowedLeafBranches = (from branch in branches
|
---|
[5510] | 201 | where branch.SubTrees.Count() == 0
|
---|
[3997] | 202 | select branch).ToList();
|
---|
| 203 | if (allowedLeafBranches.Count > 0) {
|
---|
| 204 | return allowedLeafBranches.SelectRandom(random);
|
---|
| 205 | } else {
|
---|
| 206 | allowedInternalBranches = (from branch in branches
|
---|
[5510] | 207 | where branch.SubTrees.Count() > 0
|
---|
[3997] | 208 | select branch).ToList();
|
---|
| 209 | return allowedInternalBranches.SelectRandom(random);
|
---|
| 210 | }
|
---|
[3237] | 211 | }
|
---|
| 212 | }
|
---|
| 213 |
|
---|
[5499] | 214 | private static int GetBranchLevel(ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode point) {
|
---|
[3237] | 215 | if (root == point) return 0;
|
---|
| 216 | foreach (var subtree in root.SubTrees) {
|
---|
| 217 | int branchLevel = GetBranchLevel(subtree, point);
|
---|
| 218 | if (branchLevel < int.MaxValue) return 1 + branchLevel;
|
---|
| 219 | }
|
---|
| 220 | return int.MaxValue;
|
---|
| 221 | }
|
---|
[645] | 222 | }
|
---|
| 223 | }
|
---|