Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP/Recombination/GPCrossoverBase.cs @ 911

Last change on this file since 911 was 835, checked in by gkronber, 16 years ago
  • added another abstract base class for GP crossover operators with maxsize and maxheight constraints
  • changed StandardCrossOver to inherit from SizeConstrictedGPCrossoverBase
  • changed SizeFairCrossOver to inherit from SizeConstrictedGPCrossoverBase
  • generally improved code of SizeFairCrossOver
  • changed LangdonHomologousCrossOver to inherit from SizeFairCrossOver and implemented only the method to finally select branches.

#393 (Refactor GP crossover operators to extract common code into the abstract base class GPCrossoverBase)

File size: 3.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  public abstract class GPCrossoverBase : OperatorBase {
35    public GPCrossoverBase()
36      : base() {
37      AddVariableInfo(new VariableInfo("Random", "Pseudo random number generator", typeof(MersenneTwister), VariableKind.In));
38      AddVariableInfo(new VariableInfo("OperatorLibrary", "The operator library containing all available operators", typeof(GPOperatorLibrary), VariableKind.In));
39      AddVariableInfo(new VariableInfo("FunctionTree", "The tree to mutate", typeof(IFunctionTree), VariableKind.In | VariableKind.New));
40      AddVariableInfo(new VariableInfo("TreeSize", "The size (number of nodes) of the tree", typeof(IntData), VariableKind.New));
41      AddVariableInfo(new VariableInfo("TreeHeight", "The height of the tree", typeof(IntData), VariableKind.New));
42    }
43
44    public override IOperation Apply(IScope scope) {
45      MersenneTwister random = GetVariableValue<MersenneTwister>("Random", scope, true);
46      GPOperatorLibrary opLibrary = GetVariableValue<GPOperatorLibrary>("OperatorLibrary", scope, true);
47      TreeGardener gardener = new TreeGardener(random, opLibrary);
48
49      if ((scope.SubScopes.Count % 2) != 0)
50        throw new InvalidOperationException("Number of parents is not even");
51
52      int children = scope.SubScopes.Count / 2;
53      for (int i = 0; i < children; i++) {
54        IFunctionTree parent0 = TakeNextParent(scope);
55        IFunctionTree parent1 = TakeNextParent(scope);
56
57        // randomly swap parents to remove a possible bias from selection (e.g. when using gender-specific selection)
58        if (random.NextDouble() < 0.5) {
59          IFunctionTree tmp = parent0;
60          parent0 = parent1;
61          parent1 = tmp;
62        }
63
64        IFunctionTree child = Cross(scope, gardener, random, parent0, parent1);
65        Debug.Assert(gardener.IsValidTree(child));
66        IScope childScope = new Scope(i.ToString());
67        childScope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("FunctionTree"), child));
68        childScope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TreeSize"), new IntData(child.Size)));
69        childScope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TreeHeight"), new IntData(child.Height)));
70        scope.AddSubScope(childScope);
71      }
72
73      return null;
74    }
75
76    internal abstract IFunctionTree Cross(IScope scope, TreeGardener gardener, MersenneTwister random, IFunctionTree tree0, IFunctionTree tree1);
77
78    private IFunctionTree TakeNextParent(IScope scope) {
79      IFunctionTree parent = GetVariableValue<IFunctionTree>("FunctionTree", scope.SubScopes[0], false);
80      scope.RemoveSubScope(scope.SubScopes[0]);
81      return parent;
82    }
83  }
84}
Note: See TracBrowser for help on using the repository browser.