Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Crossovers/SubtreeCrossover.cs @ 4501

Last change on this file since 4501 was 4106, checked in by gkronber, 14 years ago

Fixed bugs in SubtreeCrossover, ArgumentCreater and ArgumentDuplicater and updated unit tests for symbolic expression tree operators. #1103

File size: 9.6 KB
RevLine 
[645]1#region License Information
2/* HeuristicLab
[3237]3 * Copyright (C) 2002-2010 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]22using System;
[645]23using System.Collections.Generic;
[4068]24using System.Linq;
[645]25using HeuristicLab.Core;
[3237]26using HeuristicLab.Data;
27using HeuristicLab.Parameters;
[4068]28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[645]29
[3462]30namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Crossovers {
[3237]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"]; }
[645]42    }
43
[3237]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
[3338]49    protected override SymbolicExpressionTree Cross(IRandom random,
[3237]50      SymbolicExpressionTree parent0, SymbolicExpressionTree parent1,
[3294]51      IntValue maxTreeSize, IntValue maxTreeHeight, out bool success) {
[3338]52      return Cross(random, parent0, parent1, InternalCrossoverPointProbabilityParameter.ActualValue.Value, maxTreeSize.Value, maxTreeHeight.Value, out success);
[3237]53    }
54
[3338]55    public static SymbolicExpressionTree Cross(IRandom random,
[3237]56      SymbolicExpressionTree parent0, SymbolicExpressionTree parent1,
[3294]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;
[3369]61      SelectCrossoverPoint(random, parent0, internalCrossoverPointProbability, maxTreeSize - 1, maxTreeHeight - 1, out crossoverPoint0, out replacedSubtreeIndex);
[645]62
[3294]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);
[645]66
[3997]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      });
[645]74
[3997]75      if (allowedBranches.Count == 0) {
[3297]76        success = false;
77        return parent0;
78      } else {
[3294]79        var selectedBranch = SelectRandomBranch(random, allowedBranches, internalCrossoverPointProbability);
[645]80
[3294]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;
[645]87      }
88    }
89
[3338]90    private static bool IsMatchingPointType(SymbolicExpressionTreeNode parent, int replacedSubtreeIndex, SymbolicExpressionTreeNode branch) {
91      // check syntax constraints of direct parent - child relation
[4106]92      if (!parent.Grammar.ContainsSymbol(branch.Symbol) ||
93          !parent.Grammar.IsAllowedChild(parent.Symbol, branch.Symbol, replacedSubtreeIndex)) return false;
[3338]94
[3997]95      bool result = true;
96      // check point type for the whole branch
97      branch.ForEachNodePostfix((n) => {
[3998]98        result =
99          result &&
[4106]100          parent.Grammar.ContainsSymbol(n.Symbol) &&
[3998]101          n.SubTrees.Count >= parent.Grammar.GetMinSubtreeCount(n.Symbol) &&
[4106]102          n.SubTrees.Count <= parent.Grammar.GetMaxSubtreeCount(n.Symbol);
[3997]103      });
104      return result;
[3294]105    }
106
[3369]107    private static void SelectCrossoverPoint(IRandom random, SymbolicExpressionTree parent0, double internalNodeProbability, int maxBranchSize, int maxBranchHeight, out SymbolicExpressionTreeNode crossoverPoint, out int subtreeIndex) {
[3997]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
[3294]140        var selectedCrossoverPoint = leafCrossoverPoints[random.Next(leafCrossoverPoints.Count)];
[3997]141        crossoverPoint = selectedCrossoverPoint.Parent;
[3294]142        subtreeIndex = selectedCrossoverPoint.SubtreeIndex;
[3997]143      } else {
144        // otherwise select internal crossover point
[3237]145        var selectedCrossoverPoint = internalCrossoverPoints[random.Next(internalCrossoverPoints.Count)];
[3997]146        crossoverPoint = selectedCrossoverPoint.Parent;
[3237]147        subtreeIndex = selectedCrossoverPoint.SubtreeIndex;
[645]148      }
149    }
[3237]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");
[3997]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
[3989]165                                 where branch.SubTrees.Count == 0
166                                 select branch).ToList();
[3997]167          return allowedLeafBranches.SelectRandom(random);
168        }
[3237]169      } else {
[3997]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        }
[3237]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    }
[645]193  }
194}
Note: See TracBrowser for help on using the repository browser.