[2900] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[16057] | 3 | * Copyright (C) 2002-2018 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;
|
---|
| 25 | using HeuristicLab.Data;
|
---|
[11970] | 26 | using HeuristicLab.Optimization;
|
---|
[2913] | 27 | using HeuristicLab.Parameters;
|
---|
[2900] | 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 29 |
|
---|
[3053] | 30 | namespace HeuristicLab.Encodings.RealVectorEncoding {
|
---|
[2900] | 31 | /// <summary>
|
---|
[2913] | 32 | /// Blend alpha-beta crossover for real vectors (BLX-a-b). Creates a new offspring by selecting a
|
---|
[2900] | 33 | /// random value from the interval between the two alleles of the parent solutions.
|
---|
| 34 | /// The interval is increased in both directions as follows: Into the direction of the 'better'
|
---|
| 35 | /// solution by the factor alpha, into the direction of the 'worse' solution by the factor beta.
|
---|
| 36 | /// </summary>
|
---|
[2913] | 37 | /// <remarks>
|
---|
| 38 | /// It is implemented as described in Takahashi, M. and Kita, H. 2001. A crossover operator using independent component analysis for real-coded genetic algorithms Proceedings of the 2001 Congress on Evolutionary Computation, pp. 643-649.<br/>
|
---|
| 39 | /// The default value for alpha is 0.75, the default value for beta is 0.25.
|
---|
| 40 | /// </remarks>
|
---|
[3907] | 41 | [Item("BlendAlphaBetaCrossover", "The blend alpha beta crossover (BLX-a-b) for real vectors is similar to the blend alpha crossover (BLX-a), but distinguishes between the better and worse of the parents. The interval from which to choose the new offspring can be extended beyond the better parent by specifying a higher alpha value, and beyond the worse parent by specifying a higher beta value. The new offspring is sampled uniformly in the extended range. It is implemented as described in Takahashi, M. and Kita, H. 2001. A crossover operator using independent component analysis for real-coded genetic algorithms Proceedings of the 2001 Congress on Evolutionary Computation, pp. 643-649.")]
|
---|
[3017] | 42 | [StorableClass]
|
---|
[11970] | 43 | public class BlendAlphaBetaCrossover : RealVectorCrossover, ISingleObjectiveOperator {
|
---|
[2921] | 44 | /// <summary>
|
---|
| 45 | /// Whether the problem is a maximization or minimization problem.
|
---|
| 46 | /// </summary>
|
---|
[3048] | 47 | public ValueLookupParameter<BoolValue> MaximizationParameter {
|
---|
| 48 | get { return (ValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
[2900] | 49 | }
|
---|
[2921] | 50 | /// <summary>
|
---|
| 51 | /// The quality of the parents.
|
---|
| 52 | /// </summary>
|
---|
[3659] | 53 | public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
|
---|
| 54 | get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
[2900] | 55 | }
|
---|
[2921] | 56 | /// <summary>
|
---|
[3907] | 57 | /// The Alpha parameter controls the extension of the range beyond the better parent. The value must be >= 0 and does not depend on Beta.
|
---|
[2921] | 58 | /// </summary>
|
---|
[3048] | 59 | public ValueLookupParameter<DoubleValue> AlphaParameter {
|
---|
| 60 | get { return (ValueLookupParameter<DoubleValue>)Parameters["Alpha"]; }
|
---|
[2900] | 61 | }
|
---|
[2921] | 62 | /// <summary>
|
---|
[3907] | 63 | /// The Beta parameter controls the extension of the range beyond the worse parent. The value must be >= 0 and does not depend on Alpha.
|
---|
[2921] | 64 | /// </summary>
|
---|
[3048] | 65 | public ValueLookupParameter<DoubleValue> BetaParameter {
|
---|
| 66 | get { return (ValueLookupParameter<DoubleValue>)Parameters["Beta"]; }
|
---|
[2900] | 67 | }
|
---|
[2921] | 68 |
|
---|
[4722] | 69 | [StorableConstructor]
|
---|
| 70 | protected BlendAlphaBetaCrossover(bool deserializing) : base(deserializing) { }
|
---|
| 71 | protected BlendAlphaBetaCrossover(BlendAlphaBetaCrossover original, Cloner cloner) : base(original, cloner) { }
|
---|
[2900] | 72 | /// <summary>
|
---|
| 73 | /// Initializes a new instance of <see cref="BlendAlphaBetaCrossover"/> with four additional parameters
|
---|
| 74 | /// (<c>Maximization</c>, <c>Quality</c>, <c>Alpha</c> and <c>Beta</c>).
|
---|
| 75 | /// </summary>
|
---|
| 76 | public BlendAlphaBetaCrossover()
|
---|
| 77 | : base() {
|
---|
[3048] | 78 | Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "Whether the problem is a maximization problem or not."));
|
---|
[3659] | 79 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The quality values of the parents."));
|
---|
[3907] | 80 | Parameters.Add(new ValueLookupParameter<DoubleValue>("Alpha", "The Alpha parameter controls the extension of the range beyond the better parent. The value must be >= 0 and does not depend on Beta.", new DoubleValue(0.75)));
|
---|
| 81 | Parameters.Add(new ValueLookupParameter<DoubleValue>("Beta", "The Beta parameter controls the extension of the range beyond the worse parent. The value must be >= 0 and does not depend on Alpha.", new DoubleValue(0.25)));
|
---|
[2900] | 82 | }
|
---|
| 83 |
|
---|
[4722] | 84 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 85 | return new BlendAlphaBetaCrossover(this, cloner);
|
---|
| 86 | }
|
---|
| 87 |
|
---|
[2913] | 88 | /// <summary>
|
---|
| 89 | /// Performs the blend alpha beta crossover (BLX-a-b) on two parent vectors.
|
---|
| 90 | /// </summary>
|
---|
| 91 | /// <exception cref="ArgumentException">
|
---|
| 92 | /// Thrown when either:<br/>
|
---|
| 93 | /// <list type="bullet">
|
---|
| 94 | /// <item><description>The length of <paramref name="betterParent"/> and <paramref name="worseParent"/> is not equal.</description></item>
|
---|
| 95 | /// <item><description>The parameter <paramref name="alpha"/> is smaller than 0.</description></item>
|
---|
| 96 | /// <item><description>The parameter <paramref name="beta"/> is smaller than 0.</description></item>
|
---|
| 97 | /// </list>
|
---|
| 98 | /// </exception>
|
---|
| 99 | /// <param name="random">The random number generator to use.</param>
|
---|
| 100 | /// <param name="betterParent">The better of the two parents with regard to their fitness.</param>
|
---|
| 101 | /// <param name="worseParent">The worse of the two parents with regard to their fitness.</param>
|
---|
| 102 | /// <param name="alpha">The parameter alpha.</param>
|
---|
| 103 | /// <param name="beta">The parameter beta.</param>
|
---|
| 104 | /// <returns>The real vector that results from the crossover.</returns>
|
---|
[3060] | 105 | public static RealVector Apply(IRandom random, RealVector betterParent, RealVector worseParent, DoubleValue alpha, DoubleValue beta) {
|
---|
[2900] | 106 | if (betterParent.Length != worseParent.Length) throw new ArgumentException("BlendAlphaBetaCrossover: The parents' vectors are of different length.", "betterParent");
|
---|
[2913] | 107 | if (alpha.Value < 0) throw new ArgumentException("BlendAlphaBetaCrossover: Parameter alpha must be greater or equal to 0.", "alpha");
|
---|
| 108 | if (beta.Value < 0) throw new ArgumentException("BlendAlphaBetaCrossover: Parameter beta must be greater or equal to 0.", "beta");
|
---|
[2900] | 109 | int length = betterParent.Length;
|
---|
[2913] | 110 | double min, max, d;
|
---|
[3060] | 111 | RealVector result = new RealVector(length);
|
---|
[2900] | 112 |
|
---|
| 113 | for (int i = 0; i < length; i++) {
|
---|
[2913] | 114 | d = Math.Abs(betterParent[i] - worseParent[i]);
|
---|
| 115 | if (betterParent[i] <= worseParent[i]) {
|
---|
| 116 | min = betterParent[i] - d * alpha.Value;
|
---|
| 117 | max = worseParent[i] + d * beta.Value;
|
---|
| 118 | } else {
|
---|
| 119 | min = worseParent[i] - d * beta.Value;
|
---|
| 120 | max = betterParent[i] + d * alpha.Value;
|
---|
| 121 | }
|
---|
| 122 | result[i] = min + random.NextDouble() * (max - min);
|
---|
[2900] | 123 | }
|
---|
| 124 | return result;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
[2913] | 127 | /// <summary>
|
---|
[3060] | 128 | /// Checks if the number of parents is equal to 2, if all parameters are available and forwards the call to <see cref="Apply(IRandom, RealVector, RealVector, DoubleValue, DoubleValue)"/>.
|
---|
[2913] | 129 | /// </summary>
|
---|
| 130 | /// <exception cref="ArgumentException">Thrown when the number of parents is not equal to 2.</exception>
|
---|
| 131 | /// <exception cref="InvalidOperationException">
|
---|
| 132 | /// Thrown when either:<br/>
|
---|
| 133 | /// <list type="bullet">
|
---|
| 134 | /// <item><description>Maximization parameter could not be found.</description></item>
|
---|
| 135 | /// <item><description>Quality parameter could not be found or the number of quality values is not equal to the number of parents.</description></item>
|
---|
| 136 | /// <item><description>Alpha parameter could not be found.</description></item>
|
---|
| 137 | /// <item><description>Beta parameter could not be found.</description></item>
|
---|
| 138 | /// </list>
|
---|
| 139 | /// </exception>
|
---|
| 140 | /// <param name="random">The random number generator to use.</param>
|
---|
| 141 | /// <param name="parents">The collection of parents (must be of size 2).</param>
|
---|
| 142 | /// <returns>The real vector that results from the crossover.</returns>
|
---|
[3060] | 143 | protected override RealVector Cross(IRandom random, ItemArray<RealVector> parents) {
|
---|
[2913] | 144 | if (parents.Length != 2) throw new ArgumentException("BlendAlphaBetaCrossover: Number of parents is not equal to 2.", "parents");
|
---|
[2900] | 145 | if (MaximizationParameter.ActualValue == null) throw new InvalidOperationException("BlendAlphaBetaCrossover: Parameter " + MaximizationParameter.ActualName + " could not be found.");
|
---|
| 146 | if (QualityParameter.ActualValue == null || QualityParameter.ActualValue.Length != parents.Length) throw new InvalidOperationException("BlendAlphaBetaCrossover: Parameter " + QualityParameter.ActualName + " could not be found, or not in the same quantity as there are parents.");
|
---|
| 147 | if (AlphaParameter.ActualValue == null || BetaParameter.ActualValue == null) throw new InvalidOperationException("BlendAlphaBetaCrossover: Parameter " + AlphaParameter.ActualName + " or paramter " + BetaParameter.ActualName + " could not be found.");
|
---|
[4068] | 148 |
|
---|
[3048] | 149 | ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
|
---|
[2900] | 150 | bool maximization = MaximizationParameter.ActualValue.Value;
|
---|
| 151 | if (maximization && qualities[0].Value >= qualities[1].Value || !maximization && qualities[0].Value <= qualities[1].Value)
|
---|
| 152 | return Apply(random, parents[0], parents[1], AlphaParameter.ActualValue, BetaParameter.ActualValue);
|
---|
| 153 | else {
|
---|
| 154 | return Apply(random, parents[1], parents[0], AlphaParameter.ActualValue, BetaParameter.ActualValue);
|
---|
| 155 | }
|
---|
| 156 | }
|
---|
| 157 | }
|
---|
| 158 | }
|
---|