Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceOverhaul/HeuristicLab.Encodings.IntegerVectorEncoding/3.3/Crossovers/RoundedLocalCrossover.cs @ 13806

Last change on this file since 13806 was 13368, checked in by ascheibe, 9 years ago

#2520 added guids to storable classes

File size: 4.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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 HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Encodings.IntegerVectorEncoding {
29  /// <summary>
30  /// The rounded local crossover for integer vectors is similar to the <see cref="UniformArithmeticCrossover"/>, but where the factor alpha is chosen randomly in the interval [0;1) for each position.
31  /// </summary>cribed in Dumitrescu, D. et al. (2000), Evolutionary computation, CRC Press, Boca Raton, FL, p. 194.
32  /// </remarks>
33  [Item("RoundedLocalCrossover", @"The runded local crossover is similar to the arithmetic crossover, but uses a random alpha for each position x = alpha * p1 + (1-alpha) * p2.")]
34  [StorableClass("09A11920-6228-4E34-B947-10FD8DCEC59D")]
35  public class RoundedLocalCrossover : BoundedIntegerVectorCrossover {
36    [StorableConstructor]
37    protected RoundedLocalCrossover(bool deserializing) : base(deserializing) { }
38    protected RoundedLocalCrossover(RoundedLocalCrossover original, Cloner cloner) : base(original, cloner) { }
39    public RoundedLocalCrossover() : base() { }
40
41    public override IDeepCloneable Clone(Cloner cloner) {
42      return new RoundedLocalCrossover(this, cloner);
43    }
44
45    /// <summary>
46    /// Performs a local crossover on the two given parent vectors.
47    /// </summary>
48    /// <exception cref="ArgumentException">Thrown when two parents are not of the same length.</exception>
49    /// <param name="random">The random number generator.</param>
50    /// <param name="parent1">The first parent for the crossover operation.</param>
51    /// <param name="parent2">The second parent for the crossover operation.</param>
52    /// <returns>The newly created integer vector, resulting from the local crossover.</returns>
53    public static IntegerVector Apply(IRandom random, IntegerVector parent1, IntegerVector parent2, IntMatrix bounds) {
54      if (parent1.Length != parent2.Length)
55        throw new ArgumentException("RoundedLocalCrossover: the two parents are not of the same length");
56
57      double factor;
58      int length = parent1.Length;
59      var result = new IntegerVector(length);
60
61      int min, max, step = 1;
62      for (int i = 0; i < length; i++) {
63        min = bounds[i % bounds.Rows, 0];
64        max = bounds[i % bounds.Rows, 1];
65        if (bounds.Columns > 2) step = bounds[i % bounds.Rows, 2];
66        max = FloorFeasible(min, max, step, max - 1);
67        factor = random.NextDouble();
68        result[i] = RoundFeasible(min, max, step, (factor * parent1[i]) + ((1 - factor) * parent2[i]));
69      }
70      return result;
71    }
72
73    /// <summary>
74    /// Performs a local crossover operation for two given parent integer vectors.
75    /// </summary>
76    /// <exception cref="ArgumentException">Thrown if there are not exactly two parents.</exception>
77    /// <param name="random">A random number generator.</param>
78    /// <param name="parents">An array containing the two real vectors that should be crossed.</param>
79    /// <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>
80    /// <returns>The newly created integer vector, resulting from the crossover operation.</returns>
81    protected override IntegerVector CrossBounded(IRandom random, ItemArray<IntegerVector> parents, IntMatrix bounds) {
82      if (parents.Length != 2) throw new ArgumentException("RoundedLocalCrossover: The number of parents is not equal to 2");
83      return Apply(random, parents[0], parents[1], bounds);
84    }
85  }
86}
Note: See TracBrowser for help on using the repository browser.