[4360] | 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.Drawing;
|
---|
| 25 | using System.Linq;
|
---|
| 26 | using HeuristicLab.Common;
|
---|
| 27 | using HeuristicLab.Core;
|
---|
| 28 | using HeuristicLab.Data;
|
---|
| 29 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
| 30 | using HeuristicLab.Optimization;
|
---|
| 31 | using HeuristicLab.Parameters;
|
---|
| 32 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 33 | using HeuristicLab.PluginInfrastructure;
|
---|
[4362] | 34 | using HeuristicLab.Problems.VehicleRouting.Interfaces;
|
---|
| 35 | using HeuristicLab.Problems.VehicleRouting.Parsers;
|
---|
| 36 | using HeuristicLab.Problems.VehicleRouting.ProblemInstances;
|
---|
[4365] | 37 | using HeuristicLab.Problems.VehicleRouting.Variants;
|
---|
[4360] | 38 |
|
---|
| 39 | namespace HeuristicLab.Problems.VehicleRouting {
|
---|
| 40 | [Item("Vehicle Routing Problem", "Represents a Vehicle Routing Problem.")]
|
---|
| 41 | [Creatable("Problems")]
|
---|
| 42 | [StorableClass]
|
---|
[5867] | 43 | public sealed class VehicleRoutingProblem : ParameterizedNamedItem, ISingleObjectiveHeuristicOptimizationProblem, IStorableContent {
|
---|
[4444] | 44 | public string Filename { get; set; }
|
---|
| 45 |
|
---|
[4360] | 46 | public override Image ItemImage {
|
---|
[5867] | 47 | get { return HeuristicLab.Common.Resources.VSImageLibrary.Type; }
|
---|
[4360] | 48 | }
|
---|
| 49 |
|
---|
[4362] | 50 | #region Parameter Properties
|
---|
| 51 | public ValueParameter<BoolValue> MaximizationParameter {
|
---|
| 52 | get { return (ValueParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
[4360] | 53 | }
|
---|
[5867] | 54 | IParameter ISingleObjectiveHeuristicOptimizationProblem.MaximizationParameter {
|
---|
[4362] | 55 | get { return MaximizationParameter; }
|
---|
[4360] | 56 | }
|
---|
[4362] | 57 | public ValueParameter<IVRPProblemInstance> ProblemInstanceParameter {
|
---|
| 58 | get { return (ValueParameter<IVRPProblemInstance>)Parameters["ProblemInstance"]; }
|
---|
| 59 | }
|
---|
| 60 | public OptionalValueParameter<DoubleValue> BestKnownQualityParameter {
|
---|
| 61 | get { return (OptionalValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
|
---|
| 62 | }
|
---|
[5867] | 63 | IParameter ISingleObjectiveHeuristicOptimizationProblem.BestKnownQualityParameter {
|
---|
[4362] | 64 | get { return BestKnownQualityParameter; }
|
---|
| 65 | }
|
---|
[4860] | 66 | public OptionalValueParameter<VRPSolution> BestKnownSolutionParameter {
|
---|
| 67 | get { return (OptionalValueParameter<VRPSolution>)Parameters["BestKnownSolution"]; }
|
---|
| 68 | }
|
---|
[4365] | 69 | public IValueParameter<IVRPCreator> SolutionCreatorParameter {
|
---|
| 70 | get { return (IValueParameter<IVRPCreator>)Parameters["SolutionCreator"]; }
|
---|
[4360] | 71 | }
|
---|
[5867] | 72 | IParameter IHeuristicOptimizationProblem.SolutionCreatorParameter {
|
---|
[4365] | 73 | get { return SolutionCreatorParameter; }
|
---|
[4362] | 74 | }
|
---|
[4365] | 75 | public IValueParameter<IVRPEvaluator> EvaluatorParameter {
|
---|
| 76 | get { return (IValueParameter<IVRPEvaluator>)Parameters["Evaluator"]; }
|
---|
| 77 | }
|
---|
[5867] | 78 | IParameter IHeuristicOptimizationProblem.EvaluatorParameter {
|
---|
[4365] | 79 | get { return EvaluatorParameter; }
|
---|
| 80 | }
|
---|
[4362] | 81 | #endregion
|
---|
[4360] | 82 |
|
---|
[4362] | 83 | #region Properties
|
---|
| 84 | public IVRPProblemInstance ProblemInstance {
|
---|
| 85 | get { return ProblemInstanceParameter.Value; }
|
---|
| 86 | set { ProblemInstanceParameter.Value = value; }
|
---|
[4360] | 87 | }
|
---|
| 88 |
|
---|
[4860] | 89 | public VRPSolution BestKnownSolution {
|
---|
| 90 | get { return BestKnownSolutionParameter.Value; }
|
---|
| 91 | set { BestKnownSolutionParameter.Value = value; }
|
---|
| 92 | }
|
---|
| 93 |
|
---|
[4362] | 94 | public ISingleObjectiveEvaluator Evaluator {
|
---|
[4365] | 95 | get { return ProblemInstance.EvaluatorParameter.Value; }
|
---|
[4360] | 96 | }
|
---|
| 97 |
|
---|
[5867] | 98 | IEvaluator IHeuristicOptimizationProblem.Evaluator {
|
---|
[4362] | 99 | get { return this.Evaluator; }
|
---|
[4360] | 100 | }
|
---|
| 101 |
|
---|
[4362] | 102 | public ISolutionCreator SolutionCreator {
|
---|
[4365] | 103 | get { return ProblemInstance.SolutionCreatorParameter.Value; }
|
---|
[4360] | 104 | }
|
---|
| 105 |
|
---|
[4365] | 106 | [Storable]
|
---|
| 107 | private List<IOperator> operators;
|
---|
| 108 |
|
---|
[4362] | 109 | public IEnumerable<IOperator> Operators {
|
---|
[4365] | 110 | get { return operators; }
|
---|
[4360] | 111 | }
|
---|
| 112 | #endregion
|
---|
| 113 |
|
---|
| 114 | [StorableConstructor]
|
---|
| 115 | private VehicleRoutingProblem(bool deserializing) : base(deserializing) { }
|
---|
| 116 | public VehicleRoutingProblem()
|
---|
| 117 | : base() {
|
---|
| 118 | Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to false as the Vehicle Routing Problem is a minimization problem.", new BoolValue(false)));
|
---|
[4362] | 119 | Parameters.Add(new ValueParameter<IVRPProblemInstance>("ProblemInstance", "The VRP problem instance"));
|
---|
| 120 | Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this VRP instance."));
|
---|
[4860] | 121 | Parameters.Add(new OptionalValueParameter<VRPSolution>("BestKnownSolution", "The best known solution of this VRP instance."));
|
---|
[4365] | 122 |
|
---|
| 123 | operators = new List<IOperator>();
|
---|
| 124 |
|
---|
[4360] | 125 | InitializeRandomVRPInstance();
|
---|
[4365] | 126 | InitializeOperators();
|
---|
[4360] | 127 |
|
---|
| 128 | AttachEventHandlers();
|
---|
[4362] | 129 | AttachProblemInstanceEventHandlers();
|
---|
[4360] | 130 | }
|
---|
| 131 |
|
---|
| 132 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[4752] | 133 | return new VehicleRoutingProblem(this, cloner);
|
---|
[4360] | 134 | }
|
---|
| 135 |
|
---|
[4752] | 136 | private VehicleRoutingProblem(VehicleRoutingProblem original, Cloner cloner)
|
---|
| 137 | : base(original, cloner) {
|
---|
| 138 | this.operators = original.operators.Select(x => (IOperator)cloner.Clone(x)).ToList();
|
---|
| 139 | this.AttachEventHandlers();
|
---|
| 140 | }
|
---|
| 141 |
|
---|
[4360] | 142 | #region Events
|
---|
| 143 | public event EventHandler SolutionCreatorChanged;
|
---|
| 144 | private void OnSolutionCreatorChanged() {
|
---|
| 145 | EventHandler handler = SolutionCreatorChanged;
|
---|
| 146 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 147 | }
|
---|
| 148 | public event EventHandler EvaluatorChanged;
|
---|
| 149 | private void OnEvaluatorChanged() {
|
---|
| 150 | EventHandler handler = EvaluatorChanged;
|
---|
| 151 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 152 | }
|
---|
| 153 | public event EventHandler OperatorsChanged;
|
---|
| 154 | private void OnOperatorsChanged() {
|
---|
| 155 | EventHandler handler = OperatorsChanged;
|
---|
| 156 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 157 | }
|
---|
| 158 | public event EventHandler Reset;
|
---|
| 159 | private void OnReset() {
|
---|
| 160 | EventHandler handler = Reset;
|
---|
| 161 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 162 | }
|
---|
| 163 | #endregion
|
---|
| 164 |
|
---|
| 165 | #region Helpers
|
---|
| 166 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 167 | private void AfterDeserializationHook() {
|
---|
| 168 | AttachEventHandlers();
|
---|
[4362] | 169 | AttachProblemInstanceEventHandlers();
|
---|
[4360] | 170 | }
|
---|
| 171 |
|
---|
| 172 | private void AttachEventHandlers() {
|
---|
[4362] | 173 | ProblemInstanceParameter.ValueChanged += new EventHandler(ProblemInstanceParameter_ValueChanged);
|
---|
[4360] | 174 | }
|
---|
[4362] | 175 |
|
---|
| 176 | private void AttachProblemInstanceEventHandlers() {
|
---|
[4365] | 177 | if (Parameters.ContainsKey("Evaluator"))
|
---|
| 178 | Parameters.Remove("Evaluator");
|
---|
| 179 |
|
---|
| 180 | if (Parameters.ContainsKey("SolutionCreator"))
|
---|
| 181 | Parameters.Remove("SolutionCreator");
|
---|
| 182 |
|
---|
[4860] | 183 | if (Parameters.ContainsKey("BestKnownSolution"))
|
---|
| 184 | Parameters.Remove("BestKnownSolution");
|
---|
| 185 |
|
---|
| 186 | if (Parameters.ContainsKey("BestKnownQuality"))
|
---|
| 187 | Parameters.Remove("BestKnownQuality");
|
---|
| 188 |
|
---|
| 189 |
|
---|
[4362] | 190 | if (ProblemInstance != null) {
|
---|
| 191 | ProblemInstance.SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
|
---|
| 192 | ProblemInstance.EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
|
---|
[4365] | 193 | Parameters.Add(ProblemInstance.EvaluatorParameter);
|
---|
| 194 | Parameters.Add(ProblemInstance.SolutionCreatorParameter);
|
---|
[4860] | 195 |
|
---|
| 196 | Parameters.Add(ProblemInstance.BestKnownQualityParameter);
|
---|
| 197 | Parameters.Add(ProblemInstance.BestKnownSolutionParameter);
|
---|
[4362] | 198 | }
|
---|
[4360] | 199 | }
|
---|
[4362] | 200 |
|
---|
| 201 | void ProblemInstanceParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 202 | AttachProblemInstanceEventHandlers();
|
---|
[4365] | 203 | InitializeOperators();
|
---|
| 204 |
|
---|
| 205 | OnSolutionCreatorChanged();
|
---|
| 206 | OnEvaluatorChanged();
|
---|
| 207 | OnOperatorsChanged();
|
---|
[4360] | 208 | }
|
---|
[4362] | 209 | private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
[4365] | 210 | ParameterizeSolutionCreator();
|
---|
| 211 |
|
---|
[4362] | 212 | OnSolutionCreatorChanged();
|
---|
[4360] | 213 | }
|
---|
[4362] | 214 | private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 215 | OnEvaluatorChanged();
|
---|
[4360] | 216 | }
|
---|
[4365] | 217 |
|
---|
| 218 | private void InitializeOperators() {
|
---|
| 219 | operators = new List<IOperator>();
|
---|
| 220 |
|
---|
| 221 | if (ProblemInstance != null) {
|
---|
| 222 | operators.AddRange(
|
---|
| 223 | ProblemInstance.Operators.Concat(
|
---|
| 224 | ApplicationManager.Manager.GetInstances<IGeneralVRPOperator>().Cast<IOperator>()).OrderBy(op => op.Name));
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 | ParameterizeOperators();
|
---|
| 228 | }
|
---|
| 229 |
|
---|
| 230 | private void ParameterizeSolutionCreator() {
|
---|
| 231 | if (SolutionCreator is IMultiVRPOperator) {
|
---|
| 232 | (SolutionCreator as IMultiVRPOperator).SetOperators(Operators);
|
---|
| 233 | }
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | private void ParameterizeOperators() {
|
---|
| 237 | foreach (IOperator op in Operators) {
|
---|
| 238 | if (op is IMultiVRPOperator) {
|
---|
| 239 | (op as IMultiVRPOperator).SetOperators(Operators);
|
---|
| 240 | }
|
---|
| 241 | }
|
---|
| 242 | }
|
---|
[4360] | 243 | #endregion
|
---|
| 244 |
|
---|
| 245 | public void ImportFromSolomon(string solomonFileName) {
|
---|
| 246 | SolomonParser parser = new SolomonParser(solomonFileName);
|
---|
| 247 | parser.Parse();
|
---|
| 248 |
|
---|
| 249 | this.Name = parser.ProblemName;
|
---|
[4362] | 250 | CVRPTWProblemInstance problem = new CVRPTWProblemInstance();
|
---|
| 251 |
|
---|
| 252 | problem.Coordinates = new DoubleMatrix(parser.Coordinates);
|
---|
| 253 | problem.Vehicles.Value = parser.Vehicles;
|
---|
| 254 | problem.Capacity.Value = parser.Capacity;
|
---|
| 255 | problem.Demand = new DoubleArray(parser.Demands);
|
---|
| 256 | problem.ReadyTime = new DoubleArray(parser.Readytimes);
|
---|
| 257 | problem.DueTime = new DoubleArray(parser.Duetimes);
|
---|
| 258 | problem.ServiceTime = new DoubleArray(parser.Servicetimes);
|
---|
[4360] | 259 |
|
---|
[4362] | 260 | this.ProblemInstance = problem;
|
---|
| 261 |
|
---|
[4360] | 262 | OnReset();
|
---|
| 263 | }
|
---|
| 264 |
|
---|
| 265 | public void ImportFromTSPLib(string tspFileName) {
|
---|
| 266 | TSPLIBParser parser = new TSPLIBParser(tspFileName);
|
---|
| 267 | parser.Parse();
|
---|
| 268 |
|
---|
| 269 | this.Name = parser.Name;
|
---|
| 270 | int problemSize = parser.Demands.Length;
|
---|
| 271 |
|
---|
[4860] | 272 | if (parser.Depot != 1) {
|
---|
| 273 | ErrorHandling.ShowErrorDialog(new Exception("Invalid depot specification"));
|
---|
| 274 | return;
|
---|
| 275 | }
|
---|
[4360] | 276 |
|
---|
[4860] | 277 | if (parser.WeightType != TSPLIBParser.TSPLIBEdgeWeightType.EUC_2D) {
|
---|
| 278 | ErrorHandling.ShowErrorDialog(new Exception("Invalid weight type"));
|
---|
| 279 | return;
|
---|
| 280 | }
|
---|
[4360] | 281 |
|
---|
[4362] | 282 | CVRPTWProblemInstance problem = new CVRPTWProblemInstance();
|
---|
| 283 | problem.Coordinates = new DoubleMatrix(parser.Vertices);
|
---|
| 284 | if (parser.Vehicles != -1)
|
---|
| 285 | problem.Vehicles.Value = parser.Vehicles;
|
---|
| 286 | else
|
---|
| 287 | problem.Vehicles.Value = problemSize - 1;
|
---|
| 288 | problem.Capacity.Value = parser.Capacity;
|
---|
| 289 | problem.Demand = new DoubleArray(parser.Demands);
|
---|
| 290 | problem.ReadyTime = new DoubleArray(problemSize);
|
---|
| 291 | problem.DueTime = new DoubleArray(problemSize);
|
---|
| 292 | problem.ServiceTime = new DoubleArray(problemSize);
|
---|
| 293 |
|
---|
| 294 | for (int i = 0; i < problemSize; i++) {
|
---|
| 295 | problem.ReadyTime[i] = 0;
|
---|
| 296 | problem.DueTime[i] = int.MaxValue;
|
---|
| 297 | problem.ServiceTime[i] = 0;
|
---|
| 298 | }
|
---|
| 299 |
|
---|
| 300 | if (parser.Distance != -1) {
|
---|
| 301 | problem.DueTime[0] = parser.Distance;
|
---|
| 302 | }
|
---|
| 303 |
|
---|
| 304 | this.ProblemInstance = problem;
|
---|
| 305 |
|
---|
[4360] | 306 | OnReset();
|
---|
| 307 | }
|
---|
| 308 |
|
---|
| 309 | public void ImportFromORLib(string orFileName) {
|
---|
| 310 | ORLIBParser parser = new ORLIBParser(orFileName);
|
---|
| 311 | parser.Parse();
|
---|
| 312 |
|
---|
| 313 | this.Name = parser.Name;
|
---|
| 314 | int problemSize = parser.Demands.Length;
|
---|
| 315 |
|
---|
[4362] | 316 | CVRPProblemInstance problem = new CVRPProblemInstance();
|
---|
| 317 |
|
---|
| 318 | problem.Coordinates = new DoubleMatrix(parser.Vertices);
|
---|
| 319 | problem.Vehicles.Value = problemSize - 1;
|
---|
| 320 | problem.Capacity.Value = parser.Capacity;
|
---|
| 321 | problem.Demand = new DoubleArray(parser.Demands);
|
---|
| 322 |
|
---|
| 323 | this.ProblemInstance = problem;
|
---|
| 324 |
|
---|
[4360] | 325 | OnReset();
|
---|
| 326 | }
|
---|
| 327 |
|
---|
| 328 | private void InitializeRandomVRPInstance() {
|
---|
| 329 | System.Random rand = new System.Random();
|
---|
| 330 |
|
---|
[4362] | 331 | CVRPTWProblemInstance problem = new CVRPTWProblemInstance();
|
---|
[4360] | 332 | int cities = 100;
|
---|
[4362] | 333 |
|
---|
| 334 | problem.Coordinates = new DoubleMatrix(cities + 1, 2);
|
---|
| 335 | problem.Demand = new DoubleArray(cities + 1);
|
---|
| 336 | problem.DueTime = new DoubleArray(cities + 1);
|
---|
| 337 | problem.ReadyTime = new DoubleArray(cities + 1);
|
---|
| 338 | problem.ServiceTime = new DoubleArray(cities + 1);
|
---|
| 339 |
|
---|
| 340 | problem.Vehicles.Value = 100;
|
---|
| 341 | problem.Capacity.Value = 200;
|
---|
| 342 |
|
---|
| 343 | for (int i = 0; i <= cities; i++) {
|
---|
| 344 | problem.Coordinates[i, 0] = rand.Next(0, 100);
|
---|
| 345 | problem.Coordinates[i, 1] = rand.Next(0, 100);
|
---|
| 346 |
|
---|
| 347 | if (i == 0) {
|
---|
| 348 | problem.Demand[i] = 0;
|
---|
| 349 | problem.DueTime[i] = Int16.MaxValue;
|
---|
| 350 | problem.ReadyTime[i] = 0;
|
---|
| 351 | problem.ServiceTime[i] = 0;
|
---|
| 352 | } else {
|
---|
| 353 | problem.Demand[i] = rand.Next(10, 50);
|
---|
| 354 | problem.DueTime[i] = rand.Next((int)Math.Ceiling(problem.GetDistance(0, i)), 1200);
|
---|
| 355 | problem.ReadyTime[i] = problem.DueTime[i] - rand.Next(0, 100);
|
---|
| 356 | problem.ServiceTime[i] = 90;
|
---|
| 357 | }
|
---|
| 358 | }
|
---|
| 359 |
|
---|
| 360 | this.ProblemInstance = problem;
|
---|
[4360] | 361 | }
|
---|
[4860] | 362 |
|
---|
| 363 | public void ImportSolution(string solutionFileName) {
|
---|
| 364 | SolutionParser parser = new SolutionParser(solutionFileName);
|
---|
| 365 | parser.Parse();
|
---|
| 366 |
|
---|
| 367 | HeuristicLab.Problems.VehicleRouting.Encodings.Potvin.PotvinEncoding encoding = new Encodings.Potvin.PotvinEncoding(ProblemInstance);
|
---|
| 368 |
|
---|
| 369 | int cities = 0;
|
---|
| 370 | foreach (List<int> route in parser.Routes) {
|
---|
| 371 | Tour tour = new Tour();
|
---|
| 372 | tour.Stops.AddRange(route);
|
---|
| 373 | cities += tour.Stops.Count;
|
---|
| 374 |
|
---|
| 375 | encoding.Tours.Add(tour);
|
---|
| 376 | }
|
---|
| 377 |
|
---|
| 378 | if (cities != ProblemInstance.Coordinates.Rows - 1)
|
---|
| 379 | ErrorHandling.ShowErrorDialog(new Exception("The optimal solution does not seem to correspond with the problem data"));
|
---|
| 380 | else {
|
---|
| 381 | VRPSolution solution = new VRPSolution(ProblemInstance, encoding, new DoubleValue(0));
|
---|
| 382 | ProblemInstance.BestKnownSolutionParameter.Value = solution;
|
---|
| 383 | }
|
---|
| 384 | }
|
---|
[4360] | 385 | }
|
---|
| 386 | }
|
---|