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 HeuristicLab.Functions;
|
---|
32 | using System.Diagnostics;
|
---|
33 |
|
---|
34 | namespace HeuristicLab.StructureIdentification {
|
---|
35 | /// <summary>
|
---|
36 | /// Implementation of a homologous one point crossover operator as described in:
|
---|
37 | /// W. B. Langdon and R. Poli. Foundations of Genetic Programming. Springer-Verlag, 2002.
|
---|
38 | /// </summary>
|
---|
39 | public class OnePointCrossOver : OperatorBase {
|
---|
40 | public override string Description {
|
---|
41 | get {
|
---|
42 | return @"";
|
---|
43 | }
|
---|
44 | }
|
---|
45 | public OnePointCrossOver()
|
---|
46 | : base() {
|
---|
47 | AddVariableInfo(new VariableInfo("Random", "Pseudo random number generator", typeof(MersenneTwister), VariableKind.In));
|
---|
48 | AddVariableInfo(new VariableInfo("OperatorLibrary", "The operator library containing all available operators", typeof(GPOperatorLibrary), VariableKind.In));
|
---|
49 | AddVariableInfo(new VariableInfo("MaxTreeHeight", "The maximal allowed height of the tree", typeof(IntData), VariableKind.In));
|
---|
50 | AddVariableInfo(new VariableInfo("MaxTreeSize", "The maximal allowed size (number of nodes) of the tree", typeof(IntData), VariableKind.In));
|
---|
51 | AddVariableInfo(new VariableInfo("FunctionTree", "The tree to mutate", typeof(IFunctionTree), VariableKind.In | VariableKind.New));
|
---|
52 | AddVariableInfo(new VariableInfo("TreeSize", "The size (number of nodes) of the tree", typeof(IntData), VariableKind.New));
|
---|
53 | AddVariableInfo(new VariableInfo("TreeHeight", "The height of the tree", typeof(IntData), VariableKind.New));
|
---|
54 | }
|
---|
55 |
|
---|
56 | public override IOperation Apply(IScope scope) {
|
---|
57 | MersenneTwister random = GetVariableValue<MersenneTwister>("Random", scope, true);
|
---|
58 | GPOperatorLibrary opLibrary = GetVariableValue<GPOperatorLibrary>("OperatorLibrary", scope, true);
|
---|
59 | int maxTreeHeight = GetVariableValue<IntData>("MaxTreeHeight", scope, true).Data;
|
---|
60 | int maxTreeSize = GetVariableValue<IntData>("MaxTreeSize", scope, true).Data;
|
---|
61 |
|
---|
62 | TreeGardener gardener = new TreeGardener(random, opLibrary);
|
---|
63 |
|
---|
64 | if((scope.SubScopes.Count % 2) != 0)
|
---|
65 | throw new InvalidOperationException("Number of parents is not even");
|
---|
66 |
|
---|
67 | CompositeOperation initOperations = new CompositeOperation();
|
---|
68 |
|
---|
69 | int children = scope.SubScopes.Count / 2;
|
---|
70 | for(int i = 0; i < children; i++) {
|
---|
71 | IScope parent1 = scope.SubScopes[0];
|
---|
72 | scope.RemoveSubScope(parent1);
|
---|
73 | IScope parent2 = scope.SubScopes[0];
|
---|
74 | scope.RemoveSubScope(parent2);
|
---|
75 | IScope child = new Scope(i.ToString());
|
---|
76 | IOperation childInitOperation = Cross(gardener, maxTreeSize, maxTreeHeight, scope, random, parent1, parent2, child);
|
---|
77 | initOperations.AddOperation(childInitOperation);
|
---|
78 | scope.AddSubScope(child);
|
---|
79 | }
|
---|
80 |
|
---|
81 | return initOperations;
|
---|
82 | }
|
---|
83 |
|
---|
84 | private IOperation Cross(TreeGardener gardener, int maxTreeSize, int maxTreeHeight,
|
---|
85 | IScope scope, MersenneTwister random, IScope parent1, IScope parent2, IScope child) {
|
---|
86 | IFunctionTree newTree = Cross(gardener, parent1, parent2,
|
---|
87 | random, maxTreeSize, maxTreeHeight);
|
---|
88 |
|
---|
89 | int newTreeSize = newTree.Size;
|
---|
90 | int newTreeHeight = newTree.Height;
|
---|
91 | child.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("FunctionTree"), newTree));
|
---|
92 | child.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TreeSize"), new IntData(newTreeSize)));
|
---|
93 | child.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TreeHeight"), new IntData(newTreeHeight)));
|
---|
94 |
|
---|
95 | // check if the new tree is valid and if the height of is still in the allowed bounds (we are not so strict for the max-size)
|
---|
96 | Debug.Assert(gardener.IsValidTree(newTree) && newTreeHeight <= maxTreeHeight && newTreeSize <= maxTreeSize);
|
---|
97 | return null;
|
---|
98 | }
|
---|
99 |
|
---|
100 |
|
---|
101 | private IFunctionTree Cross(TreeGardener gardener, IScope f, IScope g, MersenneTwister random, int maxTreeSize, int maxTreeHeight) {
|
---|
102 | IFunctionTree tree0 = f.GetVariableValue<IFunctionTree>("FunctionTree", false);
|
---|
103 | int tree0Height = f.GetVariableValue<IntData>("TreeHeight", false).Data;
|
---|
104 | int tree0Size = f.GetVariableValue<IntData>("TreeSize", false).Data;
|
---|
105 |
|
---|
106 | IFunctionTree tree1 = g.GetVariableValue<IFunctionTree>("FunctionTree", false);
|
---|
107 | int tree1Height = g.GetVariableValue<IntData>("TreeHeight", false).Data;
|
---|
108 | int tree1Size = g.GetVariableValue<IntData>("TreeSize", false).Data;
|
---|
109 |
|
---|
110 | if(tree0Size == 1 && tree1Size == 1) {
|
---|
111 | return CombineTerminals(gardener, tree0, tree1, random);
|
---|
112 | } else {
|
---|
113 | // we are going to insert tree1 into tree0 at a random place so we have to make sure that tree0 is not a terminal
|
---|
114 | // in case both trees are higher than 1 we swap the trees with probability 50%
|
---|
115 | if(tree0Height == 1 || (tree1Height > 1 && random.Next(2) == 0)) {
|
---|
116 | IFunctionTree tmp = tree0; tree0 = tree1; tree1 = tmp;
|
---|
117 | int tmpHeight = tree0Height; tree0Height = tree1Height; tree1Height = tmpHeight;
|
---|
118 | int tmpSize = tree0Size; tree0Size = tree1Size; tree1Size = tmpSize;
|
---|
119 | }
|
---|
120 |
|
---|
121 | List<IFunctionTree[]> allowedCrossOverPoints = GetCrossOverPoints(null, tree0, tree1);
|
---|
122 | if(allowedCrossOverPoints.Count == 0) {
|
---|
123 | if(random.NextDouble() < 0.5) return tree0; else return tree1;
|
---|
124 | }
|
---|
125 | IFunctionTree[] crossOverPoints = allowedCrossOverPoints[random.Next(allowedCrossOverPoints.Count)];
|
---|
126 | IFunctionTree parent = crossOverPoints[0];
|
---|
127 | IFunctionTree replacedBranch = crossOverPoints[1];
|
---|
128 | IFunctionTree insertedBranch = crossOverPoints[2];
|
---|
129 | if(parent == null) return insertedBranch;
|
---|
130 | else {
|
---|
131 | int i = 0;
|
---|
132 | while(parent.SubTrees[i] != replacedBranch) i++;
|
---|
133 | parent.RemoveSubTree(i);
|
---|
134 | parent.InsertSubTree(i, insertedBranch);
|
---|
135 | return tree0;
|
---|
136 | }
|
---|
137 | }
|
---|
138 | }
|
---|
139 |
|
---|
140 | private List<IFunctionTree[]> GetCrossOverPoints(IFunctionTree parent, IFunctionTree tree0, IFunctionTree tree1) {
|
---|
141 | List<IFunctionTree[]> results = new List<IFunctionTree[]>();
|
---|
142 | if(tree0.Function != tree1.Function) return results;
|
---|
143 |
|
---|
144 | int maxArity = Math.Min(tree0.SubTrees.Count, tree1.SubTrees.Count);
|
---|
145 | for(int i = 0; i < maxArity; i++) {
|
---|
146 | results.AddRange(GetCrossOverPoints(tree0, tree0.SubTrees[i], tree1.SubTrees[i]));
|
---|
147 | }
|
---|
148 | if(results.Count == 0) results.Add(new IFunctionTree[] { parent, tree0, tree1 });
|
---|
149 | return results;
|
---|
150 | }
|
---|
151 |
|
---|
152 | private IFunctionTree CombineTerminals(TreeGardener gardener, IFunctionTree f, IFunctionTree g, MersenneTwister random) {
|
---|
153 | if(random.NextDouble() < 0.5) return f; else return g;
|
---|
154 | }
|
---|
155 | }
|
---|
156 | }
|
---|