Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/GVR/GVREncoding.cs

Last change on this file was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

File size: 4.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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 HEAL.Attic;
27using HeuristicLab.Problems.VehicleRouting.Encodings.General;
28using HeuristicLab.Problems.VehicleRouting.Interfaces;
29using HeuristicLab.Problems.VehicleRouting.Variants;
30
31namespace HeuristicLab.Problems.VehicleRouting.Encodings.GVR {
32  [Item("GVREncoding", "Represents a potvin encoding of VRP solutions. It is implemented as described in Pereira, F.B. et al (2002). GVR: a New Genetic Representation for the Vehicle Routing Problem. AICS 2002, LNAI 2464, pp. 95-102.")]
33  [StorableType("27A8F267-9865-4AEA-9ECF-88D950D81D74")]
34  public class GVREncoding : TourEncoding {
35    public override List<Tour> GetTours() {
36      List<Tour> tours = new List<Tour>();
37
38      foreach (Tour tour in base.Tours) {
39        Tour newTour = new Tour();
40        double currentDemand = 0;
41
42        DoubleValue capacity = new DoubleValue(double.MaxValue);
43        if (ProblemInstance is IHomogenousCapacitatedProblemInstance) {
44          capacity.Value = (ProblemInstance as IHomogenousCapacitatedProblemInstance).Capacity.Value;
45        }
46
47
48        foreach (int city in tour.Stops) {
49          currentDemand += ProblemInstance.GetDemand(city);
50
51          if (currentDemand > capacity.Value) {
52            if (newTour.Stops.Count > 0)
53              tours.Add(newTour);
54
55            newTour = new Tour();
56            newTour.Stops.Add(city);
57            currentDemand = ProblemInstance.GetDemand(city);
58          } else {
59            newTour.Stops.Add(city);
60          }
61        }
62
63        if (newTour.Stops.Count > 0)
64          tours.Add(newTour);
65      }
66
67      //repair if there are too many vehicles used
68      while (tours.Count > ProblemInstance.Vehicles.Value) {
69        Tour tour = tours[tours.Count - 1];
70        tours[tours.Count - 2].Stops.AddRange(tour.Stops);
71
72        tours.Remove(tour);
73      }
74
75      return tours;
76    }
77
78    public GVREncoding(IVRPProblemInstance problemInstance)
79      : base(problemInstance) {
80    }
81
82    [StorableConstructor]
83    protected GVREncoding(StorableConstructorFlag _) : base(_) {
84    }
85
86    public override IDeepCloneable Clone(Cloner cloner) {
87      return new GVREncoding(this, cloner);
88    }
89
90    protected GVREncoding(GVREncoding original, Cloner cloner)
91      : base(original, cloner) {
92    }
93
94    public static GVREncoding ConvertFrom(IVRPEncoding encoding, IVRPProblemInstance problemInstance) {
95      GVREncoding solution = new GVREncoding(problemInstance);
96
97      TourEncoding.ConvertFrom(encoding, solution, problemInstance);
98
99      return solution;
100    }
101
102    public static GVREncoding ConvertFrom(List<int> route, IVRPProblemInstance problemInstance) {
103      GVREncoding solution = new GVREncoding(problemInstance);
104
105      TourEncoding.ConvertFrom(route, solution);
106
107      return solution;
108    }
109
110    internal void FindCustomer(int customer, out Tour tour, out int index) {
111      tour = null;
112      index = -1;
113
114      int currentTour = 0;
115      while (tour == null && currentTour < Tours.Count) {
116        int currentCity = 0;
117        while (tour == null && currentCity < Tours[currentTour].Stops.Count) {
118          if (Tours[currentTour].Stops[currentCity] == customer) {
119            tour = Tours[currentTour];
120            index = currentCity;
121          }
122
123          currentCity++;
124        }
125
126        currentTour++;
127      }
128    }
129  }
130}
Note: See TracBrowser for help on using the repository browser.