1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2014 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 |
|
---|
22 | using HeuristicLab.Common;
|
---|
23 | using HeuristicLab.Core;
|
---|
24 | using HeuristicLab.Data;
|
---|
25 | using HeuristicLab.Parameters;
|
---|
26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
27 | using HeuristicLab.Problems.VehicleRouting.Interfaces;
|
---|
28 | using HeuristicLab.Problems.VehicleRouting.Variants;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Problems.VehicleRouting.ProblemInstances {
|
---|
31 | [Item("MDCVRPEvaluator", "Represents a multi depot CVRP evaluator.")]
|
---|
32 | [StorableClass]
|
---|
33 | public class MDCVRPEvaluator : VRPEvaluator {
|
---|
34 | public ILookupParameter<DoubleValue> OverloadParameter {
|
---|
35 | get { return (ILookupParameter<DoubleValue>)Parameters["Overload"]; }
|
---|
36 | }
|
---|
37 |
|
---|
38 | protected override VRPEvaluation CreateTourEvaluation() {
|
---|
39 | return new CVRPEvaluation();
|
---|
40 | }
|
---|
41 |
|
---|
42 | protected override void EvaluateTour(VRPEvaluation eval, IVRPProblemInstance instance, Tour tour, IVRPEncoding solution) {
|
---|
43 | TourInsertionInfo tourInfo = new TourInsertionInfo(solution.GetVehicleAssignment(solution.GetTourIndex(tour)));
|
---|
44 | eval.InsertionInfo.AddTourInsertionInfo(tourInfo);
|
---|
45 | double originalQuality = eval.Quality;
|
---|
46 |
|
---|
47 | IHeterogenousCapacitatedProblemInstance cvrpInstance = instance as IHeterogenousCapacitatedProblemInstance;
|
---|
48 |
|
---|
49 | double delivered = 0.0;
|
---|
50 | double overweight = 0.0;
|
---|
51 | double distance = 0.0;
|
---|
52 |
|
---|
53 | int tourIndex = solution.GetTourIndex(tour);
|
---|
54 | int vehicle = solution.GetVehicleAssignment(tourIndex);
|
---|
55 |
|
---|
56 | double capacity = cvrpInstance.Capacity[vehicle];
|
---|
57 | for (int i = 0; i < tour.Stops.Count; i++) {
|
---|
58 | delivered += instance.GetDemand(tour.Stops[i]);
|
---|
59 | }
|
---|
60 |
|
---|
61 | double spareCapacity = capacity - delivered;
|
---|
62 |
|
---|
63 | //simulate a tour, start and end at depot
|
---|
64 | for (int i = 0; i <= tour.Stops.Count; i++) {
|
---|
65 | int start = 0;
|
---|
66 | if (i > 0)
|
---|
67 | start = tour.Stops[i - 1];
|
---|
68 | int end = 0;
|
---|
69 | if (i < tour.Stops.Count)
|
---|
70 | end = tour.Stops[i];
|
---|
71 |
|
---|
72 | //drive there
|
---|
73 | double currentDistace = instance.GetDistance(start, end, solution);
|
---|
74 | distance += currentDistace;
|
---|
75 |
|
---|
76 | CVRPInsertionInfo stopInfo = new CVRPInsertionInfo(start, end, spareCapacity);
|
---|
77 | tourInfo.AddStopInsertionInfo(stopInfo);
|
---|
78 | }
|
---|
79 |
|
---|
80 | eval.Quality += instance.FleetUsageFactor.Value;
|
---|
81 | eval.Quality += instance.DistanceFactor.Value * distance;
|
---|
82 |
|
---|
83 | eval.Distance += distance;
|
---|
84 | eval.VehicleUtilization += 1;
|
---|
85 |
|
---|
86 | if (delivered > capacity) {
|
---|
87 | overweight = delivered - capacity;
|
---|
88 | }
|
---|
89 |
|
---|
90 | (eval as CVRPEvaluation).Overload += overweight;
|
---|
91 | double penalty = overweight * cvrpInstance.OverloadPenalty.Value;
|
---|
92 | eval.Penalty += penalty;
|
---|
93 | eval.Quality += penalty;
|
---|
94 | tourInfo.Penalty = penalty;
|
---|
95 | tourInfo.Quality = eval.Quality - originalQuality;
|
---|
96 | }
|
---|
97 |
|
---|
98 | protected override double GetTourInsertionCosts(IVRPProblemInstance instance, IVRPEncoding solution, TourInsertionInfo tourInsertionInfo, int index, int customer,
|
---|
99 | out bool feasible) {
|
---|
100 | CVRPInsertionInfo insertionInfo = tourInsertionInfo.GetStopInsertionInfo(index) as CVRPInsertionInfo;
|
---|
101 |
|
---|
102 | double costs = 0;
|
---|
103 | feasible = tourInsertionInfo.Penalty < double.Epsilon;
|
---|
104 |
|
---|
105 | IHeterogenousCapacitatedProblemInstance cvrp = instance as IHeterogenousCapacitatedProblemInstance;
|
---|
106 | double overloadPenalty = cvrp.OverloadPenalty.Value;
|
---|
107 |
|
---|
108 | double startDistance, endDistance;
|
---|
109 | costs += instance.GetInsertionDistance(insertionInfo.Start, customer, insertionInfo.End, solution, out startDistance, out endDistance);
|
---|
110 |
|
---|
111 | double demand = instance.GetDemand(customer);
|
---|
112 | if (demand > insertionInfo.SpareCapacity) {
|
---|
113 | feasible = false;
|
---|
114 |
|
---|
115 | if (insertionInfo.SpareCapacity >= 0)
|
---|
116 | costs += (demand - insertionInfo.SpareCapacity) * overloadPenalty;
|
---|
117 | else
|
---|
118 | costs += demand * overloadPenalty;
|
---|
119 | }
|
---|
120 |
|
---|
121 | return costs;
|
---|
122 | }
|
---|
123 |
|
---|
124 | protected override void InitResultParameters() {
|
---|
125 | base.InitResultParameters();
|
---|
126 |
|
---|
127 | OverloadParameter.ActualValue = new DoubleValue(0);
|
---|
128 | }
|
---|
129 |
|
---|
130 | protected override void SetResultParameters(VRPEvaluation tourEvaluation) {
|
---|
131 | base.SetResultParameters(tourEvaluation);
|
---|
132 |
|
---|
133 | OverloadParameter.ActualValue.Value = (tourEvaluation as CVRPEvaluation).Overload;
|
---|
134 | }
|
---|
135 |
|
---|
136 | [StorableConstructor]
|
---|
137 | protected MDCVRPEvaluator(bool deserializing) : base(deserializing) { }
|
---|
138 |
|
---|
139 | public MDCVRPEvaluator() {
|
---|
140 | Parameters.Add(new LookupParameter<DoubleValue>("Overload", "The overload."));
|
---|
141 | }
|
---|
142 |
|
---|
143 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
144 | return new MDCVRPEvaluator(this, cloner);
|
---|
145 | }
|
---|
146 |
|
---|
147 | protected MDCVRPEvaluator(MDCVRPEvaluator original, Cloner cloner)
|
---|
148 | : base(original, cloner) {
|
---|
149 | }
|
---|
150 | }
|
---|
151 | } |
---|