Free cookie consent management tool by TermsFeed Policy Generator

source: tags/3.3.1/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/General/Creators/PushForwardInsertionCreator.cs @ 13398

Last change on this file since 13398 was 4352, checked in by svonolfe, 14 years ago

Merged r4351 of the VRP feature branch into trunk (#1039)

File size: 11.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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 HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Encodings.PermutationEncoding;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Problems.VehicleRouting.Encodings.General {
32  [Item("PushForwardCreator", "The push forward insertion heuristic.  It is implemented as described in Sam, and Thangiah, R. (1999). A Hybrid Genetic Algorithms, Simulated Annealing and Tabu Search Heuristic for Vehicle Routing Problems with Time Windows. Practical Handbook of Genetic Algorithms, Volume III, pp 347–381.")]
33  [StorableClass]
34  public sealed class PushForwardCreator : DefaultRepresentationCreator, IStochasticOperator {
35    #region IStochasticOperator Members
36    public ILookupParameter<IRandom> RandomParameter {
37      get { return (LookupParameter<IRandom>)Parameters["Random"]; }
38    }
39    #endregion
40
41    public IValueParameter<DoubleValue> Alpha {
42      get { return (IValueParameter<DoubleValue>)Parameters["Alpha"]; }
43    }
44    public IValueParameter<DoubleValue> AlphaVariance {
45      get { return (IValueParameter<DoubleValue>)Parameters["AlphaVariance"]; }
46    }
47    public IValueParameter<DoubleValue> Beta {
48      get { return (IValueParameter<DoubleValue>)Parameters["Beta"]; }
49    }
50    public IValueParameter<DoubleValue> BetaVariance {
51      get { return (IValueParameter<DoubleValue>)Parameters["BetaVariance"]; }
52    }
53    public IValueParameter<DoubleValue> Gamma {
54      get { return (IValueParameter<DoubleValue>)Parameters["Gamma"]; }
55    }
56    public IValueParameter<DoubleValue> GammaVariance {
57      get { return (IValueParameter<DoubleValue>)Parameters["GammaVariance"]; }
58    }
59
60    [StorableConstructor]
61    private PushForwardCreator(bool deserializing) : base(deserializing) { }
62
63    public PushForwardCreator()
64      : base() {
65      Parameters.Add(new LookupParameter<IRandom>("Random", "The pseudo random number generator."));
66      Parameters.Add(new ValueParameter<DoubleValue>("Alpha", "The alpha value.", new DoubleValue(0.7)));
67      Parameters.Add(new ValueParameter<DoubleValue>("AlphaVariance", "The alpha variance.", new DoubleValue(0.5)));
68      Parameters.Add(new ValueParameter<DoubleValue>("Beta", "The beta value.", new DoubleValue(0.1)));
69      Parameters.Add(new ValueParameter<DoubleValue>("BetaVariance", "The beta variance.", new DoubleValue(0.07)));
70      Parameters.Add(new ValueParameter<DoubleValue>("Gamma", "The gamma value.", new DoubleValue(0.2)));
71      Parameters.Add(new ValueParameter<DoubleValue>("GammaVariance", "The gamma variance.", new DoubleValue(0.14)));
72    }
73
74    // use the Box-Mueller transform in the polar form to generate a N(0,1) random variable out of two uniformly distributed random variables
75    private double Gauss(IRandom random) {
76      double u = 0.0, v = 0.0, s = 0.0;
77      do {
78        u = (random.NextDouble() * 2) - 1;
79        v = (random.NextDouble() * 2) - 1;
80        s = Math.Sqrt(u * u + v * v);
81      } while (s < Double.Epsilon || s > 1);
82      return u * Math.Sqrt((-2.0 * Math.Log(s)) / s);
83    }
84
85    private double N(double mu, double sigma, IRandom random) {
86      return mu + (sigma * Gauss(random)); // transform the random variable sampled from N(0,1) to N(mu,sigma)
87    }
88
89    private double CalculateDistance(int start, int end) {
90      double distance = 0.0;
91      DoubleMatrix coordinates = CoordinatesParameter.ActualValue;
92
93      distance =
94          Math.Sqrt(
95            Math.Pow(coordinates[start, 0] - coordinates[end, 0], 2) +
96            Math.Pow(coordinates[start, 1] - coordinates[end, 1], 2));
97
98      return distance;
99    }
100
101    private DoubleMatrix CreateDistanceMatrix() {
102      DoubleMatrix coordinates = CoordinatesParameter.ActualValue;
103      DoubleMatrix distanceMatrix = new DoubleMatrix(coordinates.Rows, coordinates.Rows);
104
105      for (int i = 0; i < distanceMatrix.Rows; i++) {
106        for (int j = i; j < distanceMatrix.Columns; j++) {
107          double distance = CalculateDistance(i, j);
108
109          distanceMatrix[i, j] = distance;
110          distanceMatrix[j, i] = distance;
111        }
112      }
113
114      return distanceMatrix;
115    }
116
117    private double Distance(int start, int end) {
118      double distance = 0.0;
119
120      if (UseDistanceMatrixParameter.ActualValue.Value) {
121        if (DistanceMatrixParameter.ActualValue == null) {
122          DistanceMatrixParameter.ActualValue = CreateDistanceMatrix();
123        }
124
125        distance = DistanceMatrixParameter.ActualValue[start, end];
126      } else {
127        distance = CalculateDistance(start, end);
128      }
129
130      return distance;
131    }
132
133    private double TravelDistance(List<int> route, int begin) {
134      double distance = 0;
135      for (int i = begin; i < route.Count - 1 && (i == begin || route[i] != 0); i++) {
136        distance += Distance(route[i], route[i + 1]);
137      }
138      return distance;
139    }
140
141    private bool SubrouteConstraintsOK(List<int> route, int begin) {
142      double t = 0.0, o = 0.0;
143      for (int i = begin + 1; i < route.Count; i++) {
144        t += Distance(route[i - 1], route[i]);
145        if (route[i] == 0) return (t < DueTimeParameter.ActualValue[0]); // violation on capacity constraint is handled below
146        else {
147          if (t > DueTimeParameter.ActualValue[route[i]]) return false;
148          t = Math.Max(ReadyTimeParameter.ActualValue[route[i]], t);
149          t += ServiceTimeParameter.ActualValue[route[i]];
150          o += DemandParameter.ActualValue[route[i]];
151          if (o > CapacityParameter.ActualValue.Value) return false; // premature exit on capacity constraint violation
152        }
153      }
154      return true;
155    }
156
157    private bool SubrouteTardinessOK(List<int> route, int begin) {
158      double t = 0.0;
159      for (int i = begin + 1; i < route.Count; i++) {
160        t += Distance(route[i - 1], route[i]);
161        if (route[i] == 0) {
162          if (t < DueTimeParameter.ActualValue[0]) return true;
163          else return false;
164        } else {
165          if (t > DueTimeParameter.ActualValue[route[i]]) return false;
166          t = Math.Max(ReadyTimeParameter.ActualValue[route[i]], t);
167          t += ServiceTimeParameter.ActualValue[route[i]];
168        }
169      }
170      return true;
171    }
172
173    private bool SubrouteLoadOK(List<int> route, int begin) {
174      double o = 0.0;
175      for (int i = begin + 1; i < route.Count; i++) {
176        if (route[i] == 0) return (o < CapacityParameter.ActualValue.Value);
177        else {
178          o += DemandParameter.ActualValue[route[i]];
179        }
180      }
181      return (o < CapacityParameter.ActualValue.Value);
182    }
183
184    protected override List<int> CreateSolution() {
185      double alpha, beta, gamma;
186      alpha = N(Alpha.Value.Value, Math.Sqrt(AlphaVariance.Value.Value), RandomParameter.ActualValue);
187      beta = N(Beta.Value.Value, Math.Sqrt(BetaVariance.Value.Value), RandomParameter.ActualValue);
188      gamma = N(Gamma.Value.Value, Math.Sqrt(GammaVariance.Value.Value), RandomParameter.ActualValue);
189
190      double x0 = CoordinatesParameter.ActualValue[0, 0];
191      double y0 = CoordinatesParameter.ActualValue[0, 1];
192      double distance = 0;
193      double cost = 0;
194      double minimumCost = double.MaxValue;
195      List<int> unroutedList = new List<int>();
196      List<double> costList = new List<double>();
197      int index;
198      int indexOfMinimumCost = -1;
199      int indexOfCustomer = -1;
200
201      /*-----------------------------------------------------------------------------
202       * generate cost list
203       *-----------------------------------------------------------------------------
204       */
205      for (int i = 1; i <= Cities; i++) {
206        distance = Distance(i, 0);
207        if (CoordinatesParameter.ActualValue[i, 0] < x0) distance = -distance;
208        cost = -alpha * distance + // distance 0 <-> City[i]
209                 beta * (DueTimeParameter.ActualValue[i]) + // latest arrival time
210                 gamma * (Math.Asin((CoordinatesParameter.ActualValue[i, 1] - y0) / distance) / 360 * distance); // polar angle
211
212        index = 0;
213        while (index < costList.Count && costList[index] < cost) index++;
214        costList.Insert(index, cost);
215        unroutedList.Insert(index, i);
216      }
217
218      /*------------------------------------------------------------------------------
219       * route customers according to cost list
220       *------------------------------------------------------------------------------
221       */
222      int routeIndex = 0;
223      int currentRoute = 0;
224      int c;
225      int customer = -1;
226      int subTourCount = 1;
227      List<int> route = new List<int>(Cities + VehiclesParameter.ActualValue.Value - 1);
228      minimumCost = double.MaxValue;
229      indexOfMinimumCost = -1;
230      route.Add(0);
231      route.Add(0);
232      route.Insert(1, unroutedList[0]);
233      unroutedList.RemoveAt(0);
234      currentRoute = routeIndex;
235      routeIndex++;
236
237      do {
238        for (c = 0; c < unroutedList.Count; c++) {
239          for (int i = currentRoute + 1; i < route.Count; i++) {
240            route.Insert(i, (int)unroutedList[c]);
241            if (route[currentRoute] != 0) { throw new Exception("currentRoute not depot"); }
242            cost = TravelDistance(route, currentRoute);
243            if (cost < minimumCost && SubrouteConstraintsOK(route, currentRoute)) {
244              minimumCost = cost;
245              indexOfMinimumCost = i;
246              customer = (int)unroutedList[c];
247              indexOfCustomer = c;
248            }
249            route.RemoveAt(i);
250          }
251        }
252        // insert customer if found
253        if (indexOfMinimumCost != -1) {
254          route.Insert(indexOfMinimumCost, customer);
255          routeIndex++;
256          unroutedList.RemoveAt(indexOfCustomer);
257          costList.RemoveAt(indexOfCustomer);
258        } else { // no feasible customer found
259          routeIndex++;
260          route.Insert(routeIndex, 0);
261          currentRoute = routeIndex;
262          route.Insert(route.Count - 1, (int)unroutedList[0]);
263          unroutedList.RemoveAt(0);
264          routeIndex++;
265          subTourCount++;
266        }
267        // reset minimum   
268        minimumCost = double.MaxValue;
269        indexOfMinimumCost = -1;
270        indexOfCustomer = -1;
271        customer = -1;
272      } while (unroutedList.Count > 0);
273      while (route.Count < Cities + VehiclesParameter.ActualValue.Value - 1)
274        route.Add(0);
275
276      return route;
277    }
278  }
279}
Note: See TracBrowser for help on using the repository browser.