Free cookie consent management tool by TermsFeed Policy Generator

source: branches/IntegerVectorEncoding/HeuristicLab.Encodings.IntegerVectorEncoding/3.3/Crossovers/AverageCrossover.cs @ 7681

Last change on this file since 7681 was 7681, checked in by abeham, 12 years ago

#1775: added branch of plugin and new operators

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