Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/General/Creators/IterativeInsertionCreator.cs @ 6448

Last change on this file since 6448 was 6448, checked in by svonolfe, 13 years ago

Included additional operators (#1561)

File size: 6.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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.Collections.Generic;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Optimization;
26using HeuristicLab.Parameters;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Data;
29using System;
30using HeuristicLab.Problems.VehicleRouting.Encodings.Potvin;
31
32namespace HeuristicLab.Problems.VehicleRouting.Encodings.General {
33  [Item("IterativeInsertionCreator", "Creates a randomly initialized VRP solution using the iterative insertion Heuristic.")]
34  [StorableClass]
35  public sealed class IterativeInsertionCreator : DefaultRepresentationCreator, IStochasticOperator {
36    #region IStochasticOperator Members
37    public ILookupParameter<IRandom> RandomParameter {
38      get { return (LookupParameter<IRandom>)Parameters["Random"]; }
39    }
40    #endregion
41
42    [StorableConstructor]
43    private IterativeInsertionCreator(bool deserializing) : base(deserializing) { }
44    private IterativeInsertionCreator(IterativeInsertionCreator original, Cloner cloner) : base(original, cloner) { }
45    public override IDeepCloneable Clone(Cloner cloner) {
46      return new IterativeInsertionCreator(this, cloner);
47    }
48
49    public IterativeInsertionCreator()
50      : base() {
51      Parameters.Add(new LookupParameter<IRandom>("Random", "The pseudo random number generator."));
52    }
53
54    private static double CalculateAngleToDepot(DoubleMatrix coordinates, int city) {
55      double dx = coordinates[0, 0];
56      double dy = coordinates[0, 1];
57
58      double cx = coordinates[city, 0];
59      double cy = coordinates[city, 1];
60
61      double alpha = Math.Atan((cx - dx) / (dy - cy)) * (180.0 / Math.PI);
62      if (cx > dx && cy > dy)
63        alpha = (90.0 + alpha) + 90.0;
64      else if (cx < dx && cy > dy)
65        alpha = alpha + 180.0;
66      else if (cx < dx && cy < dy)
67        alpha = (90.0 + alpha) + 270.0;
68
69      return alpha;
70    }
71
72    private int FindBestInsertionPlace(Tour tour, int city, DoubleArray dueTime,
73      DoubleArray serviceTime, DoubleArray readyTime, DoubleArray demand, DoubleValue capacity,
74      DistanceMatrix distMatrix) {
75      int place = -1;
76      double minQuality = -1;
77
78      for (int i = 0; i <= tour.Cities.Count; i++) {
79        tour.Cities.Insert(i, city);
80       
81        TourEvaluation evaluation = VRPEvaluator.EvaluateTour(tour,
82          dueTime, serviceTime, readyTime, demand, capacity,
83          new DoubleValue(0), new DoubleValue(0), new DoubleValue(1), new DoubleValue(100), new DoubleValue(100),
84            distMatrix);
85        double quality = evaluation.Quality;
86
87        if (place < 0 || quality < minQuality) {
88          place = i;
89          minQuality = quality;
90        }
91
92        tour.Cities.RemoveAt(i);
93      }
94
95      if (place == -1)
96        place = 0;
97
98      return place;
99    }
100
101    protected override List<int> CreateSolution() {
102      int cities = Cities;
103      int vehicles = VehiclesParameter.ActualValue.Value;
104      DoubleMatrix coordinates = CoordinatesParameter.ActualValue;
105      IRandom random = RandomParameter.ActualValue;
106      DoubleArray dueTime = DueTimeParameter.ActualValue;
107      DoubleArray serviceTime = ServiceTimeParameter.ActualValue;
108      DoubleArray readyTime = ReadyTimeParameter.ActualValue;
109      DoubleArray demand = DemandParameter.ActualValue;
110      DoubleValue capacity = CapacityParameter.ActualValue;
111      BoolValue useDistanceMatrix = UseDistanceMatrixParameter.ActualValue;
112
113      DistanceMatrix distMatrix = VRPUtilities.GetDistanceMatrix(coordinates, DistanceMatrixParameter, useDistanceMatrix);
114
115      PotvinEncoding solution = new PotvinEncoding();
116
117      List<int> customers = new List<int>();
118      for (int i = 0; i < Cities; i++)
119        customers.Add(i + 1);
120      customers.Sort(delegate(int city1, int city2) {
121            double angle1 = CalculateAngleToDepot(coordinates, city1);
122            double angle2 = CalculateAngleToDepot(coordinates, city2);
123
124            return angle1.CompareTo(angle2);
125          });
126
127      Tour currentTour = new Tour();
128      solution.Tours.Add(currentTour);
129      int j = random.Next(customers.Count);
130      for (int i = 0; i < customers.Count; i++) {
131        int index = (i + j) % customers.Count;
132
133        int stopIdx = FindBestInsertionPlace(currentTour, customers[index],
134          dueTime, serviceTime, readyTime, demand, capacity,
135            distMatrix);
136        currentTour.Cities.Insert(stopIdx, customers[index]);
137
138        TourEvaluation evaluation = VRPEvaluator.EvaluateTour(currentTour,
139          dueTime, serviceTime, readyTime, demand, capacity,
140          new DoubleValue(0), new DoubleValue(0), new DoubleValue(1), new DoubleValue(100), new DoubleValue(100),
141            distMatrix);
142        if (solution.Tours.Count < vehicles &&
143          ((evaluation.Overload > 0 || evaluation.Tardiness > 0))) {
144          currentTour.Cities.RemoveAt(stopIdx);
145
146          currentTour = new Tour();
147          solution.Tours.Add(currentTour);
148          currentTour.Cities.Add(customers[index]);
149        }
150      }
151
152      List<int> result = new List<int>();
153
154      bool first = true;
155      foreach (Tour tour in solution.GetTours()) {
156        if (first)
157          first = false;
158        else
159          result.Add(0);
160
161        result.AddRange(tour.Cities);
162      }
163
164      while (result.Count < cities + vehicles)
165        result.Add(0);
166
167      return result;
168    }
169  }
170}
Note: See TracBrowser for help on using the repository browser.