1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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 HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Encodings.VariableVector {
|
---|
30 | [Item("SinglePointCrossover", "Description missing")]
|
---|
31 | [StorableClass]
|
---|
32 | public class SinglePointCrossover : VariableVectorCrossover {
|
---|
33 |
|
---|
34 | #region Parameter Properties
|
---|
35 | #endregion
|
---|
36 |
|
---|
37 | [StorableConstructor]
|
---|
38 | protected SinglePointCrossover(bool deserializing) : base(deserializing) { }
|
---|
39 | protected SinglePointCrossover(SinglePointCrossover original, Cloner cloner)
|
---|
40 | : base(original, cloner) {
|
---|
41 | }
|
---|
42 | public SinglePointCrossover() : base() { }
|
---|
43 |
|
---|
44 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
45 | return new SinglePointCrossover(this, cloner);
|
---|
46 | }
|
---|
47 |
|
---|
48 | public static VariableVector Apply(IRandom random, VariableVector parent1, VariableVector parent2) {
|
---|
49 | var parent1Condition = parent1.Condition;
|
---|
50 | var parent2Condition = parent2.Condition;
|
---|
51 |
|
---|
52 | var parent1Action = parent1.Action;
|
---|
53 | var parent2Action = parent2.Action;
|
---|
54 |
|
---|
55 | if (parent1.Count != parent2.Count && parent1.VirtualLength != parent2.VirtualLength
|
---|
56 | && parent1Condition.VariableDictionary.Count != parent2Condition.VariableDictionary.Count && parent1Condition.VirtualLength != parent2Condition.VirtualLength
|
---|
57 | && parent1Action.VariableDictionary.Count != parent2Action.VariableDictionary.Count && parent1Action.VirtualLength != parent2Action.VirtualLength)
|
---|
58 | throw new ArgumentException("SinglePointCrossover: The parents are of different length.");
|
---|
59 |
|
---|
60 | int conditionLength = parent1Condition.VirtualLength;
|
---|
61 | int breakPoint = random.Next(1, conditionLength);
|
---|
62 | IList<IVariable> newCondition = new List<IVariable>(parent1Condition.VariableDictionary.Count);
|
---|
63 |
|
---|
64 | var keyEnumerator = parent1Condition.VariableDictionary.Keys.GetEnumerator();
|
---|
65 |
|
---|
66 | int index = 0;
|
---|
67 | IVariable curVariable = null;
|
---|
68 | while (index < breakPoint) {
|
---|
69 | if (!keyEnumerator.MoveNext()) { throw new IndexOutOfRangeException("Enumerator has no next element!"); }
|
---|
70 | curVariable = parent1Condition.VariableDictionary[keyEnumerator.Current];
|
---|
71 | if (index + curVariable.VirtualLength <= breakPoint) {
|
---|
72 | newCondition.Add(curVariable.GetSetCopy());
|
---|
73 | }
|
---|
74 | index += curVariable.VirtualLength;
|
---|
75 | }
|
---|
76 |
|
---|
77 | // breakpoint is in between a variable
|
---|
78 | if (index != breakPoint) {
|
---|
79 | newCondition.Add(parent1Condition.VariableDictionary[keyEnumerator.Current].CrossParentsAtPosition(parent2Condition.VariableDictionary[keyEnumerator.Current], breakPoint - (index - curVariable.VirtualLength)));
|
---|
80 | }
|
---|
81 |
|
---|
82 | while (index < conditionLength) {
|
---|
83 | keyEnumerator.MoveNext();
|
---|
84 | curVariable = parent2Condition.VariableDictionary[keyEnumerator.Current];
|
---|
85 | newCondition.Add(curVariable.GetSetCopy());
|
---|
86 | index += curVariable.VirtualLength;
|
---|
87 | }
|
---|
88 |
|
---|
89 | var action = random.Next(2) == 1 ? parent1Action.VariableDictionary.Values.Select(x => x.GetSetCopy()) : parent2Action.VariableDictionary.Values.Select(x => x.GetSetCopy());
|
---|
90 |
|
---|
91 | return new VariableVector(newCondition, action);
|
---|
92 | }
|
---|
93 |
|
---|
94 | protected override VariableVector Cross(IRandom random, ItemArray<VariableVector> parents) {
|
---|
95 | if (parents.Length != 2) throw new ArgumentException("ERROR in SinglePointCrossover: The number of parents is not equal to 2");
|
---|
96 | return Apply(random, parents[0], parents[1]);
|
---|
97 | }
|
---|
98 | }
|
---|
99 | }
|
---|