1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
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.Persistence.Default.CompositeSerializers.Storable;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
29 | using HeuristicLab.Data;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
33 |
|
---|
34 | [Item("DepthConstrainedCrossover", "An operator which performs subtree swapping within a specific depth range.")]
|
---|
35 | public sealed class SymbolicDataAnalysisExpressionDepthConstrainedCrossover<T> :
|
---|
36 | SymbolicDataAnalysisExpressionCrossover<T> where T : class, IDataAnalysisProblemData {
|
---|
37 | private enum Ranges { HighLevel, Standard, LowLevel };
|
---|
38 | private const string DepthRangeParameterName = "DepthRange";
|
---|
39 | #region Parameter properties
|
---|
40 |
|
---|
41 | public ConstrainedValueParameter<StringValue> DepthRangeParameter {
|
---|
42 | get { return (ConstrainedValueParameter<StringValue>)Parameters[DepthRangeParameterName]; }
|
---|
43 | }
|
---|
44 | #endregion
|
---|
45 |
|
---|
46 | #region Properties
|
---|
47 | public StringValue DepthRange {
|
---|
48 | get { return (StringValue)DepthRangeParameter.ActualValue; }
|
---|
49 | }
|
---|
50 | #endregion
|
---|
51 |
|
---|
52 | [StorableConstructor]
|
---|
53 | private SymbolicDataAnalysisExpressionDepthConstrainedCrossover(bool deserializing) : base(deserializing) { }
|
---|
54 | private SymbolicDataAnalysisExpressionDepthConstrainedCrossover(SymbolicDataAnalysisExpressionCrossover<T> original, Cloner cloner)
|
---|
55 | : base(original, cloner) { }
|
---|
56 | public SymbolicDataAnalysisExpressionDepthConstrainedCrossover()
|
---|
57 | : base() {
|
---|
58 | Parameters.Add(new ConstrainedValueParameter<StringValue>(DepthRangeParameterName, "Depth range specifier"));
|
---|
59 | DepthRangeParameter.ValidValues.Add(new StringValue(Enum.GetName(typeof(Ranges), Ranges.HighLevel)));
|
---|
60 | DepthRangeParameter.ValidValues.Add(new StringValue(Enum.GetName(typeof(Ranges), Ranges.Standard)));
|
---|
61 | DepthRangeParameter.ValidValues.Add(new StringValue(Enum.GetName(typeof(Ranges), Ranges.LowLevel)));
|
---|
62 | Name = "DepthConstrainedCrossover";
|
---|
63 | }
|
---|
64 | public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicDataAnalysisExpressionDepthConstrainedCrossover<T>(this, cloner); }
|
---|
65 |
|
---|
66 | protected override ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1) {
|
---|
67 | return Cross(random, parent0, parent1, MaximumSymbolicExpressionTreeDepth.Value, MaximumSymbolicExpressionTreeLength.Value, DepthRange.Value);
|
---|
68 | }
|
---|
69 |
|
---|
70 | public override ISymbolicExpressionTree Crossover(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1) {
|
---|
71 | return Cross(random, parent0, parent1);
|
---|
72 | }
|
---|
73 |
|
---|
74 | /// <summary>
|
---|
75 | /// Takes two parent individuals P0 and P1.
|
---|
76 | /// Randomly choose nodes that fall within the specified depth range in both parents.
|
---|
77 | /// </summary>
|
---|
78 | /// <param name="random">Pseudo-random number generator.</param>
|
---|
79 | /// <param name="parent0">First parent.</param>
|
---|
80 | /// <param name="parent1">Second parent.</param>
|
---|
81 | /// <param name="maxDepth">Maximum allowed length depth.</param>
|
---|
82 | /// <param name="maxLength">Maximum allowed tree length.</param>
|
---|
83 | /// <param name="mode">Controls the crossover behavior:
|
---|
84 | /// - HighLevel (upper 25% of the tree),
|
---|
85 | /// - Standard (mid 50%)
|
---|
86 | /// - LowLevel (low 25%)</param>
|
---|
87 | /// <returns></returns>
|
---|
88 | public static ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1, int maxDepth, int maxLength, string mode) {
|
---|
89 | int depth = parent0.Root.GetDepth() - 1; // subtract 1 because the tree levels are counted from 0
|
---|
90 | var depthRange = new IntRange();
|
---|
91 | const int depthOffset = 2; // skip the first 2 levels (root + startNode)
|
---|
92 | switch ((int)Enum.Parse(typeof(Ranges), mode)) {
|
---|
93 | case (int)Ranges.HighLevel:
|
---|
94 | depthRange.Start = depthOffset; // skip the first 2 levels (root + startNode)
|
---|
95 | depthRange.End = depthRange.Start + (int)Math.Round(depth * 0.25);
|
---|
96 | break;
|
---|
97 | case (int)Ranges.Standard:
|
---|
98 | depthRange.Start = depthOffset + (int)Math.Round(depth * 0.25);
|
---|
99 | depthRange.End = depthRange.Start + (int)Math.Round(depth * 0.5);
|
---|
100 | break;
|
---|
101 | case (int)Ranges.LowLevel:
|
---|
102 | depthRange.Start = depthOffset + (int)Math.Round(depth * 0.75);
|
---|
103 | depthRange.End = Math.Max(depthRange.Start, depth);
|
---|
104 | break;
|
---|
105 | }
|
---|
106 |
|
---|
107 | // make sure that the depth range does not exceeded the actual depth of parent0
|
---|
108 | if (depthRange.Start > depth)
|
---|
109 | depthRange.Start = depth;
|
---|
110 | if (depthRange.End < depthRange.Start)
|
---|
111 | depthRange.End = depthRange.Start;
|
---|
112 |
|
---|
113 | var crossoverPoints0 = (from node in GetNodesAtDepth(parent0.Root, depthRange) select new CutPoint(node.Parent, node)).ToList();
|
---|
114 |
|
---|
115 | if (crossoverPoints0.Count == 0)
|
---|
116 | throw new Exception("No crossover points available in the first parent");
|
---|
117 |
|
---|
118 | var crossoverPoint0 = crossoverPoints0.SelectRandom(random);
|
---|
119 |
|
---|
120 | int level = parent0.Root.GetBranchLevel(crossoverPoint0.Child);
|
---|
121 | int length = parent0.Root.GetLength() - crossoverPoint0.Child.GetLength();
|
---|
122 |
|
---|
123 | var allowedBranches = (from s in GetNodesAtDepth(parent1.Root, depthRange)
|
---|
124 | where
|
---|
125 | crossoverPoint0.IsMatchingPointType(s) &&
|
---|
126 | s.GetDepth() + level <= maxDepth &&
|
---|
127 | s.GetLength() + length <= maxLength
|
---|
128 | select s).ToList();
|
---|
129 | if (allowedBranches.Count == 0) return parent0;
|
---|
130 | var selectedBranch = allowedBranches.SelectRandom(random);
|
---|
131 | swap(crossoverPoint0, selectedBranch);
|
---|
132 | return parent0;
|
---|
133 | }
|
---|
134 |
|
---|
135 | private static IEnumerable<ISymbolicExpressionTreeNode> GetNodesAtDepth(ISymbolicExpressionTreeNode root, IntRange range) {
|
---|
136 | var list = new List<Tuple<ISymbolicExpressionTreeNode, int>> { new Tuple<ISymbolicExpressionTreeNode, int>(root, 0) };
|
---|
137 | int offset = 0;
|
---|
138 | int level = 0;
|
---|
139 | while (level < range.End) {
|
---|
140 | ++level;
|
---|
141 | int count = list.Count;
|
---|
142 | for (int i = offset; i != count; ++i) {
|
---|
143 | if (list[i].Item1.Subtrees.Any())
|
---|
144 | list.AddRange(from s in list[i].Item1.Subtrees select new Tuple<ISymbolicExpressionTreeNode, int>(s, level));
|
---|
145 | }
|
---|
146 | offset = count;
|
---|
147 | }
|
---|
148 | // taking advantage of the fact that the list is already sorted by level
|
---|
149 | for (int i = list.Count - 1; i >= 0; --i) {
|
---|
150 | if (list[i].Item2 >= range.Start)
|
---|
151 | yield return list[i].Item1;
|
---|
152 | else break;
|
---|
153 | }
|
---|
154 | }
|
---|
155 |
|
---|
156 | private static void swap(CutPoint crossoverPoint, ISymbolicExpressionTreeNode selectedBranch) {
|
---|
157 | // perform the actual swap
|
---|
158 | if (crossoverPoint.Child != null) {
|
---|
159 | // manipulate the tree of parent0 in place
|
---|
160 | // replace the branch in tree0 with the selected branch from tree1
|
---|
161 | crossoverPoint.Parent.RemoveSubtree(crossoverPoint.ChildIndex);
|
---|
162 | if (selectedBranch != null) {
|
---|
163 | crossoverPoint.Parent.InsertSubtree(crossoverPoint.ChildIndex, selectedBranch);
|
---|
164 | }
|
---|
165 | } else {
|
---|
166 | // child is null (additional child should be added under the parent)
|
---|
167 | if (selectedBranch != null) {
|
---|
168 | crossoverPoint.Parent.AddSubtree(selectedBranch);
|
---|
169 | }
|
---|
170 | }
|
---|
171 | }
|
---|
172 | }
|
---|
173 | }
|
---|