Free cookie consent management tool by TermsFeed Policy Generator

source: branches/plugins/HeuristicLab.GP/3.2/Recombination/StandardCrossOver.cs @ 3043

Last change on this file since 3043 was 1196, checked in by gkronber, 16 years ago

fixed #479 (Crossover operators create trees that are larger than the allowed max size)

File size: 3.9 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 : SizeConstrictedGPCrossoverBase {
35    private int MaxRecombinationTries { get { return 20; } }
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
46    internal override IFunctionTree Cross(TreeGardener gardener, MersenneTwister random, IFunctionTree tree0, IFunctionTree tree1, int maxTreeSize, int maxTreeHeight) {
47      int tries = 0;
48      List<IFunctionTree> allowedCrossoverPoints = null;
49      IFunctionTree parent0;
50      int replacedChildIndex;
51      do {
52        // select a random crossover point in the first parent tree0
53        parent0 = null;
54        while (parent0 == null) parent0 = gardener.GetRandomParentNode(tree0);
55        // select a random branch to replace
56        replacedChildIndex = random.Next(parent0.SubTrees.Count);
57
58        // calculate the max size and height that the inserted branch can have
59        int maxInsertedBranchSize = maxTreeSize - (tree0.Size - parent0.SubTrees[replacedChildIndex].Size);
60        int maxInsertedBranchHeight = maxTreeHeight - gardener.GetBranchLevel(tree0, parent0); // branchlevel is 1 if tree0==parent0
61
62        IList<IFunction> allowedFunctions = gardener.GetAllowedSubFunctions(parent0.Function, replacedChildIndex);
63        allowedCrossoverPoints = GetPossibleCrossoverPoints(gardener, tree1, maxInsertedBranchSize, maxInsertedBranchHeight, allowedFunctions);
64      } while (allowedCrossoverPoints.Count == 0 && tries++ < MaxRecombinationTries);
65
66      if (allowedCrossoverPoints.Count > 0) {
67        IFunctionTree branch1 = allowedCrossoverPoints[random.Next(allowedCrossoverPoints.Count)];
68
69        // replace the branch in tree0 with the selected branch from tree1
70        parent0.RemoveSubTree(replacedChildIndex);
71        parent0.InsertSubTree(replacedChildIndex, branch1);
72      }
73      return tree0;
74    }
75
76    private List<IFunctionTree> GetPossibleCrossoverPoints(TreeGardener gardener, IFunctionTree tree, int maxInsertedBranchSize, int maxInsertedBranchHeight, IList<IFunction> allowedFunctions) {
77      List<IFunctionTree> crossoverPoints = new List<IFunctionTree>();
78      foreach (IFunctionTree possiblePoint in gardener.GetAllSubTrees(tree)) {
79        if (allowedFunctions.Contains(possiblePoint.Function) && possiblePoint.Size <= maxInsertedBranchSize && possiblePoint.Height <= maxInsertedBranchHeight)
80          crossoverPoints.Add(possiblePoint);
81      }
82      return crossoverPoints;
83    }
84  }
85}
Note: See TracBrowser for help on using the repository browser.