Free cookie consent management tool by TermsFeed Policy Generator

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

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

fixed #305 (Implementation of GP onepoint crossover doesn't match description as given in FoGP)

File size: 5.4 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("FunctionTree", "The tree to mutate", typeof(IFunctionTree), VariableKind.In | VariableKind.New));
48      AddVariableInfo(new VariableInfo("TreeSize", "The size (number of nodes) of the tree", typeof(IntData), VariableKind.In | VariableKind.New));
49      AddVariableInfo(new VariableInfo("TreeHeight", "The height of the tree", typeof(IntData), VariableKind.In | VariableKind.New));
50    }
51
52    public override IOperation Apply(IScope scope) {
53      MersenneTwister random = GetVariableValue<MersenneTwister>("Random", scope, true);
54
55      if((scope.SubScopes.Count % 2) != 0)
56        throw new InvalidOperationException("Number of parents is not even");
57
58      CompositeOperation initOperations = new CompositeOperation();
59
60      int children = scope.SubScopes.Count / 2;
61      for(int i = 0; i < children; i++) {
62        IScope parent1 = scope.SubScopes[0];
63        scope.RemoveSubScope(parent1);
64        IScope parent2 = scope.SubScopes[0];
65        scope.RemoveSubScope(parent2);
66        IScope child = new Scope(i.ToString());
67        IOperation childInitOperation = Cross(scope, random, parent1, parent2, child);
68        initOperations.AddOperation(childInitOperation);
69        scope.AddSubScope(child);
70      }
71
72      return initOperations;
73    }
74
75    private IOperation Cross(IScope scope, MersenneTwister random, IScope parent1, IScope parent2, IScope child) {
76      IFunctionTree newTree = Cross(random, parent1, parent2);
77
78      int newTreeSize = newTree.Size;
79      int newTreeHeight = newTree.Height;
80      child.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("FunctionTree"), newTree));
81      child.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TreeSize"), new IntData(newTreeSize)));
82      child.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TreeHeight"), new IntData(newTreeHeight)));
83
84      return null;
85    }
86
87
88    private IFunctionTree Cross(MersenneTwister random, IScope f, IScope g) {
89      IFunctionTree tree0 = f.GetVariableValue<IFunctionTree>("FunctionTree", false);
90      int tree0Height = f.GetVariableValue<IntData>("TreeHeight", false).Data;
91      int tree0Size = f.GetVariableValue<IntData>("TreeSize", false).Data;
92
93      IFunctionTree tree1 = g.GetVariableValue<IFunctionTree>("FunctionTree", false);
94      int tree1Height = g.GetVariableValue<IntData>("TreeHeight", false).Data;
95      int tree1Size = g.GetVariableValue<IntData>("TreeSize", false).Data;
96
97      List<IFunctionTree[]> allowedCrossOverPoints = GetCrossOverPoints(null, tree0, tree1);
98      if(allowedCrossOverPoints.Count == 0) {
99        if(random.NextDouble() < 0.5) return tree0; else return tree1;
100      }
101      IFunctionTree[] crossOverPoints = allowedCrossOverPoints[random.Next(allowedCrossOverPoints.Count)];
102      IFunctionTree parent = crossOverPoints[0];
103      IFunctionTree replacedBranch = crossOverPoints[1];
104      IFunctionTree insertedBranch = crossOverPoints[2];
105      if(parent == null) return insertedBranch;
106      else {
107        int i = 0;
108        while(parent.SubTrees[i] != replacedBranch) i++;
109        parent.RemoveSubTree(i);
110        parent.InsertSubTree(i, insertedBranch);
111        return tree0;
112      }
113    }
114
115    private List<IFunctionTree[]> GetCrossOverPoints(IFunctionTree parent, IFunctionTree tree0, IFunctionTree tree1) {
116      List<IFunctionTree[]> results = new List<IFunctionTree[]>();
117      if(tree0.SubTrees.Count != tree1.SubTrees.Count) return results;
118      // invariant arity - number of subtrees is equal in both trees
119
120      results.Add(new IFunctionTree[] { parent, tree0, tree1 });
121      for(int i = 0; i < tree0.SubTrees.Count; i++) {
122        results.AddRange(GetCrossOverPoints(tree0, tree0.SubTrees[i], tree1.SubTrees[i]));
123      }
124      return results;
125    }
126  }
127}
Note: See TracBrowser for help on using the repository browser.