1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2011 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.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Parameters;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
|
---|
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]
|
---|
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
|
---|
45 | public IValueLookupParameter<PercentValue> InternalCrossoverPointProbabilityParameter {
|
---|
46 | get { return (IValueLookupParameter<PercentValue>)Parameters[InternalCrossoverPointProbabilityParameterName]; }
|
---|
47 | }
|
---|
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
|
---|
66 | [StorableConstructor]
|
---|
67 | private SubtreeCrossover(bool deserializing) : base(deserializing) { }
|
---|
68 | private SubtreeCrossover(SubtreeCrossover original, Cloner cloner) : base(original, cloner) { }
|
---|
69 | public SubtreeCrossover()
|
---|
70 | : base() {
|
---|
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)));
|
---|
74 | }
|
---|
75 |
|
---|
76 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
77 | return new SubtreeCrossover(this, cloner);
|
---|
78 | }
|
---|
79 |
|
---|
80 | protected override ISymbolicExpressionTree Cross(IRandom random,
|
---|
81 | ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1) {
|
---|
82 | return Cross(random, parent0, parent1, InternalCrossoverPointProbability.Value,
|
---|
83 | MaximumSymbolicExpressionTreeLength.Value, MaximumSymbolicExpressionTreeDepth.Value);
|
---|
84 | }
|
---|
85 |
|
---|
86 | public static ISymbolicExpressionTree Cross(IRandom random,
|
---|
87 | ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1,
|
---|
88 | double internalCrossoverPointProbability, int maxTreeLength, int maxTreeDepth) {
|
---|
89 | // select a random crossover point in the first parent
|
---|
90 | ISymbolicExpressionTreeNode crossoverPoint0;
|
---|
91 | int replacedSubtreeIndex;
|
---|
92 | SelectCrossoverPoint(random, parent0, internalCrossoverPointProbability, maxTreeLength, maxTreeDepth, out crossoverPoint0, out replacedSubtreeIndex);
|
---|
93 |
|
---|
94 | // calculate the max length and depth that the inserted branch can have
|
---|
95 | int maxInsertedBranchLength = maxTreeLength - (parent0.Length - crossoverPoint0.GetSubTree(replacedSubtreeIndex).GetLength());
|
---|
96 | int maxInsertedBranchDepth = maxTreeDepth - GetBranchLevel(parent0.Root, crossoverPoint0);
|
---|
97 |
|
---|
98 | List<ISymbolicExpressionTreeNode> allowedBranches = new List<ISymbolicExpressionTreeNode>();
|
---|
99 | parent1.Root.ForEachNodePostfix((n) => {
|
---|
100 | if (n.GetLength() <= maxInsertedBranchLength &&
|
---|
101 | n.GetDepth() <= maxInsertedBranchDepth &&
|
---|
102 | IsMatchingPointType(crossoverPoint0, replacedSubtreeIndex, n))
|
---|
103 | allowedBranches.Add(n);
|
---|
104 | });
|
---|
105 |
|
---|
106 | if (allowedBranches.Count == 0) {
|
---|
107 | return parent0;
|
---|
108 | } else {
|
---|
109 | var selectedBranch = SelectRandomBranch(random, allowedBranches, internalCrossoverPointProbability);
|
---|
110 |
|
---|
111 | // manipulate the tree of parent0 in place
|
---|
112 | // replace the branch in tree0 with the selected branch from tree1
|
---|
113 | crossoverPoint0.RemoveSubTree(replacedSubtreeIndex);
|
---|
114 | crossoverPoint0.InsertSubTree(replacedSubtreeIndex, selectedBranch);
|
---|
115 | return parent0;
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | private static bool IsMatchingPointType(ISymbolicExpressionTreeNode parent, int replacedSubtreeIndex, ISymbolicExpressionTreeNode branch) {
|
---|
120 | // check syntax constraints of direct parent - child relation
|
---|
121 | if (!parent.Grammar.ContainsSymbol(branch.Symbol) ||
|
---|
122 | !parent.Grammar.IsAllowedChildSymbol(parent.Symbol, branch.Symbol, replacedSubtreeIndex)) return false;
|
---|
123 |
|
---|
124 | bool result = true;
|
---|
125 | // check point type for the whole branch
|
---|
126 | branch.ForEachNodePostfix((n) => {
|
---|
127 | result =
|
---|
128 | result &&
|
---|
129 | parent.Grammar.ContainsSymbol(n.Symbol) &&
|
---|
130 | n.SubtreesCount >= parent.Grammar.GetMinimumSubtreeCount(n.Symbol) &&
|
---|
131 | n.SubtreesCount <= parent.Grammar.GetMaximumSubtreeCount(n.Symbol);
|
---|
132 | });
|
---|
133 | return result;
|
---|
134 | }
|
---|
135 |
|
---|
136 | private static void SelectCrossoverPoint(IRandom random, ISymbolicExpressionTree parent0, double internalNodeProbability, int maxBranchLength, int maxBranchDepth, out ISymbolicExpressionTreeNode crossoverPoint, out int subtreeIndex) {
|
---|
137 | if (internalNodeProbability < 0.0 || internalNodeProbability > 1.0) throw new ArgumentException("internalNodeProbability");
|
---|
138 | List<CutPoint> internalCrossoverPoints = new List<CutPoint>();
|
---|
139 | List<CutPoint> leafCrossoverPoints = new List<CutPoint>();
|
---|
140 | parent0.Root.ForEachNodePostfix((n) => {
|
---|
141 | if (n.SubTrees.Any() && n != parent0.Root) {
|
---|
142 | foreach (var child in n.SubTrees) {
|
---|
143 | if (child.GetLength() <= maxBranchLength &&
|
---|
144 | child.GetDepth() <= maxBranchDepth) {
|
---|
145 | if (child.SubTrees.Any())
|
---|
146 | internalCrossoverPoints.Add(new CutPoint(n, child));
|
---|
147 | else
|
---|
148 | leafCrossoverPoints.Add(new CutPoint(n, child));
|
---|
149 | }
|
---|
150 | }
|
---|
151 | }
|
---|
152 | });
|
---|
153 |
|
---|
154 | if (random.NextDouble() < internalNodeProbability) {
|
---|
155 | // select from internal node if possible
|
---|
156 | if (internalCrossoverPoints.Count > 0) {
|
---|
157 | // select internal crossover point or leaf
|
---|
158 | var selectedCrossoverPoint = internalCrossoverPoints[random.Next(internalCrossoverPoints.Count)];
|
---|
159 | crossoverPoint = selectedCrossoverPoint.Parent;
|
---|
160 | subtreeIndex = selectedCrossoverPoint.ChildIndex;
|
---|
161 | } else {
|
---|
162 | // otherwise select external node
|
---|
163 | var selectedCrossoverPoint = leafCrossoverPoints[random.Next(leafCrossoverPoints.Count)];
|
---|
164 | crossoverPoint = selectedCrossoverPoint.Parent;
|
---|
165 | subtreeIndex = selectedCrossoverPoint.ChildIndex;
|
---|
166 | }
|
---|
167 | } else if (leafCrossoverPoints.Count > 0) {
|
---|
168 | // select from leaf crossover point if possible
|
---|
169 | var selectedCrossoverPoint = leafCrossoverPoints[random.Next(leafCrossoverPoints.Count)];
|
---|
170 | crossoverPoint = selectedCrossoverPoint.Parent;
|
---|
171 | subtreeIndex = selectedCrossoverPoint.ChildIndex;
|
---|
172 | } else {
|
---|
173 | // otherwise select internal crossover point
|
---|
174 | var selectedCrossoverPoint = internalCrossoverPoints[random.Next(internalCrossoverPoints.Count)];
|
---|
175 | crossoverPoint = selectedCrossoverPoint.Parent;
|
---|
176 | subtreeIndex = selectedCrossoverPoint.ChildIndex;
|
---|
177 | }
|
---|
178 | }
|
---|
179 |
|
---|
180 | private static ISymbolicExpressionTreeNode SelectRandomBranch(IRandom random, IEnumerable<ISymbolicExpressionTreeNode> branches, double internalNodeProbability) {
|
---|
181 | if (internalNodeProbability < 0.0 || internalNodeProbability > 1.0) throw new ArgumentException("internalNodeProbability");
|
---|
182 | List<ISymbolicExpressionTreeNode> allowedInternalBranches;
|
---|
183 | List<ISymbolicExpressionTreeNode> allowedLeafBranches;
|
---|
184 | if (random.NextDouble() < internalNodeProbability) {
|
---|
185 | // select internal node if possible
|
---|
186 | allowedInternalBranches = (from branch in branches
|
---|
187 | where branch.SubTrees.Any()
|
---|
188 | select branch).ToList();
|
---|
189 | if (allowedInternalBranches.Count > 0) {
|
---|
190 | return allowedInternalBranches.SelectRandom(random);
|
---|
191 | } else {
|
---|
192 | // no internal nodes allowed => select leaf nodes
|
---|
193 | allowedLeafBranches = (from branch in branches
|
---|
194 | where !branch.SubTrees.Any()
|
---|
195 | select branch).ToList();
|
---|
196 | return allowedLeafBranches.SelectRandom(random);
|
---|
197 | }
|
---|
198 | } else {
|
---|
199 | // select leaf node if possible
|
---|
200 | allowedLeafBranches = (from branch in branches
|
---|
201 | where !branch.SubTrees.Any()
|
---|
202 | select branch).ToList();
|
---|
203 | if (allowedLeafBranches.Count > 0) {
|
---|
204 | return allowedLeafBranches.SelectRandom(random);
|
---|
205 | } else {
|
---|
206 | allowedInternalBranches = (from branch in branches
|
---|
207 | where branch.SubTrees.Any()
|
---|
208 | select branch).ToList();
|
---|
209 | return allowedInternalBranches.SelectRandom(random);
|
---|
210 | }
|
---|
211 | }
|
---|
212 | }
|
---|
213 |
|
---|
214 | private static int GetBranchLevel(ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode point) {
|
---|
215 | if (root == point) return 0;
|
---|
216 | foreach (var subtree in root.SubTrees) {
|
---|
217 | int branchLevel = GetBranchLevel(subtree, point);
|
---|
218 | if (branchLevel < int.MaxValue) return 1 + branchLevel;
|
---|
219 | }
|
---|
220 | return int.MaxValue;
|
---|
221 | }
|
---|
222 | }
|
---|
223 | }
|
---|