Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 7775 was 7775, checked in by jkarder, 13 years ago

#1331:

  • added operators for TestFunctions problems
  • added more path relinking operators for the TravelingSalesman and the Knapsack problem
  • added 2-tier version of the SolutionPoolUpdateMethod
  • added parameters and adjusted types
  • added some exception handling
  • adjusted event handling
  • updated plugin dependencies
  • 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    #region Parameter properties
38    public IValueLookupParameter<IntValue> NParameter {
39      get { return (IValueLookupParameter<IntValue>)Parameters["N"]; }
40    }
41    #endregion
42
43    [StorableConstructor]
44    private NChildCrossover(bool deserializing) : base(deserializing) { }
45    private NChildCrossover(NChildCrossover original, Cloner cloner) : base(original, cloner) { }
46    public NChildCrossover()
47      : base() {
48      #region Create parameters
49      Parameters.Add(new ValueLookupParameter<IntValue>("N", "Number of children.", new IntValue(2)));
50      #endregion
51    }
52
53    public override IDeepCloneable Clone(Cloner cloner) {
54      return new NChildCrossover(this, cloner);
55    }
56
57    public static ItemArray<BinaryVector> Apply(IRandom random, BinaryVector parent1, BinaryVector parent2, IntValue n) {
58      if (parent1.Length != parent2.Length)
59        throw new ArgumentException("NChildCrossover: The parents are of different length.");
60      if (n.Value > Math.Pow(2, parent1.Length) - 2)
61        throw new ArgumentException("NChildCrossover: There cannot be more children than 2^size of parents - 2.");
62      if (n.Value < 1)
63        throw new ArgumentException("NChildCrossover: N cannot be < 1.");
64
65      var solutions = new BinaryVector[n.Value];
66      for (int i = 0; i < n.Value; i++) {
67        var solution = new BinaryVector(parent1.Length);
68        for (int j = 0; j < solution.Length; j++)
69          solution[j] = random.Next(2) % 2 == 0 ? parent1[j] : parent2[j];
70        solutions[i] = solution;
71      }
72
73      return new ItemArray<BinaryVector>(solutions);
74    }
75
76    protected override ItemArray<BinaryVector> Cross(IRandom random, ItemArray<BinaryVector> parents) {
77      if (parents.Length != 2)
78        throw new ArgumentException("NChildCrossover: The number of parents is not equal to 2.");
79      if (NParameter.ActualValue == null)
80        throw new InvalidOperationException("NChildCrossover: Parameter " + NParameter.ActualName + " could not be found.");
81      return Apply(random, parents[0], parents[1], NParameter.Value);
82    }
83  }
84}
Note: See TracBrowser for help on using the repository browser.