[2900] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[9456] | 3 | * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[2900] | 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 |
|
---|
| 22 | using System;
|
---|
[4722] | 23 | using HeuristicLab.Common;
|
---|
[2900] | 24 | using HeuristicLab.Core;
|
---|
[2928] | 25 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[2900] | 26 |
|
---|
[3053] | 27 | namespace HeuristicLab.Encodings.RealVectorEncoding {
|
---|
[2900] | 28 | /// <summary>
|
---|
| 29 | /// Discrete crossover for real vectors: For each position in the new vector an allele
|
---|
| 30 | /// of one of the parents is randomly selected.
|
---|
| 31 | /// </summary>
|
---|
[2928] | 32 | /// <remarks>
|
---|
| 33 | /// This operator is also called dominant recombination and unlike other crossovers works on more than two parents also.<br />
|
---|
| 34 | /// 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.
|
---|
| 35 | /// </remarks>
|
---|
| 36 | [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.")]
|
---|
[3017] | 37 | [StorableClass]
|
---|
[2928] | 38 | public class DiscreteCrossover : RealVectorCrossover {
|
---|
[4722] | 39 | [StorableConstructor]
|
---|
| 40 | protected DiscreteCrossover(bool deserializing) : base(deserializing) { }
|
---|
| 41 | protected DiscreteCrossover(DiscreteCrossover original, Cloner cloner) : base(original, cloner) { }
|
---|
| 42 | public DiscreteCrossover() : base() { }
|
---|
| 43 |
|
---|
| 44 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 45 | return new DiscreteCrossover(this, cloner);
|
---|
| 46 | }
|
---|
| 47 |
|
---|
[2900] | 48 | /// <summary>
|
---|
[2928] | 49 | /// Performs a discrete crossover operation on multiple parents.
|
---|
[2900] | 50 | /// </summary>
|
---|
[2928] | 51 | /// <exception cref="ArgumentException">Thrown when the vectors of the parents are of different length.</exception>
|
---|
[2900] | 52 | /// <param name="random">A random number generator.</param>
|
---|
| 53 | /// <param name="parents">An array containing the parents that should be crossed.</param>
|
---|
| 54 | /// <returns>The newly created real vector, resulting from the crossover operation.</returns>
|
---|
[3060] | 55 | public static RealVector Apply(IRandom random, ItemArray<RealVector> parents) {
|
---|
[2900] | 56 | int length = parents[0].Length;
|
---|
[4068] | 57 |
|
---|
| 58 | for (int i = 0; i < parents.Length; i++) {
|
---|
| 59 | if (parents[i].Length != length)
|
---|
[2936] | 60 | throw new ArgumentException("DiscreteCrossover: The parents' vectors are of different length.", "parents");
|
---|
| 61 | }
|
---|
[4068] | 62 |
|
---|
[3060] | 63 | RealVector result = new RealVector(length);
|
---|
[2936] | 64 | for (int i = 0; i < length; i++) {
|
---|
| 65 | result[i] = parents[random.Next(parents.Length)][i];
|
---|
[4068] | 66 | }
|
---|
| 67 |
|
---|
[2900] | 68 | return result;
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | /// <summary>
|
---|
[3060] | 72 | /// Checks number of parents and forwards the call to <see cref="Apply(IRandom, ItemArray<RealVector>)"/>.
|
---|
[2900] | 73 | /// </summary>
|
---|
[2928] | 74 | /// <exception cref="ArgumentException">Thrown when <paramref name="parents"/> contains less than 2 elements.</exception>
|
---|
| 75 | /// <param name="random">The random number generator.</param>
|
---|
| 76 | /// <param name="parents">The collection of parents (must be of size 2 or greater).</param>
|
---|
| 77 | /// <returns>The real vector resulting from the crossover.</returns>
|
---|
[3060] | 78 | protected override RealVector Cross(IRandom random, ItemArray<RealVector> parents) {
|
---|
[2928] | 79 | if (parents.Length < 2) throw new ArgumentException("DiscreteCrossover: The number of parents is less than 2.", "parents");
|
---|
[2900] | 80 | return Apply(random, parents);
|
---|
| 81 | }
|
---|
| 82 | }
|
---|
| 83 | }
|
---|