1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2010 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 HeuristicLab.Core;
|
---|
23 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
24 | using HeuristicLab.Parameters;
|
---|
25 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using System.Collections.Generic;
|
---|
28 | using HeuristicLab.Problems.VehicleRouting.Variants;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Problems.VehicleRouting.Encodings.Zhu {
|
---|
31 | [Item("ZhuMergeCrossover2", "The Zhu Merge Crossover (Version 2). It is implemented as described in Zhu, K.Q. (2000). A New Genetic Algorithm For VRPTW. Proceedings of the International Conference on Artificial Intelligence.")]
|
---|
32 | [StorableClass]
|
---|
33 | public sealed class ZhuMergeCrossover2 : ZhuCrossover {
|
---|
34 | [StorableConstructor]
|
---|
35 | private ZhuMergeCrossover2(bool deserializing) : base(deserializing) { }
|
---|
36 |
|
---|
37 | public ZhuMergeCrossover2()
|
---|
38 | : base() {
|
---|
39 | }
|
---|
40 |
|
---|
41 | protected override ZhuEncoding Crossover(IRandom random, ZhuEncoding parent1, ZhuEncoding parent2) {
|
---|
42 | List<int> p1 = new List<int>(parent1);
|
---|
43 | List<int> p2 = new List<int>(parent2);
|
---|
44 |
|
---|
45 | ZhuEncoding child = parent2.Clone() as ZhuEncoding;
|
---|
46 |
|
---|
47 | int breakPoint = random.Next(child.Length);
|
---|
48 | int i = breakPoint;
|
---|
49 |
|
---|
50 | int parent1Index = i;
|
---|
51 | int parent2Index = i;
|
---|
52 |
|
---|
53 | DoubleArray dueTime = null;
|
---|
54 | if (ProblemInstance is ITimeWindowedProblemInstance) {
|
---|
55 | dueTime = (ProblemInstance as ITimeWindowedProblemInstance).DueTime;
|
---|
56 | }
|
---|
57 |
|
---|
58 | do {
|
---|
59 | if (i == breakPoint) {
|
---|
60 | child[i] = p1[parent1Index];
|
---|
61 | } else {
|
---|
62 | if (dueTime != null &&
|
---|
63 | (dueTime[p1[parent1Index] + 1] <
|
---|
64 | dueTime[p2[parent2Index] + 1])) {
|
---|
65 | child[i] = p1[parent1Index];
|
---|
66 | } else {
|
---|
67 | child[i] = p2[parent2Index];
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 | p1.Remove(child[i]);
|
---|
72 | if (parent1Index >= p1.Count)
|
---|
73 | parent1Index = 0;
|
---|
74 |
|
---|
75 | p2.Remove(child[i]);
|
---|
76 | if (parent2Index >= p2.Count)
|
---|
77 | parent2Index = 0;
|
---|
78 |
|
---|
79 | i = (i + 1) % child.Length;
|
---|
80 | } while (i != breakPoint);
|
---|
81 |
|
---|
82 | return child;
|
---|
83 | }
|
---|
84 | }
|
---|
85 | }
|
---|