[8017] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17180] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[8017] | 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;
|
---|
[11970] | 26 | using HeuristicLab.Optimization;
|
---|
[8017] | 27 | using HeuristicLab.Parameters;
|
---|
[16565] | 28 | using HEAL.Attic;
|
---|
[8017] | 29 |
|
---|
| 30 | namespace HeuristicLab.Encodings.IntegerVectorEncoding {
|
---|
| 31 | /// <summary>
|
---|
| 32 | /// Heuristic crossover for integer 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).
|
---|
| 33 | /// The result is then rounded to the next feasible integer.
|
---|
| 34 | /// 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).
|
---|
| 35 | /// </summary>
|
---|
| 36 | [Item("RoundedHeuristicCrossover", "The heuristic crossover produces offspring that extend the better parent in direction from the worse to the better parent.")]
|
---|
[16565] | 37 | [StorableType("94963FD3-4092-4B76-88E0-5FE5AC2DA9E2")]
|
---|
[11970] | 38 | public class RoundedHeuristicCrossover : BoundedIntegerVectorCrossover, ISingleObjectiveOperator {
|
---|
[8017] | 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 ScopeTreeLookupParameter<DoubleValue> QualityParameter {
|
---|
| 49 | get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | [StorableConstructor]
|
---|
[16565] | 53 | protected RoundedHeuristicCrossover(StorableConstructorFlag _) : base(_) { }
|
---|
[8017] | 54 | protected RoundedHeuristicCrossover(RoundedHeuristicCrossover original, Cloner cloner) : base(original, cloner) { }
|
---|
| 55 | /// <summary>
|
---|
| 56 | /// Initializes a new instance of <see cref="RoundedHeuristicCrossover"/> with two variable infos
|
---|
| 57 | /// (<c>Maximization</c> and <c>Quality</c>).
|
---|
| 58 | /// </summary>
|
---|
| 59 | public RoundedHeuristicCrossover()
|
---|
| 60 | : base() {
|
---|
| 61 | Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "Whether the problem is a maximization problem or not."));
|
---|
| 62 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The quality values of the parents."));
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 66 | return new RoundedHeuristicCrossover(this, cloner);
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | /// <summary>
|
---|
| 70 | /// Perfomrs a heuristic crossover on the two given parents.
|
---|
| 71 | /// </summary>
|
---|
| 72 | /// <exception cref="ArgumentException">Thrown when two parents are not of the same length.</exception>
|
---|
| 73 | /// <param name="random">The random number generator.</param>
|
---|
| 74 | /// <param name="betterParent">The first parent for the crossover operation.</param>
|
---|
| 75 | /// <param name="worseParent">The second parent for the crossover operation.</param>
|
---|
| 76 | /// <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>
|
---|
| 77 | /// <returns>The newly created integer vector, resulting from the heuristic crossover.</returns>
|
---|
| 78 | public static IntegerVector Apply(IRandom random, IntegerVector betterParent, IntegerVector worseParent, IntMatrix bounds) {
|
---|
| 79 | if (betterParent.Length != worseParent.Length)
|
---|
| 80 | throw new ArgumentException("HeuristicCrossover: the two parents are not of the same length");
|
---|
| 81 |
|
---|
| 82 | int length = betterParent.Length;
|
---|
| 83 | var result = new IntegerVector(length);
|
---|
| 84 | double factor = random.NextDouble();
|
---|
| 85 |
|
---|
| 86 | int min, max, step = 1;
|
---|
| 87 | for (int i = 0; i < length; i++) {
|
---|
| 88 | min = bounds[i % bounds.Rows, 0];
|
---|
| 89 | max = bounds[i % bounds.Rows, 1];
|
---|
| 90 | if (bounds.Columns > 2) step = bounds[i % bounds.Rows, 2];
|
---|
[8790] | 91 | max = FloorFeasible(min, max, step, max - 1);
|
---|
[8017] | 92 | result[i] = RoundFeasible(min, max, step, betterParent[i] + factor * (betterParent[i] - worseParent[i]));
|
---|
| 93 | }
|
---|
| 94 | return result;
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | /// <summary>
|
---|
| 98 | /// Performs a heuristic crossover operation for two given parent integer vectors.
|
---|
| 99 | /// </summary>
|
---|
| 100 | /// <exception cref="ArgumentException">Thrown when the number of parents is not equal to 2.</exception>
|
---|
| 101 | /// <exception cref="InvalidOperationException">
|
---|
| 102 | /// Thrown when either:<br/>
|
---|
| 103 | /// <list type="bullet">
|
---|
| 104 | /// <item><description>Maximization parameter could not be found.</description></item>
|
---|
| 105 | /// <item><description>Quality parameter could not be found or the number of quality values is not equal to the number of parents.</description></item>
|
---|
| 106 | /// </list>
|
---|
| 107 | /// </exception>
|
---|
| 108 | /// <param name="random">A random number generator.</param>
|
---|
| 109 | /// <param name="parents">An array containing the two real vectors that should be crossed.</param>
|
---|
| 110 | /// /// <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>
|
---|
| 111 | /// <returns>The newly created integer vector, resulting from the crossover operation.</returns>
|
---|
| 112 | protected override IntegerVector CrossBounded(IRandom random, ItemArray<IntegerVector> parents, IntMatrix bounds) {
|
---|
| 113 | if (parents.Length != 2) throw new ArgumentException("RoundedHeuristicCrossover: The number of parents is not equal to 2");
|
---|
| 114 |
|
---|
| 115 | if (MaximizationParameter.ActualValue == null) throw new InvalidOperationException("RoundedHeuristicCrossover: Parameter " + MaximizationParameter.ActualName + " could not be found.");
|
---|
| 116 | if (QualityParameter.ActualValue == null || QualityParameter.ActualValue.Length != parents.Length) throw new InvalidOperationException("RoundedHeuristicCrossover: Parameter " + QualityParameter.ActualName + " could not be found, or not in the same quantity as there are parents.");
|
---|
| 117 |
|
---|
| 118 | ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
|
---|
| 119 | bool maximization = MaximizationParameter.ActualValue.Value;
|
---|
| 120 |
|
---|
| 121 | if (maximization && qualities[0].Value >= qualities[1].Value || !maximization && qualities[0].Value <= qualities[1].Value)
|
---|
| 122 | return Apply(random, parents[0], parents[1], bounds);
|
---|
| 123 | else
|
---|
| 124 | return Apply(random, parents[1], parents[0], bounds);
|
---|
| 125 | }
|
---|
| 126 | }
|
---|
| 127 | }
|
---|