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 |
|
---|
22 | using System.Collections.Generic;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
27 | using HeuristicLab.Problems.VehicleRouting.Interfaces;
|
---|
28 | using HeuristicLab.Problems.VehicleRouting.Variants;
|
---|
29 | using HeuristicLab.Random;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin {
|
---|
32 | [Item("GeographicDistanceClusterCreator", "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.")]
|
---|
33 | [StorableClass]
|
---|
34 | public sealed class GeographicDistanceClusterCreator : ClusterCreator {
|
---|
35 |
|
---|
36 | [StorableConstructor]
|
---|
37 | private GeographicDistanceClusterCreator(bool deserializing) : base(deserializing) { }
|
---|
38 |
|
---|
39 | public GeographicDistanceClusterCreator() : base() {
|
---|
40 | }
|
---|
41 |
|
---|
42 | private GeographicDistanceClusterCreator(GeographicDistanceClusterCreator original, Cloner cloner)
|
---|
43 | : base(original, cloner) {
|
---|
44 | }
|
---|
45 |
|
---|
46 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
47 | return new GeographicDistanceClusterCreator(this, cloner);
|
---|
48 | }
|
---|
49 |
|
---|
50 | public static List<SpatialDistanceClusterElement> CreateClusterObjects(IVRPProblemInstance instance) {
|
---|
51 | IPickupAndDeliveryProblemInstance pdp = instance as IPickupAndDeliveryProblemInstance;
|
---|
52 |
|
---|
53 | // add all customers
|
---|
54 | List<int> customers = new List<int>();
|
---|
55 | for (int i = 1; i <= instance.Cities.Value; i++) {
|
---|
56 | if (pdp == null || pdp.GetDemand(i) >= 0)
|
---|
57 | customers.Add(i);
|
---|
58 | }
|
---|
59 |
|
---|
60 | // wrap stops in SpatialDistanceClusterElement
|
---|
61 | List<SpatialDistanceClusterElement> clusterObjects = new List<SpatialDistanceClusterElement>();
|
---|
62 | foreach (int customer in customers) {
|
---|
63 | clusterObjects.Add(new SpatialDistanceClusterElement(customer, instance.GetCoordinates(customer)));
|
---|
64 | }
|
---|
65 | return clusterObjects;
|
---|
66 | }
|
---|
67 |
|
---|
68 | public static PotvinEncoding CreateSolution(IVRPProblemInstance instance, IRandom random, int minK, int maxK, double clusterChangeThreshold, int creationOption) {
|
---|
69 | PotvinEncoding result = new PotvinEncoding(instance);
|
---|
70 |
|
---|
71 | List<SpatialDistanceClusterElement> clusterObjects = CreateClusterObjects(instance);
|
---|
72 |
|
---|
73 | int k = random.Next(minK, maxK);
|
---|
74 | List<SpatialDistanceCluster> clusters = KMeans(instance, random, clusterObjects, k, clusterChangeThreshold);
|
---|
75 |
|
---|
76 | // (3) build tours
|
---|
77 | // (a) shuffle
|
---|
78 | // (b) greedy
|
---|
79 | foreach (var c in clusters) {
|
---|
80 | Tour newTour = new Tour();
|
---|
81 | result.Tours.Add(newTour);
|
---|
82 |
|
---|
83 | if (creationOption == 0) {
|
---|
84 | // (a) shuffle
|
---|
85 | c.Objects.Shuffle(random);
|
---|
86 | foreach (var o in c.Objects) {
|
---|
87 | newTour.Stops.Add(o.Id);
|
---|
88 | }
|
---|
89 | } else {
|
---|
90 | // (b) greedy
|
---|
91 | foreach (var o in c.Objects) {
|
---|
92 | newTour.Stops.Add(o.Id);
|
---|
93 | }
|
---|
94 | GreedyTourCreation(instance, result, newTour, false);
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | return result;
|
---|
99 | }
|
---|
100 |
|
---|
101 | private static List<SpatialDistanceCluster> KMeans(IVRPProblemInstance instance, IRandom random, List<SpatialDistanceClusterElement> objects, int k, double changeTreshold) {
|
---|
102 |
|
---|
103 | List<SpatialDistanceCluster> clusters = new List<SpatialDistanceCluster>();
|
---|
104 | HashSet<int> initMeans = new HashSet<int>();
|
---|
105 | int nextMean = -1;
|
---|
106 |
|
---|
107 | // (1) initialize each cluster with a random first object (i.e. mean)
|
---|
108 | for (int i = 0; i < k && i < objects.Count; i++) {
|
---|
109 | SpatialDistanceCluster cluster = new SpatialDistanceCluster(instance, i);
|
---|
110 |
|
---|
111 | do {
|
---|
112 | nextMean = random.Next(0, objects.Count);
|
---|
113 | } while (initMeans.Contains(nextMean));
|
---|
114 | initMeans.Add(nextMean);
|
---|
115 | cluster.SetMean(objects[nextMean]);
|
---|
116 | clusters.Add(cluster);
|
---|
117 | }
|
---|
118 |
|
---|
119 | // (2) repeat clustering until change rate is below threshold
|
---|
120 | int changes = 0;
|
---|
121 | double changeRate = 1.0;
|
---|
122 |
|
---|
123 | do {
|
---|
124 | changes = KMeansRun(clusters, objects);
|
---|
125 | changeRate = changes / objects.Count;
|
---|
126 |
|
---|
127 | } while (changeRate > changeTreshold);
|
---|
128 |
|
---|
129 | // remove empty clusters
|
---|
130 | clusters.RemoveAll(c => c.Objects.Count.Equals(0));
|
---|
131 |
|
---|
132 | return clusters;
|
---|
133 | }
|
---|
134 |
|
---|
135 | private static int KMeansRun(List<SpatialDistanceCluster> clusters, List<SpatialDistanceClusterElement> objects) {
|
---|
136 | int changes = 0;
|
---|
137 |
|
---|
138 | foreach (var c in clusters) {
|
---|
139 | c.Objects.Clear();
|
---|
140 | }
|
---|
141 |
|
---|
142 | foreach (var o in objects) {
|
---|
143 | int bestClusterIdx = -1;
|
---|
144 | double minImpact = double.MaxValue;
|
---|
145 | for (int i = 0; i < clusters.Count; i++) {
|
---|
146 | double impact = clusters[i].CalculateImpact(o);
|
---|
147 | if (impact < minImpact) {
|
---|
148 | minImpact = impact;
|
---|
149 | bestClusterIdx = i;
|
---|
150 | }
|
---|
151 | }
|
---|
152 | if (clusters[bestClusterIdx].AddObject(o))
|
---|
153 | changes++;
|
---|
154 | }
|
---|
155 |
|
---|
156 | foreach (var c in clusters) {
|
---|
157 | c.CalculateMean();
|
---|
158 | }
|
---|
159 |
|
---|
160 | return changes;
|
---|
161 | }
|
---|
162 |
|
---|
163 | public override IOperation InstrumentedApply() {
|
---|
164 | IRandom random = RandomParameter.ActualValue;
|
---|
165 |
|
---|
166 | int minK = (MinK.Value.Value > 0) ? MinK.Value.Value : 1;
|
---|
167 | int maxK = (MaxK.Value != null) ? MaxK.Value.Value : ProblemInstance.Vehicles.Value;
|
---|
168 | double clusterChangeThreshold = (ClusterChangeThreshold.Value.Value >= 0.0 &&
|
---|
169 | ClusterChangeThreshold.Value.Value <= 1.0)
|
---|
170 | ? ClusterChangeThreshold.Value.Value
|
---|
171 | : 0.0;
|
---|
172 |
|
---|
173 | // normalize probabilities
|
---|
174 | double max = TourCreationProbabilities.Value.Max();
|
---|
175 | double[] probabilites = new double[2];
|
---|
176 | for (int i = 0; i < TourCreationProbabilities.Value.Length; i++) {
|
---|
177 | probabilites[i] = TourCreationProbabilities.Value[i] / max;
|
---|
178 | }
|
---|
179 |
|
---|
180 | List<int> creationOptions = new List<int>() { 0, 1 };
|
---|
181 | int creationOption = creationOptions.SampleProportional(random, 1, probabilites, false, false).First();
|
---|
182 |
|
---|
183 | VRPToursParameter.ActualValue = CreateSolution(ProblemInstance, random, minK, maxK, clusterChangeThreshold, creationOption);
|
---|
184 | return base.InstrumentedApply();
|
---|
185 | }
|
---|
186 | }
|
---|
187 | }
|
---|