Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/Potvin/PotvinEncoding.cs @ 5658

Last change on this file since 5658 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

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