Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OaaS/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/Potvin/PotvinEncoding.cs @ 15762

Last change on this file since 15762 was 7259, checked in by swagner, 13 years ago

Updated year of copyrights to 2012 (#1716)

File size: 3.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Collections.Generic;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27using HeuristicLab.Problems.VehicleRouting.Encodings.General;
28
29namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin {
30  [Item("PotvinEncoding", "Represents a potvin encoding of VRP solutions. It is implemented as described in Potvin, J.-Y. and Bengio, S. (1996). The Vehicle Routing Problem with Time Windows - Part II: Genetic Search. INFORMS Journal of Computing, 8:165–172.")]
31  [StorableClass]
32  public class PotvinEncoding : TourEncoding {
33    [Storable]
34    public List<int> Unrouted { get; set; }
35
36    [StorableConstructor]
37    protected PotvinEncoding(bool deserializing) : base(deserializing) { }
38    protected PotvinEncoding(PotvinEncoding original, Cloner cloner)
39      : base(original, cloner) {
40      Tours = cloner.Clone(original.Tours);
41      Unrouted = new List<int>(original.Unrouted);
42    }
43    public PotvinEncoding()
44      : base() {
45      Unrouted = new List<int>();
46    }
47
48    public override IDeepCloneable Clone(Cloner cloner) {
49      return new PotvinEncoding(this, cloner);
50    }
51
52    public static PotvinEncoding ConvertFrom(IVRPEncoding encoding, ILookupParameter<DoubleMatrix> distanceMatrix) {
53      PotvinEncoding solution = new PotvinEncoding();
54
55      TourEncoding.ConvertFrom(encoding, solution, distanceMatrix);
56
57      return solution;
58    }
59
60    public static PotvinEncoding ConvertFrom(List<int> route) {
61      PotvinEncoding solution = new PotvinEncoding();
62
63      TourEncoding.ConvertFrom(route, solution);
64
65      return solution;
66    }
67
68    public bool FindInsertionPlace(
69      DoubleArray dueTimeArray,
70      DoubleArray serviceTimeArray, DoubleArray readyTimeArray, DoubleArray demandArray, DoubleValue capacity,
71      DistanceMatrix distMatrix,
72      int city, int routeToAvoid, bool allowInfeasible,
73      out int route, out int place) {
74      route = -1;
75      place = -1;
76      bool bestFeasible = false;
77      double minDetour = double.MaxValue;
78
79      for (int tour = 0; tour < Tours.Count; tour++) {
80        if (tour != routeToAvoid) {
81          for (int i = 0; i <= Tours[tour].Cities.Count; i++) {
82            double length = Tours[tour].GetLength(distMatrix);
83
84            Tours[tour].Cities.Insert(i, city);
85
86            bool feasible = Tours[tour].Feasible(dueTimeArray, serviceTimeArray, readyTimeArray, demandArray,
87              capacity, distMatrix);
88
89            if (feasible || allowInfeasible && !bestFeasible) {
90              double newLength = Tours[tour].GetLength(distMatrix);
91              double detour = newLength - length;
92
93              if (route < 0 || detour < minDetour || feasible && !bestFeasible) {
94                route = tour;
95                place = i;
96                minDetour = detour;
97
98                if (feasible)
99                  bestFeasible = true;
100              }
101            }
102
103            Tours[tour].Cities.RemoveAt(i);
104          }
105        }
106      }
107
108      return route >= 0 && place >= 0;
109    }
110  }
111}
Note: See TracBrowser for help on using the repository browser.