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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
27 | using HeuristicLab.Optimization;
|
---|
28 | using HeuristicLab.Parameters;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 | using HeuristicLab.Problems.VehicleRouting.Variants;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Problems.VehicleRouting.Encodings.Alba {
|
---|
33 | [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.")]
|
---|
34 | [StorableClass]
|
---|
35 | public sealed class PushForwardCreator : DefaultRepresentationCreator, IStochasticOperator, ITimeWindowedOperator {
|
---|
36 | #region IStochasticOperator Members
|
---|
37 | public ILookupParameter<IRandom> RandomParameter {
|
---|
38 | get { return (LookupParameter<IRandom>)Parameters["Random"]; }
|
---|
39 | }
|
---|
40 | #endregion
|
---|
41 |
|
---|
42 | public IValueParameter<DoubleValue> Alpha {
|
---|
43 | get { return (IValueParameter<DoubleValue>)Parameters["Alpha"]; }
|
---|
44 | }
|
---|
45 | public IValueParameter<DoubleValue> AlphaVariance {
|
---|
46 | get { return (IValueParameter<DoubleValue>)Parameters["AlphaVariance"]; }
|
---|
47 | }
|
---|
48 | public IValueParameter<DoubleValue> Beta {
|
---|
49 | get { return (IValueParameter<DoubleValue>)Parameters["Beta"]; }
|
---|
50 | }
|
---|
51 | public IValueParameter<DoubleValue> BetaVariance {
|
---|
52 | get { return (IValueParameter<DoubleValue>)Parameters["BetaVariance"]; }
|
---|
53 | }
|
---|
54 | public IValueParameter<DoubleValue> Gamma {
|
---|
55 | get { return (IValueParameter<DoubleValue>)Parameters["Gamma"]; }
|
---|
56 | }
|
---|
57 | public IValueParameter<DoubleValue> GammaVariance {
|
---|
58 | get { return (IValueParameter<DoubleValue>)Parameters["GammaVariance"]; }
|
---|
59 | }
|
---|
60 |
|
---|
61 | [StorableConstructor]
|
---|
62 | private PushForwardCreator(bool deserializing) : base(deserializing) { }
|
---|
63 |
|
---|
64 | public PushForwardCreator()
|
---|
65 | : base() {
|
---|
66 | Parameters.Add(new LookupParameter<IRandom>("Random", "The pseudo random number generator."));
|
---|
67 | Parameters.Add(new ValueParameter<DoubleValue>("Alpha", "The alpha value.", new DoubleValue(0.7)));
|
---|
68 | Parameters.Add(new ValueParameter<DoubleValue>("AlphaVariance", "The alpha variance.", new DoubleValue(0.5)));
|
---|
69 | Parameters.Add(new ValueParameter<DoubleValue>("Beta", "The beta value.", new DoubleValue(0.1)));
|
---|
70 | Parameters.Add(new ValueParameter<DoubleValue>("BetaVariance", "The beta variance.", new DoubleValue(0.07)));
|
---|
71 | Parameters.Add(new ValueParameter<DoubleValue>("Gamma", "The gamma value.", new DoubleValue(0.2)));
|
---|
72 | Parameters.Add(new ValueParameter<DoubleValue>("GammaVariance", "The gamma variance.", new DoubleValue(0.14)));
|
---|
73 | }
|
---|
74 |
|
---|
75 | // use the Box-Mueller transform in the polar form to generate a N(0,1) random variable out of two uniformly distributed random variables
|
---|
76 | private double Gauss(IRandom random) {
|
---|
77 | double u = 0.0, v = 0.0, s = 0.0;
|
---|
78 | do {
|
---|
79 | u = (random.NextDouble() * 2) - 1;
|
---|
80 | v = (random.NextDouble() * 2) - 1;
|
---|
81 | s = Math.Sqrt(u * u + v * v);
|
---|
82 | } while (s < Double.Epsilon || s > 1);
|
---|
83 | return u * Math.Sqrt((-2.0 * Math.Log(s)) / s);
|
---|
84 | }
|
---|
85 |
|
---|
86 | private double N(double mu, double sigma, IRandom random) {
|
---|
87 | return mu + (sigma * Gauss(random)); // transform the random variable sampled from N(0,1) to N(mu,sigma)
|
---|
88 | }
|
---|
89 |
|
---|
90 | private double Distance(int start, int end) {
|
---|
91 | return ProblemInstance.GetDistance(start, end);
|
---|
92 | }
|
---|
93 |
|
---|
94 | private double TravelDistance(List<int> route, int begin) {
|
---|
95 | double distance = 0;
|
---|
96 | for (int i = begin; i < route.Count - 1 && (i == begin || route[i] != 0); i++) {
|
---|
97 | distance += Distance(route[i], route[i + 1]);
|
---|
98 | }
|
---|
99 | return distance;
|
---|
100 | }
|
---|
101 |
|
---|
102 | private bool SubrouteConstraintsOK(List<int> route, int begin) {
|
---|
103 | Tour subroute = new Tour();
|
---|
104 | subroute.Stops.AddRange(route.GetRange(begin,
|
---|
105 | route.Count - begin));
|
---|
106 |
|
---|
107 | return ProblemInstance.Feasible(subroute);
|
---|
108 | }
|
---|
109 |
|
---|
110 | protected override List<int> CreateSolution() {
|
---|
111 | double alpha, beta, gamma;
|
---|
112 | alpha = N(Alpha.Value.Value, Math.Sqrt(AlphaVariance.Value.Value), RandomParameter.ActualValue);
|
---|
113 | beta = N(Beta.Value.Value, Math.Sqrt(BetaVariance.Value.Value), RandomParameter.ActualValue);
|
---|
114 | gamma = N(Gamma.Value.Value, Math.Sqrt(GammaVariance.Value.Value), RandomParameter.ActualValue);
|
---|
115 |
|
---|
116 | double x0 = ProblemInstance.Coordinates[0, 0];
|
---|
117 | double y0 = ProblemInstance.Coordinates[0, 1];
|
---|
118 | double distance = 0;
|
---|
119 | double cost = 0;
|
---|
120 | double minimumCost = double.MaxValue;
|
---|
121 | List<int> unroutedList = new List<int>();
|
---|
122 | List<double> costList = new List<double>();
|
---|
123 | int index;
|
---|
124 | int indexOfMinimumCost = -1;
|
---|
125 | int indexOfCustomer = -1;
|
---|
126 |
|
---|
127 | /*-----------------------------------------------------------------------------
|
---|
128 | * generate cost list
|
---|
129 | *-----------------------------------------------------------------------------
|
---|
130 | */
|
---|
131 | for (int i = 1; i <= ProblemInstance.Cities.Value; i++) {
|
---|
132 | distance = Distance(i, 0);
|
---|
133 | if (ProblemInstance.Coordinates[i, 0] < x0) distance = -distance;
|
---|
134 |
|
---|
135 | cost = -alpha * distance + // distance 0 <-> City[i]
|
---|
136 | beta * (ProblemInstance as ITimeWindowedProblemInstance).DueTime[i] + // latest arrival time
|
---|
137 | gamma * (Math.Asin((ProblemInstance.Coordinates[i, 1] - y0) / distance) / 360 * distance); // polar angle
|
---|
138 |
|
---|
139 | index = 0;
|
---|
140 | while (index < costList.Count && costList[index] < cost) index++;
|
---|
141 | costList.Insert(index, cost);
|
---|
142 | unroutedList.Insert(index, i);
|
---|
143 | }
|
---|
144 |
|
---|
145 | /*------------------------------------------------------------------------------
|
---|
146 | * route customers according to cost list
|
---|
147 | *------------------------------------------------------------------------------
|
---|
148 | */
|
---|
149 | int routeIndex = 0;
|
---|
150 | int currentRoute = 0;
|
---|
151 | int c;
|
---|
152 | int customer = -1;
|
---|
153 | int subTourCount = 1;
|
---|
154 | List<int> route = new List<int>(ProblemInstance.Cities.Value + ProblemInstance.Vehicles.Value - 1);
|
---|
155 | minimumCost = double.MaxValue;
|
---|
156 | indexOfMinimumCost = -1;
|
---|
157 | route.Add(0);
|
---|
158 | route.Add(0);
|
---|
159 | route.Insert(1, unroutedList[0]);
|
---|
160 | unroutedList.RemoveAt(0);
|
---|
161 | currentRoute = routeIndex;
|
---|
162 | routeIndex++;
|
---|
163 |
|
---|
164 | do {
|
---|
165 | for (c = 0; c < unroutedList.Count; c++) {
|
---|
166 | for (int i = currentRoute + 1; i < route.Count; i++) {
|
---|
167 | route.Insert(i, (int)unroutedList[c]);
|
---|
168 | if (route[currentRoute] != 0) { throw new Exception("currentRoute not depot"); }
|
---|
169 | cost = TravelDistance(route, currentRoute);
|
---|
170 | if (cost < minimumCost && SubrouteConstraintsOK(route, currentRoute)) {
|
---|
171 | minimumCost = cost;
|
---|
172 | indexOfMinimumCost = i;
|
---|
173 | customer = (int)unroutedList[c];
|
---|
174 | indexOfCustomer = c;
|
---|
175 | }
|
---|
176 | route.RemoveAt(i);
|
---|
177 | }
|
---|
178 | }
|
---|
179 | // insert customer if found
|
---|
180 | if (indexOfMinimumCost != -1) {
|
---|
181 | route.Insert(indexOfMinimumCost, customer);
|
---|
182 | routeIndex++;
|
---|
183 | unroutedList.RemoveAt(indexOfCustomer);
|
---|
184 | costList.RemoveAt(indexOfCustomer);
|
---|
185 | } else { // no feasible customer found
|
---|
186 | routeIndex++;
|
---|
187 | route.Insert(routeIndex, 0);
|
---|
188 | currentRoute = routeIndex;
|
---|
189 | route.Insert(route.Count - 1, (int)unroutedList[0]);
|
---|
190 | unroutedList.RemoveAt(0);
|
---|
191 | routeIndex++;
|
---|
192 | subTourCount++;
|
---|
193 | }
|
---|
194 | // reset minimum
|
---|
195 | minimumCost = double.MaxValue;
|
---|
196 | indexOfMinimumCost = -1;
|
---|
197 | indexOfCustomer = -1;
|
---|
198 | customer = -1;
|
---|
199 | } while (unroutedList.Count > 0);
|
---|
200 | while (route.Count < ProblemInstance.Cities.Value + ProblemInstance.Vehicles.Value)
|
---|
201 | route.Add(0);
|
---|
202 |
|
---|
203 | return route;
|
---|
204 | }
|
---|
205 | }
|
---|
206 | }
|
---|