Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Creators/PushForwardInsertionCreator.cs @ 6838

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

Updated interface of evaluator - added individual (#1177)

File size: 9.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;
30using HeuristicLab.Common;
31using HeuristicLab.Problems.VehicleRouting.Interfaces;
32using HeuristicLab.Problems.VehicleRouting.ProblemInstances;
33using HeuristicLab.Problems.VehicleRouting.Variants;
34
35namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin {
36  [Item("PushForwardInsertionCreator", "Creates a randomly initialized VRP solution.")]
37  [StorableClass]
38  public sealed class PushForwardInsertionCreator : PotvinCreator, IStochasticOperator {
39    #region IStochasticOperator Members
40    public ILookupParameter<IRandom> RandomParameter {
41      get { return (LookupParameter<IRandom>)Parameters["Random"]; }
42    }
43    #endregion
44
45    public IValueParameter<DoubleValue> Alpha {
46      get { return (IValueParameter<DoubleValue>)Parameters["Alpha"]; }
47    }
48    public IValueParameter<DoubleValue> AlphaVariance {
49      get { return (IValueParameter<DoubleValue>)Parameters["AlphaVariance"]; }
50    }
51    public IValueParameter<DoubleValue> Beta {
52      get { return (IValueParameter<DoubleValue>)Parameters["Beta"]; }
53    }
54    public IValueParameter<DoubleValue> BetaVariance {
55      get { return (IValueParameter<DoubleValue>)Parameters["BetaVariance"]; }
56    }
57    public IValueParameter<DoubleValue> Gamma {
58      get { return (IValueParameter<DoubleValue>)Parameters["Gamma"]; }
59    }
60    public IValueParameter<DoubleValue> GammaVariance {
61      get { return (IValueParameter<DoubleValue>)Parameters["GammaVariance"]; }
62    }
63
64    [StorableConstructor]
65    private PushForwardInsertionCreator(bool deserializing) : base(deserializing) { }
66
67    public PushForwardInsertionCreator()
68      : base() {
69      Parameters.Add(new LookupParameter<IRandom>("Random", "The pseudo random number generator."));
70      Parameters.Add(new ValueParameter<DoubleValue>("Alpha", "The alpha value.", new DoubleValue(0.7)));
71      Parameters.Add(new ValueParameter<DoubleValue>("AlphaVariance", "The alpha variance.", new DoubleValue(0.5)));
72      Parameters.Add(new ValueParameter<DoubleValue>("Beta", "The beta value.", new DoubleValue(0.1)));
73      Parameters.Add(new ValueParameter<DoubleValue>("BetaVariance", "The beta variance.", new DoubleValue(0.07)));
74      Parameters.Add(new ValueParameter<DoubleValue>("Gamma", "The gamma value.", new DoubleValue(0.2)));
75      Parameters.Add(new ValueParameter<DoubleValue>("GammaVariance", "The gamma variance.", new DoubleValue(0.14)));
76    }
77
78    public override IDeepCloneable Clone(Cloner cloner) {
79      return new PushForwardInsertionCreator(this, cloner);
80    }
81
82    private PushForwardInsertionCreator(PushForwardInsertionCreator original, Cloner cloner)
83      : base(original, cloner) {
84    }
85
86    // use the Box-Mueller transform in the polar form to generate a N(0,1) random variable out of two uniformly distributed random variables
87    private static double Gauss(IRandom random) {
88      double u = 0.0, v = 0.0, s = 0.0;
89      do {
90        u = (random.NextDouble() * 2) - 1;
91        v = (random.NextDouble() * 2) - 1;
92        s = Math.Sqrt(u * u + v * v);
93      } while (s < Double.Epsilon || s > 1);
94      return u * Math.Sqrt((-2.0 * Math.Log(s)) / s);
95    }
96
97    private static double N(double mu, double sigma, IRandom random) {
98      return mu + (sigma * Gauss(random)); // transform the random variable sampled from N(0,1) to N(mu,sigma)
99    }
100
101    private static double Distance(IVRPProblemInstance problemInstance, int start, int end) {
102      return problemInstance.GetDistance(start, end);
103    }
104
105    private static PotvinEncoding CreateSolution(IVRPProblemInstance problemInstance, IRandom random,
106      double alphaValue = 0.7, double betaValue = 0.1, double gammaValue = 0.2,
107      double alphaVariance = 0.5, double betaVariance = 0.07, double gammaVariance = 0.14) {
108      PotvinEncoding result = new PotvinEncoding(problemInstance);
109
110      double alpha, beta, gamma;
111      alpha = N(alphaValue, Math.Sqrt(alphaVariance), random);
112      beta = N(betaValue, Math.Sqrt(betaVariance), random);
113      gamma = N(gammaValue, Math.Sqrt(gammaVariance), random);
114
115      double x0 = problemInstance.Coordinates[0, 0];
116      double y0 = problemInstance.Coordinates[0, 1];
117      double distance = 0;
118      double cost = 0;
119      List<int> unroutedList = new List<int>();
120      List<double> costList = new List<double>();
121
122      IPickupAndDeliveryProblemInstance pdp = problemInstance as IPickupAndDeliveryProblemInstance;
123
124      /*-----------------------------------------------------------------------------
125       * generate cost list
126       *-----------------------------------------------------------------------------
127       */
128      for (int i = 1; i <= problemInstance.Cities.Value; i++) {
129        //only insert sources
130        if (pdp == null || problemInstance.Demand[i] >= 0) {
131          distance = Distance(problemInstance, i, 0);
132          if (problemInstance.Coordinates[i, 0] < x0) distance = -distance;
133
134          cost = -alpha * distance + // distance 0 <-> City[i]
135                 beta * (problemInstance as ITimeWindowedProblemInstance).DueTime[i] + // latest arrival time
136                 gamma * (Math.Asin((problemInstance.Coordinates[i, 1] - y0) / distance) / 360 * distance); // polar angle
137
138          int index = 0;
139          while (index < costList.Count && costList[index] < cost) index++;
140
141          costList.Insert(index, cost);
142
143          unroutedList.Insert(index, i);
144        }
145      }
146
147      double minimumCost = double.MaxValue;
148      int indexOfMinimumCost = -1;
149      int indexOfMinimumCost2 = -1;
150      int indexOfCustomer = -1;
151
152      Tour tour = new Tour();
153      result.Tours.Add(tour);
154      tour.Stops.Add(unroutedList[0]);
155      unroutedList.RemoveAt(0);
156
157      while (unroutedList.Count > 0) {
158        for (int c = 0; c < unroutedList.Count; c++) {
159          VRPEvaluation eval =
160          problemInstance.EvaluateTour(tour, result);
161          double originalCosts = eval.Quality;
162          int customer = unroutedList[c];
163
164          for (int i = 0; i <= tour.Stops.Count; i++) {
165            tour.Stops.Insert(i, customer);
166            eval = problemInstance.EvaluateTour(tour, result);
167            double tourCost = eval.Quality - originalCosts;
168
169            if (pdp != null) {
170              for (int j = i + 1; j <= tour.Stops.Count; j++) {
171                bool feasible;
172                cost = tourCost +
173                  problemInstance.GetInsertionCosts(eval, pdp.PickupDeliveryLocation[unroutedList[c]], 0, j, out feasible);
174                if (cost < minimumCost && feasible) {
175                  minimumCost = cost;
176                  indexOfMinimumCost = i;
177                  indexOfMinimumCost2 = j;
178                  indexOfCustomer = c;
179                }
180              }
181            } else {
182              cost = tourCost;
183              bool feasible = problemInstance.Feasible(eval);
184              if (cost < minimumCost && feasible) {
185                minimumCost = cost;
186                indexOfMinimumCost = i;
187                indexOfCustomer = c;
188              }
189            }
190
191            tour.Stops.RemoveAt(i);           
192          }
193        }
194
195        // insert customer if found
196        if (indexOfMinimumCost != -1) {
197          tour.Stops.Insert(indexOfMinimumCost, unroutedList[indexOfCustomer]);
198
199          if (pdp != null) {
200            tour.Stops.Insert(indexOfMinimumCost2, pdp.PickupDeliveryLocation[unroutedList[indexOfCustomer]]);
201          }
202
203          unroutedList.RemoveAt(indexOfCustomer);
204          costList.RemoveAt(indexOfCustomer);
205        } else { // no feasible customer found
206          tour = new Tour();
207          result.Tours.Add(tour);
208          tour.Stops.Add(unroutedList[0]);
209          unroutedList.RemoveAt(0);
210        }
211        // reset minimum   
212        minimumCost = double.MaxValue;
213        indexOfMinimumCost = -1;
214        indexOfMinimumCost2 = -1;
215        indexOfCustomer = -1;
216      }
217
218      return result;
219    }
220
221    public override IOperation Apply() {
222      VRPToursParameter.ActualValue = CreateSolution(ProblemInstance, RandomParameter.ActualValue,
223        Alpha.Value.Value, Beta.Value.Value, Gamma.Value.Value,
224        AlphaVariance.Value.Value, BetaVariance.Value.Value, GammaVariance.Value.Value);
225
226      return base.Apply();
227    }
228  }
229}
Note: See TracBrowser for help on using the repository browser.