1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2015 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.Collections.Generic;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Encodings.ScheduleEncoding {
|
---|
30 | [Item("PWRGeneralizationOrderCrossover", "Represents a crossover operation swapping sequences of the parents to generate offspring.")]
|
---|
31 | [StorableClass]
|
---|
32 | public class PWRGOXCrossover : PWRCrossover {
|
---|
33 |
|
---|
34 | [StorableConstructor]
|
---|
35 | protected PWRGOXCrossover(bool deserializing) : base(deserializing) { }
|
---|
36 | protected PWRGOXCrossover(PWRGOXCrossover original, Cloner cloner) : base(original, cloner) { }
|
---|
37 | public PWRGOXCrossover() : base() { }
|
---|
38 |
|
---|
39 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
40 | return new PWRGOXCrossover(this, cloner);
|
---|
41 | }
|
---|
42 |
|
---|
43 | private static int[] GetLookUpForIndividual(List<int> p) {
|
---|
44 | var result = new int[p.Count];
|
---|
45 | var lookUpTable = new Dictionary<int, int>();
|
---|
46 |
|
---|
47 | for (int i = 0; i < p.Count; i++) {
|
---|
48 | if (!lookUpTable.ContainsKey(p[i]))
|
---|
49 | lookUpTable.Add(p[i], 0);
|
---|
50 | result[i] = lookUpTable[p[i]];
|
---|
51 | lookUpTable[p[i]]++;
|
---|
52 | }
|
---|
53 |
|
---|
54 | return result;
|
---|
55 | }
|
---|
56 |
|
---|
57 | public static PWREncoding Apply(IRandom random, PWREncoding parent1, PWREncoding parent2) {
|
---|
58 | var result = new PWREncoding();
|
---|
59 |
|
---|
60 | var p1 = ((IntegerVector)(parent1.PermutationWithRepetition.Clone())).ToList();
|
---|
61 | var p2 = ((IntegerVector)(parent2.PermutationWithRepetition.Clone())).ToList();
|
---|
62 | var child = new List<int>();
|
---|
63 |
|
---|
64 | int[] lookUpArrayP1 = GetLookUpForIndividual(p1);
|
---|
65 | int[] lookUpArrayP2 = GetLookUpForIndividual(p2);
|
---|
66 |
|
---|
67 | int xStringLength = p1.Count / 2;
|
---|
68 | int xStringPosition = random.Next(p1.Count / 2);
|
---|
69 |
|
---|
70 | //insert elements from parent1 that dont conflict with the "selected" elements form parent2
|
---|
71 | for (int i = 0; i < p1.Count; i++) {
|
---|
72 | bool isSelected = false;
|
---|
73 | for (int j = xStringPosition; j < xStringPosition + xStringLength; j++) {
|
---|
74 | if (p1[i] == p2[j] && lookUpArrayP1[i] == lookUpArrayP2[j])
|
---|
75 | isSelected = true;
|
---|
76 | }
|
---|
77 | if (!isSelected)
|
---|
78 | child.Add(p1[i]);
|
---|
79 | }
|
---|
80 |
|
---|
81 | for (int i = xStringPosition; i < xStringPosition + xStringLength; i++) {
|
---|
82 | child.Insert(i, p2[i]);
|
---|
83 | }
|
---|
84 |
|
---|
85 | result.PermutationWithRepetition = new IntegerVector(child.ToArray());
|
---|
86 | return result;
|
---|
87 | }
|
---|
88 |
|
---|
89 | public override PWREncoding Cross(IRandom random, PWREncoding parent1, PWREncoding parent2) {
|
---|
90 | return Apply(random, parent1, parent2);
|
---|
91 | }
|
---|
92 |
|
---|
93 | }
|
---|
94 | }
|
---|