Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP.Operators/3.3/Recombination/StandardCrossOver.cs @ 3397

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

Merged changes from GP-refactoring branch back into the trunk #713.

File size: 3.8 KB
RevLine 
[645]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.Collections.Generic;
23using HeuristicLab.Core;
[2210]24using HeuristicLab.GP.Interfaces;
[645]25
[2212]26namespace HeuristicLab.GP.Operators {
[835]27  public class StandardCrossOver : SizeConstrictedGPCrossoverBase {
[1197]28    private int MaxRecombinationTries { get { return 20; } }
[832]29
[645]30    public override string Description {
31      get {
32        return @"Takes two parent individuals P0 and P1 each. Selects a random node N0 of P0 and a random node N1 of P1.
33And replaces the branch with root0 N0 in P0 with N1 from P1 if the tree-size limits are not violated.
34When recombination with N0 and N1 would create a tree that is too large or invalid the operator randomly selects new N0 and N1
35until a valid configuration is found.";
36      }
37    }
38
[1286]39    internal override IFunctionTree Cross(TreeGardener gardener, IRandom random, IFunctionTree tree0, IFunctionTree tree1, int maxTreeSize, int maxTreeHeight) {
[832]40      int tries = 0;
41      List<IFunctionTree> allowedCrossoverPoints = null;
42      IFunctionTree parent0;
43      int replacedChildIndex;
44      do {
45        // select a random crossover point in the first parent tree0
46        parent0 = null;
[1197]47        while (parent0 == null) parent0 = gardener.GetRandomParentNode(tree0);
[832]48        // select a random branch to replace
49        replacedChildIndex = random.Next(parent0.SubTrees.Count);
[645]50
[832]51        // calculate the max size and height that the inserted branch can have
[2210]52        int maxInsertedBranchSize = maxTreeSize - (tree0.GetSize() - parent0.SubTrees[replacedChildIndex].GetSize());
[832]53        int maxInsertedBranchHeight = maxTreeHeight - gardener.GetBranchLevel(tree0, parent0); // branchlevel is 1 if tree0==parent0
[645]54
[2202]55        IList<IFunction> allowedFunctions = new List<IFunction>(gardener.GetAllowedSubFunctions(parent0.Function, replacedChildIndex));
[2210]56        allowedCrossoverPoints = GetPossibleCrossoverPoints(tree1, maxInsertedBranchSize, maxInsertedBranchHeight, allowedFunctions);
[1197]57      } while (allowedCrossoverPoints.Count == 0 && tries++ < MaxRecombinationTries);
[645]58
[1197]59      if (allowedCrossoverPoints.Count > 0) {
[832]60        IFunctionTree branch1 = allowedCrossoverPoints[random.Next(allowedCrossoverPoints.Count)];
[645]61
[832]62        // replace the branch in tree0 with the selected branch from tree1
63        parent0.RemoveSubTree(replacedChildIndex);
64        parent0.InsertSubTree(replacedChildIndex, branch1);
[645]65      }
[832]66      return tree0;
[645]67    }
68
[2210]69    private List<IFunctionTree> GetPossibleCrossoverPoints(IFunctionTree tree, int maxInsertedBranchSize, int maxInsertedBranchHeight, IList<IFunction> allowedFunctions) {
[832]70      List<IFunctionTree> crossoverPoints = new List<IFunctionTree>();
[2210]71      foreach (IFunctionTree possiblePoint in TreeGardener.GetAllSubTrees(tree)) {
72        if (allowedFunctions.Contains(possiblePoint.Function) && possiblePoint.GetSize() <= maxInsertedBranchSize && possiblePoint.GetHeight() <= maxInsertedBranchHeight)
[832]73          crossoverPoints.Add(possiblePoint);
[645]74      }
[832]75      return crossoverPoints;
[645]76    }
77  }
78}
Note: See TracBrowser for help on using the repository browser.