Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Problems.VehicleRouting/3.4/ProblemInstances/MultiDepotVRP/MDCVRPTWProblemInstance.cs @ 17954

Last change on this file since 17954 was 17717, checked in by abeham, 4 years ago

#2521: working on VRP (refactoring all the capabilities, features, and operator discovery)

File size: 14.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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 System.Linq;
25using HEAL.Attic;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Parameters;
30using HeuristicLab.Problems.VehicleRouting.Interfaces;
31
32namespace HeuristicLab.Problems.VehicleRouting.ProblemInstances {
33  [Item("MDCVRPTWProblemInstance", "Represents a multi depot CVRPTW instance.")]
34  [StorableType("ADC41AA7-EFA6-46FB-BBA7-DC08AE0A26F0")]
35  public class MDCVRPTWProblemInstance : MDCVRPProblemInstance, ITimeWindowedProblemInstance {
36    protected IValueParameter<DoubleArray> ReadyTimeParameter {
37      get { return (IValueParameter<DoubleArray>)Parameters["ReadyTime"]; }
38    }
39    protected IValueParameter<DoubleArray> DueTimeParameter {
40      get { return (IValueParameter<DoubleArray>)Parameters["DueTime"]; }
41    }
42    protected IValueParameter<DoubleArray> ServiceTimeParameter {
43      get { return (IValueParameter<DoubleArray>)Parameters["ServiceTime"]; }
44    }
45
46    protected IValueParameter<DoubleValue> TimeFactorParameter {
47      get { return (IValueParameter<DoubleValue>)Parameters["EvalTimeFactor"]; }
48    }
49    protected IValueParameter<DoubleValue> TardinessPenaltyParameter {
50      get { return (IValueParameter<DoubleValue>)Parameters["EvalTardinessPenalty"]; }
51    }
52
53    public DoubleArray ReadyTime {
54      get { return ReadyTimeParameter.Value; }
55      set { ReadyTimeParameter.Value = value; }
56    }
57    public DoubleArray DueTime {
58      get { return DueTimeParameter.Value; }
59      set { DueTimeParameter.Value = value; }
60    }
61    public DoubleArray ServiceTime {
62      get { return ServiceTimeParameter.Value; }
63      set { ServiceTimeParameter.Value = value; }
64    }
65    public DoubleValue TimeFactor {
66      get { return TimeFactorParameter.Value; }
67      set { TimeFactorParameter.Value = value; }
68    }
69
70    protected IValueParameter<DoubleValue> CurrentTardinessPenaltyParameter {
71      get { return (IValueParameter<DoubleValue>)Parameters["CurrentTardinessPenalty"]; }
72    }
73
74    public DoubleValue TardinessPenalty {
75      get {
76        DoubleValue currentTardinessPenalty = CurrentTardinessPenaltyParameter.Value;
77        if (currentTardinessPenalty != null)
78          return currentTardinessPenalty;
79        else
80          return TardinessPenaltyParameter.Value;
81      }
82    }
83    DoubleValue ITimeWindowedProblemInstance.CurrentTardinessPenalty {
84      get { return CurrentTardinessPenaltyParameter.Value; }
85      set { CurrentTardinessPenaltyParameter.Value = value; }
86    }
87
88    public override IEnumerable<IOperator> FilterOperators(IEnumerable<IOperator> operators) {
89      return base.FilterOperators(operators).Union(operators.Where(x => x is ITimeWindowedOperator));
90    }
91
92    protected override VRPEvaluation CreateTourEvaluation() {
93      return new CVRPTWEvaluation();
94    }
95
96    protected override void EvaluateTour(VRPEvaluation eval, Tour tour, IVRPEncodedSolution solution) {
97      TourInsertionInfo tourInfo = new TourInsertionInfo(solution.GetVehicleAssignment(solution.GetTourIndex(tour)));
98      eval.InsertionInfo.AddTourInsertionInfo(tourInfo);
99      double originalQuality = eval.Quality;
100
101      int depots = Depots.Value;
102
103      double time = 0.0;
104      double waitingTime = 0.0;
105      double serviceTime = 0.0;
106      double tardiness = 0.0;
107      double delivered = 0.0;
108      double overweight = 0.0;
109      double distance = 0.0;
110
111      int tourIndex = solution.GetTourIndex(tour);
112      int vehicle = solution.GetVehicleAssignment(tourIndex);
113      int depot = VehicleDepotAssignment[vehicle];
114
115      double capacity = Capacity[vehicle];
116      for (int i = 0; i < tour.Stops.Count; i++) {
117        delivered += GetDemand(tour.Stops[i]);
118      }
119
120      double spareCapacity = capacity - delivered;
121
122      double tourStartTime = ReadyTime[depot];
123      time = tourStartTime;
124
125      //simulate a tour, start and end at depot
126      for (int i = 0; i <= tour.Stops.Count; i++) {
127        int start = 0;
128        if (i > 0)
129          start = tour.Stops[i - 1];
130        int end = 0;
131        if (i < tour.Stops.Count)
132          end = tour.Stops[i];
133
134        //drive there
135        double currentDistace = GetDistance(start, end, solution);
136        time += currentDistace;
137        distance += currentDistace;
138
139        double arrivalTime = time;
140
141        int endIndex;
142        if (end == 0)
143          endIndex = depot;
144        else
145          endIndex = end + depots - 1;
146
147        //check if it was serviced on time
148        if (time > DueTime[endIndex])
149          tardiness += time - DueTime[endIndex];
150
151        //wait
152        double currentWaitingTime = 0.0;
153        if (time < ReadyTime[endIndex])
154          currentWaitingTime = ReadyTime[endIndex] - time;
155
156        double waitTime = ReadyTime[endIndex] - time;
157
158        waitingTime += currentWaitingTime;
159        time += currentWaitingTime;
160
161        double spareTime = DueTime[endIndex] - time;
162
163        //service
164        double currentServiceTime = 0;
165        if (end > 0)
166          currentServiceTime = ServiceTime[end - 1];
167        serviceTime += currentServiceTime;
168        time += currentServiceTime;
169
170        CVRPTWInsertionInfo stopInfo = new CVRPTWInsertionInfo(start, end, spareCapacity, tourStartTime, arrivalTime, time, spareTime, waitTime);
171        tourInfo.AddStopInsertionInfo(stopInfo);
172      }
173
174      eval.Quality += FleetUsageFactor.Value;
175      eval.Quality += DistanceFactor.Value * distance;
176      eval.Distance += distance;
177      eval.VehicleUtilization += 1;
178
179      if (delivered > capacity) {
180        overweight = delivered - capacity;
181      }
182
183      (eval as CVRPEvaluation).Overload += overweight;
184      double tourPenalty = 0;
185      double penalty = overweight * OverloadPenalty.Value;
186      eval.Penalty += penalty;
187      eval.Quality += penalty;
188      tourPenalty += penalty;
189
190      (eval as CVRPTWEvaluation).Tardiness += tardiness;
191      (eval as CVRPTWEvaluation).TravelTime += time;
192
193      penalty = tardiness * TardinessPenalty.Value;
194      eval.Penalty += penalty;
195      eval.Quality += penalty;
196      tourPenalty += penalty;
197      eval.Quality += time * TimeFactor.Value;
198      tourInfo.Penalty = tourPenalty;
199      tourInfo.Quality = eval.Quality - originalQuality;
200
201      eval.IsFeasible = overweight == 0 && tardiness == 0;
202    }
203
204    protected override double GetTourInsertionCosts(IVRPEncodedSolution solution, TourInsertionInfo tourInsertionInfo, int index, int customer,
205      out bool feasible) {
206      CVRPTWInsertionInfo insertionInfo = tourInsertionInfo.GetStopInsertionInfo(index) as CVRPTWInsertionInfo;
207
208      double costs = 0;
209      feasible = tourInsertionInfo.Penalty < double.Epsilon;
210
211      double overloadPenalty = OverloadPenalty.Value;
212      double tardinessPenalty = TardinessPenalty.Value;
213      int depots = Depots.Value;
214
215      double startDistance, endDistance;
216      costs += GetInsertionDistance(insertionInfo.Start, customer, insertionInfo.End, solution, out startDistance, out endDistance);
217
218      double demand = GetDemand(customer);
219      if (demand > insertionInfo.SpareCapacity) {
220        feasible = false;
221
222        if (insertionInfo.SpareCapacity >= 0)
223          costs += (demand - insertionInfo.SpareCapacity) * overloadPenalty;
224        else
225          costs += demand * overloadPenalty;
226      }
227
228      double time = 0;
229      double tardiness = 0;
230
231      if (index > 0)
232        time = (tourInsertionInfo.GetStopInsertionInfo(index - 1) as CVRPTWInsertionInfo).LeaveTime;
233      else
234        time = insertionInfo.TourStartTime;
235
236      time += startDistance;
237
238      int customerIndex = customer + depots - 1;
239
240      if (time > DueTime[customerIndex]) {
241        tardiness += time - DueTime[customerIndex];
242      }
243      if (time < ReadyTime[customerIndex])
244        time += ReadyTime[customerIndex] - time;
245      time += ServiceTime[customer - 1];
246      time += endDistance;
247
248      double additionalTime = time - (tourInsertionInfo.GetStopInsertionInfo(index) as CVRPTWInsertionInfo).ArrivalTime;
249      for (int i = index; i < tourInsertionInfo.GetStopCount(); i++) {
250        CVRPTWInsertionInfo nextStop = tourInsertionInfo.GetStopInsertionInfo(i) as CVRPTWInsertionInfo;
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 * TimeFactor.Value;
285
286      if (tardiness > 0) {
287        feasible = false;
288      }
289
290      costs += tardiness * tardinessPenalty;
291
292      return costs;
293    }
294
295    [StorableConstructor]
296    protected MDCVRPTWProblemInstance(StorableConstructorFlag _) : base(_) { }
297
298    public MDCVRPTWProblemInstance() {
299      Parameters.Add(new ValueParameter<DoubleArray>("ReadyTime", "The ready time of each customer.", new DoubleArray()));
300      Parameters.Add(new ValueParameter<DoubleArray>("DueTime", "The due time of each customer.", new DoubleArray()));
301      Parameters.Add(new ValueParameter<DoubleArray>("ServiceTime", "The service time of each customer.", new DoubleArray()));
302
303      Parameters.Add(new ValueParameter<DoubleValue>("EvalTimeFactor", "The time factor considered in the evaluation.", new DoubleValue(0)));
304      Parameters.Add(new ValueParameter<DoubleValue>("EvalTardinessPenalty", "The tardiness penalty considered in the evaluation.", new DoubleValue(100)));
305      Parameters.Add(new OptionalValueParameter<DoubleValue>("CurrentTardinessPenalty", "The current tardiness penalty considered in the evaluation.") { Hidden = true });
306
307      AttachEventHandlers();
308    }
309
310    public override IDeepCloneable Clone(Cloner cloner) {
311      return new MDCVRPTWProblemInstance(this, cloner);
312    }
313
314    protected MDCVRPTWProblemInstance(MDCVRPTWProblemInstance original, Cloner cloner)
315      : base(original, cloner) {
316      AttachEventHandlers();
317    }
318
319    [StorableHook(HookType.AfterDeserialization)]
320    private void AfterDeserialization() {
321      AttachEventHandlers();
322    }
323
324    private void AttachEventHandlers() {
325      ReadyTimeParameter.ValueChanged += ReadyTimeParameter_ValueChanged;
326      ReadyTime.Reset += ReadyTime_Changed;
327      ReadyTime.ItemChanged += ReadyTime_Changed;
328      DueTimeParameter.ValueChanged += DueTimeParameter_ValueChanged;
329      DueTime.Reset += DueTime_Changed;
330      DueTime.ItemChanged += DueTime_Changed;
331      ServiceTimeParameter.ValueChanged += ServiceTimeParameter_ValueChanged;
332      ServiceTime.Reset += ServiceTime_Changed;
333      ServiceTime.ItemChanged += ServiceTime_Changed;
334      TardinessPenaltyParameter.ValueChanged += TardinessPenaltyParameter_ValueChanged;
335      TardinessPenalty.ValueChanged += TardinessPenalty_ValueChanged;
336      TimeFactorParameter.ValueChanged += TimeFactorParameter_ValueChanged;
337      TimeFactor.ValueChanged += TimeFactor_ValueChanged;
338    }
339
340    public override void InitializeState() {
341      base.InitializeState();
342
343      CurrentTardinessPenaltyParameter.Value = null;
344    }
345
346    #region Event handlers
347    private void ReadyTimeParameter_ValueChanged(object sender, EventArgs e) {
348      ReadyTime.Reset += ReadyTime_Changed;
349      ReadyTime.ItemChanged += ReadyTime_Changed;
350      EvalBestKnownSolution();
351    }
352    private void ReadyTime_Changed(object sender, EventArgs e) {
353      EvalBestKnownSolution();
354    }
355    private void DueTimeParameter_ValueChanged(object sender, EventArgs e) {
356      DueTime.Reset += DueTime_Changed;
357      DueTime.ItemChanged += DueTime_Changed;
358      EvalBestKnownSolution();
359    }
360    private void DueTime_Changed(object sender, EventArgs e) {
361      EvalBestKnownSolution();
362    }
363    private void ServiceTimeParameter_ValueChanged(object sender, EventArgs e) {
364      ServiceTime.Reset += ServiceTime_Changed;
365      ServiceTime.ItemChanged += ServiceTime_Changed;
366      EvalBestKnownSolution();
367    }
368    private void ServiceTime_Changed(object sender, EventArgs e) {
369      EvalBestKnownSolution();
370    }
371    private void TardinessPenaltyParameter_ValueChanged(object sender, EventArgs e) {
372      TardinessPenalty.ValueChanged += TardinessPenalty_ValueChanged;
373      EvalBestKnownSolution();
374    }
375    private void TardinessPenalty_ValueChanged(object sender, EventArgs e) {
376      EvalBestKnownSolution();
377    }
378    private void TimeFactorParameter_ValueChanged(object sender, EventArgs e) {
379      TimeFactor.ValueChanged += TimeFactor_ValueChanged;
380      EvalBestKnownSolution();
381    }
382    private void TimeFactor_ValueChanged(object sender, EventArgs e) {
383      EvalBestKnownSolution();
384    }
385    #endregion
386  }
387}
Note: See TracBrowser for help on using the repository browser.