[6851] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12009] | 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[6851] | 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.Collections.Generic;
|
---|
| 23 | using System.Linq;
|
---|
[8053] | 24 | using HeuristicLab.Common;
|
---|
[6851] | 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
| 27 | using HeuristicLab.Optimization;
|
---|
[8053] | 28 | using HeuristicLab.Parameters;
|
---|
| 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[6851] | 30 | using HeuristicLab.PluginInfrastructure;
|
---|
[8053] | 31 | using HeuristicLab.Problems.VehicleRouting.Interfaces;
|
---|
[6851] | 32 | using HeuristicLab.Problems.VehicleRouting.Variants;
|
---|
| 33 |
|
---|
| 34 | namespace HeuristicLab.Problems.VehicleRouting.ProblemInstances {
|
---|
| 35 | [Item("MultiDepotVRPProblemInstance", "Represents a multi depot VRP instance.")]
|
---|
| 36 | [StorableClass]
|
---|
[8053] | 37 | public class MultiDepotVRPProblemInstance : VRPProblemInstance, IMultiDepotProblemInstance {
|
---|
[6851] | 38 | protected IValueParameter<IntValue> DepotsParameter {
|
---|
| 39 | get { return (IValueParameter<IntValue>)Parameters["Depots"]; }
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | protected IValueParameter<IntArray> VehicleDepotAssignmentParameter {
|
---|
| 43 | get { return (IValueParameter<IntArray>)Parameters["VehicleDepotAssignment"]; }
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | public IntValue Depots {
|
---|
| 47 | get {
|
---|
| 48 | return DepotsParameter.Value;
|
---|
| 49 | }
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | public IntArray VehicleDepotAssignment {
|
---|
| 53 | get {
|
---|
| 54 | return VehicleDepotAssignmentParameter.Value;
|
---|
| 55 | }
|
---|
| 56 | set {
|
---|
| 57 | VehicleDepotAssignmentParameter.Value = value;
|
---|
| 58 | }
|
---|
| 59 | }
|
---|
[8053] | 60 |
|
---|
[6851] | 61 | protected override IEnumerable<IOperator> GetOperators() {
|
---|
| 62 | return ApplicationManager.Manager.GetInstances<IMultiDepotOperator>().Cast<IOperator>();
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | protected override IEnumerable<IOperator> GetAnalyzers() {
|
---|
| 66 | return ApplicationManager.Manager.GetInstances<IMultiDepotOperator>()
|
---|
| 67 | .Where(o => o is IAnalyzer)
|
---|
| 68 | .Cast<IOperator>();
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | public override IntValue Cities {
|
---|
| 72 | get {
|
---|
| 73 | return new IntValue(Demand.Length);
|
---|
| 74 | }
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | protected override IVRPEvaluator Evaluator {
|
---|
| 78 | get {
|
---|
| 79 | return new MultiDepotVRPEvaluator();
|
---|
| 80 | }
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | protected override IVRPCreator Creator {
|
---|
| 84 | get {
|
---|
| 85 | return new HeuristicLab.Problems.VehicleRouting.Encodings.Alba.RandomCreator();
|
---|
| 86 | }
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | public override double GetDemand(int city) {
|
---|
| 90 | return base.GetDemand(city - 1);
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | public override double[] GetCoordinates(int city) {
|
---|
| 94 | double[] coordinates;
|
---|
[8053] | 95 |
|
---|
[6851] | 96 | if (city == 0) {
|
---|
| 97 | //calculate centroid
|
---|
| 98 | coordinates = new double[Coordinates.Columns];
|
---|
| 99 |
|
---|
| 100 | for (int i = 0; i < Depots.Value; i++) {
|
---|
| 101 | for (int j = 0; j < coordinates.Length; j++) {
|
---|
| 102 | coordinates[j] += Coordinates[i, j];
|
---|
| 103 | }
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | for (int j = 0; j < coordinates.Length; j++) {
|
---|
| 107 | coordinates[j] /= (double)Depots.Value;
|
---|
| 108 | }
|
---|
| 109 | } else {
|
---|
| 110 | city += Depots.Value - 1;
|
---|
| 111 | coordinates = base.GetCoordinates(city);
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | return coordinates;
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | public int GetDepot(int customer, IVRPEncoding solution) {
|
---|
| 118 | int depot = -1;
|
---|
[8053] | 119 |
|
---|
[6851] | 120 | Tour tour =
|
---|
| 121 | solution.GetTours().FirstOrDefault(t => t.Stops.Contains(customer));
|
---|
| 122 |
|
---|
| 123 | if (tour != null) {
|
---|
| 124 | int tourIndex = solution.GetTourIndex(tour);
|
---|
| 125 | int vehicle = solution.GetVehicleAssignment(tourIndex);
|
---|
| 126 | depot = VehicleDepotAssignment[vehicle];
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | return depot;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | public override double GetDistance(int start, int end, IVRPEncoding solution) {
|
---|
| 133 | if (start == 0 && end == 0)
|
---|
| 134 | return 0;
|
---|
[8053] | 135 |
|
---|
[6851] | 136 | if (start == 0) {
|
---|
| 137 | start = GetDepot(end, solution);
|
---|
| 138 | end += Depots.Value - 1;
|
---|
| 139 | } else if (end == 0) {
|
---|
| 140 | end = GetDepot(start, solution);
|
---|
| 141 | start += Depots.Value - 1;
|
---|
| 142 | } else {
|
---|
| 143 | start += Depots.Value - 1;
|
---|
| 144 | end += Depots.Value - 1;
|
---|
| 145 | }
|
---|
[8053] | 146 |
|
---|
[6851] | 147 | return base.GetDistance(start, end, solution);
|
---|
| 148 | }
|
---|
| 149 |
|
---|
[8053] | 150 | public override double GetInsertionDistance(int start, int customer, int end, IVRPEncoding solution,
|
---|
[6854] | 151 | out double startDistance, out double endDistance) {
|
---|
[6851] | 152 | if (start == 0) {
|
---|
| 153 | start = GetDepot(end, solution);
|
---|
| 154 | end += Depots.Value - 1;
|
---|
| 155 | } else if (end == 0) {
|
---|
| 156 | end = GetDepot(start, solution);
|
---|
| 157 | start += Depots.Value - 1;
|
---|
| 158 | } else {
|
---|
| 159 | start += Depots.Value - 1;
|
---|
| 160 | end += Depots.Value - 1;
|
---|
| 161 | }
|
---|
| 162 | customer += Depots.Value - 1;
|
---|
[8053] | 163 |
|
---|
[6851] | 164 | double distance = base.GetDistance(start, end, solution);
|
---|
| 165 |
|
---|
[6854] | 166 | startDistance = base.GetDistance(start, customer, solution);
|
---|
| 167 | endDistance = base.GetDistance(customer, end, solution);
|
---|
| 168 |
|
---|
| 169 | double newDistance = startDistance + endDistance;
|
---|
| 170 |
|
---|
[6851] | 171 | return newDistance - distance;
|
---|
| 172 | }
|
---|
[8053] | 173 |
|
---|
[6851] | 174 | [StorableConstructor]
|
---|
| 175 | protected MultiDepotVRPProblemInstance(bool deserializing) : base(deserializing) { }
|
---|
| 176 |
|
---|
| 177 | public MultiDepotVRPProblemInstance() {
|
---|
| 178 | Parameters.Add(new ValueParameter<IntValue>("Depots", "The number of depots", new IntValue(0)));
|
---|
| 179 | Parameters.Add(new ValueParameter<IntArray>("VehicleDepotAssignment", "The assignment of vehicles to depots", new IntArray()));
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 183 | return new MultiDepotVRPProblemInstance(this, cloner);
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | protected MultiDepotVRPProblemInstance(MultiDepotVRPProblemInstance original, Cloner cloner)
|
---|
| 187 | : base(original, cloner) {
|
---|
| 188 | }
|
---|
| 189 | }
|
---|
| 190 | } |
---|