Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/Alba/AlbaEncoding.cs @ 4174

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

Refactored VRP, added some Potvin operators (WIP) (#1039)

File size: 4.5 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.Collections.Generic;
28
29namespace HeuristicLab.Problems.VehicleRouting.Encodings.Alba {
30  [Item("AlbaEncoding", "Represents an alba encoding of VRP solutions.")]
31  [StorableClass]
32  class AlbaEncoding : Permutation, IVRPEncoding {
33    [Storable]
34    private int cities;
35   
36    #region IVRPEncoding Members
37    public ItemList<Tour> Tours {
38      get {
39        ItemList<Tour> result = new ItemList<Tour>();
40       
41        Tour tour = new Tour();
42        for (int i = 0; i < this.array.Length; i++) {
43          if (this.array[i] >= cities) {
44            if (tour.Cities.Count > 0) {
45              result.Add(tour);
46
47              tour = new Tour();
48            }
49          } else {
50            tour.Cities.Add(this.array[i] + 1);
51          }
52        }
53
54        if (tour.Cities.Count > 0) {
55          result.Add(tour);
56        }
57
58        return result;
59      }
60    }
61
62    public int Cities {
63      get { return cities; }
64    }
65
66    public int MaxVehicles {
67      get { return Length - Cities;  }
68    }
69
70    #endregion
71
72    public override IDeepCloneable Clone(HeuristicLab.Common.Cloner cloner) {
73      AlbaEncoding clone = new AlbaEncoding(
74        new Permutation(this.PermutationType, this.array), cities);
75      cloner.RegisterClonedObject(this, clone);
76      clone.readOnly = readOnly;
77      return clone;
78    }
79
80    public AlbaEncoding(Permutation permutation, int cities)
81      : base(PermutationTypes.RelativeUndirected) {
82      this.array = new int[permutation.Length];
83      for (int i = 0; i < array.Length; i++)
84        this.array[i] = permutation[i];
85
86      this.cities = cities;
87    }
88
89    [StorableConstructor]
90    private AlbaEncoding(bool serializing)
91      : base() {
92    }
93
94    public static AlbaEncoding ConvertFrom(IVRPEncoding encoding, int vehicles) {
95      ItemList<Tour> tours = encoding.Tours;
96
97      int cities = 0;
98      foreach (Tour tour in tours) {
99        cities += tour.Cities.Count;
100      }
101
102      int emptyVehicles = vehicles - tours.Count;
103
104      int[] array = new int[cities + tours.Count + emptyVehicles - 1];
105      int delimiter = cities;
106      int arrayIndex = 0;
107
108      foreach (Tour tour in tours) {
109        foreach (int city in tour.Cities) {
110            array[arrayIndex] = city - 1;
111            arrayIndex++;
112        }
113
114        if (arrayIndex != array.Length) {
115          array[arrayIndex] = delimiter;
116          delimiter++;
117          arrayIndex++;
118        }
119      }
120
121      for (int i = 0; i < emptyVehicles - 1; i++) {
122        array[arrayIndex] = delimiter;
123        delimiter++;
124        arrayIndex++;
125      }
126           
127      AlbaEncoding solution = new AlbaEncoding(new Permutation(PermutationTypes.RelativeUndirected, new IntArray(array)), cities);
128
129      return solution;
130    }
131
132    public static AlbaEncoding ConvertFrom(List<int> routeParam) {
133      List<int> route = new List<int>(routeParam);
134     
135      int cities = 0;
136      for (int i = 0; i < route.Count; i++) {
137        if (route[i] != 0) {
138          cities++;
139        }
140      }
141
142      int vehicle = cities;
143      for (int i = 0; i < route.Count; i++) {
144        if (route[i] == 0) {
145          route[i] = vehicle;
146          vehicle++;
147        } else {
148          route[i] = route[i] - 1;
149        }
150      }
151
152      return new AlbaEncoding(
153        new Permutation(PermutationTypes.RelativeUndirected, route.ToArray()),
154        cities);
155    }
156  }
157}
Note: See TracBrowser for help on using the repository browser.