Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.RealVector/3.3/Crossovers/HeuristicCrossover.cs @ 2936

Last change on this file since 2936 was 2936, checked in by svonolfe, 15 years ago

Added heuristic, local, random convex crossover and added some initial unit tests for all RealVector operators (#890)

File size: 6.1 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Text;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Parameters;
29
30namespace HeuristicLab.Encodings.RealVector {
31  /// <summary>
32  /// Heuristic crossover for real vectors: Takes for each position the better parent and adds the difference
33  /// of the two parents times a randomly chosen factor.
34  /// </summary>
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>
38  [Item("HeuristicCrossover", "Heuristic crossover for real vectors: Takes for each position the better parent and adds the difference. 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.")]
39  [EmptyStorableClass]
40  public class HeuristicCrossover : RealVectorCrossover {
41    /// <summary>
42    /// Whether the problem is a maximization or minimization problem.
43    /// </summary>
44    public ValueLookupParameter<BoolData> MaximizationParameter {
45      get { return (ValueLookupParameter<BoolData>)Parameters["Maximization"]; }
46    }
47    /// <summary>
48    /// The quality of the parents.
49    /// </summary>
50    public SubScopesLookupParameter<DoubleData> QualityParameter {
51      get { return (SubScopesLookupParameter<DoubleData>)Parameters["Quality"]; }
52    }
53
54    /// <summary>
55    /// Initializes a new instance of <see cref="HeuristicCrossover"/> with two variable infos
56    /// (<c>Maximization</c> and <c>Quality</c>).
57    /// </summary>
58    public HeuristicCrossover()
59      : base() {
60      Parameters.Add(new ValueLookupParameter<BoolData>("Maximization", "Whether the problem is a maximization problem or not."));
61      Parameters.Add(new SubScopesLookupParameter<DoubleData>("Quality", "The quality values of the parents."));
62    }
63
64    /// <summary>
65    /// Perfomrs a heuristic crossover on the two given parents.
66    /// </summary>
67    /// <exception cref="ArgumentException">Thrown when two parents are not of the same length.</exception>
68    /// <param name="random">The random number generator.</param>
69    /// <param name="betterParent">The first parent for the crossover operation.</param>
70    /// <param name="worseParent">The second parent for the crossover operation.</param>
71    /// <returns>The newly created real vector, resulting from the heuristic crossover.</returns>
72    public static DoubleArrayData Apply(IRandom random, DoubleArrayData betterParent, DoubleArrayData worseParent) {
73      if (betterParent.Length != worseParent.Length)
74        throw new ArgumentException("ERROR in HeuristicCrossover: the two parents are not of the same length");
75     
76      int length = betterParent.Length;
77      double[] result = new double[length];
78      double factor = random.NextDouble();
79
80      for (int i = 0; i < length; i++) {
81        result[i] = betterParent[i] + factor * (betterParent[i] - worseParent[i]);
82      }
83      return new DoubleArrayData(result);
84    }
85
86    /// <summary>
87    /// Performs a heuristic crossover operation for two given parent real vectors.
88    /// </summary>
89    /// <exception cref="ArgumentException">Thrown when the number of parents is not equal to 2.</exception>
90    /// <exception cref="InvalidOperationException">
91    /// Thrown when either:<br/>
92    /// <list type="bullet">
93    /// <item><description>Maximization parameter could not be found.</description></item>
94    /// <item><description>Quality parameter could not be found or the number of quality values is not equal to the number of parents.</description></item>
95    /// </list>
96    /// </exception>
97    /// <param name="random">A random number generator.</param>
98    /// <param name="parents">An array containing the two real vectors that should be crossed.</param>
99    /// <returns>The newly created real vector, resulting from the crossover operation.</returns>
100    protected override DoubleArrayData Cross(IRandom random, ItemArray<DoubleArrayData> parents) {
101      if (parents.Length != 2) throw new ArgumentException("ERROR in HeuristicCrossover: The number of parents is not equal to 2");
102
103      if (MaximizationParameter.ActualValue == null) throw new InvalidOperationException("HeuristicCrossover: Parameter " + MaximizationParameter.ActualName + " could not be found.");
104      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.");
105
106      ItemArray<DoubleData> qualities = QualityParameter.ActualValue;
107      bool maximization = MaximizationParameter.ActualValue.Value;
108
109      if (maximization && qualities[0].Value >= qualities[1].Value || !maximization && qualities[0].Value <= qualities[1].Value)
110        return Apply(random, parents[0], parents[1]);
111      else
112        return Apply(random, parents[1], parents[0]);
113    }
114  }
115}
Note: See TracBrowser for help on using the repository browser.