[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 {
|
---|
[832] | 34 | public class StandardCrossOver : GPCrossoverBase {
|
---|
| 35 | private const int MAX_RECOMBINATION_TRIES = 100;
|
---|
| 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 | public StandardCrossOver()
|
---|
| 46 | : base() {
|
---|
| 47 | AddVariableInfo(new VariableInfo("MaxTreeHeight", "The maximal allowed height of the tree", typeof(IntData), VariableKind.In));
|
---|
| 48 | AddVariableInfo(new VariableInfo("MaxTreeSize", "The maximal allowed size (number of nodes) of the tree", typeof(IntData), VariableKind.In));
|
---|
| 49 | }
|
---|
| 50 |
|
---|
[832] | 51 | internal override IFunctionTree Cross(IScope scope, TreeGardener gardener, MersenneTwister random, IFunctionTree tree0, IFunctionTree tree1) {
|
---|
[645] | 52 | int maxTreeHeight = GetVariableValue<IntData>("MaxTreeHeight", scope, true).Data;
|
---|
| 53 | int maxTreeSize = GetVariableValue<IntData>("MaxTreeSize", scope, true).Data;
|
---|
| 54 |
|
---|
[832] | 55 | // when tree0 is terminal then try to cross into tree1, when tree1 is also terminal just return tree0 unchanged.
|
---|
| 56 | IFunctionTree newTree;
|
---|
| 57 | if(tree0.SubTrees.Count > 0) {
|
---|
| 58 | newTree = Cross(gardener, tree0, tree1, random, maxTreeSize, maxTreeHeight);
|
---|
| 59 | } else if(tree1.SubTrees.Count > 0) {
|
---|
| 60 | newTree = Cross(gardener, tree1, tree0, random, maxTreeSize, maxTreeHeight);
|
---|
| 61 | } else newTree = tree0;
|
---|
[645] | 62 |
|
---|
| 63 | // check if the new tree is valid and if the height of is still in the allowed bounds (we are not so strict for the max-size)
|
---|
[832] | 64 | Debug.Assert(gardener.IsValidTree(newTree) && newTree.Height <= maxTreeHeight && newTree.Size <= maxTreeSize);
|
---|
| 65 | return newTree;
|
---|
[645] | 66 | }
|
---|
| 67 |
|
---|
| 68 |
|
---|
[832] | 69 | private IFunctionTree Cross(TreeGardener gardener, IFunctionTree tree0, IFunctionTree tree1, MersenneTwister random, int maxTreeSize, int maxTreeHeight) {
|
---|
| 70 | int tries = 0;
|
---|
| 71 | List<IFunctionTree> allowedCrossoverPoints = null;
|
---|
| 72 | IFunctionTree parent0;
|
---|
| 73 | int replacedChildIndex;
|
---|
| 74 | do {
|
---|
| 75 | // select a random crossover point in the first parent tree0
|
---|
| 76 | parent0 = null;
|
---|
| 77 | while(parent0 == null) parent0 = gardener.GetRandomParentNode(tree0);
|
---|
| 78 | // select a random branch to replace
|
---|
| 79 | replacedChildIndex = random.Next(parent0.SubTrees.Count);
|
---|
[645] | 80 |
|
---|
[832] | 81 | // calculate the max size and height that the inserted branch can have
|
---|
| 82 | int maxInsertedBranchSize = maxTreeSize - (tree0.Size - parent0.SubTrees[replacedChildIndex].Size);
|
---|
| 83 | int maxInsertedBranchHeight = maxTreeHeight - gardener.GetBranchLevel(tree0, parent0); // branchlevel is 1 if tree0==parent0
|
---|
[645] | 84 |
|
---|
[832] | 85 | IList<IFunction> allowedFunctions = gardener.GetAllowedSubFunctions(parent0.Function, replacedChildIndex);
|
---|
| 86 | allowedCrossoverPoints = GetPossibleCrossoverPoints(gardener, tree1, maxInsertedBranchSize, maxInsertedBranchHeight, allowedFunctions);
|
---|
| 87 | } while(allowedCrossoverPoints.Count == 0 || tries++ > MAX_RECOMBINATION_TRIES);
|
---|
[645] | 88 |
|
---|
[832] | 89 | if(allowedCrossoverPoints.Count > 0) {
|
---|
| 90 | IFunctionTree branch1 = allowedCrossoverPoints[random.Next(allowedCrossoverPoints.Count)];
|
---|
[645] | 91 |
|
---|
[832] | 92 | // replace the branch in tree0 with the selected branch from tree1
|
---|
| 93 | parent0.RemoveSubTree(replacedChildIndex);
|
---|
| 94 | parent0.InsertSubTree(replacedChildIndex, branch1);
|
---|
[645] | 95 | }
|
---|
[832] | 96 | return tree0;
|
---|
[645] | 97 | }
|
---|
| 98 |
|
---|
[832] | 99 | private List<IFunctionTree> GetPossibleCrossoverPoints(TreeGardener gardener, IFunctionTree tree, int maxInsertedBranchSize, int maxInsertedBranchHeight, IList<IFunction> allowedFunctions) {
|
---|
| 100 | List<IFunctionTree> crossoverPoints = new List<IFunctionTree>();
|
---|
| 101 | foreach(IFunctionTree possiblePoint in gardener.GetAllSubTrees(tree)) {
|
---|
| 102 | if(allowedFunctions.Contains(possiblePoint.Function) && possiblePoint.Size <= maxInsertedBranchSize && possiblePoint.Height <= maxInsertedBranchHeight)
|
---|
| 103 | crossoverPoints.Add(possiblePoint);
|
---|
[645] | 104 | }
|
---|
[832] | 105 | return crossoverPoints;
|
---|
[645] | 106 | }
|
---|
| 107 | }
|
---|
| 108 | }
|
---|