Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 7895 was 7276, checked in by svonolfe, 12 years ago

Improved incremental evaluation (#1177)

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