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 |
|
---|
22 | using System;
|
---|
23 | using HeuristicLab.Core;
|
---|
24 | using HeuristicLab.Data;
|
---|
25 | using HeuristicLab.Parameters;
|
---|
26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
27 |
|
---|
28 | namespace HeuristicLab.Encodings.RealVector {
|
---|
29 | /// <summary>
|
---|
30 | /// The uniform all positions arithmetic crossover (continuous recombination) constructs an offspring by calculating x = alpha * p1 + (1-alpha) * p2 for a position x in the vector with a given probability (otherwise p1 is taken at this position).
|
---|
31 | /// </summary>
|
---|
32 | /// <remarks>
|
---|
33 | /// It is implemented as described in Dumitrescu, D. et al. (2000), Evolutionary computation, CRC Press, Boca Raton, FL, p. 191.<br />
|
---|
34 | /// Note that the implementation is a bit more general than the operator described by Dumitrescu et al. There the alpha parameter was defined to be fixed at 0.5 and thus calculating the average at some positions.
|
---|
35 | /// </remarks>
|
---|
36 | [Item("UniformSomePositionsArithmeticCrossover", "The uniform some positions arithmetic crossover (continuous recombination) constructs an offspring by calculating x = alpha * p1 + (1-alpha) * p2 for a position x in the vector with a given probability (otherwise p1 is taken at this position). It is implemented as described in Dumitrescu, D. et al. (2000), Evolutionary computation, CRC Press, Boca Raton, FL, p. 191. Note that Dumitrescu et al. specify the alpha to be 0.5.")]
|
---|
37 | class UniformSomePositionsArithmeticCrossover : RealVectorCrossover {
|
---|
38 | /// <summary>
|
---|
39 | /// 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).
|
---|
40 | /// </summary>
|
---|
41 | public ValueLookupParameter<DoubleData> AlphaParameter {
|
---|
42 | get { return (ValueLookupParameter<DoubleData>)Parameters["Alpha"]; }
|
---|
43 | }
|
---|
44 | /// <summary>
|
---|
45 | /// The probability in the range [0;1] for each position in the vector to be crossed.
|
---|
46 | /// </summary>
|
---|
47 | public ValueLookupParameter<DoubleData> ProbabilityParameter {
|
---|
48 | get { return (ValueLookupParameter<DoubleData>)Parameters["Probability"]; }
|
---|
49 | }
|
---|
50 |
|
---|
51 | /// <summary>
|
---|
52 | /// Initializes a new instance with two parameters (<c>Alpha</c> and <c>Probability</c>).
|
---|
53 | /// </summary>
|
---|
54 | public UniformSomePositionsArithmeticCrossover()
|
---|
55 | : base() {
|
---|
56 | Parameters.Add(new ValueLookupParameter<DoubleData>("Alpha", "The alpha value in the range [0;1]", new DoubleData(0.5)));
|
---|
57 | Parameters.Add(new ValueLookupParameter<DoubleData>("Probability", "The probability for crossing a position in the range [0;1]", new DoubleData(0.5)));
|
---|
58 | }
|
---|
59 |
|
---|
60 | /// <summary>
|
---|
61 | /// Performs the arithmetic crossover on some positions by taking either x = alpha * p1 + (1 - alpha) * p2 or x = p1 depending on the probability for a gene to be crossed.
|
---|
62 | /// </summary>
|
---|
63 | /// <param name="random">The random number generator.</param>
|
---|
64 | /// <param name="parent1">The first parent vector.</param>
|
---|
65 | /// <param name="parent2">The second parent vector.</param>
|
---|
66 | /// <param name="alpha">The alpha parameter (<see cref="AlphaParameter"/>).</param>
|
---|
67 | /// <param name="probability">The probability parameter (<see cref="ProbabilityParameter"/>).</param>
|
---|
68 | /// <returns>The vector resulting from the crossover.</returns>
|
---|
69 | public static DoubleArrayData Apply(IRandom random, DoubleArrayData parent1, DoubleArrayData parent2, DoubleData alpha, DoubleData probability) {
|
---|
70 | int length = parent1.Length;
|
---|
71 | if (length != parent2.Length) throw new ArgumentException("UniformSomePositionsArithmeticCrossover: The parent vectors are of different length.", "parent1");
|
---|
72 | if (alpha.Value < 0 || alpha.Value > 1) throw new ArgumentException("UniformSomePositionsArithmeticCrossover: Parameter alpha must be in the range [0;1]", "alpha");
|
---|
73 | if (probability.Value < 0 || probability.Value > 1) throw new ArgumentException("UniformSomePositionsArithmeticCrossover: Parameter probability must be in the range [0;1]", "probability");
|
---|
74 |
|
---|
75 | DoubleArrayData result = new DoubleArrayData(length);
|
---|
76 | for (int i = 0; i < length; i++) {
|
---|
77 | if (random.NextDouble() < probability.Value)
|
---|
78 | result[i] = alpha.Value * parent1[i] + (1 - alpha.Value) * parent2[i];
|
---|
79 | else result[i] = parent1[i];
|
---|
80 | }
|
---|
81 | return result;
|
---|
82 | }
|
---|
83 |
|
---|
84 | /// <summary>
|
---|
85 | /// Checks that there are exactly 2 parents, that the alpha and the probability parameter are not null and fowards the call to <see cref="Apply(IRandom, DoubleArrayData, DoubleArrrayData, DoubleData)"/>.
|
---|
86 | /// </summary>
|
---|
87 | /// <exception cref="ArgumentException">Thrown when there are not exactly two parents.</exception>
|
---|
88 | /// <exception cref="InvalidOperationException">Thrown when either the alpha parmeter or the probability parameter could not be found.</exception>
|
---|
89 | /// <param name="random">The random number generator.</param>
|
---|
90 | /// <param name="parents">The collection of parents (must be of size 2).</param>
|
---|
91 | /// <returns>The vector resulting from the crossover.</returns>
|
---|
92 | protected override DoubleArrayData Cross(IRandom random, ItemArray<DoubleArrayData> parents) {
|
---|
93 | if (parents.Length != 2) throw new ArgumentException("UniformSomePositionsArithmeticCrossover: There must be exactly two parents.", "parents");
|
---|
94 | if (AlphaParameter.ActualValue == null) throw new InvalidOperationException("UniformSomePositionsArithmeticCrossover: Parameter " + AlphaParameter.ActualName + " could not be found.");
|
---|
95 | if (ProbabilityParameter.ActualValue == null) throw new InvalidOperationException("UniformSomePositionsArithmeticCrossover: Parameter " + ProbabilityParameter.ActualName + " could not be found.");
|
---|
96 | return Apply(random, parents[0], parents[1], AlphaParameter.ActualValue, ProbabilityParameter.ActualValue);
|
---|
97 | }
|
---|
98 | }
|
---|
99 | }
|
---|