Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.VRPEnhancements/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Creators/GeographicDistanceClusterCreator.cs @ 14559

Last change on this file since 14559 was 14559, checked in by pfleck, 7 years ago

#2707 Simplified k-means clustering for ClusterCreators.

File size: 5.2 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Problems.VehicleRouting.Interfaces;
29using HeuristicLab.Problems.VehicleRouting.Variants;
30using HeuristicLab.Random;
31
32namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin {
33  [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.")]
34  [StorableClass]
35  public sealed class GeographicDistanceClusterCreator : ClusterCreator {
36
37    [StorableConstructor]
38    private GeographicDistanceClusterCreator(bool deserializing) : base(deserializing) { }
39
40    public GeographicDistanceClusterCreator() : base() {
41    }
42
43    private GeographicDistanceClusterCreator(GeographicDistanceClusterCreator original, Cloner cloner)
44      : base(original, cloner) {
45    }
46
47    public override IDeepCloneable Clone(Cloner cloner) {
48      return new GeographicDistanceClusterCreator(this, cloner);
49    }
50
51
52    public static PotvinEncoding CreateSolution(IVRPProblemInstance instance, IRandom random, int minK, int maxK, double clusterChangeThreshold, int creationOption) {
53      PotvinEncoding result = new PotvinEncoding(instance);
54
55      // (1) init data
56      var coordinates = new List<double[]>(instance.Cities.Value);
57      var pdp = instance as IPickupAndDeliveryProblemInstance;
58      for (int i = 1; i <= instance.Cities.Value; i++) {
59        if (pdp == null || pdp.GetDemand(i) >= 0)
60          coordinates.Add(instance.GetCoordinates(i));
61      }
62
63      // (2) create a random number k of clusters
64      int k = random.Next(minK, maxK);
65      var kMeans = new KMeansAlgorithm<double[]>(CalculateMeanHelper, CalculateDistanceHelper);
66      var clusters = kMeans.Run(coordinates, k, clusterChangeThreshold, random);
67
68      // (3) build tours with a (a) shuffling (b) greedy tour creation routine
69      foreach (var cluster in clusters) {
70        Tour newTour = new Tour();
71        result.Tours.Add(newTour);
72
73        if (creationOption == 0) {
74          // (a) shuffle
75          cluster.ShuffleInPlace(random);
76          newTour.Stops.AddRange(cluster);
77          foreach (var customer in cluster) {
78            newTour.Stops.Add(customer + 1);
79          }
80        } else {
81          // (b) greedy
82          foreach (var customer in cluster) {
83            newTour.Stops.Add(customer + 1);
84          }
85          GreedyTourCreation(instance, result, newTour, false);
86        }
87      }
88
89      return result;
90    }
91
92    private static double[] CalculateMeanHelper(List<double[]> coordinates) {
93      var mean = new double[coordinates[0].Length];
94      foreach (double[] coord in coordinates) {
95        for (int i = 0; i < mean.Length; i++) {
96          mean[i] += coord[i] / coordinates.Count;
97        }
98      }
99      return mean;
100    }
101    private static double CalculateDistanceHelper(double[] coord1, double[] coord2) {
102      double distance = 0.0;
103      for (int i = 0; i < coord1.Length; i++) {
104        distance += Math.Pow(coord1[i] - coord2[i], 2);
105      }
106      return Math.Sqrt(distance);
107    }
108
109    public override IOperation InstrumentedApply() {
110      IRandom random = RandomParameter.ActualValue;
111
112      int minK = (MinK.Value.Value > 0) ? MinK.Value.Value : 1;
113      int maxK = (MaxK.Value != null) ? MaxK.Value.Value : ProblemInstance.Vehicles.Value;
114      double clusterChangeThreshold = (ClusterChangeThreshold.Value.Value >= 0.0 &&
115                                       ClusterChangeThreshold.Value.Value <= 1.0)
116        ? ClusterChangeThreshold.Value.Value
117        : 0.0;
118
119      // normalize probabilities
120      double max = TourCreationProbabilities.Value.Max();
121      double[] probabilites = new double[2];
122      for (int i = 0; i < TourCreationProbabilities.Value.Length; i++) {
123        probabilites[i] = TourCreationProbabilities.Value[i] / max;
124      }
125
126      List<int> creationOptions = new List<int>() { 0, 1 };
127      int creationOption = creationOptions.SampleProportional(random, 1, probabilites, false, false).First();
128
129      VRPToursParameter.ActualValue = CreateSolution(ProblemInstance, random, minK, maxK, clusterChangeThreshold, creationOption);
130      return base.InstrumentedApply();
131    }
132  }
133}
Note: See TracBrowser for help on using the repository browser.