Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1331:

  • added path relinking and problem specific operators (KnapsackPathRelinker, TravelingSalesmanPathRelinker) for it
  • adjusted event handling
  • minor code improvements
File size: 3.5 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
61      if (n.Value > Math.Pow(2, parent1.Length) - 2)
62        throw new ArgumentException("NChildCrossover: There cannot be more children than 2^size of parents - 2.");
63
64      if (n.Value < 1)
65        throw new ArgumentException("NChildCrossover: N cannot be < 1.");
66
67      var solutions = new BinaryVector[n.Value];
68      for (int i = 0; i < n.Value; i++) {
69        var solution = new BinaryVector(parent1.Length);
70        for (int j = 0; j < solution.Length; j++) {
71          solution[j] = random.Next(2) % 2 == 0 ? parent1[j] : parent2[j];
72        }
73        solutions[i] = solution;
74      }
75
76      return new ItemArray<BinaryVector>(solutions);
77    }
78
79    protected override ItemArray<BinaryVector> Cross(IRandom random, ItemArray<BinaryVector> parents) {
80      if (parents.Length != 2) throw new ArgumentException("NChildCrossover: The number of parents is not equal to 2.");
81
82      if (NParameter.ActualValue == null) throw new InvalidOperationException("NChildCrossover: Parameter " + NParameter.ActualName + " could not be found.");
83
84      return Apply(random, parents[0], parents[1], NParameter.Value);
85    }
86  }
87}
Note: See TracBrowser for help on using the repository browser.