[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,
|
---|
[5549] | 88 | double internalCrossoverPointProbability, int maxTreeLength, int maxTreeDepth) {
|
---|
[3294] | 89 | // select a random crossover point in the first parent
|
---|
[5916] | 90 | CutPoint crossoverPoint0;
|
---|
| 91 | SelectCrossoverPoint(random, parent0, internalCrossoverPointProbability, maxTreeLength, maxTreeDepth, out crossoverPoint0);
|
---|
[645] | 92 |
|
---|
[5916] | 93 | int childLength = crossoverPoint0.Child != null ? crossoverPoint0.Child.GetLength() : 0;
|
---|
[5549] | 94 | // calculate the max length and depth that the inserted branch can have
|
---|
[5916] | 95 | int maxInsertedBranchLength = maxTreeLength - (parent0.Length - childLength);
|
---|
| 96 | int maxInsertedBranchDepth = maxTreeDepth - GetBranchLevel(parent0.Root, crossoverPoint0.Parent);
|
---|
[645] | 97 |
|
---|
[5916] | 98 |
|
---|
[5510] | 99 | List<ISymbolicExpressionTreeNode> allowedBranches = new List<ISymbolicExpressionTreeNode>();
|
---|
[3997] | 100 | parent1.Root.ForEachNodePostfix((n) => {
|
---|
[5549] | 101 | if (n.GetLength() <= maxInsertedBranchLength &&
|
---|
| 102 | n.GetDepth() <= maxInsertedBranchDepth &&
|
---|
[5916] | 103 | IsMatchingPointType(crossoverPoint0, n))
|
---|
[3997] | 104 | allowedBranches.Add(n);
|
---|
| 105 | });
|
---|
[5916] | 106 | // empty branch
|
---|
| 107 | if (IsMatchingPointType(crossoverPoint0, null)) allowedBranches.Add(null);
|
---|
[645] | 108 |
|
---|
[3997] | 109 | if (allowedBranches.Count == 0) {
|
---|
[3297] | 110 | return parent0;
|
---|
| 111 | } else {
|
---|
[3294] | 112 | var selectedBranch = SelectRandomBranch(random, allowedBranches, internalCrossoverPointProbability);
|
---|
[645] | 113 |
|
---|
[5916] | 114 | if (crossoverPoint0.Child != null) {
|
---|
| 115 | // manipulate the tree of parent0 in place
|
---|
| 116 | // replace the branch in tree0 with the selected branch from tree1
|
---|
| 117 | crossoverPoint0.Parent.RemoveSubtree(crossoverPoint0.ChildIndex);
|
---|
| 118 | if (selectedBranch != null) {
|
---|
| 119 | crossoverPoint0.Parent.InsertSubtree(crossoverPoint0.ChildIndex, selectedBranch);
|
---|
| 120 | }
|
---|
| 121 | } else {
|
---|
| 122 | // child is null (additional child should be added under the parent)
|
---|
| 123 | if (selectedBranch != null) {
|
---|
| 124 | crossoverPoint0.Parent.AddSubtree(selectedBranch);
|
---|
| 125 | }
|
---|
| 126 | }
|
---|
[3294] | 127 | return parent0;
|
---|
[645] | 128 | }
|
---|
| 129 | }
|
---|
| 130 |
|
---|
[5916] | 131 | private static bool IsMatchingPointType(CutPoint cutPoint, ISymbolicExpressionTreeNode newChild) {
|
---|
| 132 | var parent = cutPoint.Parent;
|
---|
| 133 | if (newChild == null) {
|
---|
| 134 | // make sure that one subtree can be removed and that only the last subtree is removed
|
---|
| 135 | return parent.Grammar.GetMinimumSubtreeCount(parent.Symbol) < parent.SubtreesCount &&
|
---|
| 136 | cutPoint.ChildIndex == parent.SubtreesCount - 1;
|
---|
| 137 | } else {
|
---|
| 138 | // check syntax constraints of direct parent - child relation
|
---|
| 139 | if (!parent.Grammar.ContainsSymbol(newChild.Symbol) ||
|
---|
| 140 | !parent.Grammar.IsAllowedChildSymbol(parent.Symbol, newChild.Symbol, cutPoint.ChildIndex)) return false;
|
---|
[3338] | 141 |
|
---|
[5916] | 142 | bool result = true;
|
---|
| 143 | // check point type for the whole branch
|
---|
| 144 | newChild.ForEachNodePostfix((n) => {
|
---|
| 145 | result =
|
---|
| 146 | result &&
|
---|
| 147 | parent.Grammar.ContainsSymbol(n.Symbol) &&
|
---|
| 148 | n.SubtreesCount >= parent.Grammar.GetMinimumSubtreeCount(n.Symbol) &&
|
---|
| 149 | n.SubtreesCount <= parent.Grammar.GetMaximumSubtreeCount(n.Symbol);
|
---|
| 150 | });
|
---|
| 151 | return result;
|
---|
| 152 | }
|
---|
[3294] | 153 | }
|
---|
| 154 |
|
---|
[5916] | 155 | private static void SelectCrossoverPoint(IRandom random, ISymbolicExpressionTree parent0, double internalNodeProbability, int maxBranchLength, int maxBranchDepth, out CutPoint crossoverPoint) {
|
---|
[3997] | 156 | if (internalNodeProbability < 0.0 || internalNodeProbability > 1.0) throw new ArgumentException("internalNodeProbability");
|
---|
[5686] | 157 | List<CutPoint> internalCrossoverPoints = new List<CutPoint>();
|
---|
| 158 | List<CutPoint> leafCrossoverPoints = new List<CutPoint>();
|
---|
[3997] | 159 | parent0.Root.ForEachNodePostfix((n) => {
|
---|
[5733] | 160 | if (n.Subtrees.Any() && n != parent0.Root) {
|
---|
| 161 | foreach (var child in n.Subtrees) {
|
---|
[5549] | 162 | if (child.GetLength() <= maxBranchLength &&
|
---|
| 163 | child.GetDepth() <= maxBranchDepth) {
|
---|
[5733] | 164 | if (child.Subtrees.Any())
|
---|
[5686] | 165 | internalCrossoverPoints.Add(new CutPoint(n, child));
|
---|
[5367] | 166 | else
|
---|
[5686] | 167 | leafCrossoverPoints.Add(new CutPoint(n, child));
|
---|
[5367] | 168 | }
|
---|
[3997] | 169 | }
|
---|
[5916] | 170 | // add one additional extension point if the number of sub trees for the symbol is not full
|
---|
| 171 | if (n.SubtreesCount < n.Grammar.GetMaximumSubtreeCount(n.Symbol)) {
|
---|
| 172 | // empty extension point
|
---|
| 173 | internalCrossoverPoints.Add(new CutPoint(n, n.SubtreesCount));
|
---|
| 174 | }
|
---|
[3997] | 175 | }
|
---|
| 176 | });
|
---|
[5367] | 177 |
|
---|
[3997] | 178 | if (random.NextDouble() < internalNodeProbability) {
|
---|
| 179 | // select from internal node if possible
|
---|
| 180 | if (internalCrossoverPoints.Count > 0) {
|
---|
| 181 | // select internal crossover point or leaf
|
---|
[5916] | 182 | crossoverPoint = internalCrossoverPoints[random.Next(internalCrossoverPoints.Count)];
|
---|
[3997] | 183 | } else {
|
---|
| 184 | // otherwise select external node
|
---|
[5916] | 185 | crossoverPoint = leafCrossoverPoints[random.Next(leafCrossoverPoints.Count)];
|
---|
[3997] | 186 | }
|
---|
| 187 | } else if (leafCrossoverPoints.Count > 0) {
|
---|
| 188 | // select from leaf crossover point if possible
|
---|
[5916] | 189 | crossoverPoint = leafCrossoverPoints[random.Next(leafCrossoverPoints.Count)];
|
---|
[3997] | 190 | } else {
|
---|
| 191 | // otherwise select internal crossover point
|
---|
[5916] | 192 | crossoverPoint = internalCrossoverPoints[random.Next(internalCrossoverPoints.Count)];
|
---|
[645] | 193 | }
|
---|
| 194 | }
|
---|
[3237] | 195 |
|
---|
[5510] | 196 | private static ISymbolicExpressionTreeNode SelectRandomBranch(IRandom random, IEnumerable<ISymbolicExpressionTreeNode> branches, double internalNodeProbability) {
|
---|
[3237] | 197 | if (internalNodeProbability < 0.0 || internalNodeProbability > 1.0) throw new ArgumentException("internalNodeProbability");
|
---|
[5510] | 198 | List<ISymbolicExpressionTreeNode> allowedInternalBranches;
|
---|
| 199 | List<ISymbolicExpressionTreeNode> allowedLeafBranches;
|
---|
[3997] | 200 | if (random.NextDouble() < internalNodeProbability) {
|
---|
| 201 | // select internal node if possible
|
---|
| 202 | allowedInternalBranches = (from branch in branches
|
---|
[5916] | 203 | where branch != null && branch.Subtrees.Any()
|
---|
[3997] | 204 | select branch).ToList();
|
---|
| 205 | if (allowedInternalBranches.Count > 0) {
|
---|
| 206 | return allowedInternalBranches.SelectRandom(random);
|
---|
| 207 | } else {
|
---|
| 208 | // no internal nodes allowed => select leaf nodes
|
---|
| 209 | allowedLeafBranches = (from branch in branches
|
---|
[5916] | 210 | where branch == null || !branch.Subtrees.Any()
|
---|
[3989] | 211 | select branch).ToList();
|
---|
[3997] | 212 | return allowedLeafBranches.SelectRandom(random);
|
---|
| 213 | }
|
---|
[3237] | 214 | } else {
|
---|
[3997] | 215 | // select leaf node if possible
|
---|
| 216 | allowedLeafBranches = (from branch in branches
|
---|
[5916] | 217 | where branch == null || !branch.Subtrees.Any()
|
---|
[3997] | 218 | select branch).ToList();
|
---|
| 219 | if (allowedLeafBranches.Count > 0) {
|
---|
| 220 | return allowedLeafBranches.SelectRandom(random);
|
---|
| 221 | } else {
|
---|
| 222 | allowedInternalBranches = (from branch in branches
|
---|
[5916] | 223 | where branch != null && branch.Subtrees.Any()
|
---|
[3997] | 224 | select branch).ToList();
|
---|
| 225 | return allowedInternalBranches.SelectRandom(random);
|
---|
| 226 | }
|
---|
[3237] | 227 | }
|
---|
| 228 | }
|
---|
| 229 |
|
---|
[5499] | 230 | private static int GetBranchLevel(ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode point) {
|
---|
[3237] | 231 | if (root == point) return 0;
|
---|
[5733] | 232 | foreach (var subtree in root.Subtrees) {
|
---|
[3237] | 233 | int branchLevel = GetBranchLevel(subtree, point);
|
---|
| 234 | if (branchLevel < int.MaxValue) return 1 + branchLevel;
|
---|
| 235 | }
|
---|
| 236 | return int.MaxValue;
|
---|
| 237 | }
|
---|
[645] | 238 | }
|
---|
| 239 | }
|
---|