1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2010 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.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Parameters;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Crossovers {
|
---|
31 | /// <summary>
|
---|
32 | /// Takes two parent individuals P0 and P1 each. Selects a random node N0 of P0 and a random node N1 of P1.
|
---|
33 | /// And replaces the branch with root0 N0 in P0 with N1 from P1 if the tree-size limits are not violated.
|
---|
34 | /// When recombination with N0 and N1 would create a tree that is too large or invalid the operator randomly selects new N0 and N1
|
---|
35 | /// until a valid configuration is found.
|
---|
36 | /// </summary>
|
---|
37 | [Item("SubtreeCrossover", "An operator which performs subtree swapping crossover.")]
|
---|
38 | [StorableClass]
|
---|
39 | public class SubtreeCrossover : SymbolicExpressionTreeCrossover {
|
---|
40 | public IValueLookupParameter<PercentValue> InternalCrossoverPointProbabilityParameter {
|
---|
41 | get { return (IValueLookupParameter<PercentValue>)Parameters["InternalCrossoverPointProbability"]; }
|
---|
42 | }
|
---|
43 |
|
---|
44 | public SubtreeCrossover()
|
---|
45 | : base() {
|
---|
46 | Parameters.Add(new ValueLookupParameter<PercentValue>("InternalCrossoverPointProbability", "The probability to select an internal crossover point (instead of a leaf node).", new PercentValue(0.9)));
|
---|
47 | }
|
---|
48 |
|
---|
49 | protected override SymbolicExpressionTree Cross(IRandom random,
|
---|
50 | SymbolicExpressionTree parent0, SymbolicExpressionTree parent1,
|
---|
51 | IntValue maxTreeSize, IntValue maxTreeHeight, out bool success) {
|
---|
52 | return Cross(random, parent0, parent1, InternalCrossoverPointProbabilityParameter.ActualValue.Value, maxTreeSize.Value, maxTreeHeight.Value, out success);
|
---|
53 | }
|
---|
54 |
|
---|
55 | public static SymbolicExpressionTree Cross(IRandom random,
|
---|
56 | SymbolicExpressionTree parent0, SymbolicExpressionTree parent1,
|
---|
57 | double internalCrossoverPointProbability, int maxTreeSize, int maxTreeHeight, out bool success) {
|
---|
58 | // select a random crossover point in the first parent
|
---|
59 | SymbolicExpressionTreeNode crossoverPoint0;
|
---|
60 | int replacedSubtreeIndex;
|
---|
61 | SelectCrossoverPoint(random, parent0, internalCrossoverPointProbability, maxTreeSize - 1, maxTreeHeight - 1, out crossoverPoint0, out replacedSubtreeIndex);
|
---|
62 |
|
---|
63 | // calculate the max size and height that the inserted branch can have
|
---|
64 | int maxInsertedBranchSize = maxTreeSize - (parent0.Size - crossoverPoint0.SubTrees[replacedSubtreeIndex].GetSize());
|
---|
65 | int maxInsertedBranchHeight = maxTreeHeight - GetBranchLevel(parent0.Root, crossoverPoint0);
|
---|
66 |
|
---|
67 | List<SymbolicExpressionTreeNode> allowedBranches = new List<SymbolicExpressionTreeNode>();
|
---|
68 | parent1.Root.ForEachNodePostfix((n) => {
|
---|
69 | if (n.GetSize() < maxInsertedBranchSize &&
|
---|
70 | n.GetHeight() < maxInsertedBranchHeight &&
|
---|
71 | IsMatchingPointType(crossoverPoint0, replacedSubtreeIndex, n))
|
---|
72 | allowedBranches.Add(n);
|
---|
73 | });
|
---|
74 |
|
---|
75 | if (allowedBranches.Count == 0) {
|
---|
76 | success = false;
|
---|
77 | return parent0;
|
---|
78 | } else {
|
---|
79 | var selectedBranch = SelectRandomBranch(random, allowedBranches, internalCrossoverPointProbability);
|
---|
80 |
|
---|
81 | // manipulate the tree of parent0 in place
|
---|
82 | // replace the branch in tree0 with the selected branch from tree1
|
---|
83 | crossoverPoint0.RemoveSubTree(replacedSubtreeIndex);
|
---|
84 | crossoverPoint0.InsertSubTree(replacedSubtreeIndex, selectedBranch);
|
---|
85 | success = true;
|
---|
86 | return parent0;
|
---|
87 | }
|
---|
88 | }
|
---|
89 |
|
---|
90 | private static bool IsMatchingPointType(SymbolicExpressionTreeNode parent, int replacedSubtreeIndex, SymbolicExpressionTreeNode branch) {
|
---|
91 | // check syntax constraints of direct parent - child relation
|
---|
92 | if (!parent.Grammar.ContainsSymbol(branch.Symbol) ||
|
---|
93 | !parent.Grammar.IsAllowedChild(parent.Symbol, branch.Symbol, replacedSubtreeIndex)) return false;
|
---|
94 |
|
---|
95 | bool result = true;
|
---|
96 | // check point type for the whole branch
|
---|
97 | branch.ForEachNodePostfix((n) => {
|
---|
98 | result =
|
---|
99 | result &&
|
---|
100 | parent.Grammar.ContainsSymbol(n.Symbol) &&
|
---|
101 | n.SubTrees.Count >= parent.Grammar.GetMinSubtreeCount(n.Symbol) &&
|
---|
102 | n.SubTrees.Count <= parent.Grammar.GetMaxSubtreeCount(n.Symbol);
|
---|
103 | });
|
---|
104 | return result;
|
---|
105 | }
|
---|
106 |
|
---|
107 | private static void SelectCrossoverPoint(IRandom random, SymbolicExpressionTree parent0, double internalNodeProbability, int maxBranchSize, int maxBranchHeight, out SymbolicExpressionTreeNode crossoverPoint, out int subtreeIndex) {
|
---|
108 | if (internalNodeProbability < 0.0 || internalNodeProbability > 1.0) throw new ArgumentException("internalNodeProbability");
|
---|
109 | List<CrossoverPoint> internalCrossoverPoints = new List<CrossoverPoint>();
|
---|
110 | List<CrossoverPoint> leafCrossoverPoints = new List<CrossoverPoint>();
|
---|
111 | parent0.Root.ForEachNodePostfix((n) => {
|
---|
112 | if (n.SubTrees.Count > 0 &&
|
---|
113 | n.GetSize() < maxBranchSize &&
|
---|
114 | n.GetHeight() < maxBranchHeight &&
|
---|
115 | n != parent0.Root
|
---|
116 | ) {
|
---|
117 | foreach (var child in n.SubTrees) {
|
---|
118 | if (child.SubTrees.Count > 0)
|
---|
119 | internalCrossoverPoints.Add(new CrossoverPoint(n, child));
|
---|
120 | else
|
---|
121 | leafCrossoverPoints.Add(new CrossoverPoint(n, child));
|
---|
122 | }
|
---|
123 | }
|
---|
124 | });
|
---|
125 | if (random.NextDouble() < internalNodeProbability) {
|
---|
126 | // select from internal node if possible
|
---|
127 | if (internalCrossoverPoints.Count > 0) {
|
---|
128 | // select internal crossover point or leaf
|
---|
129 | var selectedCrossoverPoint = internalCrossoverPoints[random.Next(internalCrossoverPoints.Count)];
|
---|
130 | crossoverPoint = selectedCrossoverPoint.Parent;
|
---|
131 | subtreeIndex = selectedCrossoverPoint.SubtreeIndex;
|
---|
132 | } else {
|
---|
133 | // otherwise select external node
|
---|
134 | var selectedCrossoverPoint = leafCrossoverPoints[random.Next(leafCrossoverPoints.Count)];
|
---|
135 | crossoverPoint = selectedCrossoverPoint.Parent;
|
---|
136 | subtreeIndex = selectedCrossoverPoint.SubtreeIndex;
|
---|
137 | }
|
---|
138 | } else if (leafCrossoverPoints.Count > 0) {
|
---|
139 | // select from leaf crossover point if possible
|
---|
140 | var selectedCrossoverPoint = leafCrossoverPoints[random.Next(leafCrossoverPoints.Count)];
|
---|
141 | crossoverPoint = selectedCrossoverPoint.Parent;
|
---|
142 | subtreeIndex = selectedCrossoverPoint.SubtreeIndex;
|
---|
143 | } else {
|
---|
144 | // otherwise select internal crossover point
|
---|
145 | var selectedCrossoverPoint = internalCrossoverPoints[random.Next(internalCrossoverPoints.Count)];
|
---|
146 | crossoverPoint = selectedCrossoverPoint.Parent;
|
---|
147 | subtreeIndex = selectedCrossoverPoint.SubtreeIndex;
|
---|
148 | }
|
---|
149 | }
|
---|
150 |
|
---|
151 | private static SymbolicExpressionTreeNode SelectRandomBranch(IRandom random, IEnumerable<SymbolicExpressionTreeNode> branches, double internalNodeProbability) {
|
---|
152 | if (internalNodeProbability < 0.0 || internalNodeProbability > 1.0) throw new ArgumentException("internalNodeProbability");
|
---|
153 | List<SymbolicExpressionTreeNode> allowedInternalBranches;
|
---|
154 | List<SymbolicExpressionTreeNode> allowedLeafBranches;
|
---|
155 | if (random.NextDouble() < internalNodeProbability) {
|
---|
156 | // select internal node if possible
|
---|
157 | allowedInternalBranches = (from branch in branches
|
---|
158 | where branch.SubTrees.Count > 0
|
---|
159 | select branch).ToList();
|
---|
160 | if (allowedInternalBranches.Count > 0) {
|
---|
161 | return allowedInternalBranches.SelectRandom(random);
|
---|
162 | } else {
|
---|
163 | // no internal nodes allowed => select leaf nodes
|
---|
164 | allowedLeafBranches = (from branch in branches
|
---|
165 | where branch.SubTrees.Count == 0
|
---|
166 | select branch).ToList();
|
---|
167 | return allowedLeafBranches.SelectRandom(random);
|
---|
168 | }
|
---|
169 | } else {
|
---|
170 | // select leaf node if possible
|
---|
171 | allowedLeafBranches = (from branch in branches
|
---|
172 | where branch.SubTrees.Count == 0
|
---|
173 | select branch).ToList();
|
---|
174 | if (allowedLeafBranches.Count > 0) {
|
---|
175 | return allowedLeafBranches.SelectRandom(random);
|
---|
176 | } else {
|
---|
177 | allowedInternalBranches = (from branch in branches
|
---|
178 | where branch.SubTrees.Count > 0
|
---|
179 | select branch).ToList();
|
---|
180 | return allowedInternalBranches.SelectRandom(random);
|
---|
181 | }
|
---|
182 | }
|
---|
183 | }
|
---|
184 |
|
---|
185 | private static int GetBranchLevel(SymbolicExpressionTreeNode root, SymbolicExpressionTreeNode point) {
|
---|
186 | if (root == point) return 0;
|
---|
187 | foreach (var subtree in root.SubTrees) {
|
---|
188 | int branchLevel = GetBranchLevel(subtree, point);
|
---|
189 | if (branchLevel < int.MaxValue) return 1 + branchLevel;
|
---|
190 | }
|
---|
191 | return int.MaxValue;
|
---|
192 | }
|
---|
193 | }
|
---|
194 | }
|
---|