Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Problems.VehicleRouting/3.4/ProblemInstances/SingleDepotVRP/CVRPTWProblemInstance.cs @ 18242

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

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

File size: 14.0 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("CVRPTWProblemInstance", "Represents a single depot CVRPTW instance.")]
34  [StorableType("2138BE6C-058E-4003-8578-55E8DE3706C7")]
35  public class CVRPTWProblemInstance : CVRPProblemInstance, 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)
90        .Where(x => !(x is INotTimeWindowedOperator))
91        .Union(operators.Where(x => x is ITimeWindowedOperator));
92    }
93
94    protected override VRPEvaluation CreateTourEvaluation() {
95      return new CVRPTWEvaluation();
96    }
97
98    protected override void EvaluateTour(VRPEvaluation eval, Tour tour, IVRPEncodedSolution solution) {
99      TourInsertionInfo tourInfo = new TourInsertionInfo(solution.GetVehicleAssignment(solution.GetTourIndex(tour)));
100      eval.InsertionInfo.AddTourInsertionInfo(tourInfo);
101      double originalQuality = eval.Quality;
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      double capacity = Capacity.Value;
112      for (int i = 0; i <= tour.Stops.Count; i++) {
113        int end = 0;
114        if (i < tour.Stops.Count)
115          end = tour.Stops[i];
116
117        delivered += Demand[end];
118      }
119
120      double spareCapacity = capacity - delivered;
121
122      double tourStartTime = ReadyTime[0];
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        //check if it was serviced on time
142        if (time > DueTime[end]) {
143          tardiness += time - DueTime[end];
144        }
145
146        //wait
147        double currentWaitingTime = 0.0;
148        if (time < ReadyTime[end])
149          currentWaitingTime = ReadyTime[end] - time;
150
151        double waitTime = ReadyTime[end] - time;
152
153        waitingTime += currentWaitingTime;
154        time += currentWaitingTime;
155
156        double spareTime = DueTime[end] - time;
157
158        //service
159        double currentServiceTime = ServiceTime[end];
160        serviceTime += currentServiceTime;
161        time += currentServiceTime;
162
163        CVRPTWInsertionInfo stopInfo = new CVRPTWInsertionInfo(start, end, spareCapacity, tourStartTime, arrivalTime, time, spareTime, waitTime);
164        tourInfo.AddStopInsertionInfo(stopInfo);
165      }
166
167      eval.Quality += FleetUsageFactor.Value;
168      eval.Quality += DistanceFactor.Value * distance;
169      eval.Distance += distance;
170      eval.VehicleUtilization += 1;
171
172      if (delivered > capacity) {
173        overweight = delivered - capacity;
174      }
175
176      (eval as CVRPEvaluation).Overload += overweight;
177      double tourPenalty = 0;
178      double penalty = overweight * OverloadPenalty.Value;
179      eval.Penalty += penalty;
180      eval.Quality += penalty;
181      tourPenalty += penalty;
182
183      (eval as CVRPTWEvaluation).Tardiness += tardiness;
184      (eval as CVRPTWEvaluation).TravelTime += time;
185
186      penalty = tardiness * TardinessPenalty.Value;
187      eval.Penalty += penalty;
188      eval.Quality += penalty;
189      tourPenalty += penalty;
190      eval.Quality += time * TimeFactor.Value;
191      tourInfo.Penalty = tourPenalty;
192      tourInfo.Quality = eval.Quality - originalQuality;
193
194      eval.IsFeasible = overweight == 0 && tardiness == 0;
195    }
196
197    protected override double GetTourInsertionCosts(IVRPEncodedSolution solution, TourInsertionInfo tourInsertionInfo, int index, int customer,
198      out bool feasible) {
199      CVRPTWInsertionInfo insertionInfo = tourInsertionInfo.GetStopInsertionInfo(index) as CVRPTWInsertionInfo;
200
201      double costs = 0;
202      feasible = tourInsertionInfo.Penalty < double.Epsilon;
203
204      double overloadPenalty = OverloadPenalty.Value;
205      double tardinessPenalty = TardinessPenalty.Value;
206
207      double distance = GetDistance(insertionInfo.Start, insertionInfo.End, solution);
208      double newDistance =
209        GetDistance(insertionInfo.Start, customer, solution) +
210        GetDistance(customer, insertionInfo.End, solution);
211      costs += DistanceFactor.Value * (newDistance - distance);
212
213      double demand = Demand[customer];
214      if (demand > insertionInfo.SpareCapacity) {
215        feasible = false;
216        if (insertionInfo.SpareCapacity >= 0)
217          costs += (demand - insertionInfo.SpareCapacity) * overloadPenalty;
218        else
219          costs += demand * overloadPenalty;
220      }
221
222      double time = 0;
223      double tardiness = 0;
224
225      if (index > 0)
226        time = (tourInsertionInfo.GetStopInsertionInfo(index - 1) as CVRPTWInsertionInfo).LeaveTime;
227      else
228        time = insertionInfo.TourStartTime;
229
230      time += GetDistance(insertionInfo.Start, customer, solution);
231      if (time > DueTime[customer]) {
232        tardiness += time - DueTime[customer];
233      }
234      if (time < ReadyTime[customer])
235        time += ReadyTime[customer] - time;
236      time += ServiceTime[customer];
237      time += GetDistance(customer, insertionInfo.End, solution);
238
239      double additionalTime = time - (tourInsertionInfo.GetStopInsertionInfo(index) as CVRPTWInsertionInfo).ArrivalTime;
240      for (int i = index; i < tourInsertionInfo.GetStopCount(); i++) {
241        CVRPTWInsertionInfo nextStop = tourInsertionInfo.GetStopInsertionInfo(i) as CVRPTWInsertionInfo;
242
243        if (additionalTime < 0) {
244          //arrive earlier than before
245          //wait probably
246          if (nextStop.WaitingTime < 0) {
247            double wait = nextStop.WaitingTime - additionalTime;
248            if (wait > 0)
249              additionalTime += wait;
250          } else {
251            additionalTime = 0;
252          }
253
254          //check due date, decrease tardiness
255          if (nextStop.SpareTime < 0) {
256            costs += Math.Max(nextStop.SpareTime, additionalTime) * tardinessPenalty;
257          }
258        } else {
259          //arrive later than before, probably don't have to wait
260          if (nextStop.WaitingTime > 0) {
261            additionalTime -= Math.Min(additionalTime, nextStop.WaitingTime);
262          }
263
264          //check due date
265          if (nextStop.SpareTime > 0) {
266            double spare = nextStop.SpareTime - additionalTime;
267            if (spare < 0)
268              tardiness += -spare;
269          } else {
270            tardiness += additionalTime;
271          }
272        }
273      }
274
275      costs += additionalTime * TimeFactor.Value;
276
277      if (tardiness > 0) {
278        feasible = false;
279      }
280
281      costs += tardiness * tardinessPenalty;
282
283      return costs;
284    }
285
286    [StorableConstructor]
287    protected CVRPTWProblemInstance(StorableConstructorFlag _) : base(_) { }
288
289    public CVRPTWProblemInstance() {
290      Parameters.Add(new ValueParameter<DoubleArray>("ReadyTime", "The ready time of each customer.", new DoubleArray()));
291      Parameters.Add(new ValueParameter<DoubleArray>("DueTime", "The due time of each customer.", new DoubleArray()));
292      Parameters.Add(new ValueParameter<DoubleArray>("ServiceTime", "The service time of each customer.", new DoubleArray()));
293
294      Parameters.Add(new ValueParameter<DoubleValue>("EvalTimeFactor", "The time factor considered in the evaluation.", new DoubleValue(0)));
295      Parameters.Add(new ValueParameter<DoubleValue>("EvalTardinessPenalty", "The tardiness penalty considered in the evaluation.", new DoubleValue(100)));
296      Parameters.Add(new OptionalValueParameter<DoubleValue>("CurrentTardinessPenalty", "The current tardiness penalty considered in the evaluation.") { Hidden = true });
297
298      AttachEventHandlers();
299    }
300
301    public override IDeepCloneable Clone(Cloner cloner) {
302      return new CVRPTWProblemInstance(this, cloner);
303    }
304
305    protected CVRPTWProblemInstance(CVRPTWProblemInstance original, Cloner cloner)
306      : base(original, cloner) {
307      AttachEventHandlers();
308    }
309
310    [StorableHook(HookType.AfterDeserialization)]
311    private void AfterDeserialization() {
312      AttachEventHandlers();
313    }
314
315    private void AttachEventHandlers() {
316      ReadyTimeParameter.ValueChanged += ReadyTimeParameter_ValueChanged;
317      ReadyTime.Reset += ReadyTime_Changed;
318      ReadyTime.ItemChanged += ReadyTime_Changed;
319      DueTimeParameter.ValueChanged += DueTimeParameter_ValueChanged;
320      DueTime.Reset += DueTime_Changed;
321      DueTime.ItemChanged += DueTime_Changed;
322      ServiceTimeParameter.ValueChanged += ServiceTimeParameter_ValueChanged;
323      ServiceTime.Reset += ServiceTime_Changed;
324      ServiceTime.ItemChanged += ServiceTime_Changed;
325      TardinessPenaltyParameter.ValueChanged += TardinessPenaltyParameter_ValueChanged;
326      TardinessPenalty.ValueChanged += TardinessPenalty_ValueChanged;
327      TimeFactorParameter.ValueChanged += TimeFactorParameter_ValueChanged;
328      TimeFactor.ValueChanged += TimeFactor_ValueChanged;
329    }
330
331    public override void InitializeState() {
332      base.InitializeState();
333
334      CurrentTardinessPenaltyParameter.Value = null;
335    }
336
337    #region Event handlers
338    private void ReadyTimeParameter_ValueChanged(object sender, EventArgs e) {
339      ReadyTime.Reset += ReadyTime_Changed;
340      ReadyTime.ItemChanged += ReadyTime_Changed;
341      EvalBestKnownSolution();
342    }
343    private void ReadyTime_Changed(object sender, EventArgs e) {
344      EvalBestKnownSolution();
345    }
346    private void DueTimeParameter_ValueChanged(object sender, EventArgs e) {
347      DueTime.Reset += DueTime_Changed;
348      DueTime.ItemChanged += DueTime_Changed;
349      EvalBestKnownSolution();
350    }
351    private void DueTime_Changed(object sender, EventArgs e) {
352      EvalBestKnownSolution();
353    }
354    private void ServiceTimeParameter_ValueChanged(object sender, EventArgs e) {
355      ServiceTime.Reset += ServiceTime_Changed;
356      ServiceTime.ItemChanged += ServiceTime_Changed;
357      EvalBestKnownSolution();
358    }
359    private void ServiceTime_Changed(object sender, EventArgs e) {
360      EvalBestKnownSolution();
361    }
362    private void TardinessPenaltyParameter_ValueChanged(object sender, EventArgs e) {
363      TardinessPenaltyParameter.Value.ValueChanged += TardinessPenalty_ValueChanged;
364      EvalBestKnownSolution();
365    }
366    private void TardinessPenalty_ValueChanged(object sender, EventArgs e) {
367      EvalBestKnownSolution();
368    }
369    private void TimeFactorParameter_ValueChanged(object sender, EventArgs e) {
370      TimeFactorParameter.Value.ValueChanged += TimeFactor_ValueChanged;
371      EvalBestKnownSolution();
372    }
373    private void TimeFactor_ValueChanged(object sender, EventArgs e) {
374      EvalBestKnownSolution();
375    }
376    #endregion
377  }
378}
Note: See TracBrowser for help on using the repository browser.