Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.VehicleRouting/3.3/Evaluators/VRPEvaluator.cs @ 4203

Last change on this file since 4203 was 4179, checked in by svonolfe, 14 years ago

Refactored VRP based on the code review (#1039)

File size: 9.8 KB
RevLine 
[3938]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
[4068]22using HeuristicLab.Core;
23using HeuristicLab.Data;
[3938]24using HeuristicLab.Operators;
25using HeuristicLab.Parameters;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27using HeuristicLab.Problems.VehicleRouting.Encodings;
28
29namespace HeuristicLab.Problems.VehicleRouting {
30  public struct TourEvaluation {
31    public double Quality { get; set; }
32    public double VehcilesUtilized { get; set; }
33    public double TravelTime { get; set; }
34    public double Distance { get; set; }
35    public double Overload { get; set; }
36    public double Tardiness { get; set; }
37  }
[4068]38
[3938]39  [Item("VRPEvaluator", "Evaluates solutions for the VRP problem.")]
40  [StorableClass]
[4154]41  public sealed class VRPEvaluator : VRPOperator, IVRPEvaluator {
[3938]42    #region ISingleObjectiveEvaluator Members
43    public ILookupParameter<DoubleValue> QualityParameter {
44      get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
45    }
46    #endregion
47
48    public ILookupParameter<DoubleValue> VehcilesUtilizedParameter {
49      get { return (ILookupParameter<DoubleValue>)Parameters["VehiclesUtilized"]; }
50    }
51    public ILookupParameter<DoubleValue> TravelTimeParameter {
52      get { return (ILookupParameter<DoubleValue>)Parameters["TravelTime"]; }
53    }
54    public ILookupParameter<DoubleValue> DistanceParameter {
55      get { return (ILookupParameter<DoubleValue>)Parameters["Distance"]; }
56    }
57    public ILookupParameter<DoubleValue> OverloadParameter {
58      get { return (ILookupParameter<DoubleValue>)Parameters["Overload"]; }
59    }
60    public ILookupParameter<DoubleValue> TardinessParameter {
61      get { return (ILookupParameter<DoubleValue>)Parameters["Tardiness"]; }
62    }
63
[4179]64    public ILookupParameter<IVRPEncoding> VRPToursParameter {
65      get { return (ILookupParameter<IVRPEncoding>)Parameters["VRPTours"]; }
[3938]66    }
[4154]67   
[3947]68    public ILookupParameter<DoubleValue> FleetUsageFactor {
[4179]69      get { return (ILookupParameter<DoubleValue>)Parameters["EvalFleetUsageFactor"]; }
[3938]70    }
[3947]71    public ILookupParameter<DoubleValue> TimeFactor {
[4179]72      get { return (ILookupParameter<DoubleValue>)Parameters["EvalTimeFactor"]; }
[3938]73    }
[3947]74    public ILookupParameter<DoubleValue> DistanceFactor {
[4179]75      get { return (ILookupParameter<DoubleValue>)Parameters["EvalDistanceFactor"]; }
[3938]76    }
[3947]77    public ILookupParameter<DoubleValue> OverloadPenalty {
[4179]78      get { return (ILookupParameter<DoubleValue>)Parameters["EvalOverloadPenalty"]; }
[3938]79    }
[3947]80    public ILookupParameter<DoubleValue> TardinessPenalty {
[4179]81      get { return (ILookupParameter<DoubleValue>)Parameters["EvalTardinessPenalty"]; }
[3938]82    }
83
84    public VRPEvaluator()
85      : base() {
86      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The evaluated quality of the VRP solution."));
87      Parameters.Add(new LookupParameter<DoubleValue>("VehiclesUtilized", "The number of vehicles utilized."));
88      Parameters.Add(new LookupParameter<DoubleValue>("TravelTime", "The total travel time."));
89      Parameters.Add(new LookupParameter<DoubleValue>("Distance", "The distance."));
90      Parameters.Add(new LookupParameter<DoubleValue>("Overload", "The overload."));
91      Parameters.Add(new LookupParameter<DoubleValue>("Tardiness", "The tardiness."));
[4179]92      Parameters.Add(new LookupParameter<IVRPEncoding>("VRPTours", "The VRP tours which should be evaluated."));
93      Parameters.Add(new LookupParameter<DoubleValue>("EvalFleetUsageFactor", "The fleet usage factor considered in the evaluation."));
94      Parameters.Add(new LookupParameter<DoubleValue>("EvalTimeFactor", "The time factor considered in the evaluation."));
95      Parameters.Add(new LookupParameter<DoubleValue>("EvalDistanceFactor", "The distance factor considered in the evaluation."));
96      Parameters.Add(new LookupParameter<DoubleValue>("EvalOverloadPenalty", "The overload penalty considered in the evaluation."));
97      Parameters.Add(new LookupParameter<DoubleValue>("EvalTardinessPenalty", "The tardiness penalty considered in the evaluation."));
[3938]98    }
99
100    private double CalculateFleetUsage() {
[4179]101      IVRPEncoding vrpSolution = VRPToursParameter.ActualValue;
[3938]102
103      return vrpSolution.Tours.Count;
104    }
105
[4174]106    internal static TourEvaluation EvaluateTour(Tour tour, DoubleArray dueTimeArray,
[4068]107      DoubleArray serviceTimeArray, DoubleArray readyTimeArray, DoubleArray demandArray, DoubleValue capacity,
[3938]108      DoubleValue fleetUsageFactor, DoubleValue timeFactor, DoubleValue distanceFactor, DoubleValue overloadPenalty, DoubleValue tardinessPenalty,
109      DoubleMatrix coordinates, ILookupParameter<DoubleMatrix> distanceMatrix, BoolValue useDistanceMatrix) {
110      TourEvaluation eval = new TourEvaluation();
111
112      double quality = 0.0;
113      double time = 0.0;
114
115      double distance = 0.0;
116      double waitingTime = 0.0;
117      double serviceTime = 0.0;
118      double delivered = 0.0;
119
120      double overweight = 0.0;
121      double tardiness = 0.0;
122
123      //simulate a tour, start and end at depot
[4174]124      for (int i = 0; i <= tour.Cities.Count; i++) {
[4150]125        int start = 0;
126        if(i > 0)
[4174]127          start = tour.Cities[i - 1];
[4150]128        int end = 0;
[4174]129        if (i < tour.Cities.Count)
130          end = tour.Cities[i];
[3938]131
132        //drive there
[4154]133        double currentDistace = VRPUtilities.GetDistance(start, end, coordinates, distanceMatrix, useDistanceMatrix);
[3938]134        distance += currentDistace;
135        time += currentDistace;
136
137        //check if it was serviced on time
138        if (time > dueTimeArray[end])
139          tardiness += time - dueTimeArray[end];
140
141        //wait
142        double currentWaitingTime = 0.0;
143        if (time < readyTimeArray[end])
144          currentWaitingTime = readyTimeArray[end] - time;
145        waitingTime += currentWaitingTime;
146        time += currentWaitingTime;
147
148        //service
149        delivered += demandArray[end];
150        double currentServiceTime = serviceTimeArray[end];
151        serviceTime += currentServiceTime;
152        time += currentServiceTime;
153      }
154
155      if (delivered > capacity.Value) {
156        overweight = delivered - capacity.Value;
157      }
158
159      //Fleet usage
160      quality += fleetUsageFactor.Value;
161      //Travel time
162      quality += timeFactor.Value * time;
163      //Distance
164      quality += distanceFactor.Value * distance;
165
166      //Penalties
167      quality += overloadPenalty.Value * overweight;
168      quality += tardinessPenalty.Value * tardiness;
169
170      eval.Distance = distance;
171      eval.TravelTime = time;
172      eval.VehcilesUtilized = 1;
173      eval.Overload = overweight;
174      eval.Tardiness = tardiness;
175      eval.Quality = quality;
176
177      return eval;
178    }
179
180    public static TourEvaluation Evaluate(IVRPEncoding solution, DoubleArray dueTimeArray,
181      DoubleArray serviceTimeArray, DoubleArray readyTimeArray, DoubleArray demandArray, DoubleValue capacity,
182      DoubleValue fleetUsageFactor, DoubleValue timeFactor, DoubleValue distanceFactor, DoubleValue overloadPenalty, DoubleValue tardinessPenalty,
183      DoubleMatrix coordinates, ILookupParameter<DoubleMatrix> distanceMatrix, BoolValue useDistanceMatrix) {
184      TourEvaluation sumEval = new TourEvaluation();
185      sumEval.Distance = 0;
186      sumEval.Quality = 0;
187      sumEval.TravelTime = 0;
188      sumEval.VehcilesUtilized = 0;
189      sumEval.Overload = 0;
190      sumEval.Tardiness = 0;
191
192      foreach (Tour tour in solution.Tours) {
[4068]193        TourEvaluation eval = EvaluateTour(tour, dueTimeArray, serviceTimeArray, readyTimeArray, demandArray, capacity,
194          fleetUsageFactor, timeFactor, distanceFactor, overloadPenalty, tardinessPenalty,
[3938]195          coordinates, distanceMatrix, useDistanceMatrix);
196        sumEval.Quality += eval.Quality;
197        sumEval.Distance += eval.Distance;
198        sumEval.TravelTime += eval.TravelTime;
199        sumEval.VehcilesUtilized += eval.VehcilesUtilized;
200        sumEval.Overload += eval.Overload;
201        sumEval.Tardiness += eval.Tardiness;
202      }
203
204      return sumEval;
205    }
206
207    public sealed override IOperation Apply() {
[4179]208      IVRPEncoding solution = VRPToursParameter.ActualValue;
[3938]209
[4068]210      TourEvaluation sumEval = Evaluate(solution, DueTimeParameter.ActualValue, ServiceTimeParameter.ActualValue, ReadyTimeParameter.ActualValue,
[3947]211        DemandParameter.ActualValue, CapacityParameter.ActualValue,
[4068]212        FleetUsageFactor.ActualValue, TimeFactor.ActualValue, DistanceFactor.ActualValue, OverloadPenalty.ActualValue, TardinessPenalty.ActualValue,
[3938]213        CoordinatesParameter.ActualValue, DistanceMatrixParameter, UseDistanceMatrixParameter.ActualValue);
214
215      QualityParameter.ActualValue = new DoubleValue(sumEval.Quality);
216      VehcilesUtilizedParameter.ActualValue = new DoubleValue(sumEval.VehcilesUtilized);
217      TravelTimeParameter.ActualValue = new DoubleValue(sumEval.TravelTime);
218      DistanceParameter.ActualValue = new DoubleValue(sumEval.Distance);
219      OverloadParameter.ActualValue = new DoubleValue(sumEval.Overload);
220      TardinessParameter.ActualValue = new DoubleValue(sumEval.Tardiness);
221
222      return base.Apply();
223    }
224  }
225}
Note: See TracBrowser for help on using the repository browser.