Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Alba/AlbaEncoding.cs @ 4369

Last change on this file since 4369 was 4365, checked in by svonolfe, 12 years ago

Worked on VRP operators (WIP) (#1177)

File size: 3.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;
28using HeuristicLab.Problems.VehicleRouting.Encodings.General;
29using HeuristicLab.Problems.VehicleRouting.Interfaces;
30
31namespace HeuristicLab.Problems.VehicleRouting.Encodings.Alba {
32  [Item("AlbaEncoding", "Represents an Alba encoding of VRP solutions. It is implemented as described in Alba, E. and Dorronsoro, B. (2004). Solving the Vehicle Routing Problem by Using Cellular Genetic Algorithms.")]
33  [StorableClass]
34  public class AlbaEncoding : PermutationEncoding {   
35    #region IVRPEncoding Members
36    public override List<Tour> GetTours() {
37      List<Tour> result = new List<Tour>();
38
39      int cities = ProblemInstance.Cities.Value;
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.Stops.Count > 0) {
45            result.Add(tour);
46
47            tour = new Tour();
48          }
49        } else {
50          tour.Stops.Add(this.array[i] + 1);
51        }
52      }
53
54      if (tour.Stops.Count > 0) {
55        result.Add(tour);
56      }   
57
58      return result;
59    }
60    #endregion
61
62    public override IDeepCloneable Clone(HeuristicLab.Common.Cloner cloner) {
63      AlbaEncoding clone = new AlbaEncoding(
64        new Permutation(this.PermutationType, this.array),
65        (IVRPProblemInstance)cloner.Clone(ProblemInstance));
66      cloner.RegisterClonedObject(this, clone);
67      clone.readOnly = readOnly;
68      return clone;
69    }
70
71    public AlbaEncoding(Permutation permutation, IVRPProblemInstance instance)
72      : base(permutation, instance) {
73    }
74
75    [StorableConstructor]
76    private AlbaEncoding(bool serializing)
77      : base(serializing) {
78    }
79
80    public static AlbaEncoding ConvertFrom(List<int> routeParam, IVRPProblemInstance instance) {
81      List<int> route = new List<int>(routeParam);
82      route.RemoveAt(routeParam.Count - 1);
83     
84      int cities = 0;
85      for (int i = 0; i < route.Count; i++) {
86        if (route[i] != 0) {
87          cities++;
88        }
89      }
90
91      int vehicle = cities;
92      for (int i = 0; i < route.Count; i++) {
93        if (route[i] == 0) {
94          route[i] = vehicle;
95          vehicle++;
96        } else {
97          route[i] = route[i] - 1;
98        }
99      }
100
101      return new AlbaEncoding(
102        new Permutation(PermutationTypes.RelativeUndirected, route.ToArray()),
103        instance);
104    }
105  }
106}
Note: See TracBrowser for help on using the repository browser.