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