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 |
|
---|
22 | using HeuristicLab.Common;
|
---|
23 | using HeuristicLab.Core;
|
---|
24 | using HeuristicLab.Data;
|
---|
25 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
27 |
|
---|
28 | namespace HeuristicLab.Problems.VehicleRouting.Encodings.Alba {
|
---|
29 | [Item("AlbaEncoding", "Represents an alba encoding of VRP solutions.")]
|
---|
30 | [StorableClass]
|
---|
31 | class AlbaEncoding : Permutation, IVRPEncoding {
|
---|
32 | #region IVRPEncoding Members
|
---|
33 | [Storable]
|
---|
34 | private int cities;
|
---|
35 |
|
---|
36 | public override IDeepCloneable Clone(HeuristicLab.Common.Cloner cloner) {
|
---|
37 | AlbaEncoding clone = new AlbaEncoding(
|
---|
38 | new Permutation(this.PermutationType, this.array), cities);
|
---|
39 | cloner.RegisterClonedObject(this, clone);
|
---|
40 | clone.readOnly = readOnly;
|
---|
41 | return clone;
|
---|
42 | }
|
---|
43 |
|
---|
44 | public AlbaEncoding(Permutation permutation, int cities)
|
---|
45 | : base(PermutationTypes.RelativeDirected) {
|
---|
46 | this.array = new int[permutation.Length];
|
---|
47 | for (int i = 0; i < array.Length; i++)
|
---|
48 | this.array[i] = permutation[i];
|
---|
49 |
|
---|
50 | this.cities = cities;
|
---|
51 | }
|
---|
52 |
|
---|
53 | [StorableConstructor]
|
---|
54 | private AlbaEncoding(bool serializing)
|
---|
55 | : base() {
|
---|
56 | }
|
---|
57 |
|
---|
58 | public ItemList<Tour> Tours {
|
---|
59 | get {
|
---|
60 | ItemList<Tour> result = new ItemList<Tour>();
|
---|
61 | Tour tour = new Tour();
|
---|
62 | tour.Add(new IntValue(0));
|
---|
63 |
|
---|
64 | for (int i = 0; i < this.array.Length; i++) {
|
---|
65 | if (this.array[i] >= cities) {
|
---|
66 | if (tour.Count > 1) {
|
---|
67 | tour.Add(new IntValue(0));
|
---|
68 | result.Add(tour);
|
---|
69 |
|
---|
70 | tour = new Tour();
|
---|
71 | tour.Add(new IntValue(0));
|
---|
72 | }
|
---|
73 | } else {
|
---|
74 | tour.Add(new IntValue(this.array[i] + 1));
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | if (tour.Count > 1) {
|
---|
79 | tour.Add(new IntValue(0));
|
---|
80 | result.Add(tour);
|
---|
81 | }
|
---|
82 |
|
---|
83 | return result;
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 | public int Cities {
|
---|
88 | get { return cities; }
|
---|
89 | }
|
---|
90 |
|
---|
91 | #endregion
|
---|
92 |
|
---|
93 | public static AlbaEncoding ConvertFrom(IVRPEncoding encoding) {
|
---|
94 | ItemList<Tour> tours = encoding.Tours;
|
---|
95 |
|
---|
96 | int cities = 0;
|
---|
97 | foreach (Tour tour in tours) {
|
---|
98 | cities += tour.Count;
|
---|
99 | }
|
---|
100 | int[] array = new int[cities + tours.Count - 2];
|
---|
101 | int delimiter = cities;
|
---|
102 | int arrayIndex = 0;
|
---|
103 |
|
---|
104 | foreach (Tour tour in tours) {
|
---|
105 | foreach (IntValue city in tour) {
|
---|
106 | array[arrayIndex] = city.Value;
|
---|
107 |
|
---|
108 | arrayIndex++;
|
---|
109 | }
|
---|
110 |
|
---|
111 | if (arrayIndex != array.Length) {
|
---|
112 | array[arrayIndex] = delimiter;
|
---|
113 | delimiter++;
|
---|
114 | arrayIndex++;
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | AlbaEncoding solution = new AlbaEncoding(new Permutation(PermutationTypes.RelativeUndirected), cities);
|
---|
119 |
|
---|
120 | return solution;
|
---|
121 | }
|
---|
122 |
|
---|
123 |
|
---|
124 |
|
---|
125 | }
|
---|
126 | }
|
---|