Free cookie consent management tool by TermsFeed Policy Generator

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

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

added type constraint check for one-point crossover (#305)

File size: 6.0 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      CompositeOperation initOperations = new CompositeOperation();
62
63      int children = scope.SubScopes.Count / 2;
64      for(int i = 0; i < children; i++) {
65        IScope parent1 = scope.SubScopes[0];
66        scope.RemoveSubScope(parent1);
67        IScope parent2 = scope.SubScopes[0];
68        scope.RemoveSubScope(parent2);
69        IScope child = new Scope(i.ToString());
70        IOperation childInitOperation = Cross(scope, random, gardener, parent1, parent2, child);
71        initOperations.AddOperation(childInitOperation);
72        scope.AddSubScope(child);
73      }
74
75      return initOperations;
76    }
77
78    private IOperation Cross(IScope scope, MersenneTwister random, TreeGardener gardener, IScope parent1, IScope parent2, IScope child) {
79      IFunctionTree newTree = Cross(random, gardener, parent1, parent2);
80
81      int newTreeSize = newTree.Size;
82      int newTreeHeight = newTree.Height;
83      child.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("FunctionTree"), newTree));
84      child.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TreeSize"), new IntData(newTreeSize)));
85      child.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TreeHeight"), new IntData(newTreeHeight)));
86
87      return null;
88    }
89
90
91    private IFunctionTree Cross(MersenneTwister random, TreeGardener gardener, IScope f, IScope g) {
92      IFunctionTree tree0 = f.GetVariableValue<IFunctionTree>("FunctionTree", false);
93      int tree0Height = f.GetVariableValue<IntData>("TreeHeight", false).Data;
94      int tree0Size = f.GetVariableValue<IntData>("TreeSize", false).Data;
95
96      IFunctionTree tree1 = g.GetVariableValue<IFunctionTree>("FunctionTree", false);
97      int tree1Height = g.GetVariableValue<IntData>("TreeHeight", false).Data;
98      int tree1Size = g.GetVariableValue<IntData>("TreeSize", false).Data;
99
100      List<IFunctionTree[]> allowedCrossOverPoints = GetCrossOverPoints(gardener, null, tree0, tree1);
101      if(allowedCrossOverPoints.Count == 0) {
102        if(random.NextDouble() < 0.5) return tree0; else return tree1;
103      }
104      IFunctionTree[] crossOverPoints = allowedCrossOverPoints[random.Next(allowedCrossOverPoints.Count)];
105      IFunctionTree parent = crossOverPoints[0];
106      IFunctionTree replacedBranch = crossOverPoints[1];
107      IFunctionTree insertedBranch = crossOverPoints[2];
108      if(parent == null) return insertedBranch;
109      else {
110        int i = 0;
111        while(parent.SubTrees[i] != replacedBranch) i++;
112        parent.RemoveSubTree(i);
113        parent.InsertSubTree(i, insertedBranch);
114        return tree0;
115      }
116    }
117
118    private List<IFunctionTree[]> GetCrossOverPoints(TreeGardener gardener, IFunctionTree parent, IFunctionTree tree0, IFunctionTree tree1) {
119      List<IFunctionTree[]> results = new List<IFunctionTree[]>();
120      if(tree0.SubTrees.Count != tree1.SubTrees.Count) return results;
121      // invariant arity - number of subtrees is equal in both trees
122      for(int i = 0; i < tree0.SubTrees.Count; i++) {
123        if(gardener.GetAllowedSubFunctions(tree0.Function, i).Contains(tree1.SubTrees[i].Function)) {
124          results.Add(new IFunctionTree[] { tree0, tree0.SubTrees[i], tree1.SubTrees[i]});
125        }
126        results.AddRange(GetCrossOverPoints(gardener, tree0, tree0.SubTrees[i], tree1.SubTrees[i]));
127      }
128      return results;
129    }
130  }
131}
Note: See TracBrowser for help on using the repository browser.