Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VRP/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/GVR/GVREncoding.cs @ 4230

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

Added GVR encoding (#1039)

File size: 4.3 KB
Line 
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;
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  [StorableClass]
34  public class GVREncoding : TourEncoding {
35    [Storable]
36    private DoubleValue capacity { get; set; }
37
38    [Storable]
39    private IntValue vehicles { get; set; }
40
41    [Storable]
42    private DoubleArray demand { get; set; }
43
44    public override List<Tour> GetTours() {
45      List<Tour> tours = new List<Tour>();
46
47      int toursProcessed = 0;
48      foreach (Tour tour in base.Tours) {
49        Tour newTour = new Tour();
50        double currentDemand = 0;
51
52        foreach (int city in tour.Cities) {
53          currentDemand += demand[city];
54
55          if (vehicles.Value > tours.Count + base.Tours.Count - toursProcessed &&
56            currentDemand > capacity.Value) {
57            if(newTour.Cities.Count > 0)
58              tours.Add(newTour);
59
60            newTour = new Tour();
61            newTour.Cities.Add(city);
62            currentDemand = demand[city];
63          } else {
64            newTour.Cities.Add(city);
65          }
66        }
67
68        if (newTour.Cities.Count > 0)
69          tours.Add(newTour);
70        toursProcessed++;
71      }
72
73      return tours;
74    }
75   
76    public override IDeepCloneable Clone(HeuristicLab.Common.Cloner cloner) {
77      GVREncoding clone = new GVREncoding(capacity, demand, vehicles);
78      cloner.RegisterClonedObject(this, clone);
79      clone.Tours = (ItemList<Tour>)cloner.Clone(this.Tours);
80     
81      return clone;
82    }
83
84    public GVREncoding(DoubleValue capacity, DoubleArray demand, IntValue vehicles)
85      : base() {
86        this.capacity = capacity;
87        this.demand = demand;
88        this.vehicles = vehicles;
89    }
90
91    [StorableConstructor]
92    private GVREncoding(bool serializing)
93      : base() {
94    }
95
96    public static GVREncoding ConvertFrom(IVRPEncoding encoding, DoubleValue capacity, DoubleArray demand, IntValue vehicles) {
97      GVREncoding solution = new GVREncoding(capacity, demand, vehicles);
98
99      TourEncoding.ConvertFrom(encoding, solution);
100
101      return solution;
102    }
103
104    public static GVREncoding ConvertFrom(List<int> route, DoubleValue capacity, DoubleArray demand, IntValue vehicles) {
105      GVREncoding solution = new GVREncoding(capacity, demand, vehicles);
106
107      TourEncoding.ConvertFrom(route, solution);
108
109      return solution;
110    }
111
112    internal void FindCustomer(int customer, out Tour tour, out int index) {
113      tour = null;
114      index = -1;
115
116      int currentTour = 0;
117      while (tour == null && currentTour < Tours.Count) {
118        int currentCity = 0;
119        while (tour == null && currentCity < Tours[currentTour].Cities.Count) {
120          if (Tours[currentTour].Cities[currentCity] == customer) {
121            tour = Tours[currentTour];
122            index = currentCity;
123          }
124
125          currentCity++;
126        }
127
128        currentTour++;
129      }
130    }
131  }
132}
Note: See TracBrowser for help on using the repository browser.