Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.VRPEnhancements/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Creators/TemporalDistanceClusterCreator.cs @ 14442

Last change on this file since 14442 was 14442, checked in by jzenisek, 7 years ago

#2707 fixed min variance bug in time window based k-means clustering

File size: 5.5 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("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.")]
34  [StorableClass]
35  public sealed class TemporalDistanceClusterCreator : ClusterCreator {
36
37    [StorableConstructor]
38    private TemporalDistanceClusterCreator(bool deserializing) : base(deserializing) { }
39
40    public TemporalDistanceClusterCreator() : base() {
41    }
42
43    private TemporalDistanceClusterCreator(TemporalDistanceClusterCreator original, Cloner cloner)
44      : base(original, cloner) {
45    }
46
47    public override IDeepCloneable Clone(Cloner cloner) {
48      return new TemporalDistanceClusterCreator(this, cloner);
49    }
50
51    public static List<TemporalDistanceClusterElement> CreateClusterElements(ITimeWindowedProblemInstance instance) {
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
61      // wrap stops in TemporalDistanceClusterElement
62      List<TemporalDistanceClusterElement> clusterObjects = new List<TemporalDistanceClusterElement>();
63      foreach (int customer in customers) {
64        clusterObjects.Add(new TemporalDistanceClusterElement(customer, instance.ReadyTime[customer], instance.DueTime[customer]));
65      }
66      return clusterObjects;
67    }
68
69    public static PotvinEncoding CreateSolution(ITimeWindowedProblemInstance instance, IRandom random, int minK, int maxK, double clusterChangeThreshold, int creationOption) {
70      PotvinEncoding result = new PotvinEncoding(instance);
71
72      // (1) wrap stops in cluster elements
73      List<TemporalDistanceClusterElement> clusterElements = CreateClusterElements(instance);
74
75      // (2) create a random number k of clusters
76      int k = random.Next(minK, maxK);
77      List<TemporalDistanceCluster> clusters = ClusterAlgorithm<TemporalDistanceCluster, TemporalDistanceClusterElement>
78        .KMeans(random, clusterElements, k, clusterChangeThreshold);
79
80      // (3) build tours with a (a) shuffling (b) greedy tour creation routine
81      foreach (var c in clusters) {
82        Tour newTour = new Tour();
83        result.Tours.Add(newTour);
84
85        if (creationOption == 0) {
86          // (a) shuffle
87          c.Elements.Shuffle(random);
88          foreach (var o in c.Elements) {
89            newTour.Stops.Add(o.Id);
90          }
91        } else {
92          // (b) greedy
93          foreach (var o in c.Elements) {
94            newTour.Stops.Add(o.Id);
95          }
96          GreedyTourCreation(instance, result, newTour, true); // "true" means include costs
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
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);
129      return base.InstrumentedApply();
130    }
131  }
132}
Note: See TracBrowser for help on using the repository browser.