[2900] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[11576] | 3 | * Copyright (C) 2002-2014 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;
|
---|
[2969] | 26 | using HeuristicLab.Parameters;
|
---|
[2936] | 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[2900] | 28 |
|
---|
[3053] | 29 | namespace HeuristicLab.Encodings.RealVectorEncoding {
|
---|
[2900] | 30 | /// <summary>
|
---|
[2964] | 31 | /// 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).
|
---|
| 32 | /// 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).
|
---|
[2900] | 33 | /// </summary>
|
---|
[2936] | 34 | /// <remarks>
|
---|
| 35 | /// 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.
|
---|
| 36 | /// </remarks>
|
---|
[2964] | 37 | [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.")]
|
---|
[3017] | 38 | [StorableClass]
|
---|
[2936] | 39 | public class HeuristicCrossover : RealVectorCrossover {
|
---|
[2900] | 40 | /// <summary>
|
---|
[2936] | 41 | /// Whether the problem is a maximization or minimization problem.
|
---|
| 42 | /// </summary>
|
---|
[3048] | 43 | public ValueLookupParameter<BoolValue> MaximizationParameter {
|
---|
| 44 | get { return (ValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
[2936] | 45 | }
|
---|
| 46 | /// <summary>
|
---|
| 47 | /// The quality of the parents.
|
---|
| 48 | /// </summary>
|
---|
[3659] | 49 | public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
|
---|
| 50 | get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
[2936] | 51 | }
|
---|
| 52 |
|
---|
[4722] | 53 | [StorableConstructor]
|
---|
| 54 | protected HeuristicCrossover(bool deserializing) : base(deserializing) { }
|
---|
| 55 | protected HeuristicCrossover(HeuristicCrossover original, Cloner cloner) : base(original, cloner) { }
|
---|
[2936] | 56 | /// <summary>
|
---|
[2900] | 57 | /// Initializes a new instance of <see cref="HeuristicCrossover"/> with two variable infos
|
---|
| 58 | /// (<c>Maximization</c> and <c>Quality</c>).
|
---|
| 59 | /// </summary>
|
---|
| 60 | public HeuristicCrossover()
|
---|
| 61 | : base() {
|
---|
[3048] | 62 | Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "Whether the problem is a maximization problem or not."));
|
---|
[3659] | 63 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The quality values of the parents."));
|
---|
[2900] | 64 | }
|
---|
| 65 |
|
---|
[4722] | 66 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 67 | return new HeuristicCrossover(this, cloner);
|
---|
| 68 | }
|
---|
| 69 |
|
---|
[2900] | 70 | /// <summary>
|
---|
| 71 | /// Perfomrs a heuristic crossover on the two given parents.
|
---|
| 72 | /// </summary>
|
---|
[2936] | 73 | /// <exception cref="ArgumentException">Thrown when two parents are not of the same length.</exception>
|
---|
[2900] | 74 | /// <param name="random">The random number generator.</param>
|
---|
[2936] | 75 | /// <param name="betterParent">The first parent for the crossover operation.</param>
|
---|
| 76 | /// <param name="worseParent">The second parent for the crossover operation.</param>
|
---|
[2900] | 77 | /// <returns>The newly created real vector, resulting from the heuristic crossover.</returns>
|
---|
[3060] | 78 | public static RealVector Apply(IRandom random, RealVector betterParent, RealVector worseParent) {
|
---|
[2936] | 79 | if (betterParent.Length != worseParent.Length)
|
---|
[2964] | 80 | throw new ArgumentException("HeuristicCrossover: the two parents are not of the same length");
|
---|
[4068] | 81 |
|
---|
[2936] | 82 | int length = betterParent.Length;
|
---|
[2900] | 83 | double[] result = new double[length];
|
---|
| 84 | double factor = random.NextDouble();
|
---|
| 85 |
|
---|
| 86 | for (int i = 0; i < length; i++) {
|
---|
[2936] | 87 | result[i] = betterParent[i] + factor * (betterParent[i] - worseParent[i]);
|
---|
[2900] | 88 | }
|
---|
[3060] | 89 | return new RealVector(result);
|
---|
[2900] | 90 | }
|
---|
| 91 |
|
---|
| 92 | /// <summary>
|
---|
| 93 | /// Performs a heuristic crossover operation for two given parent real vectors.
|
---|
| 94 | /// </summary>
|
---|
[2936] | 95 | /// <exception cref="ArgumentException">Thrown when the number of parents is not equal to 2.</exception>
|
---|
| 96 | /// <exception cref="InvalidOperationException">
|
---|
| 97 | /// Thrown when either:<br/>
|
---|
| 98 | /// <list type="bullet">
|
---|
| 99 | /// <item><description>Maximization parameter could not be found.</description></item>
|
---|
| 100 | /// <item><description>Quality parameter could not be found or the number of quality values is not equal to the number of parents.</description></item>
|
---|
| 101 | /// </list>
|
---|
| 102 | /// </exception>
|
---|
[2900] | 103 | /// <param name="random">A random number generator.</param>
|
---|
| 104 | /// <param name="parents">An array containing the two real vectors that should be crossed.</param>
|
---|
| 105 | /// <returns>The newly created real vector, resulting from the crossover operation.</returns>
|
---|
[3060] | 106 | protected override RealVector Cross(IRandom random, ItemArray<RealVector> parents) {
|
---|
[2964] | 107 | if (parents.Length != 2) throw new ArgumentException("HeuristicCrossover: The number of parents is not equal to 2");
|
---|
[2900] | 108 |
|
---|
[2936] | 109 | if (MaximizationParameter.ActualValue == null) throw new InvalidOperationException("HeuristicCrossover: Parameter " + MaximizationParameter.ActualName + " could not be found.");
|
---|
| 110 | 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.");
|
---|
| 111 |
|
---|
[3048] | 112 | ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
|
---|
[2936] | 113 | bool maximization = MaximizationParameter.ActualValue.Value;
|
---|
| 114 |
|
---|
| 115 | if (maximization && qualities[0].Value >= qualities[1].Value || !maximization && qualities[0].Value <= qualities[1].Value)
|
---|
| 116 | return Apply(random, parents[0], parents[1]);
|
---|
| 117 | else
|
---|
| 118 | return Apply(random, parents[1], parents[0]);
|
---|
[2900] | 119 | }
|
---|
| 120 | }
|
---|
| 121 | }
|
---|