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> CreateClusterElements(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> clusterElements = new List<SpatialDistanceClusterElement>();
|
---|
62 | foreach (int customer in customers) {
|
---|
63 | clusterElements.Add(new SpatialDistanceClusterElement(customer, instance.GetCoordinates(customer)));
|
---|
64 | }
|
---|
65 | return clusterElements;
|
---|
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 | // (1) wrap stops in cluster elements
|
---|
72 | List<SpatialDistanceClusterElement> clusterElements = CreateClusterElements(instance);
|
---|
73 |
|
---|
74 | // (2) create a random number k of clusters
|
---|
75 | int k = random.Next(minK, maxK);
|
---|
76 | List<SpatialDistanceCluster> clusters = ClusterAlgorithm<SpatialDistanceCluster, SpatialDistanceClusterElement>
|
---|
77 | .KMeans(random, clusterElements, k, clusterChangeThreshold);
|
---|
78 |
|
---|
79 | // (3) build tours with a (a) shuffling (b) greedy tour creation routine
|
---|
80 | foreach (var c in clusters) {
|
---|
81 | Tour newTour = new Tour();
|
---|
82 | result.Tours.Add(newTour);
|
---|
83 |
|
---|
84 | if (creationOption == 0) {
|
---|
85 | // (a) shuffle
|
---|
86 | c.Elements.Shuffle(random);
|
---|
87 | foreach (var o in c.Elements) {
|
---|
88 | newTour.Stops.Add(o.Id);
|
---|
89 | }
|
---|
90 | } else {
|
---|
91 | // (b) greedy
|
---|
92 | foreach (var o in c.Elements) {
|
---|
93 | newTour.Stops.Add(o.Id);
|
---|
94 | }
|
---|
95 | GreedyTourCreation(instance, result, newTour, false);
|
---|
96 | }
|
---|
97 | }
|
---|
98 |
|
---|
99 | return result;
|
---|
100 | }
|
---|
101 |
|
---|
102 | public override IOperation InstrumentedApply() {
|
---|
103 | IRandom random = RandomParameter.ActualValue;
|
---|
104 |
|
---|
105 | int minK = (MinK.Value.Value > 0) ? MinK.Value.Value : 1;
|
---|
106 | int maxK = (MaxK.Value != null) ? MaxK.Value.Value : ProblemInstance.Vehicles.Value;
|
---|
107 | double clusterChangeThreshold = (ClusterChangeThreshold.Value.Value >= 0.0 &&
|
---|
108 | ClusterChangeThreshold.Value.Value <= 1.0)
|
---|
109 | ? ClusterChangeThreshold.Value.Value
|
---|
110 | : 0.0;
|
---|
111 |
|
---|
112 | // normalize probabilities
|
---|
113 | double max = TourCreationProbabilities.Value.Max();
|
---|
114 | double[] probabilites = new double[2];
|
---|
115 | for (int i = 0; i < TourCreationProbabilities.Value.Length; i++) {
|
---|
116 | probabilites[i] = TourCreationProbabilities.Value[i] / max;
|
---|
117 | }
|
---|
118 |
|
---|
119 | List<int> creationOptions = new List<int>() { 0, 1 };
|
---|
120 | int creationOption = creationOptions.SampleProportional(random, 1, probabilites, false, false).First();
|
---|
121 |
|
---|
122 | VRPToursParameter.ActualValue = CreateSolution(ProblemInstance, random, minK, maxK, clusterChangeThreshold, creationOption);
|
---|
123 | return base.InstrumentedApply();
|
---|
124 | }
|
---|
125 | }
|
---|
126 | }
|
---|