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 | /// Blend alpha-beta crossover for real vectors (BLX-a-b). Creates a new offspring by selecting a
|
---|
31 | /// random value from the interval between the two alleles of the parent solutions.
|
---|
32 | /// The interval is increased in both directions as follows: Into the direction of the 'better'
|
---|
33 | /// solution by the factor alpha, into the direction of the 'worse' solution by the factor beta.
|
---|
34 | /// </summary>
|
---|
35 | /// <remarks>
|
---|
36 | /// 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/>
|
---|
37 | /// The default value for alpha is 0.75, the default value for beta is 0.25.
|
---|
38 | /// </remarks>
|
---|
39 | [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 more around the better parent by specifying a higher alpha value. 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.")]
|
---|
40 | [StorableClass]
|
---|
41 | public class BlendAlphaBetaCrossover : RealVectorCrossover {
|
---|
42 | /// <summary>
|
---|
43 | /// Whether the problem is a maximization or minimization problem.
|
---|
44 | /// </summary>
|
---|
45 | public ValueLookupParameter<BoolData> MaximizationParameter {
|
---|
46 | get { return (ValueLookupParameter<BoolData>)Parameters["Maximization"]; }
|
---|
47 | }
|
---|
48 | /// <summary>
|
---|
49 | /// The quality of the parents.
|
---|
50 | /// </summary>
|
---|
51 | public SubScopesLookupParameter<DoubleData> QualityParameter {
|
---|
52 | get { return (SubScopesLookupParameter<DoubleData>)Parameters["Quality"]; }
|
---|
53 | }
|
---|
54 | /// <summary>
|
---|
55 | /// The alpha parameter specifies how much the interval between the parents should be extended in direction of the better parent.
|
---|
56 | /// </summary>
|
---|
57 | public ValueLookupParameter<DoubleData> AlphaParameter {
|
---|
58 | get { return (ValueLookupParameter<DoubleData>)Parameters["Alpha"]; }
|
---|
59 | }
|
---|
60 | /// <summary>
|
---|
61 | /// The beta parameter specifies how much the interval between the parents should be extended in direction of the worse parent.
|
---|
62 | /// </summary>
|
---|
63 | public ValueLookupParameter<DoubleData> BetaParameter {
|
---|
64 | get { return (ValueLookupParameter<DoubleData>)Parameters["Beta"]; }
|
---|
65 | }
|
---|
66 |
|
---|
67 | /// <summary>
|
---|
68 | /// Initializes a new instance of <see cref="BlendAlphaBetaCrossover"/> with four additional parameters
|
---|
69 | /// (<c>Maximization</c>, <c>Quality</c>, <c>Alpha</c> and <c>Beta</c>).
|
---|
70 | /// </summary>
|
---|
71 | public BlendAlphaBetaCrossover()
|
---|
72 | : base() {
|
---|
73 | Parameters.Add(new ValueLookupParameter<BoolData>("Maximization", "Whether the problem is a maximization problem or not."));
|
---|
74 | Parameters.Add(new SubScopesLookupParameter<DoubleData>("Quality", "The quality values of the parents."));
|
---|
75 | Parameters.Add(new ValueLookupParameter<DoubleData>("Alpha", "The value for alpha.", new DoubleData(0.75)));
|
---|
76 | Parameters.Add(new ValueLookupParameter<DoubleData>("Beta", "The value for beta.", new DoubleData(0.25)));
|
---|
77 | }
|
---|
78 |
|
---|
79 | /// <summary>
|
---|
80 | /// Performs the blend alpha beta crossover (BLX-a-b) on two parent vectors.
|
---|
81 | /// </summary>
|
---|
82 | /// <exception cref="ArgumentException">
|
---|
83 | /// Thrown when either:<br/>
|
---|
84 | /// <list type="bullet">
|
---|
85 | /// <item><description>The length of <paramref name="betterParent"/> and <paramref name="worseParent"/> is not equal.</description></item>
|
---|
86 | /// <item><description>The parameter <paramref name="alpha"/> is smaller than 0.</description></item>
|
---|
87 | /// <item><description>The parameter <paramref name="beta"/> is smaller than 0.</description></item>
|
---|
88 | /// </list>
|
---|
89 | /// </exception>
|
---|
90 | /// <param name="random">The random number generator to use.</param>
|
---|
91 | /// <param name="betterParent">The better of the two parents with regard to their fitness.</param>
|
---|
92 | /// <param name="worseParent">The worse of the two parents with regard to their fitness.</param>
|
---|
93 | /// <param name="alpha">The parameter alpha.</param>
|
---|
94 | /// <param name="beta">The parameter beta.</param>
|
---|
95 | /// <returns>The real vector that results from the crossover.</returns>
|
---|
96 | public static DoubleArrayData Apply(IRandom random, DoubleArrayData betterParent, DoubleArrayData worseParent, DoubleData alpha, DoubleData beta) {
|
---|
97 | if (betterParent.Length != worseParent.Length) throw new ArgumentException("BlendAlphaBetaCrossover: The parents' vectors are of different length.", "betterParent");
|
---|
98 | if (alpha.Value < 0) throw new ArgumentException("BlendAlphaBetaCrossover: Parameter alpha must be greater or equal to 0.", "alpha");
|
---|
99 | if (beta.Value < 0) throw new ArgumentException("BlendAlphaBetaCrossover: Parameter beta must be greater or equal to 0.", "beta");
|
---|
100 | int length = betterParent.Length;
|
---|
101 | double min, max, d;
|
---|
102 | DoubleArrayData result = new DoubleArrayData(length);
|
---|
103 |
|
---|
104 | for (int i = 0; i < length; i++) {
|
---|
105 | d = Math.Abs(betterParent[i] - worseParent[i]);
|
---|
106 | if (betterParent[i] <= worseParent[i]) {
|
---|
107 | min = betterParent[i] - d * alpha.Value;
|
---|
108 | max = worseParent[i] + d * beta.Value;
|
---|
109 | } else {
|
---|
110 | min = worseParent[i] - d * beta.Value;
|
---|
111 | max = betterParent[i] + d * alpha.Value;
|
---|
112 | }
|
---|
113 | result[i] = min + random.NextDouble() * (max - min);
|
---|
114 | }
|
---|
115 | return result;
|
---|
116 | }
|
---|
117 |
|
---|
118 | /// <summary>
|
---|
119 | /// Checks if the number of parents is equal to 2, if all parameters are available and forwards the call to <see cref="Apply(IRandom, DoubleArrayData, DoubleArrayData, DoubleData, DoubleData)"/>.
|
---|
120 | /// </summary>
|
---|
121 | /// <exception cref="ArgumentException">Thrown when the number of parents is not equal to 2.</exception>
|
---|
122 | /// <exception cref="InvalidOperationException">
|
---|
123 | /// Thrown when either:<br/>
|
---|
124 | /// <list type="bullet">
|
---|
125 | /// <item><description>Maximization parameter could not be found.</description></item>
|
---|
126 | /// <item><description>Quality parameter could not be found or the number of quality values is not equal to the number of parents.</description></item>
|
---|
127 | /// <item><description>Alpha parameter could not be found.</description></item>
|
---|
128 | /// <item><description>Beta parameter could not be found.</description></item>
|
---|
129 | /// </list>
|
---|
130 | /// </exception>
|
---|
131 | /// <param name="random">The random number generator to use.</param>
|
---|
132 | /// <param name="parents">The collection of parents (must be of size 2).</param>
|
---|
133 | /// <returns>The real vector that results from the crossover.</returns>
|
---|
134 | protected override DoubleArrayData Cross(IRandom random, ItemArray<DoubleArrayData> parents) {
|
---|
135 | if (parents.Length != 2) throw new ArgumentException("BlendAlphaBetaCrossover: Number of parents is not equal to 2.", "parents");
|
---|
136 | if (MaximizationParameter.ActualValue == null) throw new InvalidOperationException("BlendAlphaBetaCrossover: Parameter " + MaximizationParameter.ActualName + " could not be found.");
|
---|
137 | 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.");
|
---|
138 | if (AlphaParameter.ActualValue == null || BetaParameter.ActualValue == null) throw new InvalidOperationException("BlendAlphaBetaCrossover: Parameter " + AlphaParameter.ActualName + " or paramter " + BetaParameter.ActualName + " could not be found.");
|
---|
139 |
|
---|
140 | ItemArray<DoubleData> qualities = QualityParameter.ActualValue;
|
---|
141 | bool maximization = MaximizationParameter.ActualValue.Value;
|
---|
142 | // the better parent
|
---|
143 | if (maximization && qualities[0].Value >= qualities[1].Value || !maximization && qualities[0].Value <= qualities[1].Value)
|
---|
144 | return Apply(random, parents[0], parents[1], AlphaParameter.ActualValue, BetaParameter.ActualValue);
|
---|
145 | else {
|
---|
146 | return Apply(random, parents[1], parents[0], AlphaParameter.ActualValue, BetaParameter.ActualValue);
|
---|
147 | }
|
---|
148 | }
|
---|
149 | }
|
---|
150 | }
|
---|