Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Creators/IterativeInsertionCreator.cs @ 6713

Last change on this file since 6713 was 6713, checked in by svonolfe, 13 years ago

Fixed minor bugs (#1177)

File size: 4.9 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Encodings.PermutationEncoding;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Common;
31using HeuristicLab.Problems.VehicleRouting.Interfaces;
32using HeuristicLab.Problems.VehicleRouting.ProblemInstances;
33
34namespace 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}
Note: See TracBrowser for help on using the repository browser.