Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2707

  • Fixed a bug in cluster creators.
  • Added the vehicle rr to the each tour in the solution view.
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          foreach (var customer in cluster) {
77            newTour.Stops.Add(customer + 1);
78          }
79        } else {
80          // (b) greedy
81          foreach (var customer in cluster) {
82            newTour.Stops.Add(customer + 1);
83          }
84          GreedyTourCreation(instance, result, newTour, false);
85        }
86      }
87
88      return result;
89    }
90
91    private static double[] CalculateMeanHelper(List<double[]> coordinates) {
92      var mean = new double[coordinates[0].Length];
93      foreach (double[] coord in coordinates) {
94        for (int i = 0; i < mean.Length; i++) {
95          mean[i] += coord[i] / coordinates.Count;
96        }
97      }
98      return mean;
99    }
100    private static double CalculateDistanceHelper(double[] coord1, double[] coord2) {
101      double distance = 0.0;
102      for (int i = 0; i < coord1.Length; i++) {
103        distance += Math.Pow(coord1[i] - coord2[i], 2);
104      }
105      return Math.Sqrt(distance);
106    }
107
108    public override IOperation InstrumentedApply() {
109      IRandom random = RandomParameter.ActualValue;
110
111      int minK = (MinK.Value.Value > 0) ? MinK.Value.Value : 1;
112      int maxK = (MaxK.Value != null) ? MaxK.Value.Value : ProblemInstance.Vehicles.Value;
113      double clusterChangeThreshold = (ClusterChangeThreshold.Value.Value >= 0.0 &&
114                                       ClusterChangeThreshold.Value.Value <= 1.0)
115        ? ClusterChangeThreshold.Value.Value
116        : 0.0;
117
118      // normalize probabilities
119      double max = TourCreationProbabilities.Value.Max();
120      double[] probabilites = new double[2];
121      for (int i = 0; i < TourCreationProbabilities.Value.Length; i++) {
122        probabilites[i] = TourCreationProbabilities.Value[i] / max;
123      }
124
125      List<int> creationOptions = new List<int>() { 0, 1 };
126      int creationOption = creationOptions.SampleProportional(random, 1, probabilites, false, false).First();
127
128      VRPToursParameter.ActualValue = CreateSolution(ProblemInstance, random, minK, maxK, clusterChangeThreshold, creationOption);
129      return base.InstrumentedApply();
130    }
131  }
132}
Note: See TracBrowser for help on using the repository browser.