Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP/Recombination/StandardCrossOver.cs @ 832

Last change on this file since 832 was 832, checked in by gkronber, 15 years ago

simplified StandardCrossOver to a simple sub-tree swapping crossover with max size and height constraints.
The old version should probably be revived as HL2StandardCrossover.

#393 (Refactor GP crossover operators to extract common code into the abstract base class GPCrossoverBase)

File size: 5.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Core;
27using HeuristicLab.Operators;
28using HeuristicLab.Random;
29using HeuristicLab.Data;
30using HeuristicLab.Constraints;
31using System.Diagnostics;
32
33namespace HeuristicLab.GP {
34  public class StandardCrossOver : GPCrossoverBase {
35    private const int MAX_RECOMBINATION_TRIES = 100;
36
37    public override string Description {
38      get {
39        return @"Takes two parent individuals P0 and P1 each. Selects a random node N0 of P0 and a random node N1 of P1.
40And replaces the branch with root0 N0 in P0 with N1 from P1 if the tree-size limits are not violated.
41When recombination with N0 and N1 would create a tree that is too large or invalid the operator randomly selects new N0 and N1
42until a valid configuration is found.";
43      }
44    }
45    public StandardCrossOver()
46      : base() {
47      AddVariableInfo(new VariableInfo("MaxTreeHeight", "The maximal allowed height of the tree", typeof(IntData), VariableKind.In));
48      AddVariableInfo(new VariableInfo("MaxTreeSize", "The maximal allowed size (number of nodes) of the tree", typeof(IntData), VariableKind.In));
49    }
50
51    internal override IFunctionTree Cross(IScope scope, TreeGardener gardener, MersenneTwister random, IFunctionTree tree0, IFunctionTree tree1) {
52      int maxTreeHeight = GetVariableValue<IntData>("MaxTreeHeight", scope, true).Data;
53      int maxTreeSize = GetVariableValue<IntData>("MaxTreeSize", scope, true).Data;
54
55      // when tree0 is terminal then try to cross into tree1, when tree1 is also terminal just return tree0 unchanged.
56      IFunctionTree newTree;
57      if(tree0.SubTrees.Count > 0) {
58        newTree = Cross(gardener, tree0, tree1, random, maxTreeSize, maxTreeHeight);
59      } else if(tree1.SubTrees.Count > 0) {
60        newTree = Cross(gardener, tree1, tree0, random, maxTreeSize, maxTreeHeight);
61      } else newTree = tree0;
62
63      // check if the new tree is valid and if the height of is still in the allowed bounds (we are not so strict for the max-size)
64      Debug.Assert(gardener.IsValidTree(newTree) && newTree.Height <= maxTreeHeight && newTree.Size <= maxTreeSize);
65      return newTree;
66    }
67
68
69    private IFunctionTree Cross(TreeGardener gardener, IFunctionTree tree0, IFunctionTree tree1, MersenneTwister random, int maxTreeSize, int maxTreeHeight) {
70      int tries = 0;
71      List<IFunctionTree> allowedCrossoverPoints = null;
72      IFunctionTree parent0;
73      int replacedChildIndex;
74      do {
75        // select a random crossover point in the first parent tree0
76        parent0 = null;
77        while(parent0 == null) parent0 = gardener.GetRandomParentNode(tree0);
78        // select a random branch to replace
79        replacedChildIndex = random.Next(parent0.SubTrees.Count);
80
81        // calculate the max size and height that the inserted branch can have
82        int maxInsertedBranchSize = maxTreeSize - (tree0.Size - parent0.SubTrees[replacedChildIndex].Size);
83        int maxInsertedBranchHeight = maxTreeHeight - gardener.GetBranchLevel(tree0, parent0); // branchlevel is 1 if tree0==parent0
84
85        IList<IFunction> allowedFunctions = gardener.GetAllowedSubFunctions(parent0.Function, replacedChildIndex);
86        allowedCrossoverPoints = GetPossibleCrossoverPoints(gardener, tree1, maxInsertedBranchSize, maxInsertedBranchHeight, allowedFunctions);
87      } while(allowedCrossoverPoints.Count == 0 || tries++ > MAX_RECOMBINATION_TRIES);
88
89      if(allowedCrossoverPoints.Count > 0) {
90        IFunctionTree branch1 = allowedCrossoverPoints[random.Next(allowedCrossoverPoints.Count)];
91
92        // replace the branch in tree0 with the selected branch from tree1
93        parent0.RemoveSubTree(replacedChildIndex);
94        parent0.InsertSubTree(replacedChildIndex, branch1);
95      }
96      return tree0;
97    }
98
99    private List<IFunctionTree> GetPossibleCrossoverPoints(TreeGardener gardener, IFunctionTree tree, int maxInsertedBranchSize, int maxInsertedBranchHeight, IList<IFunction> allowedFunctions) {
100      List<IFunctionTree> crossoverPoints = new List<IFunctionTree>();
101      foreach(IFunctionTree possiblePoint in gardener.GetAllSubTrees(tree)) {
102        if(allowedFunctions.Contains(possiblePoint.Function) && possiblePoint.Size <= maxInsertedBranchSize && possiblePoint.Height <= maxInsertedBranchHeight)
103          crossoverPoints.Add(possiblePoint);
104      }
105      return crossoverPoints;
106    }
107  }
108}
Note: See TracBrowser for help on using the repository browser.