[5127] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17181] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[5127] | 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;
|
---|
[8053] | 24 | using HeuristicLab.Common;
|
---|
[5127] | 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
| 27 | using HeuristicLab.Optimization;
|
---|
| 28 | using HeuristicLab.Parameters;
|
---|
[17097] | 29 | using HEAL.Attic;
|
---|
[5127] | 30 | using HeuristicLab.Problems.VehicleRouting.Interfaces;
|
---|
| 31 | using HeuristicLab.Problems.VehicleRouting.ProblemInstances;
|
---|
[6857] | 32 | using HeuristicLab.Problems.VehicleRouting.Variants;
|
---|
[5127] | 33 |
|
---|
| 34 | namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin {
|
---|
| 35 | [Item("IterativeInsertionCreator", "Creates a randomly initialized VRP solution.")]
|
---|
[17097] | 36 | [StorableType("7504083B-79FC-441C-BE32-DAFCB9E81448")]
|
---|
[5127] | 37 | public sealed class IterativeInsertionCreator : PotvinCreator, IStochasticOperator {
|
---|
| 38 | #region IStochasticOperator Members
|
---|
| 39 | public ILookupParameter<IRandom> RandomParameter {
|
---|
| 40 | get { return (LookupParameter<IRandom>)Parameters["Random"]; }
|
---|
| 41 | }
|
---|
| 42 | #endregion
|
---|
| 43 |
|
---|
| 44 | public IValueParameter<BoolValue> AdhereTimeWindowsParameter {
|
---|
| 45 | get { return (IValueParameter<BoolValue>)Parameters["AdhereTimeWindows"]; }
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | [StorableConstructor]
|
---|
[17097] | 49 | private IterativeInsertionCreator(StorableConstructorFlag _) : base(_) { }
|
---|
[5127] | 50 |
|
---|
| 51 | public IterativeInsertionCreator()
|
---|
| 52 | : base() {
|
---|
| 53 | Parameters.Add(new LookupParameter<IRandom>("Random", "The pseudo random number generator."));
|
---|
| 54 | Parameters.Add(new ValueParameter<BoolValue>("AdhereTimeWindows", "Specifies if the time windows should be considered during construction.", new BoolValue(true)));
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 58 | return new IterativeInsertionCreator(this, cloner);
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | private IterativeInsertionCreator(IterativeInsertionCreator original, Cloner cloner)
|
---|
| 62 | : base(original, cloner) {
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | private static double CalculateAngleToDepot(IVRPProblemInstance instance, int city) {
|
---|
[6857] | 66 | double dx = instance.GetCoordinates(0)[0];
|
---|
| 67 | double dy = instance.GetCoordinates(0)[1];
|
---|
[5127] | 68 |
|
---|
[6857] | 69 | double cx = instance.GetCoordinates(city)[0];
|
---|
| 70 | double cy = instance.GetCoordinates(city)[1];
|
---|
[5127] | 71 |
|
---|
| 72 | double alpha = Math.Atan((cx - dx) / (dy - cy)) * (180.0 / Math.PI);
|
---|
| 73 | if (cx > dx && cy > dy)
|
---|
| 74 | alpha = (90.0 + alpha) + 90.0;
|
---|
| 75 | else if (cx < dx && cy > dy)
|
---|
| 76 | alpha = alpha + 180.0;
|
---|
| 77 | else if (cx < dx && cy < dy)
|
---|
| 78 | alpha = (90.0 + alpha) + 270.0;
|
---|
| 79 |
|
---|
| 80 | return alpha;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
[10744] | 83 | public static PotvinEncoding CreateSolution(IVRPProblemInstance instance, IRandom random, bool adhereTimeWindows) {
|
---|
[5127] | 84 | PotvinEncoding result = new PotvinEncoding(instance);
|
---|
| 85 |
|
---|
[8053] | 86 | IPickupAndDeliveryProblemInstance pdp = instance as IPickupAndDeliveryProblemInstance;
|
---|
[6857] | 87 |
|
---|
[5127] | 88 | List<int> customers = new List<int>();
|
---|
[6857] | 89 | for (int i = 1; i <= instance.Cities.Value; i++)
|
---|
[8053] | 90 | if (pdp == null || pdp.GetDemand(i) >= 0)
|
---|
[6857] | 91 | customers.Add(i);
|
---|
| 92 |
|
---|
[10744] | 93 | customers.Sort((city1, city2) => {
|
---|
| 94 | double angle1 = CalculateAngleToDepot(instance, city1);
|
---|
| 95 | double angle2 = CalculateAngleToDepot(instance, city2);
|
---|
[5127] | 96 |
|
---|
[10744] | 97 | return angle1.CompareTo(angle2);
|
---|
| 98 | });
|
---|
[5127] | 99 |
|
---|
| 100 | Tour currentTour = new Tour();
|
---|
[6857] | 101 | result.Tours.Add(currentTour);
|
---|
[6713] | 102 |
|
---|
[5127] | 103 | int j = random.Next(customers.Count);
|
---|
| 104 | for (int i = 0; i < customers.Count; i++) {
|
---|
| 105 | int index = (i + j) % customers.Count;
|
---|
| 106 |
|
---|
[6857] | 107 | int stopIdx = 0;
|
---|
[8053] | 108 | if (currentTour.Stops.Count > 0)
|
---|
[11005] | 109 | stopIdx = result.FindBestInsertionPlace(currentTour, customers[index]);
|
---|
[5127] | 110 | currentTour.Stops.Insert(stopIdx, customers[index]);
|
---|
| 111 |
|
---|
[6857] | 112 | if (pdp != null) {
|
---|
| 113 | stopIdx = result.FindBestInsertionPlace(currentTour, pdp.GetPickupDeliveryLocation(customers[index]));
|
---|
| 114 | currentTour.Stops.Insert(stopIdx, pdp.GetPickupDeliveryLocation(customers[index]));
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | CVRPEvaluation evaluation = instance.EvaluateTour(currentTour, result) as CVRPEvaluation;
|
---|
[8053] | 118 | if (result.Tours.Count < instance.Vehicles.Value &&
|
---|
[6607] | 119 | ((adhereTimeWindows && !instance.Feasible(evaluation)) || ((!adhereTimeWindows) && evaluation.Overload > double.Epsilon))) {
|
---|
[8053] | 120 | currentTour.Stops.Remove(customers[index]);
|
---|
| 121 | if (pdp != null)
|
---|
| 122 | currentTour.Stops.Remove(pdp.GetPickupDeliveryLocation(customers[index]));
|
---|
[5127] | 123 |
|
---|
[6857] | 124 | if (currentTour.Stops.Count == 0)
|
---|
| 125 | result.Tours.Remove(currentTour);
|
---|
[5127] | 126 | currentTour = new Tour();
|
---|
[6857] | 127 | result.Tours.Add(currentTour);
|
---|
[8053] | 128 |
|
---|
[5127] | 129 | currentTour.Stops.Add(customers[index]);
|
---|
[6857] | 130 | if (pdp != null) {
|
---|
| 131 | currentTour.Stops.Add(pdp.GetPickupDeliveryLocation(customers[index]));
|
---|
| 132 | }
|
---|
[5127] | 133 | }
|
---|
| 134 | }
|
---|
| 135 |
|
---|
[6857] | 136 | if (currentTour.Stops.Count == 0)
|
---|
| 137 | result.Tours.Remove(currentTour);
|
---|
[6713] | 138 |
|
---|
[5127] | 139 | return result;
|
---|
| 140 | }
|
---|
| 141 |
|
---|
[10507] | 142 | public override IOperation InstrumentedApply() {
|
---|
[5127] | 143 | VRPToursParameter.ActualValue = CreateSolution(ProblemInstance, RandomParameter.ActualValue, AdhereTimeWindowsParameter.Value.Value);
|
---|
| 144 |
|
---|
[10507] | 145 | return base.InstrumentedApply();
|
---|
[5127] | 146 | }
|
---|
| 147 | }
|
---|
| 148 | }
|
---|