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;
|
---|
28 | using System.Collections.Generic;
|
---|
29 | using HeuristicLab.Common;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Problems.VehicleRouting.Encodings.Alba {
|
---|
32 | [Item("AlbaLambdaInterchangeManipulator", "An operator which applies the lambda interchange operation to a VRP representation. It is implemented as described in Alba, E. and Dorronsoro, B. (2004). Solving the Vehicle Routing Problem by Using Cellular Genetic Algorithms.")]
|
---|
33 | [StorableClass]
|
---|
34 | public sealed class AlbaLambdaInterchangeManipulator : AlbaManipulator {
|
---|
35 | public IValueParameter<IntValue> LambdaParameter {
|
---|
36 | get { return (IValueParameter<IntValue>)Parameters["Lambda"]; }
|
---|
37 | }
|
---|
38 |
|
---|
39 | [StorableConstructor]
|
---|
40 | private AlbaLambdaInterchangeManipulator(bool deserializing) : base(deserializing) { }
|
---|
41 |
|
---|
42 | public AlbaLambdaInterchangeManipulator()
|
---|
43 | : base() {
|
---|
44 | Parameters.Add(new ValueParameter<IntValue>("Lambda", "The lambda value.", new IntValue(1)));
|
---|
45 | }
|
---|
46 |
|
---|
47 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
48 | return new AlbaLambdaInterchangeManipulator(this, cloner);
|
---|
49 | }
|
---|
50 |
|
---|
51 | private AlbaLambdaInterchangeManipulator(AlbaLambdaInterchangeManipulator original, Cloner cloner)
|
---|
52 | : base(original, cloner) {
|
---|
53 | }
|
---|
54 |
|
---|
55 | public static void Apply(AlbaEncoding individual, int tour1Index, int position1, int length1,
|
---|
56 | int tour2Index, int position2, int length2) {
|
---|
57 | List<Tour> tours = individual.GetTours();
|
---|
58 |
|
---|
59 | Tour tour1 = tours[tour1Index];
|
---|
60 | int tour1Start = -1;
|
---|
61 | for (int i = 0; i < individual.Length; i++) {
|
---|
62 | if (individual[i] == tour1.Stops[0] - 1) {
|
---|
63 | tour1Start = i;
|
---|
64 | break;
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | Tour tour2 = tours[tour2Index];
|
---|
69 | int tour2Start = -1;
|
---|
70 | for (int i = 0; i < individual.Length; i++) {
|
---|
71 | if (individual[i] == tour2.Stops[0] - 1) {
|
---|
72 | tour2Start = i;
|
---|
73 | break;
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | AlbaEncoding original = individual.Clone() as AlbaEncoding;
|
---|
78 | int index = 0;
|
---|
79 |
|
---|
80 | int start1 = tour1Start + position1;
|
---|
81 | int end1 = start1 + length1;
|
---|
82 |
|
---|
83 | int start2 = tour2Start + position2;
|
---|
84 | int end2 = start2 + length2;
|
---|
85 |
|
---|
86 | for (int i = 0; i < original.Length; i++) {
|
---|
87 | if (index == start1) {
|
---|
88 | if (end2 - start2 == 0)
|
---|
89 | index = end1;
|
---|
90 | else
|
---|
91 | index = start2;
|
---|
92 | } else if (index == start2) {
|
---|
93 | if (end1 - start1 == 0)
|
---|
94 | index = end2;
|
---|
95 | else
|
---|
96 | index = start1;
|
---|
97 | } else if (index == end1) {
|
---|
98 | index = end2;
|
---|
99 | } else if (index == end2) {
|
---|
100 | index = end1;
|
---|
101 | }
|
---|
102 |
|
---|
103 | individual[i] = original[index];
|
---|
104 |
|
---|
105 | index++;
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | protected override void Manipulate(IRandom rand, AlbaEncoding individual) {
|
---|
110 | List<Tour> tours = individual.GetTours();
|
---|
111 | if (tours.Count > 1) {
|
---|
112 | int lambda = LambdaParameter.Value.Value;
|
---|
113 |
|
---|
114 | int route1Index = rand.Next(tours.Count);
|
---|
115 | Tour route1 = tours[route1Index];
|
---|
116 |
|
---|
117 |
|
---|
118 | int route2Index = rand.Next(tours.Count - 1);
|
---|
119 | if (route2Index >= route1Index)
|
---|
120 | route2Index += 1;
|
---|
121 | Tour route2 = tours[route2Index];
|
---|
122 |
|
---|
123 | int length1 = rand.Next(Math.Min(lambda + 1, route1.Stops.Count + 1));
|
---|
124 | int index1 = rand.Next(route1.Stops.Count - length1 + 1);
|
---|
125 |
|
---|
126 | int l2Min = 0;
|
---|
127 | if (length1 == 0)
|
---|
128 | l2Min = 1;
|
---|
129 | int length2 = rand.Next(l2Min, Math.Min(lambda + 1, route2.Stops.Count + 1));
|
---|
130 | int index2 = rand.Next(route2.Stops.Count - length2 + 1);
|
---|
131 |
|
---|
132 | Apply(individual, route1Index, index1, length1,
|
---|
133 | route2Index, index2, length2);
|
---|
134 | }
|
---|
135 | }
|
---|
136 | }
|
---|
137 | }
|
---|