[14424] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2016 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 |
|
---|
[14430] | 22 | using System;
|
---|
[14424] | 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 28 | using HeuristicLab.Problems.VehicleRouting.Interfaces;
|
---|
| 29 | using HeuristicLab.Problems.VehicleRouting.Variants;
|
---|
| 30 | using HeuristicLab.Random;
|
---|
| 31 |
|
---|
| 32 | namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin {
|
---|
[14430] | 33 | [Item("TemporalDistanceClusterCreator", "Creates a VRP solution by clustering customers first with a KMeans-algorithm based on their geographic position and building tours afterwards alternatevly in a random or a greedy fashion.")]
|
---|
[14424] | 34 | [StorableClass]
|
---|
[14430] | 35 | public sealed class TemporalDistanceClusterCreator : ClusterCreator {
|
---|
[14424] | 36 |
|
---|
| 37 | [StorableConstructor]
|
---|
[14430] | 38 | private TemporalDistanceClusterCreator(bool deserializing) : base(deserializing) { }
|
---|
[14424] | 39 |
|
---|
[14430] | 40 | public TemporalDistanceClusterCreator() : base() {
|
---|
[14424] | 41 | }
|
---|
| 42 |
|
---|
[14430] | 43 | private TemporalDistanceClusterCreator(TemporalDistanceClusterCreator original, Cloner cloner)
|
---|
[14424] | 44 | : base(original, cloner) {
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[14430] | 48 | return new TemporalDistanceClusterCreator(this, cloner);
|
---|
[14424] | 49 | }
|
---|
| 50 |
|
---|
[14431] | 51 | public static List<TemporalDistanceClusterElement> CreateClusterElements(ITimeWindowedProblemInstance instance) {
|
---|
[14424] | 52 | IPickupAndDeliveryProblemInstance pdp = instance as IPickupAndDeliveryProblemInstance;
|
---|
| 53 |
|
---|
| 54 | // add all customers
|
---|
| 55 | List<int> customers = new List<int>();
|
---|
| 56 | for (int i = 1; i <= instance.Cities.Value; i++) {
|
---|
| 57 | if (pdp == null || pdp.GetDemand(i) >= 0)
|
---|
| 58 | customers.Add(i);
|
---|
| 59 | }
|
---|
| 60 |
|
---|
[14430] | 61 | // wrap stops in TemporalDistanceClusterElement
|
---|
| 62 | List<TemporalDistanceClusterElement> clusterObjects = new List<TemporalDistanceClusterElement>();
|
---|
[14424] | 63 | foreach (int customer in customers) {
|
---|
[14430] | 64 | clusterObjects.Add(new TemporalDistanceClusterElement(customer, instance.ReadyTime[customer], instance.DueTime[customer]));
|
---|
[14424] | 65 | }
|
---|
| 66 | return clusterObjects;
|
---|
| 67 | }
|
---|
| 68 |
|
---|
[14430] | 69 | public static PotvinEncoding CreateSolution(ITimeWindowedProblemInstance instance, IRandom random, int minK, int maxK, double clusterChangeThreshold, int creationOption) {
|
---|
[14424] | 70 | PotvinEncoding result = new PotvinEncoding(instance);
|
---|
| 71 |
|
---|
[14431] | 72 | // (1) wrap stops in cluster elements
|
---|
| 73 | List<TemporalDistanceClusterElement> clusterElements = CreateClusterElements(instance);
|
---|
[14424] | 74 |
|
---|
[14431] | 75 | // (2) create a random number k of clusters
|
---|
[14424] | 76 | int k = random.Next(minK, maxK);
|
---|
[14431] | 77 | List<TemporalDistanceCluster> clusters = ClusterAlgorithm<TemporalDistanceCluster, TemporalDistanceClusterElement>
|
---|
[14442] | 78 | .KMeans(random, clusterElements, k, clusterChangeThreshold);
|
---|
[14424] | 79 |
|
---|
[14431] | 80 | // (3) build tours with a (a) shuffling (b) greedy tour creation routine
|
---|
[14424] | 81 | foreach (var c in clusters) {
|
---|
| 82 | Tour newTour = new Tour();
|
---|
| 83 | result.Tours.Add(newTour);
|
---|
| 84 |
|
---|
| 85 | if (creationOption == 0) {
|
---|
| 86 | // (a) shuffle
|
---|
[14430] | 87 | c.Elements.Shuffle(random);
|
---|
| 88 | foreach (var o in c.Elements) {
|
---|
[14424] | 89 | newTour.Stops.Add(o.Id);
|
---|
| 90 | }
|
---|
| 91 | } else {
|
---|
| 92 | // (b) greedy
|
---|
[14430] | 93 | foreach (var o in c.Elements) {
|
---|
[14424] | 94 | newTour.Stops.Add(o.Id);
|
---|
| 95 | }
|
---|
[14431] | 96 | GreedyTourCreation(instance, result, newTour, true); // "true" means include costs
|
---|
[14424] | 97 | }
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | return result;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | public override IOperation InstrumentedApply() {
|
---|
| 104 | IRandom random = RandomParameter.ActualValue;
|
---|
| 105 |
|
---|
| 106 | int minK = (MinK.Value.Value > 0) ? MinK.Value.Value : 1;
|
---|
| 107 | int maxK = (MaxK.Value != null) ? MaxK.Value.Value : ProblemInstance.Vehicles.Value;
|
---|
| 108 | double clusterChangeThreshold = (ClusterChangeThreshold.Value.Value >= 0.0 &&
|
---|
| 109 | ClusterChangeThreshold.Value.Value <= 1.0)
|
---|
| 110 | ? ClusterChangeThreshold.Value.Value
|
---|
| 111 | : 0.0;
|
---|
| 112 |
|
---|
| 113 | // normalize probabilities
|
---|
| 114 | double max = TourCreationProbabilities.Value.Max();
|
---|
| 115 | double[] probabilites = new double[2];
|
---|
| 116 | for (int i = 0; i < TourCreationProbabilities.Value.Length; i++) {
|
---|
| 117 | probabilites[i] = TourCreationProbabilities.Value[i] / max;
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | List<int> creationOptions = new List<int>() { 0, 1 };
|
---|
| 121 | int creationOption = creationOptions.SampleProportional(random, 1, probabilites, false, false).First();
|
---|
| 122 |
|
---|
[14430] | 123 | var instance = ProblemInstance as ITimeWindowedProblemInstance;
|
---|
| 124 | if (instance == null) {
|
---|
| 125 | throw new ArgumentException(string.Format("Cannot initialize {0} with data from {1}", instance.GetType(), ProblemInstance.GetType()));
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | VRPToursParameter.ActualValue = CreateSolution(instance, random, minK, maxK, clusterChangeThreshold, creationOption);
|
---|
[14424] | 129 | return base.InstrumentedApply();
|
---|
| 130 | }
|
---|
| 131 | }
|
---|
| 132 | }
|
---|