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 |
|
---|
34 | namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin {
|
---|
35 | [Item("IterativeInsertionCreator", "Creates a randomly initialized VRP solution.")]
|
---|
36 | [StorableClass]
|
---|
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]
|
---|
49 | private IterativeInsertionCreator(bool deserializing) : base(deserializing) { }
|
---|
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) {
|
---|
66 | double dx = instance.Coordinates[0, 0];
|
---|
67 | double dy = instance.Coordinates[0, 1];
|
---|
68 |
|
---|
69 | double cx = instance.Coordinates[city, 0];
|
---|
70 | double cy = instance.Coordinates[city, 1];
|
---|
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 |
|
---|
83 | private static PotvinEncoding CreateSolution(IVRPProblemInstance instance, IRandom random, bool adhereTimeWindows) {
|
---|
84 | PotvinEncoding result = new PotvinEncoding(instance);
|
---|
85 |
|
---|
86 | List<int> customers = new List<int>();
|
---|
87 | for (int i = 0; i < instance.Cities.Value; i++)
|
---|
88 | customers.Add(i + 1);
|
---|
89 | customers.Sort(delegate(int city1, int city2)
|
---|
90 | {
|
---|
91 | double angle1 = CalculateAngleToDepot(instance, city1);
|
---|
92 | double angle2 = CalculateAngleToDepot(instance, city2);
|
---|
93 |
|
---|
94 | return angle1.CompareTo(angle2);
|
---|
95 | });
|
---|
96 |
|
---|
97 | Tour currentTour = new Tour();
|
---|
98 |
|
---|
99 | int j = random.Next(customers.Count);
|
---|
100 | for (int i = 0; i < customers.Count; i++) {
|
---|
101 | int index = (i + j) % customers.Count;
|
---|
102 |
|
---|
103 | int stopIdx = result.FindBestInsertionPlace(currentTour, customers[index]);
|
---|
104 | currentTour.Stops.Insert(stopIdx, customers[index]);
|
---|
105 |
|
---|
106 | CVRPEvaluation evaluation = instance.Evaluate(currentTour) as CVRPEvaluation;
|
---|
107 | if (result.Tours.Count < instance.Vehicles.Value &&
|
---|
108 | ((adhereTimeWindows && !instance.Feasible(evaluation)) || ((!adhereTimeWindows) && evaluation.Overload > double.Epsilon))) {
|
---|
109 | currentTour.Stops.RemoveAt(stopIdx);
|
---|
110 |
|
---|
111 | if(currentTour.Stops.Count > 0)
|
---|
112 | result.Tours.Add(currentTour);
|
---|
113 | currentTour = new Tour();
|
---|
114 |
|
---|
115 | currentTour.Stops.Add(customers[index]);
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | if (currentTour.Stops.Count > 0)
|
---|
120 | result.Tours.Add(currentTour);
|
---|
121 |
|
---|
122 | return result;
|
---|
123 | }
|
---|
124 |
|
---|
125 | public override IOperation Apply() {
|
---|
126 | VRPToursParameter.ActualValue = CreateSolution(ProblemInstance, RandomParameter.ActualValue, AdhereTimeWindowsParameter.Value.Value);
|
---|
127 |
|
---|
128 | return base.Apply();
|
---|
129 | }
|
---|
130 | }
|
---|
131 | }
|
---|