[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 {
|
---|
| 34 | /// <summary>
|
---|
| 35 | /// Implementation of a homologous one point crossover operator as described in:
|
---|
| 36 | /// W. B. Langdon and R. Poli. Foundations of Genetic Programming. Springer-Verlag, 2002.
|
---|
| 37 | /// </summary>
|
---|
| 38 | public class OnePointCrossOver : OperatorBase {
|
---|
| 39 | public override string Description {
|
---|
| 40 | get {
|
---|
| 41 | return @"";
|
---|
| 42 | }
|
---|
| 43 | }
|
---|
| 44 | public OnePointCrossOver()
|
---|
| 45 | : base() {
|
---|
| 46 | AddVariableInfo(new VariableInfo("Random", "Pseudo random number generator", typeof(MersenneTwister), VariableKind.In));
|
---|
[707] | 47 | AddVariableInfo(new VariableInfo("OperatorLibrary", "The operator library containing all available operators", typeof(GPOperatorLibrary), VariableKind.In));
|
---|
[645] | 48 | AddVariableInfo(new VariableInfo("FunctionTree", "The tree to mutate", typeof(IFunctionTree), VariableKind.In | VariableKind.New));
|
---|
[666] | 49 | AddVariableInfo(new VariableInfo("TreeSize", "The size (number of nodes) of the tree", typeof(IntData), VariableKind.In | VariableKind.New));
|
---|
| 50 | AddVariableInfo(new VariableInfo("TreeHeight", "The height of the tree", typeof(IntData), VariableKind.In | VariableKind.New));
|
---|
[645] | 51 | }
|
---|
| 52 |
|
---|
| 53 | public override IOperation Apply(IScope scope) {
|
---|
| 54 | MersenneTwister random = GetVariableValue<MersenneTwister>("Random", scope, true);
|
---|
[707] | 55 | GPOperatorLibrary opLibrary = GetVariableValue<GPOperatorLibrary>("OperatorLibrary", scope, true);
|
---|
| 56 | TreeGardener gardener = new TreeGardener(random, opLibrary);
|
---|
[645] | 57 |
|
---|
[809] | 58 | if ((scope.SubScopes.Count % 2) != 0)
|
---|
[645] | 59 | throw new InvalidOperationException("Number of parents is not even");
|
---|
| 60 |
|
---|
[809] | 61 | int crossoverEvents = scope.SubScopes.Count / 2;
|
---|
| 62 | for (int i = 0; i < crossoverEvents; i++) {
|
---|
[645] | 63 | IScope parent1 = scope.SubScopes[0];
|
---|
| 64 | scope.RemoveSubScope(parent1);
|
---|
| 65 | IScope parent2 = scope.SubScopes[0];
|
---|
| 66 | scope.RemoveSubScope(parent2);
|
---|
[809] | 67 | IScope child0 = new Scope((i * 2).ToString());
|
---|
| 68 | IScope child1 = new Scope((i * 2 + 1).ToString());
|
---|
| 69 | Cross(scope, random, gardener, parent1, parent2, child0, child1);
|
---|
| 70 | scope.AddSubScope(child0);
|
---|
| 71 | scope.AddSubScope(child1);
|
---|
[645] | 72 | }
|
---|
[809] | 73 | return null;
|
---|
[645] | 74 | }
|
---|
| 75 |
|
---|
[809] | 76 | private void Cross(IScope scope, MersenneTwister random, TreeGardener gardener, IScope parent1, IScope parent2, IScope child0, IScope child1) {
|
---|
| 77 | IFunctionTree newTree0;
|
---|
| 78 | IFunctionTree newTree1;
|
---|
| 79 | Cross(random, gardener, parent1, parent2, out newTree0, out newTree1);
|
---|
[645] | 80 |
|
---|
[809] | 81 | child0.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("FunctionTree"), newTree0));
|
---|
| 82 | child0.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TreeSize"), new IntData(newTree0.Size)));
|
---|
| 83 | child0.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TreeHeight"), new IntData(newTree0.Height)));
|
---|
| 84 | child1.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("FunctionTree"), newTree1));
|
---|
| 85 | child1.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TreeSize"), new IntData(newTree1.Size)));
|
---|
| 86 | child1.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TreeHeight"), new IntData(newTree1.Height)));
|
---|
[645] | 87 | }
|
---|
| 88 |
|
---|
| 89 |
|
---|
[809] | 90 | private void Cross(MersenneTwister random, TreeGardener gardener, IScope f, IScope g, out IFunctionTree child0, out IFunctionTree child1) {
|
---|
[645] | 91 | IFunctionTree tree0 = f.GetVariableValue<IFunctionTree>("FunctionTree", false);
|
---|
| 92 | int tree0Height = f.GetVariableValue<IntData>("TreeHeight", false).Data;
|
---|
| 93 | int tree0Size = f.GetVariableValue<IntData>("TreeSize", false).Data;
|
---|
| 94 |
|
---|
| 95 | IFunctionTree tree1 = g.GetVariableValue<IFunctionTree>("FunctionTree", false);
|
---|
| 96 | int tree1Height = g.GetVariableValue<IntData>("TreeHeight", false).Data;
|
---|
| 97 | int tree1Size = g.GetVariableValue<IntData>("TreeSize", false).Data;
|
---|
| 98 |
|
---|
[809] | 99 | List<CrossoverPoint> allowedCrossOverPoints = GetCrossOverPoints(gardener, tree0, tree1);
|
---|
| 100 | if (allowedCrossOverPoints.Count > 0) {
|
---|
| 101 | CrossoverPoint crossOverPoint = allowedCrossOverPoints[random.Next(allowedCrossOverPoints.Count)];
|
---|
| 102 | IFunctionTree parent0 = crossOverPoint.parent0;
|
---|
| 103 | IFunctionTree parent1 = crossOverPoint.parent1;
|
---|
| 104 | IFunctionTree branch0 = crossOverPoint.parent0.SubTrees[crossOverPoint.childIndex];
|
---|
| 105 | IFunctionTree branch1 = crossOverPoint.parent1.SubTrees[crossOverPoint.childIndex];
|
---|
| 106 | parent0.RemoveSubTree(crossOverPoint.childIndex);
|
---|
| 107 | parent1.RemoveSubTree(crossOverPoint.childIndex);
|
---|
| 108 | parent0.InsertSubTree(crossOverPoint.childIndex, branch1);
|
---|
| 109 | parent1.InsertSubTree(crossOverPoint.childIndex, branch0);
|
---|
[645] | 110 | }
|
---|
[809] | 111 |
|
---|
| 112 | if (random.NextDouble() < 0.5) {
|
---|
| 113 | child0 = tree0;
|
---|
| 114 | child1 = tree1;
|
---|
| 115 | } else {
|
---|
| 116 | child0 = tree1;
|
---|
| 117 | child1 = tree0;
|
---|
[666] | 118 | }
|
---|
[645] | 119 | }
|
---|
[809] | 120 | class CrossoverPoint {
|
---|
| 121 | public IFunctionTree parent0;
|
---|
| 122 | public IFunctionTree parent1;
|
---|
| 123 | public int childIndex;
|
---|
| 124 | }
|
---|
[645] | 125 |
|
---|
[809] | 126 | private List<CrossoverPoint> GetCrossOverPoints(TreeGardener gardener, IFunctionTree branch0, IFunctionTree branch1) {
|
---|
| 127 | List<CrossoverPoint> results = new List<CrossoverPoint>();
|
---|
| 128 | if (branch0.SubTrees.Count != branch1.SubTrees.Count) return results;
|
---|
| 129 |
|
---|
| 130 | for (int i = 0; i < branch0.SubTrees.Count; i++) {
|
---|
| 131 | // if the branches fit to the parent
|
---|
| 132 | if (gardener.GetAllowedSubFunctions(branch0.Function, i).Contains(branch1.SubTrees[i].Function) &&
|
---|
| 133 | gardener.GetAllowedSubFunctions(branch1.Function, i).Contains(branch0.SubTrees[i].Function)) {
|
---|
| 134 | CrossoverPoint p = new CrossoverPoint();
|
---|
| 135 | p.childIndex = i;
|
---|
| 136 | p.parent0 = branch0;
|
---|
| 137 | p.parent1 = branch1;
|
---|
| 138 | results.Add(p);
|
---|
[707] | 139 | }
|
---|
[809] | 140 | results.AddRange(GetCrossOverPoints(gardener, branch0.SubTrees[i], branch1.SubTrees[i]));
|
---|
[645] | 141 | }
|
---|
| 142 | return results;
|
---|
| 143 | }
|
---|
| 144 | }
|
---|
| 145 | }
|
---|