Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/ProblemInstances/SingleDepotVRP/CVRP/CVRPTW/CVRPPDTW/CVRPPDTWEvaluator.cs @ 6710

Last change on this file since 6710 was 6710, checked in by svonolfe, 13 years ago

Added support for pickups and deliveries (#1177)

File size: 6.0 KB
Line 
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;
35using HeuristicLab.Common;
36
37namespace HeuristicLab.Problems.VehicleRouting.ProblemInstances {
38  [Item("CVRPPDTWEvaluator", "Represents a single depot CVRPPDTW evaluator.")]
39  [StorableClass]
40  public class CVRPPDTWEvaluator: CVRPTWEvaluator {
41    public ILookupParameter<IntValue> PickupViolationsParameter {
42      get { return (ILookupParameter<IntValue>)Parameters["PickupViolations"]; }
43    }
44
45    protected override VRPEvaluation CreateTourEvaluation() {
46      return new CVRPPDTWEvaluation();
47    }
48
49    protected override void EvaluateTour(VRPEvaluation eval, IVRPProblemInstance instance, Tour tour) {
50      IHomogenousCapacitatedProblemInstance cvrpInstance = instance as IHomogenousCapacitatedProblemInstance;
51      DoubleArray demand = instance.Demand;
52
53      ITimeWindowedProblemInstance vrptw = instance as ITimeWindowedProblemInstance;
54      DoubleArray dueTime = vrptw.DueTime;
55      DoubleArray readyTime = vrptw.ReadyTime;
56      DoubleArray serviceTimes = vrptw.ServiceTime;
57
58      IPickupAndDeliveryProblemInstance pdp = instance as IPickupAndDeliveryProblemInstance;
59      IntArray pickupDeliveryLocation = pdp.PickupDeliveryLocation;
60
61      double capacity = cvrpInstance.Capacity.Value;
62
63      double time = 0.0;
64      double waitingTime = 0.0;
65      double serviceTime = 0.0;
66      double tardiness = 0.0;
67      double overweight = 0.0;
68      double distance = 0.0;
69
70      double currentLoad = 0.0;
71      Dictionary<int, bool> stops = new Dictionary<int, bool>();
72      int pickupViolations = 0;
73
74      //simulate a tour, start and end at depot
75      for (int i = 0; i <= tour.Stops.Count; i++) {
76        int start = 0;
77        if (i > 0)
78          start = tour.Stops[i - 1];
79        int end = 0;
80        if (i < tour.Stops.Count)
81          end = tour.Stops[i];
82
83        //drive there
84        double currentDistace = vrptw.GetDistance(start, end);
85        time += currentDistace;
86        distance += currentDistace;
87
88        //check if it was serviced on time
89        if (time > dueTime[end])
90          tardiness += time - dueTime[end];
91
92        //wait
93        double currentWaitingTime = 0.0;
94        if (time < readyTime[end])
95          currentWaitingTime = readyTime[end] - time;
96        waitingTime += currentWaitingTime;
97        time += currentWaitingTime;
98
99        //service
100        double currentServiceTime = serviceTimes[end];
101        serviceTime += currentServiceTime;
102        time += currentServiceTime;
103
104        //Pickup / deliver
105        bool validPickupDelivery =
106          validPickupDelivery =
107          ((demand[end] >= 0) ||
108           (stops.ContainsKey(pickupDeliveryLocation[end])));
109
110        if (validPickupDelivery) {
111          currentLoad += demand[end];
112
113          if (currentLoad > capacity)
114            overweight += currentLoad - capacity;
115        } else {
116          pickupViolations++;
117        }
118
119        stops.Add(end, true);
120      }
121
122      eval.Quality += instance.FleetUsageFactor.Value;
123      eval.Quality += instance.DistanceFactor.Value * distance;
124      eval.Distance += distance;
125      eval.VehicleUtilization += 1;
126
127      (eval as CVRPEvaluation).Overload += overweight;
128      double penalty = overweight * cvrpInstance.OverloadPenalty.Value;
129      eval.Penalty += penalty;
130      eval.Quality += penalty;
131
132      (eval as CVRPTWEvaluation).Tardiness += tardiness;
133      (eval as CVRPTWEvaluation).TravelTime += time;
134
135      penalty = tardiness * vrptw.TardinessPenalty.Value;
136      eval.Penalty += penalty;
137      eval.Quality += penalty;
138
139      (eval as CVRPPDTWEvaluation).PickupViolations += pickupViolations;
140      penalty = pickupViolations * pdp.PickupViolationPenalty.Value;
141      eval.Penalty += penalty;
142
143      eval.Quality += penalty;
144      eval.Quality += time * vrptw.TimeFactor.Value;
145    }
146
147    protected override void InitResultParameters() {
148      base.InitResultParameters();
149
150      PickupViolationsParameter.ActualValue = new IntValue(0);
151    }
152
153    protected override void SetResultParameters(VRPEvaluation tourEvaluation) {
154      base.SetResultParameters(tourEvaluation);
155
156      PickupViolationsParameter.ActualValue.Value = (tourEvaluation as CVRPPDTWEvaluation).PickupViolations;
157    }
158   
159    [StorableConstructor]
160    protected CVRPPDTWEvaluator(bool deserializing) : base(deserializing) { }
161
162    public CVRPPDTWEvaluator() {
163      Parameters.Add(new LookupParameter<IntValue>("PickupViolations", "The number of pickup violations."));
164    }
165
166    public override IDeepCloneable Clone(Cloner cloner) {
167      return new CVRPPDTWEvaluator(this, cloner);
168    }
169
170    protected CVRPPDTWEvaluator(CVRPPDTWEvaluator original, Cloner cloner)
171      : base(original, cloner) {
172    }
173  }
174}
Note: See TracBrowser for help on using the repository browser.