Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTree/3.3/Crossovers/StandardCrossOver.cs @ 3218

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

copied classes for SymbolicTreeSolutionEncoding form HL.GP plugins. #937 (Data types and operators for symbolic expression tree encoding)

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