[4376] | 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]
|
---|
[6710] | 37 | public class PotvinEncoding : TourEncoding {
|
---|
[4376] | 38 | [Storable]
|
---|
| 39 | public List<int> Unrouted { get; set; }
|
---|
| 40 |
|
---|
[6857] | 41 | [Storable]
|
---|
| 42 | public Permutation VehicleAssignment { get; private set; }
|
---|
| 43 |
|
---|
[4376] | 44 | public PotvinEncoding(IVRPProblemInstance instance)
|
---|
| 45 | : base(instance) {
|
---|
| 46 | Unrouted = new List<int>();
|
---|
[6857] | 47 | VehicleAssignment = new Permutation(PermutationTypes.Absolute, instance.Vehicles.Value);
|
---|
[4376] | 48 | }
|
---|
| 49 |
|
---|
| 50 | [StorableConstructor]
|
---|
[6851] | 51 | protected PotvinEncoding(bool serializing)
|
---|
[4376] | 52 | : base(serializing) {
|
---|
| 53 | }
|
---|
| 54 |
|
---|
[4752] | 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);
|
---|
[6857] | 62 | this.VehicleAssignment = cloner.Clone<Permutation>(original.VehicleAssignment);
|
---|
[4752] | 63 | }
|
---|
| 64 |
|
---|
[6907] | 65 | public override void Repair() {
|
---|
| 66 | List<Tour> toBeRemoved = new List<Tour>();
|
---|
| 67 | foreach (Tour tour in Tours) {
|
---|
| 68 | if (tour.Stops.Count == 0)
|
---|
| 69 | toBeRemoved.Add(tour);
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | foreach (Tour tour in toBeRemoved) {
|
---|
| 73 | int index = Tours.IndexOf(tour);
|
---|
| 74 | if (index < VehicleAssignment.Length) {
|
---|
| 75 | int vehicle = VehicleAssignment[index];
|
---|
| 76 |
|
---|
| 77 | Tours.Remove(tour);
|
---|
| 78 | for (int i = index; i < VehicleAssignment.Length - 1; i++) {
|
---|
| 79 | VehicleAssignment[i] = VehicleAssignment[i + 1];
|
---|
| 80 | }
|
---|
| 81 | VehicleAssignment[VehicleAssignment.Length - 1] = vehicle;
|
---|
| 82 | }
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | while (Tours.Count > ProblemInstance.Vehicles.Value) {
|
---|
| 86 | Tour tour = Tours[Tours.Count - 1];
|
---|
| 87 | Tours[Tours.Count - 2].Stops.AddRange(tour.Stops);
|
---|
| 88 |
|
---|
| 89 | Tours.Remove(tour);
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
| 92 |
|
---|
[6857] | 93 | public override int GetVehicleAssignment(int tour) {
|
---|
| 94 | return VehicleAssignment[tour];
|
---|
| 95 | }
|
---|
| 96 |
|
---|
[4376] | 97 | public static PotvinEncoding ConvertFrom(IVRPEncoding encoding, IVRPProblemInstance instance) {
|
---|
| 98 | PotvinEncoding solution = new PotvinEncoding(instance);
|
---|
| 99 |
|
---|
| 100 | TourEncoding.ConvertFrom(encoding, solution, instance);
|
---|
| 101 |
|
---|
[6857] | 102 | List<int> vehicles = new List<int>();
|
---|
| 103 | for (int i = 0; i < instance.Vehicles.Value; i++)
|
---|
| 104 | vehicles.Add(i);
|
---|
| 105 |
|
---|
| 106 | int[] assignment = new int[instance.Vehicles.Value];
|
---|
| 107 | for (int i = 0; i < assignment.Length; i++)
|
---|
| 108 | assignment[i] = -1;
|
---|
| 109 |
|
---|
| 110 | for (int i = 0; i < solution.Tours.Count; i++) {
|
---|
| 111 | int vehicle = encoding.GetVehicleAssignment(i);
|
---|
| 112 | assignment[i] = vehicle;
|
---|
| 113 | vehicles.Remove(vehicle);
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | for (int i = 0; i < instance.Vehicles.Value; i++) {
|
---|
| 117 | if (assignment[i] == -1) {
|
---|
| 118 | int vehicle = vehicles[0];
|
---|
| 119 | assignment[i] = vehicle;
|
---|
| 120 | vehicles.RemoveAt(0);
|
---|
| 121 | }
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | solution.VehicleAssignment = new Permutation(PermutationTypes.Absolute, assignment);
|
---|
| 125 |
|
---|
[4376] | 126 | return solution;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | public static PotvinEncoding ConvertFrom(List<int> route, IVRPProblemInstance instance) {
|
---|
| 130 | PotvinEncoding solution = new PotvinEncoding(instance);
|
---|
| 131 |
|
---|
| 132 | TourEncoding.ConvertFrom(route, solution);
|
---|
| 133 |
|
---|
| 134 | return solution;
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | public double GetTourLength(Tour tour) {
|
---|
[6851] | 138 | return tour.GetTourLength(ProblemInstance, this);
|
---|
[4376] | 139 | }
|
---|
| 140 |
|
---|
[6771] | 141 | public int FindBestInsertionPlace(Tour tour, int city, int positionToAvoid = -1) {
|
---|
[6857] | 142 | if (tour.Stops.Count == 0)
|
---|
| 143 | return 0;
|
---|
| 144 |
|
---|
[5127] | 145 | int place = -1;
|
---|
| 146 | double minQuality = -1;
|
---|
| 147 |
|
---|
[6838] | 148 | VRPEvaluation eval = ProblemInstance.EvaluateTour(tour, this);
|
---|
[6752] | 149 |
|
---|
[5127] | 150 | for (int i = 0; i <= tour.Stops.Count; i++) {
|
---|
[6771] | 151 | if (positionToAvoid != i) {
|
---|
| 152 | bool feasible;
|
---|
[6851] | 153 | double quality = ProblemInstance.GetInsertionCosts(eval, this, city, 0, i, out feasible);
|
---|
[6771] | 154 | if (place < 0 || quality < minQuality) {
|
---|
| 155 | place = i;
|
---|
| 156 | minQuality = quality;
|
---|
| 157 | }
|
---|
[5127] | 158 | }
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | if (place == -1)
|
---|
| 162 | place = 0;
|
---|
| 163 |
|
---|
| 164 | return place;
|
---|
| 165 | }
|
---|
| 166 |
|
---|
[6960] | 167 | public void InsertPair(Tour tour, int source, int target, IVRPProblemInstance problemInstance, int positionToAvoid = -1, int positionToAvoid2 = -1) {
|
---|
| 168 | int stops = tour.Stops.Count;
|
---|
| 169 | VRPEvaluation eval = problemInstance.EvaluateTour(tour, this);
|
---|
| 170 | double minCosts = double.MaxValue;
|
---|
| 171 | int sourceLocation = -1;
|
---|
| 172 | int targetLocation = -1;
|
---|
| 173 |
|
---|
| 174 | for (int i = 0; i <= stops; i++) {
|
---|
| 175 | tour.Stops.Insert(i, source);
|
---|
| 176 | VRPEvaluation tourEval = problemInstance.EvaluateTour(tour, this);
|
---|
| 177 | double sourceCosts = tourEval.Quality - eval.Quality;
|
---|
| 178 |
|
---|
| 179 | for (int j = i + 1; j <= stops + 1; j++) {
|
---|
| 180 | if (positionToAvoid != i || positionToAvoid2 != j || stops == 0) {
|
---|
| 181 | bool feasible;
|
---|
| 182 | double targetCosts = problemInstance.GetInsertionCosts(tourEval, this, target, 0, j, out feasible);
|
---|
| 183 |
|
---|
| 184 | double costs = sourceCosts + targetCosts;
|
---|
| 185 | if (costs < minCosts) {
|
---|
| 186 | minCosts = costs;
|
---|
| 187 | sourceLocation = i;
|
---|
| 188 | targetLocation = j;
|
---|
| 189 | }
|
---|
| 190 | }
|
---|
| 191 | }
|
---|
| 192 | tour.Stops.Remove(source);
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | tour.Stops.Insert(sourceLocation, source);
|
---|
| 196 | tour.Stops.Insert(targetLocation, target);
|
---|
| 197 | }
|
---|
| 198 |
|
---|
[6607] | 199 | public bool FindInsertionPlace(int city, int routeToAvoid, bool allowInfeasible, out int route, out int place) {
|
---|
[4376] | 200 | route = -1;
|
---|
| 201 | place = -1;
|
---|
[6607] | 202 | double minDetour = double.MaxValue;
|
---|
[4376] | 203 |
|
---|
[6752] | 204 | VRPEvaluation eval = ProblemInstance.Evaluate(this);
|
---|
| 205 | bool originalFeasible = ProblemInstance.Feasible(eval);
|
---|
| 206 |
|
---|
[4376] | 207 | for (int tour = 0; tour < Tours.Count; tour++) {
|
---|
| 208 | if (tour != routeToAvoid) {
|
---|
[6710] | 209 | double length = eval.Quality;
|
---|
| 210 |
|
---|
[6607] | 211 | for (int i = 0; i <= Tours[tour].Stops.Count; i++) {
|
---|
[6752] | 212 | bool feasible;
|
---|
[6851] | 213 | double detour = ProblemInstance.GetInsertionCosts(eval, this, city, tour, i, out feasible);
|
---|
| 214 |
|
---|
[6752] | 215 | if (feasible || allowInfeasible) {
|
---|
| 216 | if (route < 0 || detour < minDetour) {
|
---|
[4376] | 217 | route = tour;
|
---|
| 218 | place = i;
|
---|
| 219 | minDetour = detour;
|
---|
| 220 | }
|
---|
| 221 | }
|
---|
| 222 | }
|
---|
| 223 | }
|
---|
| 224 | }
|
---|
| 225 |
|
---|
| 226 | return route >= 0 && place >= 0;
|
---|
| 227 | }
|
---|
| 228 | }
|
---|
| 229 | }
|
---|