[645] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17180] | 3 | * Copyright (C) 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;
|
---|
[16565] | 29 | using HEAL.Attic;
|
---|
[12422] | 30 | using HeuristicLab.Random;
|
---|
[645] | 31 |
|
---|
[5499] | 32 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
|
---|
[3237] | 33 | /// <summary>
|
---|
| 34 | /// Takes two parent individuals P0 and P1 each. Selects a random node N0 of P0 and a random node N1 of P1.
|
---|
| 35 | /// And replaces the branch with root0 N0 in P0 with N1 from P1 if the tree-size limits are not violated.
|
---|
| 36 | /// When recombination with N0 and N1 would create a tree that is too large or invalid the operator randomly selects new N0 and N1
|
---|
| 37 | /// until a valid configuration is found.
|
---|
| 38 | /// </summary>
|
---|
[7506] | 39 | [Item("SubtreeSwappingCrossover", "An operator which performs subtree swapping crossover.")]
|
---|
[16565] | 40 | [StorableType("2A2552C0-11C8-4F60-90B2-5FDDD3AB2444")]
|
---|
[7506] | 41 | public class SubtreeCrossover : SymbolicExpressionTreeCrossover, ISymbolicExpressionTreeSizeConstraintOperator {
|
---|
[5499] | 42 | private const string InternalCrossoverPointProbabilityParameterName = "InternalCrossoverPointProbability";
|
---|
| 43 | private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength";
|
---|
| 44 | private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth";
|
---|
[17928] | 45 | private const string CrossoverProbabilityParameterName = "CrossoverProbability";
|
---|
[7506] | 46 |
|
---|
[5499] | 47 | #region Parameter Properties
|
---|
[3237] | 48 | public IValueLookupParameter<PercentValue> InternalCrossoverPointProbabilityParameter {
|
---|
[5499] | 49 | get { return (IValueLookupParameter<PercentValue>)Parameters[InternalCrossoverPointProbabilityParameterName]; }
|
---|
[645] | 50 | }
|
---|
[5499] | 51 | public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter {
|
---|
| 52 | get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; }
|
---|
| 53 | }
|
---|
| 54 | public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter {
|
---|
| 55 | get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; }
|
---|
| 56 | }
|
---|
[17928] | 57 | public IFixedValueParameter<PercentValue> CrossoverProbabilityParameter {
|
---|
| 58 | get { return (IFixedValueParameter<PercentValue>)Parameters[CrossoverProbabilityParameterName]; }
|
---|
| 59 | }
|
---|
[5499] | 60 | #endregion
|
---|
| 61 | #region Properties
|
---|
| 62 | public PercentValue InternalCrossoverPointProbability {
|
---|
| 63 | get { return InternalCrossoverPointProbabilityParameter.ActualValue; }
|
---|
| 64 | }
|
---|
| 65 | public IntValue MaximumSymbolicExpressionTreeLength {
|
---|
| 66 | get { return MaximumSymbolicExpressionTreeLengthParameter.ActualValue; }
|
---|
| 67 | }
|
---|
| 68 | public IntValue MaximumSymbolicExpressionTreeDepth {
|
---|
| 69 | get { return MaximumSymbolicExpressionTreeDepthParameter.ActualValue; }
|
---|
| 70 | }
|
---|
[17928] | 71 | public double CrossoverProbability {
|
---|
| 72 | get { return CrossoverProbabilityParameter.Value.Value; }
|
---|
| 73 | set { CrossoverProbabilityParameter.Value.Value = value; }
|
---|
| 74 | }
|
---|
[5499] | 75 | #endregion
|
---|
[4722] | 76 | [StorableConstructor]
|
---|
[16565] | 77 | protected SubtreeCrossover(StorableConstructorFlag _) : base(_) { }
|
---|
[7506] | 78 | protected SubtreeCrossover(SubtreeCrossover original, Cloner cloner) : base(original, cloner) { }
|
---|
[3237] | 79 | public SubtreeCrossover()
|
---|
| 80 | : base() {
|
---|
[5499] | 81 | Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, "The maximal length (number of nodes) of the symbolic expression tree."));
|
---|
| 82 | Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0)."));
|
---|
[17928] | 83 | Parameters.Add(new ValueLookupParameter<PercentValue>(InternalCrossoverPointProbabilityParameterName, "The probability to select an internal crossover point (instead of a leaf node).", new PercentValue(0.9, true)));
|
---|
| 84 | Parameters.Add(new FixedValueParameter<PercentValue>(CrossoverProbabilityParameterName, "The probability that a crossover is performed. Otherwise a copy of the first parent is returned.", new PercentValue(1, true)));
|
---|
[3237] | 85 | }
|
---|
| 86 |
|
---|
[17928] | 87 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 88 | private void AfterDeserialization() {
|
---|
| 89 | // replace with PercentValue for files which had CrossoverProbability as DoubleValue
|
---|
| 90 | if (Parameters.ContainsKey(CrossoverProbabilityParameterName) && (Parameters[CrossoverProbabilityParameterName] is IFixedValueParameter<DoubleValue> oldParam)) {
|
---|
| 91 | var oldValue = oldParam.Value.Value;
|
---|
| 92 | Parameters.Remove(oldParam);
|
---|
| 93 | Parameters.Add(new FixedValueParameter<PercentValue>(CrossoverProbabilityParameterName, "The probability that a crossover is performed. Otherwise a copy of the first parent is returned.", new PercentValue(oldValue)));
|
---|
| 94 | }
|
---|
| 95 | if (!Parameters.ContainsKey(CrossoverProbabilityParameterName)) {
|
---|
| 96 | Parameters.Add(new FixedValueParameter<PercentValue>(CrossoverProbabilityParameterName, "The probability that a crossover is performed. Otherwise a copy of the first parent is returned.", new PercentValue(1, true)));
|
---|
| 97 | }
|
---|
| 98 | }
|
---|
| 99 |
|
---|
[4722] | 100 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 101 | return new SubtreeCrossover(this, cloner);
|
---|
| 102 | }
|
---|
| 103 |
|
---|
[7506] | 104 | public override ISymbolicExpressionTree Crossover(IRandom random,
|
---|
[5510] | 105 | ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1) {
|
---|
[17928] | 106 | return Cross(random, parent0, parent1, CrossoverProbability, InternalCrossoverPointProbability.Value,
|
---|
[5499] | 107 | MaximumSymbolicExpressionTreeLength.Value, MaximumSymbolicExpressionTreeDepth.Value);
|
---|
[3237] | 108 | }
|
---|
| 109 |
|
---|
[5510] | 110 | public static ISymbolicExpressionTree Cross(IRandom random,
|
---|
| 111 | ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1,
|
---|
[17928] | 112 | double probability,
|
---|
[5549] | 113 | double internalCrossoverPointProbability, int maxTreeLength, int maxTreeDepth) {
|
---|
[17928] | 114 | if ((probability < 1) && (random.NextDouble() >= probability)) return parent0;
|
---|
[3294] | 115 | // select a random crossover point in the first parent
|
---|
[5916] | 116 | CutPoint crossoverPoint0;
|
---|
| 117 | SelectCrossoverPoint(random, parent0, internalCrossoverPointProbability, maxTreeLength, maxTreeDepth, out crossoverPoint0);
|
---|
[645] | 118 |
|
---|
[5916] | 119 | int childLength = crossoverPoint0.Child != null ? crossoverPoint0.Child.GetLength() : 0;
|
---|
[5549] | 120 | // calculate the max length and depth that the inserted branch can have
|
---|
[14198] | 121 | int maxInsertedBranchLength = Math.Max(0, maxTreeLength - (parent0.Length - childLength));
|
---|
| 122 | int maxInsertedBranchDepth = Math.Max(0, maxTreeDepth - parent0.Root.GetBranchLevel(crossoverPoint0.Parent));
|
---|
[645] | 123 |
|
---|
[5510] | 124 | List<ISymbolicExpressionTreeNode> allowedBranches = new List<ISymbolicExpressionTreeNode>();
|
---|
[3997] | 125 | parent1.Root.ForEachNodePostfix((n) => {
|
---|
[5549] | 126 | if (n.GetLength() <= maxInsertedBranchLength &&
|
---|
[7506] | 127 | n.GetDepth() <= maxInsertedBranchDepth && crossoverPoint0.IsMatchingPointType(n))
|
---|
[3997] | 128 | allowedBranches.Add(n);
|
---|
| 129 | });
|
---|
[5916] | 130 | // empty branch
|
---|
[7506] | 131 | if (crossoverPoint0.IsMatchingPointType(null)) allowedBranches.Add(null);
|
---|
[645] | 132 |
|
---|
[3997] | 133 | if (allowedBranches.Count == 0) {
|
---|
[3297] | 134 | return parent0;
|
---|
| 135 | } else {
|
---|
[3294] | 136 | var selectedBranch = SelectRandomBranch(random, allowedBranches, internalCrossoverPointProbability);
|
---|
[14221] | 137 | if (selectedBranch != null)
|
---|
| 138 | selectedBranch = (ISymbolicExpressionTreeNode)selectedBranch.Clone();
|
---|
[645] | 139 |
|
---|
[5916] | 140 | if (crossoverPoint0.Child != null) {
|
---|
| 141 | // manipulate the tree of parent0 in place
|
---|
| 142 | // replace the branch in tree0 with the selected branch from tree1
|
---|
| 143 | crossoverPoint0.Parent.RemoveSubtree(crossoverPoint0.ChildIndex);
|
---|
| 144 | if (selectedBranch != null) {
|
---|
| 145 | crossoverPoint0.Parent.InsertSubtree(crossoverPoint0.ChildIndex, selectedBranch);
|
---|
| 146 | }
|
---|
| 147 | } else {
|
---|
| 148 | // child is null (additional child should be added under the parent)
|
---|
| 149 | if (selectedBranch != null) {
|
---|
| 150 | crossoverPoint0.Parent.AddSubtree(selectedBranch);
|
---|
| 151 | }
|
---|
| 152 | }
|
---|
[3294] | 153 | return parent0;
|
---|
[645] | 154 | }
|
---|
| 155 | }
|
---|
| 156 |
|
---|
[5916] | 157 | private static void SelectCrossoverPoint(IRandom random, ISymbolicExpressionTree parent0, double internalNodeProbability, int maxBranchLength, int maxBranchDepth, out CutPoint crossoverPoint) {
|
---|
[3997] | 158 | if (internalNodeProbability < 0.0 || internalNodeProbability > 1.0) throw new ArgumentException("internalNodeProbability");
|
---|
[5686] | 159 | List<CutPoint> internalCrossoverPoints = new List<CutPoint>();
|
---|
| 160 | List<CutPoint> leafCrossoverPoints = new List<CutPoint>();
|
---|
[3997] | 161 | parent0.Root.ForEachNodePostfix((n) => {
|
---|
[7506] | 162 | if (n.SubtreeCount > 0 && n != parent0.Root) {
|
---|
[12509] | 163 | //avoid linq to reduce memory pressure
|
---|
| 164 | for (int i = 0; i < n.SubtreeCount; i++) {
|
---|
| 165 | var child = n.GetSubtree(i);
|
---|
[5549] | 166 | if (child.GetLength() <= maxBranchLength &&
|
---|
| 167 | child.GetDepth() <= maxBranchDepth) {
|
---|
[7506] | 168 | if (child.SubtreeCount > 0)
|
---|
[5686] | 169 | internalCrossoverPoints.Add(new CutPoint(n, child));
|
---|
[5367] | 170 | else
|
---|
[5686] | 171 | leafCrossoverPoints.Add(new CutPoint(n, child));
|
---|
[5367] | 172 | }
|
---|
[3997] | 173 | }
|
---|
[7506] | 174 |
|
---|
[5916] | 175 | // add one additional extension point if the number of sub trees for the symbol is not full
|
---|
[6803] | 176 | if (n.SubtreeCount < n.Grammar.GetMaximumSubtreeCount(n.Symbol)) {
|
---|
[5916] | 177 | // empty extension point
|
---|
[6803] | 178 | internalCrossoverPoints.Add(new CutPoint(n, n.SubtreeCount));
|
---|
[5916] | 179 | }
|
---|
[3997] | 180 | }
|
---|
[7506] | 181 | }
|
---|
| 182 | );
|
---|
[5367] | 183 |
|
---|
[3997] | 184 | if (random.NextDouble() < internalNodeProbability) {
|
---|
| 185 | // select from internal node if possible
|
---|
| 186 | if (internalCrossoverPoints.Count > 0) {
|
---|
| 187 | // select internal crossover point or leaf
|
---|
[5916] | 188 | crossoverPoint = internalCrossoverPoints[random.Next(internalCrossoverPoints.Count)];
|
---|
[3997] | 189 | } else {
|
---|
| 190 | // otherwise select external node
|
---|
[5916] | 191 | crossoverPoint = leafCrossoverPoints[random.Next(leafCrossoverPoints.Count)];
|
---|
[3997] | 192 | }
|
---|
| 193 | } else if (leafCrossoverPoints.Count > 0) {
|
---|
| 194 | // select from leaf crossover point if possible
|
---|
[5916] | 195 | crossoverPoint = leafCrossoverPoints[random.Next(leafCrossoverPoints.Count)];
|
---|
[3997] | 196 | } else {
|
---|
| 197 | // otherwise select internal crossover point
|
---|
[5916] | 198 | crossoverPoint = internalCrossoverPoints[random.Next(internalCrossoverPoints.Count)];
|
---|
[645] | 199 | }
|
---|
| 200 | }
|
---|
[3237] | 201 |
|
---|
[5510] | 202 | private static ISymbolicExpressionTreeNode SelectRandomBranch(IRandom random, IEnumerable<ISymbolicExpressionTreeNode> branches, double internalNodeProbability) {
|
---|
[3237] | 203 | if (internalNodeProbability < 0.0 || internalNodeProbability > 1.0) throw new ArgumentException("internalNodeProbability");
|
---|
[5510] | 204 | List<ISymbolicExpressionTreeNode> allowedInternalBranches;
|
---|
| 205 | List<ISymbolicExpressionTreeNode> allowedLeafBranches;
|
---|
[3997] | 206 | if (random.NextDouble() < internalNodeProbability) {
|
---|
| 207 | // select internal node if possible
|
---|
| 208 | allowedInternalBranches = (from branch in branches
|
---|
[7506] | 209 | where branch != null && branch.SubtreeCount > 0
|
---|
[3997] | 210 | select branch).ToList();
|
---|
| 211 | if (allowedInternalBranches.Count > 0) {
|
---|
[12422] | 212 | return allowedInternalBranches.SampleRandom(random);
|
---|
| 213 |
|
---|
[3997] | 214 | } else {
|
---|
| 215 | // no internal nodes allowed => select leaf nodes
|
---|
| 216 | allowedLeafBranches = (from branch in branches
|
---|
[7506] | 217 | where branch == null || branch.SubtreeCount == 0
|
---|
[3989] | 218 | select branch).ToList();
|
---|
[12422] | 219 | return allowedLeafBranches.SampleRandom(random);
|
---|
[3997] | 220 | }
|
---|
[3237] | 221 | } else {
|
---|
[3997] | 222 | // select leaf node if possible
|
---|
| 223 | allowedLeafBranches = (from branch in branches
|
---|
[7506] | 224 | where branch == null || branch.SubtreeCount == 0
|
---|
[3997] | 225 | select branch).ToList();
|
---|
| 226 | if (allowedLeafBranches.Count > 0) {
|
---|
[12422] | 227 | return allowedLeafBranches.SampleRandom(random);
|
---|
[3997] | 228 | } else {
|
---|
| 229 | allowedInternalBranches = (from branch in branches
|
---|
[7506] | 230 | where branch != null && branch.SubtreeCount > 0
|
---|
[3997] | 231 | select branch).ToList();
|
---|
[12422] | 232 | return allowedInternalBranches.SampleRandom(random);
|
---|
| 233 |
|
---|
[3997] | 234 | }
|
---|
[3237] | 235 | }
|
---|
| 236 | }
|
---|
[645] | 237 | }
|
---|
| 238 | }
|
---|