Free cookie consent management tool by TermsFeed Policy Generator

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

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

Added possibility to allow infeasible solutions (#1561)

File size: 4.1 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,
[6459]71      DistanceMatrix distMatrix,
72      int city, int routeToAvoid, bool allowInfeasible,
73      out int route, out int place) {
[4174]74      route = -1;
75      place = -1;
[6459]76      bool bestFeasible = false;
[4174]77      double minDetour = 0;
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++) {
[6449]82            double length = Tours[tour].GetLength(distMatrix);
[4174]83
84            Tours[tour].Cities.Insert(i, city);
85
[6449]86            bool feasible = Tours[tour].Feasible(dueTimeArray, serviceTimeArray, readyTimeArray, demandArray,
87              capacity, distMatrix);
[4174]88
[6459]89            if ((!allowInfeasible && feasible) || (allowInfeasible && (!bestFeasible || feasible))) {
[6449]90              double newLength = Tours[tour].GetLength(distMatrix);
[4174]91              double detour = newLength - length;
92
[6459]93              if (route <= 0 || detour < minDetour ||
94                (allowInfeasible && ((!(bestFeasible && !feasible)) && detour < minDetour || (feasible && !bestFeasible)))) {
[4174]95                route = tour;
96                place = i;
97                minDetour = detour;
[6459]98
99                if (feasible)
100                  bestFeasible = true;
[4174]101              }
102            }
103
104            Tours[tour].Cities.RemoveAt(i);
105          }
106        }
107      }
108
109      return route >= 0 && place >= 0;
110    }
[4150]111  }
112}
Note: See TracBrowser for help on using the repository browser.