Free cookie consent management tool by TermsFeed Policy Generator

source: branches/EnhancedProgress/HeuristicLab.Problems.VehicleRouting/3.4/ProblemInstances/SingleDepotVRP/CVRP/CVRPTW/CVRPTWEvaluator.cs @ 15632

Last change on this file since 15632 was 14185, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

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