Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/GVR/GVREncodedSolution.cs @ 18242

Last change on this file since 18242 was 17717, checked in by abeham, 4 years ago

#2521: working on VRP (refactoring all the capabilities, features, and operator discovery)

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