[7476] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17209] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[7476] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
[7481] | 27 | using HeuristicLab.Data;
|
---|
[7476] | 28 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 29 | using HeuristicLab.Parameters;
|
---|
[16565] | 30 | using HEAL.Attic;
|
---|
[12422] | 31 | using HeuristicLab.Random;
|
---|
[7476] | 32 |
|
---|
| 33 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
[7497] | 34 | [Item("DepthConstrainedCrossover", "An operator which performs subtree swapping within a specific depth range. The range parameter controls the crossover behavior:\n" +
|
---|
| 35 | "- HighLevel (upper 25% of the tree)\n" +
|
---|
| 36 | "- Standard (mid 50% of the tree)\n" +
|
---|
| 37 | "- LowLevel (lower 25% of the tree)")]
|
---|
[16565] | 38 | [StorableType("E48D1FEB-8D3F-4394-9AD4-7C4A24116FD4")]
|
---|
[7476] | 39 | public sealed class SymbolicDataAnalysisExpressionDepthConstrainedCrossover<T> :
|
---|
| 40 | SymbolicDataAnalysisExpressionCrossover<T> where T : class, IDataAnalysisProblemData {
|
---|
[16565] | 41 | [StorableType("24941bf7-da85-44e9-9e01-44c285ac41c3")]
|
---|
[7476] | 42 | private enum Ranges { HighLevel, Standard, LowLevel };
|
---|
| 43 | private const string DepthRangeParameterName = "DepthRange";
|
---|
[7489] | 44 |
|
---|
[7476] | 45 | #region Parameter properties
|
---|
[8203] | 46 | public IConstrainedValueParameter<StringValue> DepthRangeParameter {
|
---|
| 47 | get { return (IConstrainedValueParameter<StringValue>)Parameters[DepthRangeParameterName]; }
|
---|
[7476] | 48 | }
|
---|
| 49 | #endregion
|
---|
| 50 |
|
---|
| 51 | #region Properties
|
---|
| 52 | public StringValue DepthRange {
|
---|
| 53 | get { return (StringValue)DepthRangeParameter.ActualValue; }
|
---|
| 54 | }
|
---|
| 55 | #endregion
|
---|
| 56 |
|
---|
| 57 | [StorableConstructor]
|
---|
[16565] | 58 | private SymbolicDataAnalysisExpressionDepthConstrainedCrossover(StorableConstructorFlag _) : base(_) { }
|
---|
[7476] | 59 | private SymbolicDataAnalysisExpressionDepthConstrainedCrossover(SymbolicDataAnalysisExpressionCrossover<T> original, Cloner cloner)
|
---|
| 60 | : base(original, cloner) { }
|
---|
| 61 | public SymbolicDataAnalysisExpressionDepthConstrainedCrossover()
|
---|
| 62 | : base() {
|
---|
| 63 | Parameters.Add(new ConstrainedValueParameter<StringValue>(DepthRangeParameterName, "Depth range specifier"));
|
---|
[7490] | 64 | DepthRangeParameter.ValidValues.Add(new StringValue(Enum.GetName(typeof(Ranges), Ranges.HighLevel)).AsReadOnly());
|
---|
| 65 | DepthRangeParameter.ValidValues.Add(new StringValue(Enum.GetName(typeof(Ranges), Ranges.Standard)).AsReadOnly());
|
---|
| 66 | DepthRangeParameter.ValidValues.Add(new StringValue(Enum.GetName(typeof(Ranges), Ranges.LowLevel)).AsReadOnly());
|
---|
[7521] | 67 | name = "DepthConstrainedCrossover";
|
---|
[7476] | 68 | }
|
---|
| 69 | public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicDataAnalysisExpressionDepthConstrainedCrossover<T>(this, cloner); }
|
---|
| 70 |
|
---|
[7481] | 71 | public override ISymbolicExpressionTree Crossover(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1) {
|
---|
[7476] | 72 | return Cross(random, parent0, parent1, MaximumSymbolicExpressionTreeDepth.Value, MaximumSymbolicExpressionTreeLength.Value, DepthRange.Value);
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 |
|
---|
| 76 | /// <summary>
|
---|
| 77 | /// Takes two parent individuals P0 and P1.
|
---|
| 78 | /// Randomly choose nodes that fall within the specified depth range in both parents.
|
---|
| 79 | /// </summary>
|
---|
| 80 | /// <param name="random">Pseudo-random number generator.</param>
|
---|
| 81 | /// <param name="parent0">First parent.</param>
|
---|
| 82 | /// <param name="parent1">Second parent.</param>
|
---|
| 83 | /// <param name="maxDepth">Maximum allowed length depth.</param>
|
---|
| 84 | /// <param name="maxLength">Maximum allowed tree length.</param>
|
---|
| 85 | /// <param name="mode">Controls the crossover behavior:
|
---|
| 86 | /// - HighLevel (upper 25% of the tree),
|
---|
| 87 | /// - Standard (mid 50%)
|
---|
| 88 | /// - LowLevel (low 25%)</param>
|
---|
| 89 | /// <returns></returns>
|
---|
| 90 | public static ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1, int maxDepth, int maxLength, string mode) {
|
---|
[7497] | 91 | int depth = parent0.Root.GetDepth() - 1; // substract 1 because the tree levels are counted from 0
|
---|
[7476] | 92 | var depthRange = new IntRange();
|
---|
| 93 | const int depthOffset = 2; // skip the first 2 levels (root + startNode)
|
---|
[8203] | 94 | switch ((Ranges)Enum.Parse(typeof(Ranges), mode)) {
|
---|
| 95 | case Ranges.HighLevel:
|
---|
[7476] | 96 | depthRange.Start = depthOffset; // skip the first 2 levels (root + startNode)
|
---|
| 97 | depthRange.End = depthRange.Start + (int)Math.Round(depth * 0.25);
|
---|
| 98 | break;
|
---|
[8203] | 99 | case Ranges.Standard:
|
---|
[7476] | 100 | depthRange.Start = depthOffset + (int)Math.Round(depth * 0.25);
|
---|
| 101 | depthRange.End = depthRange.Start + (int)Math.Round(depth * 0.5);
|
---|
| 102 | break;
|
---|
[8203] | 103 | case Ranges.LowLevel:
|
---|
[7476] | 104 | depthRange.Start = depthOffset + (int)Math.Round(depth * 0.75);
|
---|
| 105 | depthRange.End = Math.Max(depthRange.Start, depth);
|
---|
| 106 | break;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | // make sure that the depth range does not exceeded the actual depth of parent0
|
---|
| 110 | if (depthRange.Start > depth)
|
---|
| 111 | depthRange.Start = depth;
|
---|
| 112 | if (depthRange.End < depthRange.Start)
|
---|
| 113 | depthRange.End = depthRange.Start;
|
---|
| 114 |
|
---|
[7489] | 115 | var crossoverPoints0 = (from node in GetNodesAtDepth(parent0, depthRange) select new CutPoint(node.Parent, node)).ToList();
|
---|
[7476] | 116 |
|
---|
| 117 | if (crossoverPoints0.Count == 0)
|
---|
| 118 | throw new Exception("No crossover points available in the first parent");
|
---|
| 119 |
|
---|
[12422] | 120 | var crossoverPoint0 = crossoverPoints0.SampleRandom(random);
|
---|
[7476] | 121 | int level = parent0.Root.GetBranchLevel(crossoverPoint0.Child);
|
---|
| 122 | int length = parent0.Root.GetLength() - crossoverPoint0.Child.GetLength();
|
---|
| 123 |
|
---|
[7489] | 124 | var allowedBranches = (from s in GetNodesAtDepth(parent1, depthRange)
|
---|
[7497] | 125 | where s.GetDepth() + level <= maxDepth
|
---|
| 126 | where s.GetLength() + length <= maxLength
|
---|
| 127 | where crossoverPoint0.IsMatchingPointType(s)
|
---|
[7476] | 128 | select s).ToList();
|
---|
| 129 | if (allowedBranches.Count == 0) return parent0;
|
---|
[12422] | 130 |
|
---|
| 131 | var selectedBranch = allowedBranches.SampleRandom(random);
|
---|
[7497] | 132 | Swap(crossoverPoint0, selectedBranch);
|
---|
[7476] | 133 | return parent0;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
[7489] | 136 | private static IEnumerable<ISymbolicExpressionTreeNode> GetNodesAtDepth(ISymbolicExpressionTree tree, IntRange depthRange) {
|
---|
| 137 | var treeDepth = tree.Root.GetDepth();
|
---|
| 138 | return from node in tree.Root.IterateNodesPostfix()
|
---|
| 139 | let depth = treeDepth - node.GetDepth()
|
---|
| 140 | where depthRange.Start <= depth
|
---|
| 141 | where depth <= depthRange.End
|
---|
| 142 | select node;
|
---|
[7476] | 143 | }
|
---|
| 144 | }
|
---|
| 145 | }
|
---|