Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Problems.VehicleRouting/3.4/ProblemInstances/SingleDepotVRP/CVRP/CVRPTW/CVRPPDTW/CVRPPDTWEvaluator.cs

Last change on this file was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 11.7 KB
RevLine 
[6710]1#region License Information
2/* HeuristicLab
[17180]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[6710]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;
[8053]24using HeuristicLab.Common;
[6710]25using HeuristicLab.Core;
[8053]26using HeuristicLab.Data;
[6710]27using HeuristicLab.Parameters;
[16565]28using HEAL.Attic;
[8053]29using HeuristicLab.Problems.VehicleRouting.Interfaces;
[6710]30using HeuristicLab.Problems.VehicleRouting.Variants;
31
32namespace HeuristicLab.Problems.VehicleRouting.ProblemInstances {
33  [Item("CVRPPDTWEvaluator", "Represents a single depot CVRPPDTW evaluator.")]
[16565]34  [StorableType("6324F32C-727E-4593-BB21-4625755F844A")]
[8053]35  public class CVRPPDTWEvaluator : CVRPTWEvaluator {
[6710]36    public ILookupParameter<IntValue> PickupViolationsParameter {
37      get { return (ILookupParameter<IntValue>)Parameters["PickupViolations"]; }
38    }
39
40    protected override VRPEvaluation CreateTourEvaluation() {
41      return new CVRPPDTWEvaluation();
42    }
43
[6838]44    protected override void EvaluateTour(VRPEvaluation eval, IVRPProblemInstance instance, Tour tour, IVRPEncoding solution) {
[6883]45      TourInsertionInfo tourInfo = new TourInsertionInfo(solution.GetVehicleAssignment(solution.GetTourIndex(tour)));
[6752]46      eval.InsertionInfo.AddTourInsertionInfo(tourInfo);
[7276]47      double originalQuality = eval.Quality;
[6752]48
[6710]49      IHomogenousCapacitatedProblemInstance cvrpInstance = instance as IHomogenousCapacitatedProblemInstance;
50      DoubleArray demand = instance.Demand;
51
52      ITimeWindowedProblemInstance vrptw = instance as ITimeWindowedProblemInstance;
53      DoubleArray dueTime = vrptw.DueTime;
54      DoubleArray readyTime = vrptw.ReadyTime;
55      DoubleArray serviceTimes = vrptw.ServiceTime;
56
57      IPickupAndDeliveryProblemInstance pdp = instance as IPickupAndDeliveryProblemInstance;
58      IntArray pickupDeliveryLocation = pdp.PickupDeliveryLocation;
59
60      double capacity = cvrpInstance.Capacity.Value;
61
62      double time = 0.0;
63      double waitingTime = 0.0;
64      double serviceTime = 0.0;
65      double tardiness = 0.0;
66      double overweight = 0.0;
67      double distance = 0.0;
68
69      double currentLoad = 0.0;
70      Dictionary<int, bool> stops = new Dictionary<int, bool>();
71      int pickupViolations = 0;
72
[6855]73      double tourStartTime = readyTime[0];
74      time = tourStartTime;
75
[6710]76      //simulate a tour, start and end at depot
77      for (int i = 0; i <= tour.Stops.Count; i++) {
78        int start = 0;
79        if (i > 0)
80          start = tour.Stops[i - 1];
81        int end = 0;
82        if (i < tour.Stops.Count)
83          end = tour.Stops[i];
84
85        //drive there
[6851]86        double currentDistace = vrptw.GetDistance(start, end, solution);
[6710]87        time += currentDistace;
88        distance += currentDistace;
89
[6752]90        double arrivalTime = time;
91
[6710]92        //check if it was serviced on time
93        if (time > dueTime[end])
94          tardiness += time - dueTime[end];
95
96        //wait
97        double currentWaitingTime = 0.0;
98        if (time < readyTime[end])
99          currentWaitingTime = readyTime[end] - time;
[6752]100
101        double waitTime = readyTime[end] - time;
102
[6710]103        waitingTime += currentWaitingTime;
104        time += currentWaitingTime;
105
[6752]106        double spareTime = dueTime[end] - time;
107
[6710]108        //service
109        double currentServiceTime = serviceTimes[end];
110        serviceTime += currentServiceTime;
111        time += currentServiceTime;
112
113        //Pickup / deliver
[6752]114        double arrivalSpareCapacity = capacity - currentLoad;
115
[8053]116        bool validPickupDelivery =
[6710]117          validPickupDelivery =
118          ((demand[end] >= 0) ||
119           (stops.ContainsKey(pickupDeliveryLocation[end])));
120
121        if (validPickupDelivery) {
122          currentLoad += demand[end];
123        } else {
124          pickupViolations++;
125        }
126
[6752]127        if (currentLoad > capacity)
128          overweight += currentLoad - capacity;
129
130        double spareCapacity = capacity - currentLoad;
[8053]131        CVRPPDTWInsertionInfo stopInfo = new CVRPPDTWInsertionInfo(start, end, spareCapacity, tourStartTime,
[6855]132          arrivalTime, time, spareTime, waitTime, new List<int>(stops.Keys), arrivalSpareCapacity);
[6752]133        tourInfo.AddStopInsertionInfo(stopInfo);
134
[6710]135        stops.Add(end, true);
136      }
137
138      eval.Quality += instance.FleetUsageFactor.Value;
139      eval.Quality += instance.DistanceFactor.Value * distance;
140      eval.Distance += distance;
141      eval.VehicleUtilization += 1;
142
143      (eval as CVRPEvaluation).Overload += overweight;
[6752]144      double tourPenalty = 0;
[6710]145      double penalty = overweight * cvrpInstance.OverloadPenalty.Value;
146      eval.Penalty += penalty;
147      eval.Quality += penalty;
[6752]148      tourPenalty += penalty;
[6710]149
150      (eval as CVRPTWEvaluation).Tardiness += tardiness;
151      (eval as CVRPTWEvaluation).TravelTime += time;
152
153      penalty = tardiness * vrptw.TardinessPenalty.Value;
154      eval.Penalty += penalty;
155      eval.Quality += penalty;
[6752]156      tourPenalty += penalty;
[6710]157
158      (eval as CVRPPDTWEvaluation).PickupViolations += pickupViolations;
159      penalty = pickupViolations * pdp.PickupViolationPenalty.Value;
160      eval.Penalty += penalty;
[6752]161      tourPenalty += penalty;
[6710]162
163      eval.Quality += penalty;
164      eval.Quality += time * vrptw.TimeFactor.Value;
[6752]165      tourInfo.Penalty = tourPenalty;
[7276]166      tourInfo.Quality = eval.Quality - originalQuality;
[6710]167    }
168
[8053]169    protected override double GetTourInsertionCosts(IVRPProblemInstance instance, IVRPEncoding solution, TourInsertionInfo tourInsertionInfo, int index, int customer,
[6752]170      out bool feasible) {
171      CVRPPDTWInsertionInfo insertionInfo = tourInsertionInfo.GetStopInsertionInfo(index) as CVRPPDTWInsertionInfo;
172
173      double costs = 0;
174      feasible = tourInsertionInfo.Penalty < double.Epsilon;
175      bool tourFeasible = true;
176
177      ICapacitatedProblemInstance cvrp = instance as ICapacitatedProblemInstance;
178      double overloadPenalty = cvrp.OverloadPenalty.Value;
179
180      ITimeWindowedProblemInstance vrptw = instance as ITimeWindowedProblemInstance;
181      DoubleArray dueTime = vrptw.DueTime;
182      DoubleArray readyTime = vrptw.ReadyTime;
183      DoubleArray serviceTimes = vrptw.ServiceTime;
184      double tardinessPenalty = vrptw.TardinessPenalty.Value;
185
186      IPickupAndDeliveryProblemInstance pdp = instance as IPickupAndDeliveryProblemInstance;
187      IntArray pickupDeliveryLocation = pdp.PickupDeliveryLocation;
188      double pickupPenalty = pdp.PickupViolationPenalty.Value;
189
[6851]190      double distance = instance.GetDistance(insertionInfo.Start, insertionInfo.End, solution);
[6752]191      double newDistance =
[6851]192        instance.GetDistance(insertionInfo.Start, customer, solution) +
193        instance.GetDistance(customer, insertionInfo.End, solution);
[6752]194      costs += instance.DistanceFactor.Value * (newDistance - distance);
195
196      double demand = instance.Demand[customer];
197      if (demand > insertionInfo.ArrivalSpareCapacity) {
198        tourFeasible = feasible = false;
199        if (insertionInfo.ArrivalSpareCapacity >= 0)
200          costs += (demand - insertionInfo.ArrivalSpareCapacity) * overloadPenalty;
201        else
202          costs += demand * overloadPenalty;
203      }
204      int destination = pickupDeliveryLocation[customer];
205
206      bool validPickup = true;
207      if (demand < 0 && !insertionInfo.Visited.Contains(destination)) {
208        tourFeasible = feasible = false;
209        validPickup = false;
210        costs += pickupPenalty;
211      }
212
213      double time = 0;
214      double tardiness = 0;
215
216      if (index > 0)
217        time = (tourInsertionInfo.GetStopInsertionInfo(index - 1) as CVRPTWInsertionInfo).LeaveTime;
[6855]218      else
219        time = insertionInfo.TourStartTime;
220
[6851]221      time += instance.GetDistance(insertionInfo.Start, customer, solution);
[6752]222      if (time > dueTime[customer]) {
223        tardiness += time - dueTime[customer];
224      }
225      if (time < readyTime[customer])
226        time += readyTime[customer] - time;
227      time += serviceTimes[customer];
[6851]228      time += instance.GetDistance(customer, insertionInfo.End, solution);
[6752]229
230      double additionalTime = time - (tourInsertionInfo.GetStopInsertionInfo(index) as CVRPTWInsertionInfo).ArrivalTime;
231      for (int i = index; i < tourInsertionInfo.GetStopCount(); i++) {
232        CVRPTWInsertionInfo nextStop = tourInsertionInfo.GetStopInsertionInfo(i) as CVRPTWInsertionInfo;
233
234        if (demand >= 0) {
235          if (nextStop.End == destination) {
236            demand = 0;
237            costs -= pickupPenalty;
238            if (tourInsertionInfo.Penalty == pickupPenalty && tourFeasible)
239              feasible = true;
240          } else if (nextStop.SpareCapacity < 0) {
241            costs += demand * overloadPenalty;
242          } else if (nextStop.SpareCapacity < demand) {
243            tourFeasible = feasible = false;
244            costs += (demand - nextStop.SpareCapacity) * overloadPenalty;
245          }
246        } else if (validPickup) {
247          if (nextStop.SpareCapacity < 0) {
248            costs += Math.Max(demand, nextStop.SpareCapacity) * overloadPenalty;
249          }
250        }
251
252        if (additionalTime < 0) {
253          //arrive earlier than before
254          //wait probably
255          if (nextStop.WaitingTime < 0) {
256            double wait = nextStop.WaitingTime - additionalTime;
257            if (wait > 0)
258              additionalTime += wait;
259          } else {
260            additionalTime = 0;
261          }
262
263          //check due date, decrease tardiness
264          if (nextStop.SpareTime < 0) {
265            costs += Math.Max(nextStop.SpareTime, additionalTime) * tardinessPenalty;
266          }
267        } else {
268          //arrive later than before, probably don't have to wait
269          if (nextStop.WaitingTime > 0) {
270            additionalTime -= Math.Min(additionalTime, nextStop.WaitingTime);
271          }
272
273          //check due date
274          if (nextStop.SpareTime > 0) {
275            double spare = nextStop.SpareTime - additionalTime;
276            if (spare < 0)
277              tardiness += -spare;
278          } else {
279            tardiness += additionalTime;
280          }
281        }
282      }
283
284      costs += additionalTime * vrptw.TimeFactor.Value;
285
286      if (tardiness > 0) {
287        tourFeasible = feasible = false;
288      }
289
290      costs += tardiness * tardinessPenalty;
291
292      return costs;
293    }
294
[6710]295    protected override void InitResultParameters() {
296      base.InitResultParameters();
297
298      PickupViolationsParameter.ActualValue = new IntValue(0);
299    }
300
301    protected override void SetResultParameters(VRPEvaluation tourEvaluation) {
302      base.SetResultParameters(tourEvaluation);
303
304      PickupViolationsParameter.ActualValue.Value = (tourEvaluation as CVRPPDTWEvaluation).PickupViolations;
305    }
[8053]306
[6710]307    [StorableConstructor]
[16565]308    protected CVRPPDTWEvaluator(StorableConstructorFlag _) : base(_) { }
[6710]309
310    public CVRPPDTWEvaluator() {
311      Parameters.Add(new LookupParameter<IntValue>("PickupViolations", "The number of pickup violations."));
312    }
313
314    public override IDeepCloneable Clone(Cloner cloner) {
315      return new CVRPPDTWEvaluator(this, cloner);
316    }
317
318    protected CVRPPDTWEvaluator(CVRPPDTWEvaluator original, Cloner cloner)
319      : base(original, cloner) {
320    }
321  }
322}
Note: See TracBrowser for help on using the repository browser.