[99] | 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>
|
---|
[1218] | 30 | /// Discrete crossover for real vectors: creates a new offspring by combining the alleles in the parents such that each allele is
|
---|
[1184] | 31 | /// randomly selected from one parent. It will also use the same strategy to combine the endogenous
|
---|
[1218] | 32 | /// strategy parameter vector.
|
---|
[1184] | 33 | /// </summary>
|
---|
[1218] | 34 | public class SelfAdaptiveDiscreteCrossover : RealVectorCrossoverBase {
|
---|
[1184] | 35 | /// <inheritdoc select="summary"/>
|
---|
[99] | 36 | public override string Description {
|
---|
| 37 | get {
|
---|
[1218] | 38 | return @"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 will also use the same strategy to combine the endogenous strategy parameter vector.";
|
---|
[99] | 39 | }
|
---|
| 40 | }
|
---|
| 41 |
|
---|
[1184] | 42 | /// <summary>
|
---|
[1218] | 43 | /// Initializes a new instance of <see cref="SelfAdaptiveDiscreteCrossover"/> with one additional
|
---|
| 44 | /// variable infos (<c>StrategyVector</c>).
|
---|
[1184] | 45 | /// </summary>
|
---|
[1218] | 46 | public SelfAdaptiveDiscreteCrossover()
|
---|
| 47 | : base() {
|
---|
| 48 | AddVariableInfo(new VariableInfo("StrategyVector", "Endogenous strategy parameter vector", typeof(DoubleArrayData), VariableKind.In | VariableKind.New));
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | /// <summary>
|
---|
| 52 | /// Performs a discrete crossover operation for multiple given parent real vectors that combines the strategy vectors in the same way.
|
---|
| 53 | /// </summary>
|
---|
| 54 | /// <param name="random">A random number generator.</param>
|
---|
| 55 | /// <param name="parents">An array containing the parents that should be crossed.</param>
|
---|
| 56 | /// <param name="strategies">An array containing the strategy vectors of the parents.</param>
|
---|
| 57 | /// <param name="child">The newly created real vector, resulting from the crossover operation (output parameter).</param>
|
---|
| 58 | /// <param name="strategy">The newly created strategy vector, resulting from the crossover operation (output parameter).</param>
|
---|
| 59 | public static void Apply(IRandom random, double[][] parents, double[][] strategies, out double[] child, out double[] strategy) {
|
---|
| 60 | child = new double[parents[0].Length];
|
---|
| 61 | strategy = new double[strategies[0].Length];
|
---|
| 62 | for (int i = 0; i < child.Length; i++) {
|
---|
| 63 | int nextParent = random.Next(parents.Length);
|
---|
| 64 | child[i] = parents[nextParent][i];
|
---|
| 65 | strategy[i] = strategies[nextParent][i];
|
---|
[99] | 66 | }
|
---|
| 67 | }
|
---|
[111] | 68 |
|
---|
[1184] | 69 | /// <summary>
|
---|
[1218] | 70 | /// Performs a discrete crossover operation for multiple given parent real vectors that combines the strategy vectors in the same way.
|
---|
[1184] | 71 | /// </summary>
|
---|
[1218] | 72 | /// <exception cref="InvalidOperationException">Thrown if there are less than two parents or if the length of the strategy vectors is not the same as the length of the real vectors.</exception>
|
---|
[1184] | 73 | /// <param name="scope">The current scope.</param>
|
---|
[1218] | 74 | /// <param name="random">A random number generator.</param>
|
---|
| 75 | /// <param name="parents">An array containing the real vectors that should be crossed.</param>
|
---|
| 76 | /// <returns>The newly created real vector, resulting from the crossover operation.</returns>
|
---|
| 77 | protected override double[] Cross(IScope scope, IRandom random, double[][] parents) {
|
---|
| 78 | if (parents.Length < 2) throw new InvalidOperationException("ERROR in SelfAdaptiveDiscreteCrossover: The number of parents is less than 2");
|
---|
| 79 | double[][] strategies = new double[scope.SubScopes.Count][];
|
---|
| 80 | int length = parents[0].Length;
|
---|
| 81 | for (int i = 0; i < scope.SubScopes.Count; i++) {
|
---|
| 82 | strategies[i] = scope.SubScopes[i].GetVariableValue<DoubleArrayData>("StrategyVector", false).Data;
|
---|
| 83 | if (strategies[i].Length != length) throw new InvalidOperationException("ERROR in SelfAdaptiveDiscreteCrossover: Strategy vectors do not have the same length as the real vectors");
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | double[] child, strategy;
|
---|
| 87 | Apply(random, parents, strategies, out child, out strategy);
|
---|
| 88 | scope.AddVariable(new Variable(scope.TranslateName("StrategyVector"), new DoubleArrayData(strategy)));
|
---|
| 89 | return child;
|
---|
[111] | 90 | }
|
---|
[99] | 91 | }
|
---|
| 92 | }
|
---|