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.RealVectorEncoding {
|
---|
29 | /// <summary>
|
---|
30 | /// Heuristic crossover for real vectors: Calculates the vector from the worse to the better parent and adds that to the better parent weighted with a factor in the interval [0;1).
|
---|
31 | /// The idea is that going further in direction from the worse to the better leads to even better solutions (naturally this depends on the fitness landscape).
|
---|
32 | /// </summary>
|
---|
33 | /// <remarks>
|
---|
34 | /// It is implemented as described in Wright, A.H. (1994), Genetic algorithms for real parameter optimization, Foundations of Genetic Algorithms, G.J.E. Rawlins (Ed.), Morgan Kaufmann, San Mateo, CA, 205-218.
|
---|
35 | /// </remarks>
|
---|
36 | [Item("HeuristicCrossover", "The heuristic crossover produces offspring that extend the better parent in direction from the worse to the better parent. It is implemented as described in Wright, A.H. (1994), Genetic algorithms for real parameter optimization, Foundations of Genetic Algorithms, G.J.E. Rawlins (Ed.), Morgan Kaufmann, San Mateo, CA, 205-218.")]
|
---|
37 | [StorableClass]
|
---|
38 | public class HeuristicCrossover : RealVectorCrossover {
|
---|
39 | /// <summary>
|
---|
40 | /// Whether the problem is a maximization or minimization problem.
|
---|
41 | /// </summary>
|
---|
42 | public ValueLookupParameter<BoolValue> MaximizationParameter {
|
---|
43 | get { return (ValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
44 | }
|
---|
45 | /// <summary>
|
---|
46 | /// The quality of the parents.
|
---|
47 | /// </summary>
|
---|
48 | public SubScopesLookupParameter<DoubleValue> QualityParameter {
|
---|
49 | get { return (SubScopesLookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
50 | }
|
---|
51 |
|
---|
52 | /// <summary>
|
---|
53 | /// Initializes a new instance of <see cref="HeuristicCrossover"/> with two variable infos
|
---|
54 | /// (<c>Maximization</c> and <c>Quality</c>).
|
---|
55 | /// </summary>
|
---|
56 | public HeuristicCrossover()
|
---|
57 | : base() {
|
---|
58 | Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "Whether the problem is a maximization problem or not."));
|
---|
59 | Parameters.Add(new SubScopesLookupParameter<DoubleValue>("Quality", "The quality values of the parents."));
|
---|
60 | }
|
---|
61 |
|
---|
62 | /// <summary>
|
---|
63 | /// Perfomrs a heuristic crossover on the two given parents.
|
---|
64 | /// </summary>
|
---|
65 | /// <exception cref="ArgumentException">Thrown when two parents are not of the same length.</exception>
|
---|
66 | /// <param name="random">The random number generator.</param>
|
---|
67 | /// <param name="betterParent">The first parent for the crossover operation.</param>
|
---|
68 | /// <param name="worseParent">The second parent for the crossover operation.</param>
|
---|
69 | /// <returns>The newly created real vector, resulting from the heuristic crossover.</returns>
|
---|
70 | public static RealVector Apply(IRandom random, RealVector betterParent, RealVector worseParent) {
|
---|
71 | if (betterParent.Length != worseParent.Length)
|
---|
72 | throw new ArgumentException("HeuristicCrossover: the two parents are not of the same length");
|
---|
73 |
|
---|
74 | int length = betterParent.Length;
|
---|
75 | double[] result = new double[length];
|
---|
76 | double factor = random.NextDouble();
|
---|
77 |
|
---|
78 | for (int i = 0; i < length; i++) {
|
---|
79 | result[i] = betterParent[i] + factor * (betterParent[i] - worseParent[i]);
|
---|
80 | }
|
---|
81 | return new RealVector(result);
|
---|
82 | }
|
---|
83 |
|
---|
84 | /// <summary>
|
---|
85 | /// Performs a heuristic crossover operation for two given parent real vectors.
|
---|
86 | /// </summary>
|
---|
87 | /// <exception cref="ArgumentException">Thrown when the number of parents is not equal to 2.</exception>
|
---|
88 | /// <exception cref="InvalidOperationException">
|
---|
89 | /// Thrown when either:<br/>
|
---|
90 | /// <list type="bullet">
|
---|
91 | /// <item><description>Maximization parameter could not be found.</description></item>
|
---|
92 | /// <item><description>Quality parameter could not be found or the number of quality values is not equal to the number of parents.</description></item>
|
---|
93 | /// </list>
|
---|
94 | /// </exception>
|
---|
95 | /// <param name="random">A random number generator.</param>
|
---|
96 | /// <param name="parents">An array containing the two real vectors that should be crossed.</param>
|
---|
97 | /// <returns>The newly created real vector, resulting from the crossover operation.</returns>
|
---|
98 | protected override RealVector Cross(IRandom random, ItemArray<RealVector> parents) {
|
---|
99 | if (parents.Length != 2) throw new ArgumentException("HeuristicCrossover: The number of parents is not equal to 2");
|
---|
100 |
|
---|
101 | if (MaximizationParameter.ActualValue == null) throw new InvalidOperationException("HeuristicCrossover: Parameter " + MaximizationParameter.ActualName + " could not be found.");
|
---|
102 | if (QualityParameter.ActualValue == null || QualityParameter.ActualValue.Length != parents.Length) throw new InvalidOperationException("HeuristicCrossover: Parameter " + QualityParameter.ActualName + " could not be found, or not in the same quantity as there are parents.");
|
---|
103 |
|
---|
104 | ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
|
---|
105 | bool maximization = MaximizationParameter.ActualValue.Value;
|
---|
106 |
|
---|
107 | if (maximization && qualities[0].Value >= qualities[1].Value || !maximization && qualities[0].Value <= qualities[1].Value)
|
---|
108 | return Apply(random, parents[0], parents[1]);
|
---|
109 | else
|
---|
110 | return Apply(random, parents[1], parents[0]);
|
---|
111 | }
|
---|
112 | }
|
---|
113 | }
|
---|