Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/Knapsack/NChildCrossover.cs @ 7740

Last change on this file since 7740 was 7740, checked in by jkarder, 12 years ago

#1331:

  • added custom crossover operator (NChildCrossover)
  • added parameters and adjusted types
  • replaced SolutionCombinationMethod with a placeholder
  • adjusted event handling
  • changed access levels
  • minor code improvements
File size: 3.4 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.Encodings.BinaryVectorEncoding;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Algorithms.ScatterSearch.Knapsack {
31  /// <summary>
32  /// N child crossover for binary vectors.
33  /// </summary>
34  [Item("NChildCrossover", "N child crossover for binary vectors.")]
35  [StorableClass]
36  public sealed class NChildCrossover : NBinaryVectorCrossover {
37    public IValueLookupParameter<IntValue> NParameter {
38      get { return (IValueLookupParameter<IntValue>)Parameters["N"]; }
39    }
40
41    [StorableConstructor]
42    private NChildCrossover(bool deserializing) : base(deserializing) { }
43    private NChildCrossover(NChildCrossover original, Cloner cloner) : base(original, cloner) { }
44    public NChildCrossover()
45      : base() {
46      Parameters.Add(new ValueLookupParameter<IntValue>("N", "Number of children.", new IntValue(2)));
47    }
48
49    public override IDeepCloneable Clone(Cloner cloner) {
50      return new NChildCrossover(this, cloner);
51    }
52
53    public static ItemArray<BinaryVector> Apply(IRandom random, BinaryVector parent1, BinaryVector parent2, IntValue n) {
54      if (parent1.Length != parent2.Length)
55        throw new ArgumentException("NPointCrossover: The parents are of different length.");
56
57      if (n.Value > Math.Pow(2, parent1.Length) - 2)
58        throw new ArgumentException("NPointCrossover: There cannot be more breakpoints than the size of the parents.");
59
60      if (n.Value < 1)
61        throw new ArgumentException("NPointCrossover: N cannot be < 1.");
62
63      var solutions = new BinaryVector[n.Value];
64      for (int i = 0; i < n.Value; i++) {
65        var solution = new BinaryVector(parent1.Length);
66        for (int j = 0; j < solution.Length; j++) {
67          solution[j] = random.Next(2) % 2 == 0 ? parent1[j] : parent2[j];
68        }
69        solutions[i] = solution;
70      }
71
72      return new ItemArray<BinaryVector>(solutions);
73    }
74
75    protected override ItemArray<BinaryVector> Cross(IRandom random, ItemArray<BinaryVector> parents) {
76      if (parents.Length != 2) throw new ArgumentException("ERROR in NChildCrossover: The number of parents is not equal to 2");
77
78      if (NParameter.ActualValue == null) throw new InvalidOperationException("NChildCrossover: Parameter " + NParameter.ActualName + " could not be found.");
79
80      return Apply(random, parents[0], parents[1], NParameter.Value);
81    }
82  }
83}
Note: See TracBrowser for help on using the repository browser.