Free cookie consent management tool by TermsFeed Policy Generator

source: branches/LearningClassifierSystems/HeuristicLab.Encodings.CombinedIntegerVectorEncoding/3.3/Crossovers/SinglePointCrossover.cs @ 15529

Last change on this file since 15529 was 9090, checked in by sforsten, 12 years ago

#1980: implemented covering and changed SinglePointCrossover for CombinedIntegerVectorEncoding

File size: 3.1 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.Persistence.Default.CompositeSerializers.Storable;
26
27namespace HeuristicLab.Encodings.CombinedIntegerVectorEncoding {
28  [Item("SinglePointCrossover", "Single point crossover for integer vectors. It is implemented as described in Michalewicz, Z. 1999. Genetic Algorithms + Data Structures = Evolution Programs. Third, Revised and Extended Edition, Spring-Verlag Berlin Heidelberg.")]
29  [StorableClass]
30  public class SinglePointCrossover : CombinedIntegerVectorCrossover {
31    [StorableConstructor]
32    protected SinglePointCrossover(bool deserializing) : base(deserializing) { }
33    protected SinglePointCrossover(SinglePointCrossover original, Cloner cloner) : base(original, cloner) { }
34    public SinglePointCrossover() : base() { }
35
36    public override IDeepCloneable Clone(Cloner cloner) {
37      return new SinglePointCrossover(this, cloner);
38    }
39
40    public static CombinedIntegerVector Apply(IRandom random, CombinedIntegerVector parent1, CombinedIntegerVector parent2) {
41      if (parent1.Length != parent2.Length && parent1.ActionLength != parent2.ActionLength && parent1.Bounds.Equals(parent2.Bounds))
42        throw new ArgumentException("SinglePointCrossover: The parents are of different length.");
43
44      int length = parent1.Length;
45      int actionLength = parent1.ActionLength;
46      int conditionLength = length - actionLength;
47      int[] result = new int[length];
48      int breakPoint = random.Next(1, conditionLength);
49
50      for (int i = 0; i < breakPoint; i++) {
51        result[i] = parent1[i];
52      }
53      for (int i = breakPoint; i < conditionLength; i++) {
54        result[i] = parent2[i];
55      }
56
57      CombinedIntegerVector actionParent = random.Next(2) == 1 ? parent1 : parent2;
58      for (int i = conditionLength; i < length; i++) {
59        result[i] = actionParent[i];
60      }
61
62      return new CombinedIntegerVector(result, actionLength, parent1.Bounds);
63    }
64
65    protected override CombinedIntegerVector Cross(IRandom random, ItemArray<CombinedIntegerVector> parents) {
66      if (parents.Length != 2) throw new ArgumentException("ERROR in SinglePointCrossover: The number of parents is not equal to 2");
67      return Apply(random, parents[0], parents[1]);
68    }
69  }
70}
Note: See TracBrowser for help on using the repository browser.