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 |
|
---|
117 | d = Math.Abs(betterParent[i] - worseParent[i]);
|
---|
118 | if (betterParent[i] <= worseParent[i]) {
|
---|
119 | min = FloorFeasible(minBound, maxBound, step, betterParent[i] - d * alpha.Value);
|
---|
120 | max = CeilingFeasible(minBound, maxBound, step, worseParent[i] + d * beta.Value);
|
---|
121 | } else {
|
---|
122 | min = FloorFeasible(minBound, maxBound, step, worseParent[i] - d * beta.Value);
|
---|
123 | max = CeilingFeasible(minBound, maxBound, step, betterParent[i] + d * alpha.Value);
|
---|
124 | }
|
---|
125 | result[i] = RoundFeasible(minBound, maxBound, step, min + random.NextDouble() * (max - min));
|
---|
126 | }
|
---|
127 | return result;
|
---|
128 | }
|
---|
129 |
|
---|
130 | /// <summary>
|
---|
131 | /// 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)"/>.
|
---|
132 | /// </summary>
|
---|
133 | /// <exception cref="ArgumentException">Thrown when the number of parents is not equal to 2.</exception>
|
---|
134 | /// <exception cref="InvalidOperationException">
|
---|
135 | /// Thrown when either:<br/>
|
---|
136 | /// <list type="bullet">
|
---|
137 | /// <item><description>Maximization parameter could not be found.</description></item>
|
---|
138 | /// <item><description>Quality parameter could not be found or the number of quality values is not equal to the number of parents.</description></item>
|
---|
139 | /// <item><description>Alpha parameter could not be found.</description></item>
|
---|
140 | /// <item><description>Beta parameter could not be found.</description></item>
|
---|
141 | /// </list>
|
---|
142 | /// </exception>
|
---|
143 | /// <param name="random">The random number generator to use.</param>
|
---|
144 | /// <param name="parents">The collection of parents (must be of size 2).</param>
|
---|
145 | /// <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>
|
---|
146 | /// <returns>The integer vector that results from the crossover.</returns>
|
---|
147 | protected override IntegerVector CrossBounded(IRandom random, ItemArray<IntegerVector> parents, IntMatrix bounds) {
|
---|
148 | if (parents.Length != 2) throw new ArgumentException("RoundedBlendAlphaBetaCrossover: Number of parents is not equal to 2.", "parents");
|
---|
149 | if (MaximizationParameter.ActualValue == null) throw new InvalidOperationException("RoundedBlendAlphaBetaCrossover: Parameter " + MaximizationParameter.ActualName + " could not be found.");
|
---|
150 | 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.");
|
---|
151 | if (AlphaParameter.ActualValue == null || BetaParameter.ActualValue == null) throw new InvalidOperationException("RoundedBlendAlphaBetaCrossover: Parameter " + AlphaParameter.ActualName + " or paramter " + BetaParameter.ActualName + " could not be found.");
|
---|
152 |
|
---|
153 | ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
|
---|
154 | bool maximization = MaximizationParameter.ActualValue.Value;
|
---|
155 | if (maximization && qualities[0].Value >= qualities[1].Value || !maximization && qualities[0].Value <= qualities[1].Value)
|
---|
156 | return Apply(random, parents[0], parents[1], bounds, AlphaParameter.ActualValue, BetaParameter.ActualValue);
|
---|
157 | else {
|
---|
158 | return Apply(random, parents[1], parents[0], bounds, AlphaParameter.ActualValue, BetaParameter.ActualValue);
|
---|
159 | }
|
---|
160 | }
|
---|
161 | }
|
---|
162 | }
|
---|