[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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using System.Text;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Operators;
|
---|
| 28 | using HeuristicLab.Random;
|
---|
| 29 | using HeuristicLab.Data;
|
---|
| 30 | using HeuristicLab.Constraints;
|
---|
| 31 | using System.Diagnostics;
|
---|
| 32 |
|
---|
| 33 | namespace HeuristicLab.GP {
|
---|
[835] | 34 | public class StandardCrossOver : SizeConstrictedGPCrossoverBase {
|
---|
[1197] | 35 | private int MaxRecombinationTries { get { return 20; } }
|
---|
[832] | 36 |
|
---|
[645] | 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.
|
---|
| 40 | And replaces the branch with root0 N0 in P0 with N1 from P1 if the tree-size limits are not violated.
|
---|
| 41 | When recombination with N0 and N1 would create a tree that is too large or invalid the operator randomly selects new N0 and N1
|
---|
| 42 | until a valid configuration is found.";
|
---|
| 43 | }
|
---|
| 44 | }
|
---|
| 45 |
|
---|
[835] | 46 | internal override IFunctionTree Cross(TreeGardener gardener, MersenneTwister random, IFunctionTree tree0, IFunctionTree tree1, int maxTreeSize, int maxTreeHeight) {
|
---|
[832] | 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;
|
---|
[1197] | 54 | while (parent0 == null) parent0 = gardener.GetRandomParentNode(tree0);
|
---|
[832] | 55 | // select a random branch to replace
|
---|
| 56 | replacedChildIndex = random.Next(parent0.SubTrees.Count);
|
---|
[645] | 57 |
|
---|
[832] | 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
|
---|
[645] | 61 |
|
---|
[832] | 62 | IList<IFunction> allowedFunctions = gardener.GetAllowedSubFunctions(parent0.Function, replacedChildIndex);
|
---|
| 63 | allowedCrossoverPoints = GetPossibleCrossoverPoints(gardener, tree1, maxInsertedBranchSize, maxInsertedBranchHeight, allowedFunctions);
|
---|
[1197] | 64 | } while (allowedCrossoverPoints.Count == 0 && tries++ < MaxRecombinationTries);
|
---|
[645] | 65 |
|
---|
[1197] | 66 | if (allowedCrossoverPoints.Count > 0) {
|
---|
[832] | 67 | IFunctionTree branch1 = allowedCrossoverPoints[random.Next(allowedCrossoverPoints.Count)];
|
---|
[645] | 68 |
|
---|
[832] | 69 | // replace the branch in tree0 with the selected branch from tree1
|
---|
| 70 | parent0.RemoveSubTree(replacedChildIndex);
|
---|
| 71 | parent0.InsertSubTree(replacedChildIndex, branch1);
|
---|
[645] | 72 | }
|
---|
[832] | 73 | return tree0;
|
---|
[645] | 74 | }
|
---|
| 75 |
|
---|
[832] | 76 | private List<IFunctionTree> GetPossibleCrossoverPoints(TreeGardener gardener, IFunctionTree tree, int maxInsertedBranchSize, int maxInsertedBranchHeight, IList<IFunction> allowedFunctions) {
|
---|
| 77 | List<IFunctionTree> crossoverPoints = new List<IFunctionTree>();
|
---|
[1197] | 78 | foreach (IFunctionTree possiblePoint in gardener.GetAllSubTrees(tree)) {
|
---|
| 79 | if (allowedFunctions.Contains(possiblePoint.Function) && possiblePoint.Size <= maxInsertedBranchSize && possiblePoint.Height <= maxInsertedBranchHeight)
|
---|
[832] | 80 | crossoverPoints.Add(possiblePoint);
|
---|
[645] | 81 | }
|
---|
[832] | 82 | return crossoverPoints;
|
---|
[645] | 83 | }
|
---|
| 84 | }
|
---|
| 85 | }
|
---|