[4363] | 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;
|
---|
[4752] | 35 | using HeuristicLab.Common;
|
---|
[4363] | 36 |
|
---|
| 37 | namespace HeuristicLab.Problems.VehicleRouting.ProblemInstances {
|
---|
| 38 | [Item("CVRPEvaluator", "Represents a single depot CVRP evaluator.")]
|
---|
| 39 | [StorableClass]
|
---|
[6851] | 40 | public class CVRPEvaluator: VRPEvaluator {
|
---|
[4363] | 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 |
|
---|
[6838] | 49 | protected override void EvaluateTour(VRPEvaluation eval, IVRPProblemInstance instance, Tour tour, IVRPEncoding solution) {
|
---|
[6752] | 50 | TourInsertionInfo tourInfo = new TourInsertionInfo();
|
---|
| 51 | eval.InsertionInfo.AddTourInsertionInfo(tourInfo);
|
---|
| 52 |
|
---|
[4376] | 53 | IHomogenousCapacitatedProblemInstance cvrpInstance = instance as IHomogenousCapacitatedProblemInstance;
|
---|
[4377] | 54 | DoubleArray demand = instance.Demand;
|
---|
[4363] | 55 |
|
---|
| 56 | double delivered = 0.0;
|
---|
| 57 | double overweight = 0.0;
|
---|
[4377] | 58 | double distance = 0.0;
|
---|
[4363] | 59 |
|
---|
[6752] | 60 | double capacity = cvrpInstance.Capacity.Value;
|
---|
| 61 | for (int i = 0; i <= tour.Stops.Count; i++) {
|
---|
| 62 | int end = 0;
|
---|
| 63 | if (i < tour.Stops.Count)
|
---|
| 64 | end = tour.Stops[i];
|
---|
| 65 |
|
---|
| 66 | delivered += demand[end];
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | double spareCapacity = capacity - delivered;
|
---|
| 70 |
|
---|
[4377] | 71 | //simulate a tour, start and end at depot
|
---|
| 72 | for (int i = 0; i <= tour.Stops.Count; i++) {
|
---|
| 73 | int start = 0;
|
---|
| 74 | if (i > 0)
|
---|
| 75 | start = tour.Stops[i - 1];
|
---|
| 76 | int end = 0;
|
---|
| 77 | if (i < tour.Stops.Count)
|
---|
| 78 | end = tour.Stops[i];
|
---|
| 79 |
|
---|
| 80 | //drive there
|
---|
[6851] | 81 | double currentDistace = instance.GetDistance(start, end, solution);
|
---|
[4377] | 82 | distance += currentDistace;
|
---|
| 83 |
|
---|
[6752] | 84 | CVRPInsertionInfo stopInfo = new CVRPInsertionInfo(start, end, spareCapacity);
|
---|
| 85 | tourInfo.AddStopInsertionInfo(stopInfo);
|
---|
[4363] | 86 | }
|
---|
| 87 |
|
---|
[4377] | 88 | eval.Quality += instance.FleetUsageFactor.Value;
|
---|
| 89 | eval.Quality += instance.DistanceFactor.Value * distance;
|
---|
| 90 |
|
---|
[4378] | 91 | eval.Distance += distance;
|
---|
| 92 | eval.VehicleUtilization += 1;
|
---|
[4377] | 93 |
|
---|
[4363] | 94 | if (delivered > capacity) {
|
---|
| 95 | overweight = delivered - capacity;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
[4378] | 98 | (eval as CVRPEvaluation).Overload += overweight;
|
---|
[4363] | 99 | double penalty = overweight * cvrpInstance.OverloadPenalty.Value;
|
---|
| 100 | eval.Penalty += penalty;
|
---|
| 101 | eval.Quality += penalty;
|
---|
[6752] | 102 | tourInfo.Penalty = penalty;
|
---|
[4363] | 103 | }
|
---|
| 104 |
|
---|
[6851] | 105 | protected override double GetTourInsertionCosts(IVRPProblemInstance instance, IVRPEncoding solution, TourInsertionInfo tourInsertionInfo, int index, int customer,
|
---|
[6752] | 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 | ICapacitatedProblemInstance cvrp = instance as ICapacitatedProblemInstance;
|
---|
| 113 | double overloadPenalty = cvrp.OverloadPenalty.Value;
|
---|
| 114 |
|
---|
[6851] | 115 | double distance = instance.GetDistance(insertionInfo.Start, insertionInfo.End, solution);
|
---|
[6752] | 116 | double newDistance =
|
---|
[6851] | 117 | instance.GetDistance(insertionInfo.Start, customer, solution) +
|
---|
| 118 | instance.GetDistance(customer, insertionInfo.End, solution);
|
---|
[6752] | 119 | costs += instance.DistanceFactor.Value * (newDistance - distance);
|
---|
| 120 |
|
---|
| 121 | double demand = instance.Demand[customer];
|
---|
| 122 | if (demand > insertionInfo.SpareCapacity) {
|
---|
| 123 | feasible = false;
|
---|
| 124 |
|
---|
| 125 | if(insertionInfo.SpareCapacity >= 0)
|
---|
| 126 | costs += (demand - insertionInfo.SpareCapacity) * overloadPenalty;
|
---|
| 127 | else
|
---|
| 128 | costs += demand * overloadPenalty;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | return costs;
|
---|
| 132 | }
|
---|
| 133 |
|
---|
[4363] | 134 | protected override void InitResultParameters() {
|
---|
| 135 | base.InitResultParameters();
|
---|
| 136 |
|
---|
| 137 | OverloadParameter.ActualValue = new DoubleValue(0);
|
---|
| 138 | }
|
---|
| 139 |
|
---|
[4520] | 140 | protected override void SetResultParameters(VRPEvaluation tourEvaluation) {
|
---|
| 141 | base.SetResultParameters(tourEvaluation);
|
---|
[4363] | 142 |
|
---|
[4520] | 143 | OverloadParameter.ActualValue.Value = (tourEvaluation as CVRPEvaluation).Overload;
|
---|
[4363] | 144 | }
|
---|
| 145 |
|
---|
| 146 | [StorableConstructor]
|
---|
| 147 | protected CVRPEvaluator(bool deserializing) : base(deserializing) { }
|
---|
| 148 |
|
---|
| 149 | public CVRPEvaluator() {
|
---|
| 150 | Parameters.Add(new LookupParameter<DoubleValue>("Overload", "The overload."));
|
---|
| 151 | }
|
---|
[4752] | 152 |
|
---|
| 153 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 154 | return new CVRPEvaluator(this, cloner);
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | protected CVRPEvaluator(CVRPEvaluator original, Cloner cloner)
|
---|
| 158 | : base(original, cloner) {
|
---|
| 159 | }
|
---|
[4363] | 160 | }
|
---|
| 161 | } |
---|