1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 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 HEAL.Attic;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.Data;
|
---|
29 | using HeuristicLab.Parameters;
|
---|
30 | using HeuristicLab.Problems.VehicleRouting.Interfaces;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Problems.VehicleRouting.ProblemInstances {
|
---|
33 | [Item("CVRPProblemInstance", "Represents a single depot CVRP instance.")]
|
---|
34 | [StorableType("CBE1D39B-9BBE-4119-801B-32739D1E8DEE")]
|
---|
35 | public class CVRPProblemInstance : SingleDepotVRPProblemInstance, IHomogenousCapacitatedProblemInstance {
|
---|
36 | protected IValueParameter<DoubleValue> CapacityParameter {
|
---|
37 | get { return (IValueParameter<DoubleValue>)Parameters["Capacity"]; }
|
---|
38 | }
|
---|
39 | protected IValueParameter<DoubleValue> OverloadPenaltyParameter {
|
---|
40 | get { return (IValueParameter<DoubleValue>)Parameters["EvalOverloadPenalty"]; }
|
---|
41 | }
|
---|
42 |
|
---|
43 | public DoubleValue Capacity {
|
---|
44 | get { return CapacityParameter.Value; }
|
---|
45 | set { CapacityParameter.Value = value; }
|
---|
46 | }
|
---|
47 |
|
---|
48 | protected IValueParameter<DoubleValue> CurrentOverloadPenaltyParameter {
|
---|
49 | get { return (IValueParameter<DoubleValue>)Parameters["CurrentOverloadPenalty"]; }
|
---|
50 | }
|
---|
51 |
|
---|
52 | public DoubleValue OverloadPenalty {
|
---|
53 | get {
|
---|
54 | DoubleValue currentOverloadPenalty = CurrentOverloadPenaltyParameter.Value;
|
---|
55 | if (currentOverloadPenalty != null)
|
---|
56 | return currentOverloadPenalty;
|
---|
57 | else
|
---|
58 | return OverloadPenaltyParameter.Value;
|
---|
59 | }
|
---|
60 | }
|
---|
61 | DoubleValue ICapacitatedProblemInstance.CurrentOverloadPenalty {
|
---|
62 | get { return CurrentOverloadPenaltyParameter.Value; }
|
---|
63 | set { CurrentOverloadPenaltyParameter.Value = value; }
|
---|
64 | }
|
---|
65 |
|
---|
66 | public override IEnumerable<IOperator> FilterOperators(IEnumerable<IOperator> operators) {
|
---|
67 | return base.FilterOperators(operators)
|
---|
68 | .Where(x => !(x is INotCapacitatedOperator))
|
---|
69 | .Union(operators.Where(x => x is IHomogenousCapacitatedOperator
|
---|
70 | || x is ICapacitatedOperator && !(x is IHeterogenousCapacitatedOperator)));
|
---|
71 | }
|
---|
72 | protected override VRPEvaluation CreateTourEvaluation() {
|
---|
73 | return new CVRPEvaluation();
|
---|
74 | }
|
---|
75 |
|
---|
76 | protected override void EvaluateTour(VRPEvaluation eval, Tour tour, IVRPEncodedSolution solution) {
|
---|
77 | TourInsertionInfo tourInfo = new TourInsertionInfo(solution.GetVehicleAssignment(solution.GetTourIndex(tour))); ;
|
---|
78 | eval.InsertionInfo.AddTourInsertionInfo(tourInfo);
|
---|
79 | double originalQuality = eval.Quality;
|
---|
80 |
|
---|
81 | double delivered = 0.0;
|
---|
82 | double overweight = 0.0;
|
---|
83 | double distance = 0.0;
|
---|
84 |
|
---|
85 | double capacity = Capacity.Value;
|
---|
86 | for (int i = 0; i <= tour.Stops.Count; i++) {
|
---|
87 | int end = 0;
|
---|
88 | if (i < tour.Stops.Count)
|
---|
89 | end = tour.Stops[i];
|
---|
90 |
|
---|
91 | delivered += Demand[end];
|
---|
92 | }
|
---|
93 |
|
---|
94 | double spareCapacity = capacity - delivered;
|
---|
95 |
|
---|
96 | //simulate a tour, start and end at depot
|
---|
97 | for (int i = 0; i <= tour.Stops.Count; i++) {
|
---|
98 | int start = 0;
|
---|
99 | if (i > 0)
|
---|
100 | start = tour.Stops[i - 1];
|
---|
101 | int end = 0;
|
---|
102 | if (i < tour.Stops.Count)
|
---|
103 | end = tour.Stops[i];
|
---|
104 |
|
---|
105 | //drive there
|
---|
106 | double currentDistace = GetDistance(start, end, solution);
|
---|
107 | distance += currentDistace;
|
---|
108 |
|
---|
109 | CVRPInsertionInfo stopInfo = new CVRPInsertionInfo(start, end, spareCapacity);
|
---|
110 | tourInfo.AddStopInsertionInfo(stopInfo);
|
---|
111 | }
|
---|
112 |
|
---|
113 | eval.Quality += FleetUsageFactor.Value;
|
---|
114 | eval.Quality += DistanceFactor.Value * distance;
|
---|
115 |
|
---|
116 | eval.Distance += distance;
|
---|
117 | eval.VehicleUtilization += 1;
|
---|
118 |
|
---|
119 | if (delivered > capacity) {
|
---|
120 | overweight = delivered - capacity;
|
---|
121 | }
|
---|
122 |
|
---|
123 | (eval as CVRPEvaluation).Overload += overweight;
|
---|
124 | double penalty = overweight * OverloadPenalty.Value;
|
---|
125 | eval.Penalty += penalty;
|
---|
126 | eval.Quality += penalty;
|
---|
127 | tourInfo.Penalty = penalty;
|
---|
128 | tourInfo.Quality = eval.Quality - originalQuality;
|
---|
129 |
|
---|
130 | eval.IsFeasible = overweight == 0;
|
---|
131 | }
|
---|
132 |
|
---|
133 | protected override double GetTourInsertionCosts(IVRPEncodedSolution solution, TourInsertionInfo tourInsertionInfo, int index, int customer,
|
---|
134 | out bool feasible) {
|
---|
135 | CVRPInsertionInfo insertionInfo = tourInsertionInfo.GetStopInsertionInfo(index) as CVRPInsertionInfo;
|
---|
136 |
|
---|
137 | double costs = 0;
|
---|
138 | feasible = tourInsertionInfo.Penalty < double.Epsilon;
|
---|
139 |
|
---|
140 | double overloadPenalty = OverloadPenalty.Value;
|
---|
141 |
|
---|
142 | double distance = GetDistance(insertionInfo.Start, insertionInfo.End, solution);
|
---|
143 | double newDistance =
|
---|
144 | GetDistance(insertionInfo.Start, customer, solution) +
|
---|
145 | GetDistance(customer, insertionInfo.End, solution);
|
---|
146 | costs += DistanceFactor.Value * (newDistance - distance);
|
---|
147 |
|
---|
148 | double demand = Demand[customer];
|
---|
149 | if (demand > insertionInfo.SpareCapacity) {
|
---|
150 | feasible = false;
|
---|
151 |
|
---|
152 | if (insertionInfo.SpareCapacity >= 0)
|
---|
153 | costs += (demand - insertionInfo.SpareCapacity) * overloadPenalty;
|
---|
154 | else
|
---|
155 | costs += demand * overloadPenalty;
|
---|
156 | }
|
---|
157 |
|
---|
158 | return costs;
|
---|
159 | }
|
---|
160 |
|
---|
161 | [StorableConstructor]
|
---|
162 | protected CVRPProblemInstance(StorableConstructorFlag _) : base(_) { }
|
---|
163 |
|
---|
164 | public CVRPProblemInstance() {
|
---|
165 | Parameters.Add(new ValueParameter<DoubleValue>("Capacity", "The capacity of each vehicle.", new DoubleValue(0)));
|
---|
166 | Parameters.Add(new ValueParameter<DoubleValue>("EvalOverloadPenalty", "The overload penalty considered in the evaluation.", new DoubleValue(100)));
|
---|
167 | Parameters.Add(new OptionalValueParameter<DoubleValue>("CurrentOverloadPenalty", "The current overload penalty considered in the evaluation.") { Hidden = true });
|
---|
168 |
|
---|
169 | AttachEventHandlers();
|
---|
170 | }
|
---|
171 |
|
---|
172 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
173 | return new CVRPProblemInstance(this, cloner);
|
---|
174 | }
|
---|
175 |
|
---|
176 | protected CVRPProblemInstance(CVRPProblemInstance original, Cloner cloner)
|
---|
177 | : base(original, cloner) {
|
---|
178 | AttachEventHandlers();
|
---|
179 | }
|
---|
180 |
|
---|
181 | [StorableHook(HookType.AfterDeserialization)]
|
---|
182 | private void AfterDeserialization() {
|
---|
183 | AttachEventHandlers();
|
---|
184 | }
|
---|
185 |
|
---|
186 | private void AttachEventHandlers() {
|
---|
187 | CapacityParameter.ValueChanged += new EventHandler(CapacityParameter_ValueChanged);
|
---|
188 | CapacityParameter.Value.ValueChanged += new EventHandler(Capacity_ValueChanged);
|
---|
189 | OverloadPenaltyParameter.ValueChanged += new EventHandler(OverloadPenaltyParameter_ValueChanged);
|
---|
190 | OverloadPenaltyParameter.Value.ValueChanged += new EventHandler(OverloadPenalty_ValueChanged);
|
---|
191 | }
|
---|
192 |
|
---|
193 | public override void InitializeState() {
|
---|
194 | base.InitializeState();
|
---|
195 |
|
---|
196 | CurrentOverloadPenaltyParameter.Value = null;
|
---|
197 | }
|
---|
198 |
|
---|
199 | #region Event handlers
|
---|
200 | void CapacityParameter_ValueChanged(object sender, EventArgs e) {
|
---|
201 | CapacityParameter.Value.ValueChanged += new EventHandler(Capacity_ValueChanged);
|
---|
202 | EvalBestKnownSolution();
|
---|
203 | }
|
---|
204 | void Capacity_ValueChanged(object sender, EventArgs e) {
|
---|
205 | EvalBestKnownSolution();
|
---|
206 | }
|
---|
207 | void OverloadPenaltyParameter_ValueChanged(object sender, EventArgs e) {
|
---|
208 | OverloadPenaltyParameter.Value.ValueChanged += new EventHandler(OverloadPenalty_ValueChanged);
|
---|
209 | EvalBestKnownSolution();
|
---|
210 | }
|
---|
211 | void OverloadPenalty_ValueChanged(object sender, EventArgs e) {
|
---|
212 | EvalBestKnownSolution();
|
---|
213 | }
|
---|
214 | #endregion
|
---|
215 | }
|
---|
216 | } |
---|