Free cookie consent management tool by TermsFeed Policy Generator

source: branches/LogResidualEvaluator/HeuristicLab.Encodings.IntegerVectorEncoding/3.3/Crossovers/RoundedAverageCrossover.cs @ 10391

Last change on this file since 10391 was 9456, checked in by swagner, 12 years ago

Updated copyright year and added some missing license headers (#1889)

File size: 4.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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  /// Rounded average crossover for integer vectors.
31  /// </summary>
32  [Item("RoundedAverageCrossover", "Average crossover for integer vectors.")]
33  [StorableClass]
34  public class RoundedAverageCrossover : BoundedIntegerVectorCrossover, IBoundedIntegerVectorOperator {
35
36    [StorableConstructor]
37    protected RoundedAverageCrossover(bool deserializing) : base(deserializing) { }
38    protected RoundedAverageCrossover(RoundedAverageCrossover original, Cloner cloner) : base(original, cloner) { }
39    public RoundedAverageCrossover() : base() { }
40
41    public override IDeepCloneable Clone(Cloner cloner) {
42      return new RoundedAverageCrossover(this, cloner);
43    }
44
45    /// <summary>
46    /// Performs an average crossover of the two given parent integer vectors.
47    /// The average is rounded and mapped to the nearest valid value (e.g. if step size is > 1)
48    /// </summary>
49    /// <param name="random">A random number generator.</param>
50    /// <param name="parents">The parents for crossover.</param>
51    /// <param name="bounds">The bounds matrix that contains for each dimension one row with minimum (inclusive), maximum (exclusive), and step size columns.
52    /// If the number of rows is smaller than the number of dimensions the matrix is cycled.</param>
53    /// <returns>The newly created integer vector, resulting from the single point crossover.</returns>
54    public static IntegerVector Apply(IRandom random, ItemArray<IntegerVector> parents, IntMatrix bounds) {
55      int length = parents[0].Length, parentsCount = parents.Length;
56      if (parents.Length < 2) throw new ArgumentException("RoundedAverageCrossover: The number of parents is less than 2.", "parents");
57      if (bounds == null || bounds.Rows < 1 || bounds.Columns < 2) throw new ArgumentException("AverageCrossover: Invalid bounds specified.", "bounds");
58
59      var result = new IntegerVector(length);
60      try {
61        double avg;
62        for (int i = 0; i < length; i++) {
63          avg = 0;
64          for (int j = 0; j < parentsCount; j++)
65            avg += parents[j][i];
66          avg /= parentsCount;
67          int min = bounds[i % bounds.Rows, 0], max = bounds[i % bounds.Rows, 1], step = 1;
68          if (bounds.Columns > 2) step = bounds[i % bounds.Rows, 2];
69          max = FloorFeasible(min, max, step, max - 1);
70          result[i] = RoundFeasible(min, max, step, avg);
71        }
72      } catch (IndexOutOfRangeException) {
73        throw new ArgumentException("RoundedAverageCrossover: The parents' vectors are of different length.", "parents");
74      }
75
76      return result;
77    }
78
79    /// <summary>
80    /// Performs an average crossover between two parent integer vectors.
81    /// </summary>
82    /// <exception cref="ArgumentException">Thrown if there are not exactly two parents.</exception>
83    /// <param name="random">A random number generator.</param>
84    /// <param name="parents">An array containing the two integer vectors that should be crossed.</param>
85    /// <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>
86    /// <returns>The newly created integer vector, resulting from the average crossover.</returns>
87    protected override IntegerVector CrossBounded(IRandom random, ItemArray<IntegerVector> parents, IntMatrix bounds) {
88      return Apply(random, parents, bounds);
89    }
90  }
91}
Note: See TracBrowser for help on using the repository browser.