Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/Crossovers/NPointCrossover.cs @ 3062

Last change on this file since 3062 was 3062, checked in by svonolfe, 14 years ago

Implemented operators and test cases for the BinaryVector encoding (#914)

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