[4230] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[7270] | 3 | * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[4230] | 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 |
|
---|
[4722] | 22 | using System.Collections.Generic;
|
---|
[4230] | 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.GVR {
|
---|
| 30 | [Item("GVREncoding", "Represents a potvin encoding of VRP solutions. It is implemented as described in Pereira, F.B. et al (2002). GVR: a New Genetic Representation for the Vehicle Routing Problem. AICS 2002, LNAI 2464, pp. 95-102.")]
|
---|
| 31 | [StorableClass]
|
---|
| 32 | public class GVREncoding : TourEncoding {
|
---|
| 33 | [Storable]
|
---|
| 34 | private DoubleValue capacity { get; set; }
|
---|
| 35 |
|
---|
| 36 | [Storable]
|
---|
| 37 | private DoubleArray demand { get; set; }
|
---|
| 38 |
|
---|
[4268] | 39 | public override List<Tour> GetTours(ILookupParameter<DoubleMatrix> distanceMatrix = null, int maxVehicles = int.MaxValue) {
|
---|
[4270] | 40 | List<Tour> tours = new List<Tour>();
|
---|
[4230] | 41 |
|
---|
| 42 | foreach (Tour tour in base.Tours) {
|
---|
[4270] | 43 | Tour newTour = new Tour();
|
---|
| 44 | double currentDemand = 0;
|
---|
[4230] | 45 |
|
---|
| 46 | foreach (int city in tour.Cities) {
|
---|
| 47 | currentDemand += demand[city];
|
---|
| 48 |
|
---|
[4270] | 49 | if (currentDemand > capacity.Value) {
|
---|
[4230] | 50 | if(newTour.Cities.Count > 0)
|
---|
| 51 | tours.Add(newTour);
|
---|
| 52 |
|
---|
| 53 | newTour = new Tour();
|
---|
| 54 | newTour.Cities.Add(city);
|
---|
| 55 | currentDemand = demand[city];
|
---|
| 56 | } else {
|
---|
| 57 | newTour.Cities.Add(city);
|
---|
| 58 | }
|
---|
| 59 | }
|
---|
| 60 |
|
---|
[4270] | 61 | if (newTour.Cities.Count > 0)
|
---|
[4230] | 62 | tours.Add(newTour);
|
---|
| 63 | }
|
---|
| 64 |
|
---|
[4270] | 65 | //repair if there are too many vehicles used
|
---|
| 66 | while (tours.Count > maxVehicles) {
|
---|
| 67 | Tour tour = tours[tours.Count - 1];
|
---|
| 68 | tours[tours.Count - 2].Cities.AddRange(tour.Cities);
|
---|
| 69 |
|
---|
| 70 | tours.Remove(tour);
|
---|
| 71 | }
|
---|
| 72 |
|
---|
[4230] | 73 | return tours;
|
---|
| 74 | }
|
---|
[4722] | 75 |
|
---|
| 76 | [StorableConstructor]
|
---|
| 77 | protected GVREncoding(bool deserializing) : base(deserializing) { }
|
---|
| 78 | protected GVREncoding(GVREncoding original, Cloner cloner)
|
---|
| 79 | : base(original, cloner) {
|
---|
| 80 | this.capacity = original.capacity;
|
---|
| 81 | this.demand = original.demand;
|
---|
| 82 | this.Tours = cloner.Clone(original.Tours);
|
---|
[4230] | 83 | }
|
---|
[4722] | 84 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 85 | return new GVREncoding(this, cloner);
|
---|
| 86 | }
|
---|
[4268] | 87 | public GVREncoding(DoubleValue capacity, DoubleArray demand)
|
---|
[4230] | 88 | : base() {
|
---|
| 89 | this.capacity = capacity;
|
---|
| 90 | this.demand = demand;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
[4268] | 93 | public static GVREncoding ConvertFrom(IVRPEncoding encoding, DoubleValue capacity, DoubleArray demand,
|
---|
| 94 | ILookupParameter<DoubleMatrix> distanceMatrix) {
|
---|
| 95 | GVREncoding solution = new GVREncoding(capacity, demand);
|
---|
[4230] | 96 |
|
---|
[4268] | 97 | TourEncoding.ConvertFrom(encoding, solution, distanceMatrix);
|
---|
[4230] | 98 |
|
---|
| 99 | return solution;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
[4268] | 102 | public static GVREncoding ConvertFrom(List<int> route, DoubleValue capacity, DoubleArray demand) {
|
---|
| 103 | GVREncoding solution = new GVREncoding(capacity, demand);
|
---|
[4230] | 104 |
|
---|
| 105 | TourEncoding.ConvertFrom(route, solution);
|
---|
| 106 |
|
---|
| 107 | return solution;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | internal void FindCustomer(int customer, out Tour tour, out int index) {
|
---|
| 111 | tour = null;
|
---|
| 112 | index = -1;
|
---|
| 113 |
|
---|
| 114 | int currentTour = 0;
|
---|
| 115 | while (tour == null && currentTour < Tours.Count) {
|
---|
| 116 | int currentCity = 0;
|
---|
| 117 | while (tour == null && currentCity < Tours[currentTour].Cities.Count) {
|
---|
| 118 | if (Tours[currentTour].Cities[currentCity] == customer) {
|
---|
| 119 | tour = Tours[currentTour];
|
---|
| 120 | index = currentCity;
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | currentCity++;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | currentTour++;
|
---|
| 127 | }
|
---|
| 128 | }
|
---|
| 129 | }
|
---|
| 130 | }
|
---|