Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Encodings.RealVectorEncoding/3.3/Crossovers/HeuristicCrossover.cs @ 18242

Last change on this file since 18242 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

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