Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 815 was 815, checked in by gkronber, 15 years ago

fixed #392 (GP uniform- and onepoint crossover should only create one child per crossover event)

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