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 System.Linq;
|
---|
25 | using System.Text;
|
---|
26 | using HeuristicLab.Problems.VehicleRouting.Interfaces;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 | using HeuristicLab.Core;
|
---|
29 | using HeuristicLab.Parameters;
|
---|
30 | using HeuristicLab.Data;
|
---|
31 | using HeuristicLab.Optimization;
|
---|
32 | using HeuristicLab.PluginInfrastructure;
|
---|
33 | using HeuristicLab.Problems.VehicleRouting.Variants;
|
---|
34 | using HeuristicLab.Problems.VehicleRouting.Encodings;
|
---|
35 | using HeuristicLab.Common;
|
---|
36 |
|
---|
37 | namespace HeuristicLab.Problems.VehicleRouting.ProblemInstances {
|
---|
38 | [Item("MDCVRPTWEvaluator", "Represents a multi depot CVRPTW evaluator.")]
|
---|
39 | [StorableClass]
|
---|
40 | public class MDCVRPTWEvaluator: MDCVRPEvaluator {
|
---|
41 | public ILookupParameter<DoubleValue> TardinessParameter {
|
---|
42 | get { return (ILookupParameter<DoubleValue>)Parameters["Tardiness"]; }
|
---|
43 | }
|
---|
44 |
|
---|
45 | public ILookupParameter<DoubleValue> TravelTimeParameter {
|
---|
46 | get { return (ILookupParameter<DoubleValue>)Parameters["TravelTime"]; }
|
---|
47 | }
|
---|
48 |
|
---|
49 | protected override VRPEvaluation CreateTourEvaluation() {
|
---|
50 | return new CVRPTWEvaluation();
|
---|
51 | }
|
---|
52 |
|
---|
53 | protected override void EvaluateTour(VRPEvaluation eval, IVRPProblemInstance instance, Tour tour, IVRPEncoding solution) {
|
---|
54 | TourInsertionInfo tourInfo = new TourInsertionInfo(solution.GetVehicleAssignment(solution.GetTourIndex(tour)));
|
---|
55 | eval.InsertionInfo.AddTourInsertionInfo(tourInfo);
|
---|
56 |
|
---|
57 | IHeterogenousCapacitatedProblemInstance cvrpInstance = instance as IHeterogenousCapacitatedProblemInstance;
|
---|
58 | DoubleArray demand = instance.Demand;
|
---|
59 |
|
---|
60 | ITimeWindowedProblemInstance vrptw = instance as ITimeWindowedProblemInstance;
|
---|
61 | DoubleArray dueTime = vrptw.DueTime;
|
---|
62 | DoubleArray readyTime = vrptw.ReadyTime;
|
---|
63 | DoubleArray serviceTimes = vrptw.ServiceTime;
|
---|
64 |
|
---|
65 | IMultiDepotProblemInstance mdp = instance as IMultiDepotProblemInstance;
|
---|
66 | IntArray vehicleAssignment = mdp.VehicleDepotAssignment;
|
---|
67 | int depots = mdp.Depots.Value;
|
---|
68 |
|
---|
69 | double time = 0.0;
|
---|
70 | double waitingTime = 0.0;
|
---|
71 | double serviceTime = 0.0;
|
---|
72 | double tardiness = 0.0;
|
---|
73 | double delivered = 0.0;
|
---|
74 | double overweight = 0.0;
|
---|
75 | double distance = 0.0;
|
---|
76 |
|
---|
77 | int tourIndex = solution.GetTourIndex(tour);
|
---|
78 | int vehicle = solution.GetVehicleAssignment(tourIndex);
|
---|
79 | int depot = vehicleAssignment[vehicle];
|
---|
80 |
|
---|
81 | double capacity = cvrpInstance.Capacity[vehicle];
|
---|
82 | for (int i = 0; i < tour.Stops.Count; i++) {
|
---|
83 | delivered += instance.GetDemand(tour.Stops[i]);
|
---|
84 | }
|
---|
85 |
|
---|
86 | double spareCapacity = capacity - delivered;
|
---|
87 |
|
---|
88 | double tourStartTime = vrptw.ReadyTime[depot];
|
---|
89 | time = tourStartTime;
|
---|
90 |
|
---|
91 | //simulate a tour, start and end at depot
|
---|
92 | for (int i = 0; i <= tour.Stops.Count; i++) {
|
---|
93 | int start = 0;
|
---|
94 | if (i > 0)
|
---|
95 | start = tour.Stops[i - 1];
|
---|
96 | int end = 0;
|
---|
97 | if (i < tour.Stops.Count)
|
---|
98 | end = tour.Stops[i];
|
---|
99 |
|
---|
100 | //drive there
|
---|
101 | double currentDistace = vrptw.GetDistance(start, end, solution);
|
---|
102 | time += currentDistace;
|
---|
103 | distance += currentDistace;
|
---|
104 |
|
---|
105 | double arrivalTime = time;
|
---|
106 |
|
---|
107 | int endIndex;
|
---|
108 | if (end == 0)
|
---|
109 | endIndex = depot;
|
---|
110 | else
|
---|
111 | endIndex = end + depots - 1;
|
---|
112 |
|
---|
113 | //check if it was serviced on time
|
---|
114 | if (time > dueTime[endIndex])
|
---|
115 | tardiness += time - dueTime[endIndex];
|
---|
116 |
|
---|
117 | //wait
|
---|
118 | double currentWaitingTime = 0.0;
|
---|
119 | if (time < readyTime[endIndex])
|
---|
120 | currentWaitingTime = readyTime[endIndex] - time;
|
---|
121 |
|
---|
122 | double waitTime = readyTime[endIndex] - time;
|
---|
123 |
|
---|
124 | waitingTime += currentWaitingTime;
|
---|
125 | time += currentWaitingTime;
|
---|
126 |
|
---|
127 | double spareTime = dueTime[endIndex] - time;
|
---|
128 |
|
---|
129 | //service
|
---|
130 | double currentServiceTime = 0;
|
---|
131 | if(end > 0)
|
---|
132 | currentServiceTime = serviceTimes[end - 1];
|
---|
133 | serviceTime += currentServiceTime;
|
---|
134 | time += currentServiceTime;
|
---|
135 |
|
---|
136 | CVRPTWInsertionInfo stopInfo = new CVRPTWInsertionInfo(start, end, spareCapacity, tourStartTime, arrivalTime, time, spareTime, waitTime);
|
---|
137 | tourInfo.AddStopInsertionInfo(stopInfo);
|
---|
138 | }
|
---|
139 |
|
---|
140 | eval.Quality += instance.FleetUsageFactor.Value;
|
---|
141 | eval.Quality += instance.DistanceFactor.Value * distance;
|
---|
142 | eval.Distance += distance;
|
---|
143 | eval.VehicleUtilization += 1;
|
---|
144 |
|
---|
145 | if (delivered > capacity) {
|
---|
146 | overweight = delivered - capacity;
|
---|
147 | }
|
---|
148 |
|
---|
149 | (eval as CVRPEvaluation).Overload += overweight;
|
---|
150 | double tourPenalty = 0;
|
---|
151 | double penalty = overweight * cvrpInstance.OverloadPenalty.Value;
|
---|
152 | eval.Penalty += penalty;
|
---|
153 | eval.Quality += penalty;
|
---|
154 | tourPenalty += penalty;
|
---|
155 |
|
---|
156 | (eval as CVRPTWEvaluation).Tardiness += tardiness;
|
---|
157 | (eval as CVRPTWEvaluation).TravelTime += time;
|
---|
158 |
|
---|
159 | penalty = tardiness * vrptw.TardinessPenalty.Value;
|
---|
160 | eval.Penalty += penalty;
|
---|
161 | eval.Quality += penalty;
|
---|
162 | tourPenalty += penalty;
|
---|
163 | eval.Quality += time * vrptw.TimeFactor.Value;
|
---|
164 | tourInfo.Penalty = tourPenalty;
|
---|
165 | }
|
---|
166 |
|
---|
167 | protected override double GetTourInsertionCosts(IVRPProblemInstance instance, IVRPEncoding solution, TourInsertionInfo tourInsertionInfo, int index, int customer,
|
---|
168 | out bool feasible) {
|
---|
169 | CVRPTWInsertionInfo insertionInfo = tourInsertionInfo.GetStopInsertionInfo(index) as CVRPTWInsertionInfo;
|
---|
170 |
|
---|
171 | double costs = 0;
|
---|
172 | feasible = tourInsertionInfo.Penalty < double.Epsilon;
|
---|
173 |
|
---|
174 | ICapacitatedProblemInstance cvrp = instance as ICapacitatedProblemInstance;
|
---|
175 | double overloadPenalty = cvrp.OverloadPenalty.Value;
|
---|
176 |
|
---|
177 | ITimeWindowedProblemInstance vrptw = instance as ITimeWindowedProblemInstance;
|
---|
178 | DoubleArray dueTime = vrptw.DueTime;
|
---|
179 | DoubleArray readyTime = vrptw.ReadyTime;
|
---|
180 | DoubleArray serviceTimes = vrptw.ServiceTime;
|
---|
181 | double tardinessPenalty = vrptw.TardinessPenalty.Value;
|
---|
182 |
|
---|
183 | IMultiDepotProblemInstance mdp = instance as IMultiDepotProblemInstance;
|
---|
184 | int depots = mdp.Depots.Value;
|
---|
185 |
|
---|
186 | double startDistance, endDistance;
|
---|
187 | costs += instance.GetInsertionDistance(insertionInfo.Start, customer, insertionInfo.End, solution, out startDistance, out endDistance);
|
---|
188 |
|
---|
189 | double demand = instance.GetDemand(customer);
|
---|
190 | if (demand > insertionInfo.SpareCapacity) {
|
---|
191 | feasible = false;
|
---|
192 |
|
---|
193 | if(insertionInfo.SpareCapacity >= 0)
|
---|
194 | costs += (demand - insertionInfo.SpareCapacity) * overloadPenalty;
|
---|
195 | else
|
---|
196 | costs += demand * overloadPenalty;
|
---|
197 | }
|
---|
198 |
|
---|
199 | double time = 0;
|
---|
200 | double tardiness = 0;
|
---|
201 |
|
---|
202 | if (index > 0)
|
---|
203 | time = (tourInsertionInfo.GetStopInsertionInfo(index - 1) as CVRPTWInsertionInfo).LeaveTime;
|
---|
204 | else
|
---|
205 | time = insertionInfo.TourStartTime;
|
---|
206 |
|
---|
207 | time += startDistance;
|
---|
208 |
|
---|
209 | int customerIndex = customer + depots - 1;
|
---|
210 |
|
---|
211 | if (time > dueTime[customerIndex]) {
|
---|
212 | tardiness += time - dueTime[customerIndex];
|
---|
213 | }
|
---|
214 | if (time < readyTime[customerIndex])
|
---|
215 | time += readyTime[customerIndex] - time;
|
---|
216 | time += serviceTimes[customer - 1];
|
---|
217 | time += endDistance;
|
---|
218 |
|
---|
219 | double additionalTime = time - (tourInsertionInfo.GetStopInsertionInfo(index) as CVRPTWInsertionInfo).ArrivalTime;
|
---|
220 | for (int i = index; i < tourInsertionInfo.GetStopCount(); i++) {
|
---|
221 | CVRPTWInsertionInfo nextStop = tourInsertionInfo.GetStopInsertionInfo(i) as CVRPTWInsertionInfo;
|
---|
222 |
|
---|
223 | if (additionalTime < 0) {
|
---|
224 | //arrive earlier than before
|
---|
225 | //wait probably
|
---|
226 | if (nextStop.WaitingTime < 0) {
|
---|
227 | double wait = nextStop.WaitingTime - additionalTime;
|
---|
228 | if (wait > 0)
|
---|
229 | additionalTime += wait;
|
---|
230 | } else {
|
---|
231 | additionalTime = 0;
|
---|
232 | }
|
---|
233 |
|
---|
234 | //check due date, decrease tardiness
|
---|
235 | if (nextStop.SpareTime < 0) {
|
---|
236 | costs += Math.Max(nextStop.SpareTime, additionalTime) * tardinessPenalty;
|
---|
237 | }
|
---|
238 | } else {
|
---|
239 | //arrive later than before, probably don't have to wait
|
---|
240 | if (nextStop.WaitingTime > 0) {
|
---|
241 | additionalTime -= Math.Min(additionalTime, nextStop.WaitingTime);
|
---|
242 | }
|
---|
243 |
|
---|
244 | //check due date
|
---|
245 | if (nextStop.SpareTime > 0) {
|
---|
246 | double spare = nextStop.SpareTime - additionalTime;
|
---|
247 | if (spare < 0)
|
---|
248 | tardiness += -spare;
|
---|
249 | } else {
|
---|
250 | tardiness += additionalTime;
|
---|
251 | }
|
---|
252 | }
|
---|
253 | }
|
---|
254 |
|
---|
255 | costs += additionalTime * vrptw.TimeFactor.Value;
|
---|
256 |
|
---|
257 | if (tardiness > 0) {
|
---|
258 | feasible = false;
|
---|
259 | }
|
---|
260 |
|
---|
261 | costs += tardiness * tardinessPenalty;
|
---|
262 |
|
---|
263 | return costs;
|
---|
264 | }
|
---|
265 |
|
---|
266 | protected override void InitResultParameters() {
|
---|
267 | base.InitResultParameters();
|
---|
268 |
|
---|
269 | TardinessParameter.ActualValue = new DoubleValue(0);
|
---|
270 | TravelTimeParameter.ActualValue = new DoubleValue(0);
|
---|
271 | }
|
---|
272 |
|
---|
273 | protected override void SetResultParameters(VRPEvaluation tourEvaluation) {
|
---|
274 | base.SetResultParameters(tourEvaluation);
|
---|
275 |
|
---|
276 | TardinessParameter.ActualValue.Value = (tourEvaluation as CVRPTWEvaluation).Tardiness;
|
---|
277 | TravelTimeParameter.ActualValue.Value = (tourEvaluation as CVRPTWEvaluation).TravelTime;
|
---|
278 | }
|
---|
279 |
|
---|
280 | [StorableConstructor]
|
---|
281 | protected MDCVRPTWEvaluator(bool deserializing) : base(deserializing) { }
|
---|
282 |
|
---|
283 | public MDCVRPTWEvaluator() {
|
---|
284 | Parameters.Add(new LookupParameter<DoubleValue>("Tardiness", "The tardiness."));
|
---|
285 | Parameters.Add(new LookupParameter<DoubleValue>("TravelTime", "The travel time."));
|
---|
286 | }
|
---|
287 |
|
---|
288 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
289 | return new MDCVRPTWEvaluator(this, cloner);
|
---|
290 | }
|
---|
291 |
|
---|
292 | protected MDCVRPTWEvaluator(MDCVRPTWEvaluator original, Cloner cloner)
|
---|
293 | : base(original, cloner) {
|
---|
294 | }
|
---|
295 | }
|
---|
296 | } |
---|