Changeset 17711 for branches/2521_ProblemRefactoring/HeuristicLab.Problems.VehicleRouting/3.4/ProblemInstances/MultiDepotVRP/MDCVRP/MDCVRPTW/MDCVRPTWProblemInstance.cs
- Timestamp:
- 08/03/20 18:06:16 (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2521_ProblemRefactoring/HeuristicLab.Problems.VehicleRouting/3.4/ProblemInstances/MultiDepotVRP/MDCVRP/MDCVRPTW/MDCVRPTWProblemInstance.cs
r17226 r17711 23 23 using System.Collections.Generic; 24 24 using System.Linq; 25 using HEAL.Attic; 25 26 using HeuristicLab.Common; 26 27 using HeuristicLab.Core; 27 28 using HeuristicLab.Data; 28 using HeuristicLab.Optimization;29 29 using HeuristicLab.Parameters; 30 using HEAL.Attic;31 using HeuristicLab.PluginInfrastructure;32 30 using HeuristicLab.Problems.VehicleRouting.Interfaces; 33 31 using HeuristicLab.Problems.VehicleRouting.Variants; … … 89 87 } 90 88 91 protected override IEnumerable<IOperator> GetOperators() { 92 return base.GetOperators() 93 .Where(o => o is ITimeWindowedOperator).Cast<IOperator>(); 94 } 95 96 protected override IEnumerable<IOperator> GetAnalyzers() { 97 return ApplicationManager.Manager.GetInstances<ITimeWindowedOperator>() 98 .Where(o => o is IAnalyzer) 99 .Cast<IOperator>().Union(base.GetAnalyzers()); 100 } 101 102 protected override IVRPEvaluator Evaluator { 103 get { 104 return new MDCVRPTWEvaluator(); 105 } 89 public override IEnumerable<IOperator> FilterOperators(IEnumerable<IOperator> operators) { 90 return base.FilterOperators(operators).Where(x => x is ITimeWindowedOperator); 91 } 92 93 protected override VRPEvaluation CreateTourEvaluation() { 94 return new CVRPTWEvaluation(); 95 } 96 97 protected override void EvaluateTour(VRPEvaluation eval, Tour tour, IVRPEncodedSolution solution) { 98 TourInsertionInfo tourInfo = new TourInsertionInfo(solution.GetVehicleAssignment(solution.GetTourIndex(tour))); 99 eval.InsertionInfo.AddTourInsertionInfo(tourInfo); 100 double originalQuality = eval.Quality; 101 102 int depots = Depots.Value; 103 104 double time = 0.0; 105 double waitingTime = 0.0; 106 double serviceTime = 0.0; 107 double tardiness = 0.0; 108 double delivered = 0.0; 109 double overweight = 0.0; 110 double distance = 0.0; 111 112 int tourIndex = solution.GetTourIndex(tour); 113 int vehicle = solution.GetVehicleAssignment(tourIndex); 114 int depot = VehicleDepotAssignment[vehicle]; 115 116 double capacity = Capacity[vehicle]; 117 for (int i = 0; i < tour.Stops.Count; i++) { 118 delivered += GetDemand(tour.Stops[i]); 119 } 120 121 double spareCapacity = capacity - delivered; 122 123 double tourStartTime = ReadyTime[depot]; 124 time = tourStartTime; 125 126 //simulate a tour, start and end at depot 127 for (int i = 0; i <= tour.Stops.Count; i++) { 128 int start = 0; 129 if (i > 0) 130 start = tour.Stops[i - 1]; 131 int end = 0; 132 if (i < tour.Stops.Count) 133 end = tour.Stops[i]; 134 135 //drive there 136 double currentDistace = GetDistance(start, end, solution); 137 time += currentDistace; 138 distance += currentDistace; 139 140 double arrivalTime = time; 141 142 int endIndex; 143 if (end == 0) 144 endIndex = depot; 145 else 146 endIndex = end + depots - 1; 147 148 //check if it was serviced on time 149 if (time > DueTime[endIndex]) 150 tardiness += time - DueTime[endIndex]; 151 152 //wait 153 double currentWaitingTime = 0.0; 154 if (time < ReadyTime[endIndex]) 155 currentWaitingTime = ReadyTime[endIndex] - time; 156 157 double waitTime = ReadyTime[endIndex] - time; 158 159 waitingTime += currentWaitingTime; 160 time += currentWaitingTime; 161 162 double spareTime = DueTime[endIndex] - time; 163 164 //service 165 double currentServiceTime = 0; 166 if (end > 0) 167 currentServiceTime = ServiceTime[end - 1]; 168 serviceTime += currentServiceTime; 169 time += currentServiceTime; 170 171 CVRPTWInsertionInfo stopInfo = new CVRPTWInsertionInfo(start, end, spareCapacity, tourStartTime, arrivalTime, time, spareTime, waitTime); 172 tourInfo.AddStopInsertionInfo(stopInfo); 173 } 174 175 eval.Quality += FleetUsageFactor.Value; 176 eval.Quality += DistanceFactor.Value * distance; 177 eval.Distance += distance; 178 eval.VehicleUtilization += 1; 179 180 if (delivered > capacity) { 181 overweight = delivered - capacity; 182 } 183 184 (eval as CVRPEvaluation).Overload += overweight; 185 double tourPenalty = 0; 186 double penalty = overweight * OverloadPenalty.Value; 187 eval.Penalty += penalty; 188 eval.Quality += penalty; 189 tourPenalty += penalty; 190 191 (eval as CVRPTWEvaluation).Tardiness += tardiness; 192 (eval as CVRPTWEvaluation).TravelTime += time; 193 194 penalty = tardiness * TardinessPenalty.Value; 195 eval.Penalty += penalty; 196 eval.Quality += penalty; 197 tourPenalty += penalty; 198 eval.Quality += time * TimeFactor.Value; 199 tourInfo.Penalty = tourPenalty; 200 tourInfo.Quality = eval.Quality - originalQuality; 201 202 eval.IsFeasible = overweight == 0 && tardiness == 0; 203 } 204 205 protected override double GetTourInsertionCosts(IVRPEncodedSolution solution, TourInsertionInfo tourInsertionInfo, int index, int customer, 206 out bool feasible) { 207 CVRPTWInsertionInfo insertionInfo = tourInsertionInfo.GetStopInsertionInfo(index) as CVRPTWInsertionInfo; 208 209 double costs = 0; 210 feasible = tourInsertionInfo.Penalty < double.Epsilon; 211 212 double overloadPenalty = OverloadPenalty.Value; 213 double tardinessPenalty = TardinessPenalty.Value; 214 int depots = Depots.Value; 215 216 double startDistance, endDistance; 217 costs += GetInsertionDistance(insertionInfo.Start, customer, insertionInfo.End, solution, out startDistance, out endDistance); 218 219 double demand = GetDemand(customer); 220 if (demand > insertionInfo.SpareCapacity) { 221 feasible = false; 222 223 if (insertionInfo.SpareCapacity >= 0) 224 costs += (demand - insertionInfo.SpareCapacity) * overloadPenalty; 225 else 226 costs += demand * overloadPenalty; 227 } 228 229 double time = 0; 230 double tardiness = 0; 231 232 if (index > 0) 233 time = (tourInsertionInfo.GetStopInsertionInfo(index - 1) as CVRPTWInsertionInfo).LeaveTime; 234 else 235 time = insertionInfo.TourStartTime; 236 237 time += startDistance; 238 239 int customerIndex = customer + depots - 1; 240 241 if (time > DueTime[customerIndex]) { 242 tardiness += time - DueTime[customerIndex]; 243 } 244 if (time < ReadyTime[customerIndex]) 245 time += ReadyTime[customerIndex] - time; 246 time += ServiceTime[customer - 1]; 247 time += endDistance; 248 249 double additionalTime = time - (tourInsertionInfo.GetStopInsertionInfo(index) as CVRPTWInsertionInfo).ArrivalTime; 250 for (int i = index; i < tourInsertionInfo.GetStopCount(); i++) { 251 CVRPTWInsertionInfo nextStop = tourInsertionInfo.GetStopInsertionInfo(i) as CVRPTWInsertionInfo; 252 253 if (additionalTime < 0) { 254 //arrive earlier than before 255 //wait probably 256 if (nextStop.WaitingTime < 0) { 257 double wait = nextStop.WaitingTime - additionalTime; 258 if (wait > 0) 259 additionalTime += wait; 260 } else { 261 additionalTime = 0; 262 } 263 264 //check due date, decrease tardiness 265 if (nextStop.SpareTime < 0) { 266 costs += Math.Max(nextStop.SpareTime, additionalTime) * tardinessPenalty; 267 } 268 } else { 269 //arrive later than before, probably don't have to wait 270 if (nextStop.WaitingTime > 0) { 271 additionalTime -= Math.Min(additionalTime, nextStop.WaitingTime); 272 } 273 274 //check due date 275 if (nextStop.SpareTime > 0) { 276 double spare = nextStop.SpareTime - additionalTime; 277 if (spare < 0) 278 tardiness += -spare; 279 } else { 280 tardiness += additionalTime; 281 } 282 } 283 } 284 285 costs += additionalTime * TimeFactor.Value; 286 287 if (tardiness > 0) { 288 feasible = false; 289 } 290 291 costs += tardiness * tardinessPenalty; 292 293 return costs; 106 294 } 107 295
Note: See TracChangeset
for help on using the changeset viewer.