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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Text;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Operators;
|
---|
28 | using HeuristicLab.Random;
|
---|
29 | using HeuristicLab.Data;
|
---|
30 | using HeuristicLab.Constraints;
|
---|
31 | using System.Diagnostics;
|
---|
32 | using HeuristicLab.Evolutionary;
|
---|
33 |
|
---|
34 | namespace 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 | }
|
---|