[645] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12009] | 3 | * Copyright (C) 2002-2015 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>
|
---|
[7506] | 38 | [Item("SubtreeSwappingCrossover", "An operator which performs subtree swapping crossover.")]
|
---|
[3237] | 39 | [StorableClass]
|
---|
[7506] | 40 | public class SubtreeCrossover : SymbolicExpressionTreeCrossover, ISymbolicExpressionTreeSizeConstraintOperator {
|
---|
[5499] | 41 | private const string InternalCrossoverPointProbabilityParameterName = "InternalCrossoverPointProbability";
|
---|
| 42 | private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength";
|
---|
| 43 | private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth";
|
---|
[7506] | 44 |
|
---|
[5499] | 45 | #region Parameter Properties
|
---|
[3237] | 46 | public IValueLookupParameter<PercentValue> InternalCrossoverPointProbabilityParameter {
|
---|
[5499] | 47 | get { return (IValueLookupParameter<PercentValue>)Parameters[InternalCrossoverPointProbabilityParameterName]; }
|
---|
[645] | 48 | }
|
---|
[5499] | 49 | public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter {
|
---|
| 50 | get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; }
|
---|
| 51 | }
|
---|
| 52 | public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter {
|
---|
| 53 | get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; }
|
---|
| 54 | }
|
---|
| 55 | #endregion
|
---|
| 56 | #region Properties
|
---|
| 57 | public PercentValue InternalCrossoverPointProbability {
|
---|
| 58 | get { return InternalCrossoverPointProbabilityParameter.ActualValue; }
|
---|
| 59 | }
|
---|
| 60 | public IntValue MaximumSymbolicExpressionTreeLength {
|
---|
| 61 | get { return MaximumSymbolicExpressionTreeLengthParameter.ActualValue; }
|
---|
| 62 | }
|
---|
| 63 | public IntValue MaximumSymbolicExpressionTreeDepth {
|
---|
| 64 | get { return MaximumSymbolicExpressionTreeDepthParameter.ActualValue; }
|
---|
| 65 | }
|
---|
| 66 | #endregion
|
---|
[4722] | 67 | [StorableConstructor]
|
---|
[7506] | 68 | protected SubtreeCrossover(bool deserializing) : base(deserializing) { }
|
---|
| 69 | protected SubtreeCrossover(SubtreeCrossover original, Cloner cloner) : base(original, cloner) { }
|
---|
[3237] | 70 | public SubtreeCrossover()
|
---|
| 71 | : base() {
|
---|
[5499] | 72 | Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, "The maximal length (number of nodes) of the symbolic expression tree."));
|
---|
| 73 | Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0)."));
|
---|
| 74 | Parameters.Add(new ValueLookupParameter<PercentValue>(InternalCrossoverPointProbabilityParameterName, "The probability to select an internal crossover point (instead of a leaf node).", new PercentValue(0.9)));
|
---|
[3237] | 75 | }
|
---|
| 76 |
|
---|
[4722] | 77 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 78 | return new SubtreeCrossover(this, cloner);
|
---|
| 79 | }
|
---|
| 80 |
|
---|
[7506] | 81 | public override ISymbolicExpressionTree Crossover(IRandom random,
|
---|
[5510] | 82 | ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1) {
|
---|
[5499] | 83 | return Cross(random, parent0, parent1, InternalCrossoverPointProbability.Value,
|
---|
| 84 | MaximumSymbolicExpressionTreeLength.Value, MaximumSymbolicExpressionTreeDepth.Value);
|
---|
[3237] | 85 | }
|
---|
| 86 |
|
---|
[5510] | 87 | public static ISymbolicExpressionTree Cross(IRandom random,
|
---|
| 88 | ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1,
|
---|
[5549] | 89 | double internalCrossoverPointProbability, int maxTreeLength, int maxTreeDepth) {
|
---|
[3294] | 90 | // select a random crossover point in the first parent
|
---|
[5916] | 91 | CutPoint crossoverPoint0;
|
---|
| 92 | SelectCrossoverPoint(random, parent0, internalCrossoverPointProbability, maxTreeLength, maxTreeDepth, out crossoverPoint0);
|
---|
[645] | 93 |
|
---|
[5916] | 94 | int childLength = crossoverPoint0.Child != null ? crossoverPoint0.Child.GetLength() : 0;
|
---|
[5549] | 95 | // calculate the max length and depth that the inserted branch can have
|
---|
[5916] | 96 | int maxInsertedBranchLength = maxTreeLength - (parent0.Length - childLength);
|
---|
[7506] | 97 | int maxInsertedBranchDepth = maxTreeDepth - parent0.Root.GetBranchLevel(crossoverPoint0.Parent);
|
---|
[645] | 98 |
|
---|
[5510] | 99 | List<ISymbolicExpressionTreeNode> allowedBranches = new List<ISymbolicExpressionTreeNode>();
|
---|
[3997] | 100 | parent1.Root.ForEachNodePostfix((n) => {
|
---|
[5549] | 101 | if (n.GetLength() <= maxInsertedBranchLength &&
|
---|
[7506] | 102 | n.GetDepth() <= maxInsertedBranchDepth && crossoverPoint0.IsMatchingPointType(n))
|
---|
[3997] | 103 | allowedBranches.Add(n);
|
---|
| 104 | });
|
---|
[5916] | 105 | // empty branch
|
---|
[7506] | 106 | if (crossoverPoint0.IsMatchingPointType(null)) allowedBranches.Add(null);
|
---|
[645] | 107 |
|
---|
[3997] | 108 | if (allowedBranches.Count == 0) {
|
---|
[3297] | 109 | return parent0;
|
---|
| 110 | } else {
|
---|
[3294] | 111 | var selectedBranch = SelectRandomBranch(random, allowedBranches, internalCrossoverPointProbability);
|
---|
[645] | 112 |
|
---|
[5916] | 113 | if (crossoverPoint0.Child != null) {
|
---|
| 114 | // manipulate the tree of parent0 in place
|
---|
| 115 | // replace the branch in tree0 with the selected branch from tree1
|
---|
| 116 | crossoverPoint0.Parent.RemoveSubtree(crossoverPoint0.ChildIndex);
|
---|
| 117 | if (selectedBranch != null) {
|
---|
| 118 | crossoverPoint0.Parent.InsertSubtree(crossoverPoint0.ChildIndex, selectedBranch);
|
---|
| 119 | }
|
---|
| 120 | } else {
|
---|
| 121 | // child is null (additional child should be added under the parent)
|
---|
| 122 | if (selectedBranch != null) {
|
---|
| 123 | crossoverPoint0.Parent.AddSubtree(selectedBranch);
|
---|
| 124 | }
|
---|
| 125 | }
|
---|
[3294] | 126 | return parent0;
|
---|
[645] | 127 | }
|
---|
| 128 | }
|
---|
| 129 |
|
---|
[5916] | 130 | private static void SelectCrossoverPoint(IRandom random, ISymbolicExpressionTree parent0, double internalNodeProbability, int maxBranchLength, int maxBranchDepth, out CutPoint crossoverPoint) {
|
---|
[3997] | 131 | if (internalNodeProbability < 0.0 || internalNodeProbability > 1.0) throw new ArgumentException("internalNodeProbability");
|
---|
[5686] | 132 | List<CutPoint> internalCrossoverPoints = new List<CutPoint>();
|
---|
| 133 | List<CutPoint> leafCrossoverPoints = new List<CutPoint>();
|
---|
[3997] | 134 | parent0.Root.ForEachNodePostfix((n) => {
|
---|
[7506] | 135 | if (n.SubtreeCount > 0 && n != parent0.Root) {
|
---|
[5733] | 136 | foreach (var child in n.Subtrees) {
|
---|
[5549] | 137 | if (child.GetLength() <= maxBranchLength &&
|
---|
| 138 | child.GetDepth() <= maxBranchDepth) {
|
---|
[7506] | 139 | if (child.SubtreeCount > 0)
|
---|
[5686] | 140 | internalCrossoverPoints.Add(new CutPoint(n, child));
|
---|
[5367] | 141 | else
|
---|
[5686] | 142 | leafCrossoverPoints.Add(new CutPoint(n, child));
|
---|
[5367] | 143 | }
|
---|
[3997] | 144 | }
|
---|
[7506] | 145 |
|
---|
[5916] | 146 | // add one additional extension point if the number of sub trees for the symbol is not full
|
---|
[6803] | 147 | if (n.SubtreeCount < n.Grammar.GetMaximumSubtreeCount(n.Symbol)) {
|
---|
[5916] | 148 | // empty extension point
|
---|
[6803] | 149 | internalCrossoverPoints.Add(new CutPoint(n, n.SubtreeCount));
|
---|
[5916] | 150 | }
|
---|
[3997] | 151 | }
|
---|
[7506] | 152 | }
|
---|
| 153 | );
|
---|
[5367] | 154 |
|
---|
[3997] | 155 | if (random.NextDouble() < internalNodeProbability) {
|
---|
| 156 | // select from internal node if possible
|
---|
| 157 | if (internalCrossoverPoints.Count > 0) {
|
---|
| 158 | // select internal crossover point or leaf
|
---|
[5916] | 159 | crossoverPoint = internalCrossoverPoints[random.Next(internalCrossoverPoints.Count)];
|
---|
[3997] | 160 | } else {
|
---|
| 161 | // otherwise select external node
|
---|
[5916] | 162 | crossoverPoint = leafCrossoverPoints[random.Next(leafCrossoverPoints.Count)];
|
---|
[3997] | 163 | }
|
---|
| 164 | } else if (leafCrossoverPoints.Count > 0) {
|
---|
| 165 | // select from leaf crossover point if possible
|
---|
[5916] | 166 | crossoverPoint = leafCrossoverPoints[random.Next(leafCrossoverPoints.Count)];
|
---|
[3997] | 167 | } else {
|
---|
| 168 | // otherwise select internal crossover point
|
---|
[5916] | 169 | crossoverPoint = internalCrossoverPoints[random.Next(internalCrossoverPoints.Count)];
|
---|
[645] | 170 | }
|
---|
| 171 | }
|
---|
[3237] | 172 |
|
---|
[5510] | 173 | private static ISymbolicExpressionTreeNode SelectRandomBranch(IRandom random, IEnumerable<ISymbolicExpressionTreeNode> branches, double internalNodeProbability) {
|
---|
[3237] | 174 | if (internalNodeProbability < 0.0 || internalNodeProbability > 1.0) throw new ArgumentException("internalNodeProbability");
|
---|
[5510] | 175 | List<ISymbolicExpressionTreeNode> allowedInternalBranches;
|
---|
| 176 | List<ISymbolicExpressionTreeNode> allowedLeafBranches;
|
---|
[3997] | 177 | if (random.NextDouble() < internalNodeProbability) {
|
---|
| 178 | // select internal node if possible
|
---|
| 179 | allowedInternalBranches = (from branch in branches
|
---|
[7506] | 180 | where branch != null && branch.SubtreeCount > 0
|
---|
[3997] | 181 | select branch).ToList();
|
---|
| 182 | if (allowedInternalBranches.Count > 0) {
|
---|
| 183 | return allowedInternalBranches.SelectRandom(random);
|
---|
| 184 | } else {
|
---|
| 185 | // no internal nodes allowed => select leaf nodes
|
---|
| 186 | allowedLeafBranches = (from branch in branches
|
---|
[7506] | 187 | where branch == null || branch.SubtreeCount == 0
|
---|
[3989] | 188 | select branch).ToList();
|
---|
[3997] | 189 | return allowedLeafBranches.SelectRandom(random);
|
---|
| 190 | }
|
---|
[3237] | 191 | } else {
|
---|
[3997] | 192 | // select leaf node if possible
|
---|
| 193 | allowedLeafBranches = (from branch in branches
|
---|
[7506] | 194 | where branch == null || branch.SubtreeCount == 0
|
---|
[3997] | 195 | select branch).ToList();
|
---|
| 196 | if (allowedLeafBranches.Count > 0) {
|
---|
| 197 | return allowedLeafBranches.SelectRandom(random);
|
---|
| 198 | } else {
|
---|
| 199 | allowedInternalBranches = (from branch in branches
|
---|
[7506] | 200 | where branch != null && branch.SubtreeCount > 0
|
---|
[3997] | 201 | select branch).ToList();
|
---|
| 202 | return allowedInternalBranches.SelectRandom(random);
|
---|
| 203 | }
|
---|
[3237] | 204 | }
|
---|
| 205 | }
|
---|
[645] | 206 | }
|
---|
| 207 | }
|
---|