Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HiveStatistics/sources/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/Crossovers/NPointCrossover.cs @ 9522

Last change on this file since 9522 was 7259, checked in by swagner, 13 years ago

Updated year of copyrights to 2012 (#1716)

File size: 5.9 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Encodings.BinaryVectorEncoding {
31  /// <summary>
32  /// N point crossover for binary vectors.
33  /// </summary>
34  /// <remarks>
35  /// It is implemented as described in Eiben, A.E. and Smith, J.E. 2003. Introduction to Evolutionary Computation. Natural Computing Series, Springer-Verlag Berlin Heidelberg..
36  /// </remarks>
37  [Item("NPointCrossover", "N point crossover for binary vectors. It is implemented as described in Eiben, A.E. and Smith, J.E. 2003. Introduction to Evolutionary Computation. Natural Computing Series, Springer-Verlag Berlin Heidelberg.")]
38  [StorableClass]
39  public sealed class NPointCrossover : BinaryVectorCrossover {
40    /// <summary>
41    /// Number of crossover points.
42    /// </summary>
43    public ValueLookupParameter<IntValue> NParameter {
44      get { return (ValueLookupParameter<IntValue>)Parameters["N"]; }
45    }
46
47    [StorableConstructor]
48    private NPointCrossover(bool deserializing) : base(deserializing) { }
49    private NPointCrossover(NPointCrossover original, Cloner cloner) : base(original, cloner) { }
50    /// <summary>
51    /// Initializes a new instance of <see cref="NPointCrossover"/>
52    /// </summary>
53    public NPointCrossover()
54      : base() {
55      Parameters.Add(new ValueLookupParameter<IntValue>("N", "Number of crossover points", new IntValue(2)));
56    }
57
58    public override IDeepCloneable Clone(Cloner cloner) {
59      return new NPointCrossover(this, cloner);
60    }
61
62    /// <summary>
63    /// Performs a N point crossover at randomly chosen positions of the two
64    /// given parent binary vectors.
65    /// </summary>
66    /// <exception cref="ArgumentException">Thrown when the value for N is invalid or when the parent vectors are of different length.</exception>
67    /// <param name="random">A random number generator.</param>
68    /// <param name="parent1">The first parent for crossover.</param>
69    /// <param name="parent2">The second parent for crossover.</param>
70    /// <param name="n">Number of crossover points.</param>
71    /// <returns>The newly created binary vector, resulting from the N point crossover.</returns>
72    public static BinaryVector Apply(IRandom random, BinaryVector parent1, BinaryVector parent2, IntValue n) {
73      if (parent1.Length != parent2.Length)
74        throw new ArgumentException("NPointCrossover: The parents are of different length.");
75
76      if (n.Value > parent1.Length)
77        throw new ArgumentException("NPointCrossover: There cannot be more breakpoints than the size of the parents.");
78
79      if (n.Value < 1)
80        throw new ArgumentException("NPointCrossover: N cannot be < 1.");
81
82      int length = parent1.Length;
83      bool[] result = new bool[length];
84      int[] breakpoints = new int[n.Value];
85
86      //choose break points
87      List<int> breakpointPool = new List<int>();
88
89      for (int i = 0; i < length; i++)
90        breakpointPool.Add(i);
91
92      for (int i = 0; i < n.Value; i++) {
93        int index = random.Next(breakpointPool.Count);
94        breakpoints[i] = breakpointPool[index];
95        breakpointPool.RemoveAt(index);
96      }
97
98      Array.Sort(breakpoints);
99
100      //perform crossover
101      int arrayIndex = 0;
102      int breakPointIndex = 0;
103      bool firstParent = true;
104
105      while (arrayIndex < length) {
106        if (breakPointIndex < breakpoints.Length &&
107          arrayIndex == breakpoints[breakPointIndex]) {
108          breakPointIndex++;
109          firstParent = !firstParent;
110        }
111
112        if (firstParent)
113          result[arrayIndex] = parent1[arrayIndex];
114        else
115          result[arrayIndex] = parent2[arrayIndex];
116
117        arrayIndex++;
118      }
119
120      return new BinaryVector(result);
121    }
122
123    /// <summary>
124    /// Performs a N point crossover at a randomly chosen position of two
125    /// given parent binary vectors.
126    /// </summary>
127    /// <exception cref="ArgumentException">Thrown if there are not exactly two parents.</exception>
128    /// <exception cref="InvalidOperationException">
129    /// Thrown when the N parameter could not be found.</description></item>
130    /// </exception>
131    /// <param name="random">A random number generator.</param>
132    /// <param name="parents">An array containing the two binary vectors that should be crossed.</param>
133    /// <returns>The newly created binary vector, resulting from the N point crossover.</returns>
134    protected override BinaryVector Cross(IRandom random, ItemArray<BinaryVector> parents) {
135      if (parents.Length != 2) throw new ArgumentException("ERROR in NPointCrossover: The number of parents is not equal to 2");
136
137      if (NParameter.ActualValue == null) throw new InvalidOperationException("NPointCrossover: Parameter " + NParameter.ActualName + " could not be found.");
138
139      return Apply(random, parents[0], parents[1], NParameter.Value);
140    }
141  }
142}
Note: See TracBrowser for help on using the repository browser.