Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP/Recombination/OnePointCrossOver.cs @ 809

Last change on this file since 809 was 809, checked in by gkronber, 16 years ago

implemented #387 (Onepoint crossover should generate two children each crossover event)

File size: 6.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;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Core;
27using HeuristicLab.Operators;
28using HeuristicLab.Random;
29using HeuristicLab.Data;
30using HeuristicLab.Constraints;
31using System.Diagnostics;
32
33namespace 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));
47      AddVariableInfo(new VariableInfo("OperatorLibrary", "The operator library containing all available operators", typeof(GPOperatorLibrary), VariableKind.In));
48      AddVariableInfo(new VariableInfo("FunctionTree", "The tree to mutate", typeof(IFunctionTree), VariableKind.In | VariableKind.New));
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));
51    }
52
53    public override IOperation Apply(IScope scope) {
54      MersenneTwister random = GetVariableValue<MersenneTwister>("Random", scope, true);
55      GPOperatorLibrary opLibrary = GetVariableValue<GPOperatorLibrary>("OperatorLibrary", scope, true);
56      TreeGardener gardener = new TreeGardener(random, opLibrary);
57
58      if ((scope.SubScopes.Count % 2) != 0)
59        throw new InvalidOperationException("Number of parents is not even");
60
61      int crossoverEvents = scope.SubScopes.Count / 2;
62      for (int i = 0; i < crossoverEvents; i++) {
63        IScope parent1 = scope.SubScopes[0];
64        scope.RemoveSubScope(parent1);
65        IScope parent2 = scope.SubScopes[0];
66        scope.RemoveSubScope(parent2);
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);
72      }
73      return null;
74    }
75
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);
80
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)));
87    }
88
89
90    private void Cross(MersenneTwister random, TreeGardener gardener, IScope f, IScope g, out IFunctionTree child0, out IFunctionTree child1) {
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
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);
110      }
111
112      if (random.NextDouble() < 0.5) {
113        child0 = tree0;
114        child1 = tree1;
115      } else {
116        child0 = tree1;
117        child1 = tree0;
118      }
119    }
120    class CrossoverPoint {
121      public IFunctionTree parent0;
122      public IFunctionTree parent1;
123      public int childIndex;
124    }
125
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);
139        }
140        results.AddRange(GetCrossOverPoints(gardener, branch0.SubTrees[i], branch1.SubTrees[i]));
141      }
142      return results;
143    }
144  }
145}
Note: See TracBrowser for help on using the repository browser.