1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2016 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 HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 | using HeuristicLab.Parameters;
|
---|
27 | using HeuristicLab.Persistence;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Encodings.RealVectorEncoding {
|
---|
30 | /// <summary>
|
---|
31 | /// The uniform all positions arithmetic crossover constructs an offspring by calculating x = alpha * p1 + (1-alpha) * p2 for every position x in the vector.
|
---|
32 | /// </summary>
|
---|
33 | /// <remarks>
|
---|
34 | /// By setting alpha = 0.5 it is the same as the <see cref="AverageCrossover"/>, but only on two parents.
|
---|
35 | /// It is implemented as described in Michalewicz, Z. 1999. Genetic Algorithms + Data Structures = Evolution Programs. Third, Revised and Extended Edition, Spring-Verlag Berlin Heidelberg.
|
---|
36 | /// </remarks>
|
---|
37 | [Item("UniformAllPositionsArithmeticCrossover", "The uniform all positions arithmetic crossover constructs an offspring by calculating x = alpha * p1 + (1-alpha) * p2 for every position x in the vector. Note that for alpha = 0.5 it is the same as the AverageCrossover (except that the AverageCrossover is defined for more than 2 parents). It is implemented as described in Michalewicz, Z. 1999. Genetic Algorithms + Data Structures = Evolution Programs. Third, Revised and Extended Edition, Spring-Verlag Berlin Heidelberg.")]
|
---|
38 | [StorableType("51c7c3c0-2d86-4523-ab3d-705e74a43486")]
|
---|
39 | public class UniformAllPositionsArithmeticCrossover : RealVectorCrossover {
|
---|
40 | /// <summary>
|
---|
41 | /// The alpha parameter needs to be in the interval [0;1] and specifies how close the resulting offspring should be either to parent1 (alpha -> 0) or parent2 (alpha -> 1).
|
---|
42 | /// </summary>
|
---|
43 | public ValueLookupParameter<DoubleValue> AlphaParameter {
|
---|
44 | get { return (ValueLookupParameter<DoubleValue>)Parameters["Alpha"]; }
|
---|
45 | }
|
---|
46 |
|
---|
47 | [StorableConstructor]
|
---|
48 | protected UniformAllPositionsArithmeticCrossover(StorableConstructorFlag deserializing) : base(deserializing) { }
|
---|
49 | protected UniformAllPositionsArithmeticCrossover(UniformAllPositionsArithmeticCrossover original, Cloner cloner) : base(original, cloner) { }
|
---|
50 | /// <summary>
|
---|
51 | /// Initializes a new instance with one parameter (<c>Alpha</c>).
|
---|
52 | /// </summary>
|
---|
53 | public UniformAllPositionsArithmeticCrossover()
|
---|
54 | : base() {
|
---|
55 | Parameters.Add(new ValueLookupParameter<DoubleValue>("Alpha", "The alpha value in the range [0;1]", new DoubleValue(0.33)));
|
---|
56 | }
|
---|
57 |
|
---|
58 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
59 | return new UniformAllPositionsArithmeticCrossover(this, cloner);
|
---|
60 | }
|
---|
61 |
|
---|
62 | /// <summary>
|
---|
63 | /// Performs the arithmetic crossover on all positions by calculating x = alpha * p1 + (1 - alpha) * p2.
|
---|
64 | /// </summary>
|
---|
65 | /// <exception cref="ArgumentException">Thrown when the parent vectors are of different length or alpha is outside the range [0;1].</exception>
|
---|
66 | /// <param name="random">The random number generator.</param>
|
---|
67 | /// <param name="parent1">The first parent vector.</param>
|
---|
68 | /// <param name="parent2">The second parent vector.</param>
|
---|
69 | /// <param name="alpha">The alpha parameter (<see cref="AlphaParameter"/>).</param>
|
---|
70 | /// <returns>The vector resulting from the crossover.</returns>
|
---|
71 | public static RealVector Apply(IRandom random, RealVector parent1, RealVector parent2, DoubleValue alpha) {
|
---|
72 | int length = parent1.Length;
|
---|
73 | if (length != parent2.Length) throw new ArgumentException("UniformAllPositionsArithmeticCrossover: The parent vectors are of different length.", "parent1");
|
---|
74 | if (alpha.Value < 0 || alpha.Value > 1) throw new ArgumentException("UniformAllPositionsArithmeticCrossover: Parameter alpha must be in the range [0;1]", "alpha");
|
---|
75 | RealVector result = new RealVector(length);
|
---|
76 | for (int i = 0; i < length; i++) {
|
---|
77 | result[i] = alpha.Value * parent1[i] + (1 - alpha.Value) * parent2[i];
|
---|
78 | }
|
---|
79 | return result;
|
---|
80 | }
|
---|
81 |
|
---|
82 | /// <summary>
|
---|
83 | /// Checks that there are exactly 2 parents, that the alpha parameter is not null and fowards the call to <see cref="Apply(IRandom, RealVector, DoubleArrrayData, DoubleValue)"/>.
|
---|
84 | /// </summary>
|
---|
85 | /// <exception cref="ArgumentException">Thrown when there are not exactly two parents.</exception>
|
---|
86 | /// <exception cref="InvalidOperationException">Thrown when the alpha parmeter could not be found.</exception>
|
---|
87 | /// <param name="random">The random number generator.</param>
|
---|
88 | /// <param name="parents">The collection of parents (must be of size 2).</param>
|
---|
89 | /// <returns>The vector resulting from the crossover.</returns>
|
---|
90 | protected override RealVector Cross(IRandom random, ItemArray<RealVector> parents) {
|
---|
91 | if (parents.Length != 2) throw new ArgumentException("UniformAllPositionsArithmeticCrossover: There must be exactly two parents.", "parents");
|
---|
92 | if (AlphaParameter.ActualValue == null) throw new InvalidOperationException("UniformAllPositionsArithmeticCrossover: Parameter " + AlphaParameter.ActualName + " could not be found.");
|
---|
93 | return Apply(random, parents[0], parents[1], AlphaParameter.ActualValue);
|
---|
94 | }
|
---|
95 | }
|
---|
96 | }
|
---|