Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.RealVector/3.3/Crossovers/DiscreteCrossover.cs @ 2996

Last change on this file since 2996 was 2994, checked in by epitzer, 15 years ago

Make StorableClass attribute compulsory for StorableSerializer to work, add named property StorableClassType to choose between Empty and MarkedOnly, later other options will be added. (#548)

File size: 3.8 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 HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Parameters;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Encodings.RealVector {
29  /// <summary>
30  /// Discrete crossover for real vectors: For each position in the new vector an allele
31  /// of one of the parents is randomly selected.
32  /// </summary>
33  /// <remarks>
34  /// This operator is also called dominant recombination and unlike other crossovers works on more than two parents also.<br />
35  /// It is implemented as described in Beyer, H.-G. and Schwefel, H.-P. 2002. Evolution Strategies - A Comprehensive Introduction Natural Computing, 1, pp. 3-52.
36  /// </remarks>
37  [Item("DiscreteCrossover", "Discrete crossover for real vectors: Creates a new offspring by combining the alleles in the parents such that each allele is randomly selected from one parent. It is implemented as described in Beyer, H.-G. and Schwefel, H.-P. 2002. Evolution Strategies - A Comprehensive Introduction Natural Computing, 1, pp. 3-52.")]
38  [StorableClass(StorableClassType.Empty)]
39  public class DiscreteCrossover : RealVectorCrossover {
40    /// <summary>
41    /// Performs a discrete crossover operation on multiple parents.
42    /// </summary>
43    /// <exception cref="ArgumentException">Thrown when the vectors of the parents are of different length.</exception>
44    /// <param name="random">A random number generator.</param>
45    /// <param name="parents">An array containing the parents that should be crossed.</param>
46    /// <returns>The newly created real vector, resulting from the crossover operation.</returns>
47    public static DoubleArrayData Apply(IRandom random, ItemArray<DoubleArrayData> parents) {
48      int length = parents[0].Length;
49     
50      for (int i = 0; i < parents.Length; i++) {
51        if(parents[i].Length != length)
52          throw new ArgumentException("DiscreteCrossover: The parents' vectors are of different length.", "parents");
53      }
54     
55      DoubleArrayData result = new DoubleArrayData(length);
56      for (int i = 0; i < length; i++) {
57        result[i] = parents[random.Next(parents.Length)][i];
58      }       
59     
60      return result;
61    }
62
63    /// <summary>
64    /// Checks number of parents and forwards the call to <see cref="Apply(IRandom, ItemArray<DoubleArrayData>)"/>.
65    /// </summary>
66    /// <exception cref="ArgumentException">Thrown when <paramref name="parents"/> contains less than 2 elements.</exception>
67    /// <param name="random">The random number generator.</param>
68    /// <param name="parents">The collection of parents (must be of size 2 or greater).</param>
69    /// <returns>The real vector resulting from the crossover.</returns>
70    protected override DoubleArrayData Cross(IRandom random, ItemArray<DoubleArrayData> parents) {
71      if (parents.Length < 2) throw new ArgumentException("DiscreteCrossover: The number of parents is less than 2.", "parents");
72      return Apply(random, parents);
73    }
74  }
75}
Note: See TracBrowser for help on using the repository browser.