Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4068 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

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