Free cookie consent management tool by TermsFeed Policy Generator

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

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

Moved interfaces and classes for deep cloning from HeuristicLab.Core to HeuristicLab.Common (#975).

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