1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2015 Joseph Helm and 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.Persistence.Default.CompositeSerializers.Storable;
|
---|
26 |
|
---|
27 | namespace HeuristicLab.Encodings.PackingEncoding.MultiComponentVector {
|
---|
28 | [Item("Sequence Based Multi Component Vector Crossover", "An operator which crosses two MultiComponentVector representations. The applied strategy is based on the SBX crossover proposed by Potvin.")]
|
---|
29 | [StorableClass]
|
---|
30 | public class SequenceBasedMultiComponentVectorCrossover : MultiComponentVectorCrossover {
|
---|
31 | [StorableConstructor]
|
---|
32 | protected SequenceBasedMultiComponentVectorCrossover (bool deserializing) : base(deserializing) { }
|
---|
33 | protected SequenceBasedMultiComponentVectorCrossover (SequenceBasedMultiComponentVectorCrossover original, Cloner cloner) : base(original, cloner) { }
|
---|
34 | public SequenceBasedMultiComponentVectorCrossover ()
|
---|
35 | : base() {
|
---|
36 | }
|
---|
37 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
38 | return new SequenceBasedMultiComponentVectorCrossover (this, cloner);
|
---|
39 | }
|
---|
40 |
|
---|
41 | public override MultiComponentVectorEncoding Cross(IRandom random, MultiComponentVectorEncoding parent1, MultiComponentVectorEncoding parent2) {
|
---|
42 | MultiComponentVectorEncoding child = new MultiComponentVectorEncoding();
|
---|
43 | int nrOfItems = parent1.NrOfItems;
|
---|
44 | bool[] itemAlreadyAssigned = new bool[nrOfItems];
|
---|
45 |
|
---|
46 | bool useParent2 = false;
|
---|
47 | int nrOfBins = parent1.NrOfBins > parent2.NrOfBins ? parent2.NrOfBins : parent1.NrOfBins;
|
---|
48 |
|
---|
49 |
|
---|
50 | for (int binNr = 0; binNr < nrOfBins; binNr++) {
|
---|
51 | MultiComponentVectorEncoding currentParent1 = useParent2 ? parent2 : parent1;
|
---|
52 | MultiComponentVectorEncoding currentParent2 = useParent2 ? parent1 : parent2;
|
---|
53 |
|
---|
54 |
|
---|
55 | var newBin = new ItemList<PackingInformation>();
|
---|
56 | double crossPointPercent = 0;
|
---|
57 |
|
---|
58 | int nrOfItems1 = currentParent1.PackingInformations[binNr].Count;
|
---|
59 | if (nrOfItems1 > 0) {
|
---|
60 | double crossPoint1 = random.Next(nrOfItems1);
|
---|
61 | crossPointPercent = crossPoint1 / nrOfItems1;
|
---|
62 | for (int i = 0; i < crossPoint1; i++) {
|
---|
63 | PackingInformation pi = new PackingInformation(currentParent1.PackingInformations[binNr][i]);
|
---|
64 | if (!itemAlreadyAssigned[pi.ItemID]) {
|
---|
65 | itemAlreadyAssigned[pi.ItemID] = true;
|
---|
66 | newBin.Add(new PackingInformation(pi));
|
---|
67 | }
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 | int nrOfItems2 = currentParent2.PackingInformations[binNr].Count;
|
---|
72 | if (nrOfItems2 > 0) {
|
---|
73 | int crossPoint2 = Convert.ToInt32(nrOfItems2 * crossPointPercent);
|
---|
74 | for (int i = crossPoint2; i < nrOfItems2; i++) {
|
---|
75 | PackingInformation pi = new PackingInformation(currentParent2.PackingInformations[binNr][i]);
|
---|
76 | if (!itemAlreadyAssigned[pi.ItemID]) {
|
---|
77 | itemAlreadyAssigned[pi.ItemID] = true;
|
---|
78 | newBin.Add(new PackingInformation(pi));
|
---|
79 | }
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | child.PackingInformations[binNr] = newBin;
|
---|
84 | useParent2 = !useParent2;
|
---|
85 | }
|
---|
86 |
|
---|
87 | for (int itemID = 0; itemID < nrOfItems; itemID++) {
|
---|
88 | if (!itemAlreadyAssigned[itemID])
|
---|
89 | child.PackingInformations[0].Add(new PackingInformation(itemID, random.Next(2) == 0 ? true : false));
|
---|
90 | }
|
---|
91 |
|
---|
92 | return child;
|
---|
93 | }
|
---|
94 | }
|
---|
95 | }
|
---|