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 |
|
---|
22 | using System;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 | using HeuristicLab.Encodings.BinaryVectorEncoding;
|
---|
27 | using HeuristicLab.Parameters;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 |
|
---|
30 | namespace 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 | }
|
---|