[86] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2008 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Text;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
| 27 |
|
---|
| 28 | namespace HeuristicLab.RealVector {
|
---|
[1184] | 29 | /// <summary>
|
---|
| 30 | /// Discrete multiple crossover for real vectors: For each position in the new vector an allele
|
---|
| 31 | /// of one of the parents is randomly selected.
|
---|
| 32 | /// </summary>
|
---|
[111] | 33 | public class DiscreteMultiCrossover : RealVectorMultiCrossoverBase {
|
---|
[1184] | 34 | /// <inheritdoc select="summary"/>
|
---|
[86] | 35 | public override string Description {
|
---|
| 36 | get {
|
---|
[111] | 37 | return @"This creates a new offspring by combining the alleles in the parents such that each allele is randomly selected from one parent";
|
---|
[86] | 38 | }
|
---|
| 39 | }
|
---|
| 40 |
|
---|
[1184] | 41 | /// <summary>
|
---|
| 42 | /// Performs a discrete multiple crossover on the given <paramref name="parents"/>.
|
---|
| 43 | /// </summary>
|
---|
| 44 | /// <exception cref="InvalidOperationException">Thrown when the parents have different lengths.</exception>
|
---|
| 45 | /// <param name="random">The random number generator.</param>
|
---|
| 46 | /// <param name="parents">The list of parents for the multiple crossover.</param>
|
---|
| 47 | /// <returns>The newly created real vector, resulting from the discrete multiple crossover.</returns>
|
---|
[111] | 48 | public static double[] Apply(IRandom random, IList<double[]> parents) {
|
---|
| 49 | int length = parents[0].Length;
|
---|
| 50 | double[] result = new double[length];
|
---|
| 51 | try {
|
---|
| 52 | for (int i = 0; i < length; i++) {
|
---|
| 53 | result[i] = parents[random.Next(parents.Count)][i];
|
---|
[86] | 54 | }
|
---|
[111] | 55 | } catch (IndexOutOfRangeException) {
|
---|
| 56 | throw new InvalidOperationException("Cannot apply multicrossover to real vectors of different length.");
|
---|
[86] | 57 | }
|
---|
[111] | 58 | return result;
|
---|
[86] | 59 | }
|
---|
[111] | 60 |
|
---|
[1184] | 61 | /// <summary>
|
---|
| 62 | /// Performs a discrete multiple crossover on the given <paramref name="parents"/>.
|
---|
| 63 | /// </summary>
|
---|
| 64 | /// <remarks>Calls <see cref="Apply"/>.</remarks>
|
---|
| 65 | /// <param name="scope">The current scope.</param>
|
---|
| 66 | /// <param name="random">The random number generator.</param>
|
---|
| 67 | /// <param name="parents">The list of parents for the multiple crossover.</param>
|
---|
| 68 | /// <returns>The newly created real vector, resulting from the discrete multiple crossover.</returns>
|
---|
[111] | 69 | protected override double[] Cross(IScope scope, IRandom random, IList<double[]> parents) {
|
---|
| 70 | return Apply(random, parents);
|
---|
| 71 | }
|
---|
[86] | 72 | }
|
---|
| 73 | }
|
---|