Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.2/sources/HeuristicLab.GP.Operators/3.3/Recombination/OnePointCrossOver.cs @ 4105

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

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

File size: 3.3 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.Collections.Generic;
23using HeuristicLab.Core;
24using HeuristicLab.GP.Interfaces;
25
26namespace HeuristicLab.GP.Operators {
27  /// <summary>
28  /// Implementation of a homologous one point crossover operator as described in:
29  /// W. B. Langdon and R. Poli.  Foundations of Genetic Programming. Springer-Verlag, 2002.
30  /// </summary>
31  public class OnePointCrossOver : SizeConstrictedGPCrossoverBase {
32    // internal data structure to represent crossover points
33    private class CrossoverPoint {
34      public IFunctionTree parent0;
35      public IFunctionTree parent1;
36      public int childIndex;
37    }
38    public override string Description {
39      get {
40        return @"One point crossover for trees as described in W. B. Langdon and R. Poli. Foundations of Genetic Programming. Springer-Verlag, 2002.";
41      }
42    }
43
44    internal override IFunctionTree Cross(TreeGardener gardener, IRandom random, IFunctionTree tree0, IFunctionTree tree1, int maxTreeSize, int maxTreeHeight) {
45      List<CrossoverPoint> allowedCrossOverPoints = new List<CrossoverPoint>();
46      GetCrossOverPoints(gardener, tree0, tree1, maxTreeSize - tree0.GetSize(), allowedCrossOverPoints);
47      if (allowedCrossOverPoints.Count > 0) {
48        CrossoverPoint crossOverPoint = allowedCrossOverPoints[random.Next(allowedCrossOverPoints.Count)];
49        IFunctionTree parent0 = crossOverPoint.parent0;
50        IFunctionTree branch1 = crossOverPoint.parent1.SubTrees[crossOverPoint.childIndex];
51        parent0.RemoveSubTree(crossOverPoint.childIndex);
52        parent0.InsertSubTree(crossOverPoint.childIndex, branch1);
53      }
54      return tree0;
55    }
56
57    private void GetCrossOverPoints(TreeGardener gardener, IFunctionTree branch0, IFunctionTree branch1, int maxNewNodes, List<CrossoverPoint> crossoverPoints) {
58      if (branch0.SubTrees.Count != branch1.SubTrees.Count) return;
59
60      for (int i = 0; i < branch0.SubTrees.Count; i++) {
61        // if the current branch can be attached as a sub-tree to branch0
62        if (gardener.GetAllowedSubFunctions(branch0.Function, i).Contains(branch1.SubTrees[i].Function) &&
63           branch1.SubTrees[i].GetSize() - branch0.SubTrees[i].GetSize() <= maxNewNodes) {
64          CrossoverPoint p = new CrossoverPoint();
65          p.childIndex = i;
66          p.parent0 = branch0;
67          p.parent1 = branch1;
68          crossoverPoints.Add(p);
69        }
70        GetCrossOverPoints(gardener, branch0.SubTrees[i], branch1.SubTrees[i], maxNewNodes, crossoverPoints);
71      }
72    }
73  }
74}
Note: See TracBrowser for help on using the repository browser.