1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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.Default.CompositeSerializers.Storable;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Encodings.IntegerVectorEncoding {
|
---|
30 | /// <summary>
|
---|
31 | /// Blend alpha-beta crossover for integer vectors (BLX-a-b). Creates a new offspring by selecting a
|
---|
32 | /// random value from the interval between the two alleles of the parent solutions and rounds the
|
---|
33 | /// result to the nearest feasible value. The interval is increased in both directions as follows:
|
---|
34 | /// Into the direction of the 'better' solution by the factor alpha, into the direction of the
|
---|
35 | /// 'worse' solution by the factor beta.
|
---|
36 | /// </summary>
|
---|
37 | [Item("RoundedBlendAlphaBetaCrossover", "The rounded blend alpha beta crossover (BLX-a-b) for integer 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 and rounded to the next feasible integer.")]
|
---|
38 | [StorableClass]
|
---|
39 | public class RoundedBlendAlphaBetaCrossover : BoundedIntegerVectorCrossover {
|
---|
40 | /// <summary>
|
---|
41 | /// Whether the problem is a maximization or minimization problem.
|
---|
42 | /// </summary>
|
---|
43 | public ValueLookupParameter<BoolValue> MaximizationParameter {
|
---|
44 | get { return (ValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
45 | }
|
---|
46 | /// <summary>
|
---|
47 | /// The quality of the parents.
|
---|
48 | /// </summary>
|
---|
49 | public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
|
---|
50 | get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
51 | }
|
---|
52 | /// <summary>
|
---|
53 | /// The Alpha parameter controls the extension of the range beyond the better parent. The value must be >= 0 and does not depend on Beta.
|
---|
54 | /// </summary>
|
---|
55 | public ValueLookupParameter<DoubleValue> AlphaParameter {
|
---|
56 | get { return (ValueLookupParameter<DoubleValue>)Parameters["Alpha"]; }
|
---|
57 | }
|
---|
58 | /// <summary>
|
---|
59 | /// The Beta parameter controls the extension of the range beyond the worse parent. The value must be >= 0 and does not depend on Alpha.
|
---|
60 | /// </summary>
|
---|
61 | public ValueLookupParameter<DoubleValue> BetaParameter {
|
---|
62 | get { return (ValueLookupParameter<DoubleValue>)Parameters["Beta"]; }
|
---|
63 | }
|
---|
64 |
|
---|
65 | [StorableConstructor]
|
---|
66 | protected RoundedBlendAlphaBetaCrossover(bool deserializing) : base(deserializing) { }
|
---|
67 | protected RoundedBlendAlphaBetaCrossover(RoundedBlendAlphaBetaCrossover original, Cloner cloner) : base(original, cloner) { }
|
---|
68 | /// <summary>
|
---|
69 | /// Initializes a new instance of <see cref="RoundedBlendAlphaBetaCrossover"/> with four additional parameters
|
---|
70 | /// (<c>Maximization</c>, <c>Quality</c>, <c>Alpha</c> and <c>Beta</c>).
|
---|
71 | /// </summary>
|
---|
72 | public RoundedBlendAlphaBetaCrossover()
|
---|
73 | : base() {
|
---|
74 | Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "Whether the problem is a maximization problem or not."));
|
---|
75 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The quality values of the parents."));
|
---|
76 | 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)));
|
---|
77 | 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)));
|
---|
78 | }
|
---|
79 |
|
---|
80 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
81 | return new RoundedBlendAlphaBetaCrossover(this, cloner);
|
---|
82 | }
|
---|
83 |
|
---|
84 | /// <summary>
|
---|
85 | /// Performs the rounded blend alpha beta crossover (BLX-a-b) on two parent vectors.
|
---|
86 | /// </summary>
|
---|
87 | /// <exception cref="ArgumentException">
|
---|
88 | /// Thrown when either:<br/>
|
---|
89 | /// <list type="bullet">
|
---|
90 | /// <item><description>The length of <paramref name="betterParent"/> and <paramref name="worseParent"/> is not equal.</description></item>
|
---|
91 | /// <item><description>The parameter <paramref name="alpha"/> is smaller than 0.</description></item>
|
---|
92 | /// <item><description>The parameter <paramref name="beta"/> is smaller than 0.</description></item>
|
---|
93 | /// </list>
|
---|
94 | /// </exception>
|
---|
95 | /// <param name="random">The random number generator to use.</param>
|
---|
96 | /// <param name="betterParent">The better of the two parents with regard to their fitness.</param>
|
---|
97 | /// <param name="worseParent">The worse of the two parents with regard to their fitness.</param>
|
---|
98 | /// <param name="bounds">The bounds and step size for each dimension (will be cycled in case there are less rows than elements in the parent vectors).</param>
|
---|
99 | /// <param name="alpha">The parameter alpha.</param>
|
---|
100 | /// <param name="beta">The parameter beta.</param>
|
---|
101 | /// <returns>The integer vector that results from the crossover.</returns>
|
---|
102 | public static IntegerVector Apply(IRandom random, IntegerVector betterParent, IntegerVector worseParent, IntMatrix bounds, DoubleValue alpha, DoubleValue beta) {
|
---|
103 | if (betterParent.Length != worseParent.Length) throw new ArgumentException("RoundedBlendAlphaBetaCrossover: The parents' vectors are of different length.", "betterParent");
|
---|
104 | if (alpha.Value < 0) throw new ArgumentException("RoundedBlendAlphaBetaCrossover: Parameter alpha must be greater or equal to 0.", "alpha");
|
---|
105 | if (beta.Value < 0) throw new ArgumentException("RoundedBlendAlphaBetaCrossover: Parameter beta must be greater or equal to 0.", "beta");
|
---|
106 | if (bounds == null || bounds.Rows < 1 || bounds.Columns < 2) throw new ArgumentException("RoundedBlendAlphaBetaCrossover: Invalid bounds specified.", "bounds");
|
---|
107 |
|
---|
108 | int length = betterParent.Length;
|
---|
109 | double min, max, d;
|
---|
110 | var result = new IntegerVector(length);
|
---|
111 | int minBound, maxBound, step = 1;
|
---|
112 | for (int i = 0; i < length; i++) {
|
---|
113 | minBound = bounds[i % bounds.Rows, 0];
|
---|
114 | maxBound = bounds[i % bounds.Rows, 1];
|
---|
115 | if (bounds.Columns > 2) step = bounds[i % bounds.Rows, 2];
|
---|
116 | maxBound = FloorFeasible(minBound, maxBound, step, maxBound - 1);
|
---|
117 |
|
---|
118 | d = Math.Abs(betterParent[i] - worseParent[i]);
|
---|
119 | if (betterParent[i] <= worseParent[i]) {
|
---|
120 | min = FloorFeasible(minBound, maxBound, step, betterParent[i] - d * alpha.Value);
|
---|
121 | max = CeilingFeasible(minBound, maxBound, step, worseParent[i] + d * beta.Value);
|
---|
122 | } else {
|
---|
123 | min = FloorFeasible(minBound, maxBound, step, worseParent[i] - d * beta.Value);
|
---|
124 | max = CeilingFeasible(minBound, maxBound, step, betterParent[i] + d * alpha.Value);
|
---|
125 | }
|
---|
126 | result[i] = RoundFeasible(minBound, maxBound, step, min + random.NextDouble() * (max - min));
|
---|
127 | }
|
---|
128 | return result;
|
---|
129 | }
|
---|
130 |
|
---|
131 | /// <summary>
|
---|
132 | /// Checks if the number of parents is equal to 2, if all parameters are available and forwards the call to <see cref="Apply(IRandom, IntegerVector, IntegerVector, IntMatrix, DoubleValue, DoubleValue)"/>.
|
---|
133 | /// </summary>
|
---|
134 | /// <exception cref="ArgumentException">Thrown when the number of parents is not equal to 2.</exception>
|
---|
135 | /// <exception cref="InvalidOperationException">
|
---|
136 | /// Thrown when either:<br/>
|
---|
137 | /// <list type="bullet">
|
---|
138 | /// <item><description>Maximization parameter could not be found.</description></item>
|
---|
139 | /// <item><description>Quality parameter could not be found or the number of quality values is not equal to the number of parents.</description></item>
|
---|
140 | /// <item><description>Alpha parameter could not be found.</description></item>
|
---|
141 | /// <item><description>Beta parameter could not be found.</description></item>
|
---|
142 | /// </list>
|
---|
143 | /// </exception>
|
---|
144 | /// <param name="random">The random number generator to use.</param>
|
---|
145 | /// <param name="parents">The collection of parents (must be of size 2).</param>
|
---|
146 | /// <param name="bounds">The bounds and step size for each dimension (will be cycled in case there are less rows than elements in the parent vectors).</param>
|
---|
147 | /// <returns>The integer vector that results from the crossover.</returns>
|
---|
148 | protected override IntegerVector CrossBounded(IRandom random, ItemArray<IntegerVector> parents, IntMatrix bounds) {
|
---|
149 | if (parents.Length != 2) throw new ArgumentException("RoundedBlendAlphaBetaCrossover: Number of parents is not equal to 2.", "parents");
|
---|
150 | if (MaximizationParameter.ActualValue == null) throw new InvalidOperationException("RoundedBlendAlphaBetaCrossover: Parameter " + MaximizationParameter.ActualName + " could not be found.");
|
---|
151 | if (QualityParameter.ActualValue == null || QualityParameter.ActualValue.Length != parents.Length) throw new InvalidOperationException("RoundedBlendAlphaBetaCrossover: Parameter " + QualityParameter.ActualName + " could not be found, or not in the same quantity as there are parents.");
|
---|
152 | if (AlphaParameter.ActualValue == null || BetaParameter.ActualValue == null) throw new InvalidOperationException("RoundedBlendAlphaBetaCrossover: Parameter " + AlphaParameter.ActualName + " or paramter " + BetaParameter.ActualName + " could not be found.");
|
---|
153 |
|
---|
154 | ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
|
---|
155 | bool maximization = MaximizationParameter.ActualValue.Value;
|
---|
156 | if (maximization && qualities[0].Value >= qualities[1].Value || !maximization && qualities[0].Value <= qualities[1].Value)
|
---|
157 | return Apply(random, parents[0], parents[1], bounds, AlphaParameter.ActualValue, BetaParameter.ActualValue);
|
---|
158 | else {
|
---|
159 | return Apply(random, parents[1], parents[0], bounds, AlphaParameter.ActualValue, BetaParameter.ActualValue);
|
---|
160 | }
|
---|
161 | }
|
---|
162 | }
|
---|
163 | }
|
---|