[4230] | 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 |
|
---|
| 31 | namespace HeuristicLab.Problems.VehicleRouting.Encodings.GVR {
|
---|
| 32 | [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.")]
|
---|
| 33 | [StorableClass]
|
---|
| 34 | public class GVREncoding : TourEncoding {
|
---|
| 35 | [Storable]
|
---|
| 36 | private DoubleValue capacity { get; set; }
|
---|
| 37 |
|
---|
| 38 | [Storable]
|
---|
| 39 | private DoubleArray demand { get; set; }
|
---|
| 40 |
|
---|
[4268] | 41 | public override List<Tour> GetTours(ILookupParameter<DoubleMatrix> distanceMatrix = null, int maxVehicles = int.MaxValue) {
|
---|
[4270] | 42 | List<Tour> tours = new List<Tour>();
|
---|
[4230] | 43 |
|
---|
| 44 | foreach (Tour tour in base.Tours) {
|
---|
[4270] | 45 | Tour newTour = new Tour();
|
---|
| 46 | double currentDemand = 0;
|
---|
[4230] | 47 |
|
---|
| 48 | foreach (int city in tour.Cities) {
|
---|
| 49 | currentDemand += demand[city];
|
---|
| 50 |
|
---|
[4270] | 51 | if (currentDemand > capacity.Value) {
|
---|
[4230] | 52 | if(newTour.Cities.Count > 0)
|
---|
| 53 | tours.Add(newTour);
|
---|
| 54 |
|
---|
| 55 | newTour = new Tour();
|
---|
| 56 | newTour.Cities.Add(city);
|
---|
| 57 | currentDemand = demand[city];
|
---|
| 58 | } else {
|
---|
| 59 | newTour.Cities.Add(city);
|
---|
| 60 | }
|
---|
| 61 | }
|
---|
| 62 |
|
---|
[4270] | 63 | if (newTour.Cities.Count > 0)
|
---|
[4230] | 64 | tours.Add(newTour);
|
---|
| 65 | }
|
---|
| 66 |
|
---|
[4270] | 67 | //repair if there are too many vehicles used
|
---|
| 68 | while (tours.Count > maxVehicles) {
|
---|
| 69 | Tour tour = tours[tours.Count - 1];
|
---|
| 70 | tours[tours.Count - 2].Cities.AddRange(tour.Cities);
|
---|
| 71 |
|
---|
| 72 | tours.Remove(tour);
|
---|
| 73 | }
|
---|
| 74 |
|
---|
[4230] | 75 | return tours;
|
---|
| 76 | }
|
---|
[4689] | 77 |
|
---|
| 78 | [StorableConstructor]
|
---|
| 79 | protected GVREncoding(bool deserializing) : base(deserializing) { }
|
---|
| 80 | protected GVREncoding(GVREncoding original, Cloner cloner)
|
---|
| 81 | : base(original, cloner) {
|
---|
| 82 | this.capacity = original.capacity;
|
---|
| 83 | this.demand = original.demand;
|
---|
| 84 | this.Tours = cloner.Clone(original.Tours);
|
---|
[4230] | 85 | }
|
---|
[4689] | 86 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 87 | return new GVREncoding(this, cloner);
|
---|
| 88 | }
|
---|
[4268] | 89 | public GVREncoding(DoubleValue capacity, DoubleArray demand)
|
---|
[4230] | 90 | : base() {
|
---|
| 91 | this.capacity = capacity;
|
---|
| 92 | this.demand = demand;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
[4268] | 95 | public static GVREncoding ConvertFrom(IVRPEncoding encoding, DoubleValue capacity, DoubleArray demand,
|
---|
| 96 | ILookupParameter<DoubleMatrix> distanceMatrix) {
|
---|
| 97 | GVREncoding solution = new GVREncoding(capacity, demand);
|
---|
[4230] | 98 |
|
---|
[4268] | 99 | TourEncoding.ConvertFrom(encoding, solution, distanceMatrix);
|
---|
[4230] | 100 |
|
---|
| 101 | return solution;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
[4268] | 104 | public static GVREncoding ConvertFrom(List<int> route, DoubleValue capacity, DoubleArray demand) {
|
---|
| 105 | GVREncoding solution = new GVREncoding(capacity, demand);
|
---|
[4230] | 106 |
|
---|
| 107 | TourEncoding.ConvertFrom(route, solution);
|
---|
| 108 |
|
---|
| 109 | return solution;
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | internal void FindCustomer(int customer, out Tour tour, out int index) {
|
---|
| 113 | tour = null;
|
---|
| 114 | index = -1;
|
---|
| 115 |
|
---|
| 116 | int currentTour = 0;
|
---|
| 117 | while (tour == null && currentTour < Tours.Count) {
|
---|
| 118 | int currentCity = 0;
|
---|
| 119 | while (tour == null && currentCity < Tours[currentTour].Cities.Count) {
|
---|
| 120 | if (Tours[currentTour].Cities[currentCity] == customer) {
|
---|
| 121 | tour = Tours[currentTour];
|
---|
| 122 | index = currentCity;
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | currentCity++;
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | currentTour++;
|
---|
| 129 | }
|
---|
| 130 | }
|
---|
| 131 | }
|
---|
| 132 | }
|
---|