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