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.Common;
|
---|
23 | using HeuristicLab.Core;
|
---|
24 | using HeuristicLab.Data;
|
---|
25 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
27 | using System.Drawing;
|
---|
28 | using System.Collections.Generic;
|
---|
29 | using HeuristicLab.Problems.VehicleRouting.Encodings.General;
|
---|
30 | using HeuristicLab.Problems.VehicleRouting.Variants;
|
---|
31 | using HeuristicLab.Problems.VehicleRouting.Interfaces;
|
---|
32 | using HeuristicLab.Problems.VehicleRouting.ProblemInstances;
|
---|
33 |
|
---|
34 | namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin {
|
---|
35 | [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.")]
|
---|
36 | [StorableClass]
|
---|
37 | public class PotvinEncoding : TourEncoding {
|
---|
38 | [Storable]
|
---|
39 | public List<int> Unrouted { get; set; }
|
---|
40 |
|
---|
41 | [Storable]
|
---|
42 | public Permutation VehicleAssignment { get; private set; }
|
---|
43 |
|
---|
44 | public PotvinEncoding(IVRPProblemInstance instance)
|
---|
45 | : base(instance) {
|
---|
46 | Unrouted = new List<int>();
|
---|
47 | VehicleAssignment = new Permutation(PermutationTypes.Absolute, instance.Vehicles.Value);
|
---|
48 | }
|
---|
49 |
|
---|
50 | [StorableConstructor]
|
---|
51 | protected PotvinEncoding(bool serializing)
|
---|
52 | : base(serializing) {
|
---|
53 | }
|
---|
54 |
|
---|
55 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
56 | return new PotvinEncoding(this, cloner);
|
---|
57 | }
|
---|
58 |
|
---|
59 | protected PotvinEncoding(PotvinEncoding original, Cloner cloner)
|
---|
60 | : base(original, cloner) {
|
---|
61 | this.Unrouted = new List<int>(original.Unrouted);
|
---|
62 | this.VehicleAssignment = cloner.Clone<Permutation>(original.VehicleAssignment);
|
---|
63 | }
|
---|
64 |
|
---|
65 | public override int GetVehicleAssignment(int tour) {
|
---|
66 | return VehicleAssignment[tour];
|
---|
67 | }
|
---|
68 |
|
---|
69 | public static PotvinEncoding ConvertFrom(IVRPEncoding encoding, IVRPProblemInstance instance) {
|
---|
70 | PotvinEncoding solution = new PotvinEncoding(instance);
|
---|
71 |
|
---|
72 | TourEncoding.ConvertFrom(encoding, solution, instance);
|
---|
73 |
|
---|
74 | List<int> vehicles = new List<int>();
|
---|
75 | for (int i = 0; i < instance.Vehicles.Value; i++)
|
---|
76 | vehicles.Add(i);
|
---|
77 |
|
---|
78 | int[] assignment = new int[instance.Vehicles.Value];
|
---|
79 | for (int i = 0; i < assignment.Length; i++)
|
---|
80 | assignment[i] = -1;
|
---|
81 |
|
---|
82 | for (int i = 0; i < solution.Tours.Count; i++) {
|
---|
83 | int vehicle = encoding.GetVehicleAssignment(i);
|
---|
84 | assignment[i] = vehicle;
|
---|
85 | vehicles.Remove(vehicle);
|
---|
86 | }
|
---|
87 |
|
---|
88 | for (int i = 0; i < instance.Vehicles.Value; i++) {
|
---|
89 | if (assignment[i] == -1) {
|
---|
90 | int vehicle = vehicles[0];
|
---|
91 | assignment[i] = vehicle;
|
---|
92 | vehicles.RemoveAt(0);
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | solution.VehicleAssignment = new Permutation(PermutationTypes.Absolute, assignment);
|
---|
97 |
|
---|
98 | return solution;
|
---|
99 | }
|
---|
100 |
|
---|
101 | public static PotvinEncoding ConvertFrom(List<int> route, IVRPProblemInstance instance) {
|
---|
102 | PotvinEncoding solution = new PotvinEncoding(instance);
|
---|
103 |
|
---|
104 | TourEncoding.ConvertFrom(route, solution);
|
---|
105 |
|
---|
106 | return solution;
|
---|
107 | }
|
---|
108 |
|
---|
109 | public double GetTourLength(Tour tour) {
|
---|
110 | return tour.GetTourLength(ProblemInstance, this);
|
---|
111 | }
|
---|
112 |
|
---|
113 | public int FindBestInsertionPlace(Tour tour, int city, int positionToAvoid = -1) {
|
---|
114 | if (tour.Stops.Count == 0)
|
---|
115 | return 0;
|
---|
116 |
|
---|
117 | int place = -1;
|
---|
118 | double minQuality = -1;
|
---|
119 |
|
---|
120 | VRPEvaluation eval = ProblemInstance.EvaluateTour(tour, this);
|
---|
121 |
|
---|
122 | for (int i = 0; i <= tour.Stops.Count; i++) {
|
---|
123 | if (positionToAvoid != i) {
|
---|
124 | bool feasible;
|
---|
125 | double quality = ProblemInstance.GetInsertionCosts(eval, this, city, 0, i, out feasible);
|
---|
126 | if (place < 0 || quality < minQuality) {
|
---|
127 | place = i;
|
---|
128 | minQuality = quality;
|
---|
129 | }
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | if (place == -1)
|
---|
134 | place = 0;
|
---|
135 |
|
---|
136 | return place;
|
---|
137 | }
|
---|
138 |
|
---|
139 | public bool FindInsertionPlace(int city, int routeToAvoid, bool allowInfeasible, out int route, out int place) {
|
---|
140 | route = -1;
|
---|
141 | place = -1;
|
---|
142 | double minDetour = double.MaxValue;
|
---|
143 |
|
---|
144 | VRPEvaluation eval = ProblemInstance.Evaluate(this);
|
---|
145 | bool originalFeasible = ProblemInstance.Feasible(eval);
|
---|
146 |
|
---|
147 | for (int tour = 0; tour < Tours.Count; tour++) {
|
---|
148 | if (tour != routeToAvoid) {
|
---|
149 | double length = eval.Quality;
|
---|
150 |
|
---|
151 | for (int i = 0; i <= Tours[tour].Stops.Count; i++) {
|
---|
152 | bool feasible;
|
---|
153 | double detour = ProblemInstance.GetInsertionCosts(eval, this, city, tour, i, out feasible);
|
---|
154 |
|
---|
155 | if (feasible || allowInfeasible) {
|
---|
156 | if (route < 0 || detour < minDetour) {
|
---|
157 | route = tour;
|
---|
158 | place = i;
|
---|
159 | minDetour = detour;
|
---|
160 | }
|
---|
161 | }
|
---|
162 | }
|
---|
163 | }
|
---|
164 | }
|
---|
165 |
|
---|
166 | return route >= 0 && place >= 0;
|
---|
167 | }
|
---|
168 | }
|
---|
169 | }
|
---|