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 System.Collections.Generic;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
27 | using HeuristicLab.Problems.VehicleRouting.Encodings.General;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin {
|
---|
30 | [Item("PotvinEncoding", "Represents a potvin encoding of VRP solutions. It is implemented as described in Potvin, J.-Y. and Bengio, S. (1996). The Vehicle Routing Problem with Time Windows - Part II: Genetic Search. INFORMS Journal of Computing, 8:165–172.")]
|
---|
31 | [StorableClass]
|
---|
32 | public class PotvinEncoding : TourEncoding {
|
---|
33 | [Storable]
|
---|
34 | public List<int> Unrouted { get; set; }
|
---|
35 |
|
---|
36 | [StorableConstructor]
|
---|
37 | protected PotvinEncoding(bool deserializing) : base(deserializing) { }
|
---|
38 | protected PotvinEncoding(PotvinEncoding original, Cloner cloner)
|
---|
39 | : base(original, cloner) {
|
---|
40 | Tours = cloner.Clone(original.Tours);
|
---|
41 | Unrouted = new List<int>(original.Unrouted);
|
---|
42 | }
|
---|
43 | public PotvinEncoding()
|
---|
44 | : base() {
|
---|
45 | Unrouted = new List<int>();
|
---|
46 | }
|
---|
47 |
|
---|
48 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
49 | return new PotvinEncoding(this, cloner);
|
---|
50 | }
|
---|
51 |
|
---|
52 | public static PotvinEncoding ConvertFrom(IVRPEncoding encoding, ILookupParameter<DoubleMatrix> distanceMatrix) {
|
---|
53 | PotvinEncoding solution = new PotvinEncoding();
|
---|
54 |
|
---|
55 | TourEncoding.ConvertFrom(encoding, solution, distanceMatrix);
|
---|
56 |
|
---|
57 | return solution;
|
---|
58 | }
|
---|
59 |
|
---|
60 | public static PotvinEncoding ConvertFrom(List<int> route) {
|
---|
61 | PotvinEncoding solution = new PotvinEncoding();
|
---|
62 |
|
---|
63 | TourEncoding.ConvertFrom(route, solution);
|
---|
64 |
|
---|
65 | return solution;
|
---|
66 | }
|
---|
67 |
|
---|
68 | public bool FindInsertionPlace(
|
---|
69 | DoubleArray dueTimeArray,
|
---|
70 | DoubleArray serviceTimeArray, DoubleArray readyTimeArray, DoubleArray demandArray, DoubleValue capacity,
|
---|
71 | DoubleMatrix coordinates, ILookupParameter<DoubleMatrix> distanceMatrix, BoolValue useDistanceMatrix,
|
---|
72 | int city, int routeToAvoid, out int route, out int place) {
|
---|
73 | route = -1;
|
---|
74 | place = -1;
|
---|
75 | double minDetour = 0;
|
---|
76 |
|
---|
77 | for (int tour = 0; tour < Tours.Count; tour++) {
|
---|
78 | if (tour != routeToAvoid) {
|
---|
79 | for (int i = 0; i <= Tours[tour].Cities.Count; i++) {
|
---|
80 | double length = Tours[tour].GetLength(coordinates, distanceMatrix, useDistanceMatrix);
|
---|
81 |
|
---|
82 | Tours[tour].Cities.Insert(i, city);
|
---|
83 |
|
---|
84 | if (Tours[tour].Feasible(dueTimeArray, serviceTimeArray, readyTimeArray, demandArray,
|
---|
85 | capacity, coordinates, distanceMatrix, useDistanceMatrix)) {
|
---|
86 | double newLength = Tours[tour].GetLength(coordinates, distanceMatrix, useDistanceMatrix);
|
---|
87 |
|
---|
88 | double detour = newLength - length;
|
---|
89 |
|
---|
90 | if (route <= 0 || detour < minDetour) {
|
---|
91 | route = tour;
|
---|
92 | place = i;
|
---|
93 | minDetour = detour;
|
---|
94 | }
|
---|
95 | }
|
---|
96 |
|
---|
97 | Tours[tour].Cities.RemoveAt(i);
|
---|
98 | }
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | return route >= 0 && place >= 0;
|
---|
103 | }
|
---|
104 | }
|
---|
105 | }
|
---|