Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VRP/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/Potvin/PotvinEncoding.cs @ 4230

Last change on this file since 4230 was 4230, checked in by svonolfe, 14 years ago

Added GVR encoding (#1039)

File size: 3.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 HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Encodings.PermutationEncoding;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27using System.Drawing;
28using System.Collections.Generic;
29using HeuristicLab.Problems.VehicleRouting.Encodings.General;
30
31namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin {
32  [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.")]
33  [StorableClass]
34  public class PotvinEncoding : TourEncoding {   
35    [Storable]
36    public List<int> Unrouted { get; set; }
37
38    public override IDeepCloneable Clone(HeuristicLab.Common.Cloner cloner) {
39      PotvinEncoding clone = new PotvinEncoding();
40      cloner.RegisterClonedObject(this, clone);
41      clone.Tours = (ItemList<Tour>)cloner.Clone(this.Tours);
42      clone.Unrouted = new List<int>(Unrouted);
43      return clone;
44    }
45
46    public PotvinEncoding(): base() {
47      Unrouted = new List<int>();
48    }
49
50    [StorableConstructor]
51    private PotvinEncoding(bool serializing)
52      : base() {
53    }
54   
55    public static PotvinEncoding ConvertFrom(IVRPEncoding encoding) {
56      PotvinEncoding solution = new PotvinEncoding();
57
58      TourEncoding.ConvertFrom(encoding, solution);
59
60      return solution;
61    }
62
63    public static PotvinEncoding ConvertFrom(List<int> route) {
64      PotvinEncoding solution = new PotvinEncoding();
65
66      TourEncoding.ConvertFrom(route, solution);
67
68      return solution;
69    }
70
71    public bool FindInsertionPlace(
72      DoubleArray dueTimeArray,
73      DoubleArray serviceTimeArray, DoubleArray readyTimeArray, DoubleArray demandArray, DoubleValue capacity,
74      DoubleMatrix coordinates, ILookupParameter<DoubleMatrix> distanceMatrix, BoolValue useDistanceMatrix,
75      int city, int routeToAvoid, out int route, out int place) {
76      route = -1;
77      place = -1;
78      double minDetour = 0;
79
80      for (int tour = 0; tour < Tours.Count; tour++) {
81        if (tour != routeToAvoid) {
82          for (int i = 0; i <= Tours[tour].Cities.Count; i++) {
83            double length = Tours[tour].GetLength(coordinates, distanceMatrix, useDistanceMatrix);
84
85            Tours[tour].Cities.Insert(i, city);
86
87            if (Tours[tour].Feasible(dueTimeArray, serviceTimeArray, readyTimeArray, demandArray,
88              capacity, coordinates, distanceMatrix, useDistanceMatrix)) {
89                double newLength = Tours[tour].GetLength(coordinates, distanceMatrix, useDistanceMatrix);
90
91              double detour = newLength - length;
92
93              if (route <= 0 || detour < minDetour) {
94                route = tour;
95                place = i;
96                minDetour = detour;
97              }
98            }
99
100            Tours[tour].Cities.RemoveAt(i);
101          }
102        }
103      }
104
105      return route >= 0 && place >= 0;
106    }
107  }
108}
Note: See TracBrowser for help on using the repository browser.