Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/GVR/GVREncoding.cs @ 4752

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

Merged relevant changes from the trunk into the branch (cloning,...) (#1177)

File size: 4.4 KB
RevLine 
[4379]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;
30using HeuristicLab.Problems.VehicleRouting.Interfaces;
31using HeuristicLab.Problems.VehicleRouting.Variants;
32
33namespace HeuristicLab.Problems.VehicleRouting.Encodings.GVR {
34  [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.")]
35  [StorableClass]
36  public class GVREncoding : TourEncoding {
37    public override List<Tour> GetTours() {
38      List<Tour> tours = new List<Tour>();   
39
40      foreach (Tour tour in base.Tours) {
41        Tour newTour = new Tour();
42        double currentDemand = 0;
43
44        DoubleArray demand = ProblemInstance.Demand;
45        DoubleValue capacity = new DoubleValue(double.MaxValue);
46        if (ProblemInstance is IHomogenousCapacitatedProblemInstance) {
47          capacity.Value = (ProblemInstance as IHomogenousCapacitatedProblemInstance).Capacity.Value;
48        }
49
50
51        foreach (int city in tour.Stops) {
52          currentDemand += demand[city];
53
54          if (currentDemand > capacity.Value) {
55            if (newTour.Stops.Count > 0)
56              tours.Add(newTour);
57
58            newTour = new Tour();
59            newTour.Stops.Add(city);
60            currentDemand = demand[city];
61          } else {
62            newTour.Stops.Add(city);
63          }
64        }
65
66        if (newTour.Stops.Count > 0)
67          tours.Add(newTour);
68      }
69
70      //repair if there are too many vehicles used
71      while (tours.Count > ProblemInstance.Vehicles.Value) {
72        Tour tour = tours[tours.Count - 1];
73        tours[tours.Count - 2].Stops.AddRange(tour.Stops);
74
75        tours.Remove(tour);
76      }
77
78      return tours;
79    }
80   
81    public GVREncoding(IVRPProblemInstance problemInstance)
82      : base(problemInstance) {
83    }
84
85    [StorableConstructor]
86    private GVREncoding(bool serializing)
87      : base(serializing) {
88    }
89
[4752]90    public override IDeepCloneable Clone(Cloner cloner) {
91      return new GVREncoding(this, cloner);
92    }
93
94    protected GVREncoding(GVREncoding original, Cloner cloner)
95      : base(original, cloner) {
96    }
97
[4379]98    public static GVREncoding ConvertFrom(IVRPEncoding encoding, IVRPProblemInstance problemInstance) {
99      GVREncoding solution = new GVREncoding(problemInstance);
100
101      TourEncoding.ConvertFrom(encoding, solution, problemInstance);
102
103      return solution;
104    }
105
106    public static GVREncoding ConvertFrom(List<int> route, IVRPProblemInstance problemInstance) {
107      GVREncoding solution = new GVREncoding(problemInstance);
108
109      TourEncoding.ConvertFrom(route, solution);
110
111      return solution;
112    }
113
114    internal void FindCustomer(int customer, out Tour tour, out int index) {
115      tour = null;
116      index = -1;
117
118      int currentTour = 0;
119      while (tour == null && currentTour < Tours.Count) {
120        int currentCity = 0;
121        while (tour == null && currentCity < Tours[currentTour].Stops.Count) {
122          if (Tours[currentTour].Stops[currentCity] == customer) {
123            tour = Tours[currentTour];
124            index = currentCity;
125          }
126
127          currentCity++;
128        }
129
130        currentTour++;
131      }
132    }
133  }
134}
Note: See TracBrowser for help on using the repository browser.