Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2707_HeuristicLab.VRPEnhancements/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Creators/ClusterCreator.cs @ 17011

Last change on this file since 17011 was 17011, checked in by pfleck, 5 years ago

#2707 Adapted to HEAL.Attic changes

File size: 5.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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.Data;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Problems.VehicleRouting.Interfaces;
31using HeuristicLab.Problems.VehicleRouting.ProblemInstances;
32using HeuristicLab.Problems.VehicleRouting.Variants;
33using HeuristicLab.Random;
34using HEAL.Attic;
35
36namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin {
37  [Item("ClusterCreator", "Creates a VRP solution by clustering customers first with a KMeans-algorithm and building tours afterwards alternatevly in a random or a greedy fashion.")]
38  [StorableType("B57A7C93-FE0B-4D3E-BC2D-DD2B8F4323CB")]
39  public abstract class ClusterCreator : PotvinCreator, IStochasticOperator {
40
41    public ILookupParameter<IRandom> RandomParameter {
42      get { return (LookupParameter<IRandom>)Parameters["Random"]; }
43    }
44
45    public IValueParameter<IntValue> MinK {
46      get { return (IValueParameter<IntValue>)Parameters["MinK"]; }
47    }
48    public IValueParameter<IntValue> MaxK {
49      get { return (IValueParameter<IntValue>)Parameters["MaxK"]; }
50    }
51
52    public IValueParameter<DoubleValue> ClusterChangeThreshold {
53      get { return (IValueParameter<DoubleValue>)Parameters["ClusterChangeThreshold"]; }
54    }
55
56    public IValueParameter<DoubleArray> TourCreationProbabilities {
57      get { return (IValueParameter<DoubleArray>)Parameters["TourCreationProbabilities"]; }
58    }
59
60    [StorableConstructor]
61    public ClusterCreator(StorableConstructorFlag _) : base(_) { }
62
63    public ClusterCreator() : base() {
64      Parameters.Add(new LookupParameter<IRandom>("Random", "The pseudo random number generator."));
65      Parameters.Add(new ValueParameter<IntValue>("MinK", "Minimum number of clusters which should be created", new IntValue(1)));
66      Parameters.Add(new OptionalValueParameter<IntValue>("MaxK", "Maximum number of clusters which should be created"));
67      Parameters.Add(new ValueParameter<DoubleValue>("ClusterChangeThreshold", "A certain ratio below which a cluster should be considered as computed satisfactory", new DoubleValue(0.0)));
68      Parameters.Add(new ValueParameter<DoubleArray>("TourCreationProbabilities", "This weightage array denotes the probability which tour creation method is employed: [shuffled; greedy]", new DoubleArray(new double[] { 1, 1 })));
69    }
70
71    protected ClusterCreator(ClusterCreator original, Cloner cloner)
72      : base(original, cloner) {
73    }
74
75    protected static double CalculateAngleToDepot(IVRPProblemInstance instance, int city) {
76      double dx = instance.GetCoordinates(0)[0];
77      double dy = instance.GetCoordinates(0)[1];
78
79      double cx = instance.GetCoordinates(city)[0];
80      double cy = instance.GetCoordinates(city)[1];
81
82      double alpha = Math.Atan((cx - dx) / (dy - cy)) * (180.0 / Math.PI);
83      if (cx > dx && cy > dy)
84        alpha = (90.0 + alpha) + 90.0;
85      else if (cx < dx && cy > dy)
86        alpha = alpha + 180.0;
87      else if (cx < dx && cy < dy)
88        alpha = (90.0 + alpha) + 270.0;
89
90      return alpha;
91    }
92
93    protected static void GreedyTourCreation(IVRPProblemInstance instance, PotvinEncoding individual, Tour tour, bool includeCosts) {
94      Tour unrouted = tour.Clone() as Tour;
95      tour.Stops.Clear();
96      VRPEvaluation evaluation = instance.EvaluateTour(tour, individual);
97      double currentQuality = (includeCosts) ? evaluation.Quality : evaluation.Distance;
98
99      while (unrouted.Stops.Count > 0) {
100        int insertionPosition = tour.Stops.Count;
101        int nextCityIdxToAdd = 0;
102
103        tour.Stops.Add(unrouted.Stops[nextCityIdxToAdd]);
104        evaluation = instance.EvaluateTour(tour, individual);
105        double minQualityIncrease = ((includeCosts) ? evaluation.Quality : evaluation.Distance) - currentQuality;
106        tour.Stops.RemoveAt(insertionPosition);
107
108        for (int j = 1; j < unrouted.Stops.Count; j++) {
109          tour.Stops.Insert(insertionPosition, unrouted.Stops[j]);
110          evaluation = instance.EvaluateTour(tour, individual);
111          double qualityIncrease = ((includeCosts) ? evaluation.Quality : evaluation.Distance) - currentQuality;
112          tour.Stops.RemoveAt(insertionPosition);
113
114          if (qualityIncrease < minQualityIncrease) {
115            nextCityIdxToAdd = j;
116            minQualityIncrease = qualityIncrease;
117          }
118        }
119
120        currentQuality += minQualityIncrease;
121        tour.Stops.Insert(insertionPosition, unrouted.Stops[nextCityIdxToAdd]);
122        unrouted.Stops.RemoveAt(nextCityIdxToAdd);
123      }
124    }
125
126  }
127}
Note: See TracBrowser for help on using the repository browser.