Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive_Milestone2/sources/HeuristicLab.GP/3.3/Recombination/GPCrossoverBase.cs @ 6319

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

Adapted GP crossover operators to match the new design for crossover operators. #512 (GP crossover operators should be changed to match the new design of crossover operators (see #475)).

File size: 3.2 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;
32using HeuristicLab.Evolutionary;
33
34namespace HeuristicLab.GP {
35  public abstract class GPCrossoverBase : CrossoverBase {
36    public GPCrossoverBase()
37      : base() {
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    internal abstract IFunctionTree Cross(IScope scope, TreeGardener gardener, IRandom random, IFunctionTree tree0, IFunctionTree tree1);
45
46    protected override void Cross(IScope scope, IRandom random) {
47      GPOperatorLibrary opLibrary = GetVariableValue<GPOperatorLibrary>("OperatorLibrary", scope, true);
48      TreeGardener gardener = new TreeGardener(random, opLibrary);
49
50      if (scope.SubScopes.Count != 2)
51        throw new InvalidOperationException("Number of parents must be exactly two.");
52
53      IFunctionTree parent0 = GetVariableValue<IFunctionTree>("FunctionTree", scope.SubScopes[0], false);
54      IFunctionTree parent1 = GetVariableValue<IFunctionTree>("FunctionTree", scope.SubScopes[1], false);
55
56      // randomly swap parents to remove a possible bias from selection (e.g. when using gender-specific selection)
57      if (random.NextDouble() < 0.5) {
58        IFunctionTree tmp = parent0;
59        parent0 = parent1;
60        parent1 = tmp;
61      }
62
63      IFunctionTree child = Cross(scope, gardener, random, parent0, parent1);
64      Debug.Assert(gardener.IsValidTree(child));
65      scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("FunctionTree"), child));
66      scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TreeSize"), new IntData(child.Size)));
67      scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TreeHeight"), new IntData(child.Height)));
68    }
69  }
70}
Note: See TracBrowser for help on using the repository browser.