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 | using System.Drawing;
|
---|
28 | using System.Collections.Generic;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Problems.VehicleRouting.Encodings.General {
|
---|
31 | [Item("TourEncoding", "Represents a base class for tour encodings of VRP solutions.")]
|
---|
32 | [StorableClass]
|
---|
33 | public abstract class TourEncoding : Item, IVRPEncoding {
|
---|
34 | public override Image ItemImage {
|
---|
35 | get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Class; }
|
---|
36 | }
|
---|
37 |
|
---|
38 | #region IVRPEncoding Members
|
---|
39 | public virtual List<Tour> GetTours(ILookupParameter<DoubleMatrix> distanceMatrix = null, int maxVehicles = int.MaxValue) {
|
---|
40 | List<Tour> result = new List<Tour>(Tours);
|
---|
41 |
|
---|
42 | while (result.Count > maxVehicles) {
|
---|
43 | Tour tour = result[result.Count - 1];
|
---|
44 | result[result.Count - 2].Cities.AddRange(tour.Cities);
|
---|
45 |
|
---|
46 | result.Remove(tour);
|
---|
47 | }
|
---|
48 |
|
---|
49 | return result;
|
---|
50 | }
|
---|
51 |
|
---|
52 | public int Cities {
|
---|
53 | get
|
---|
54 | {
|
---|
55 | int cities = 0;
|
---|
56 |
|
---|
57 | foreach (Tour tour in Tours) {
|
---|
58 | cities += tour.Cities.Count;
|
---|
59 | }
|
---|
60 |
|
---|
61 | return cities;
|
---|
62 | }
|
---|
63 | }
|
---|
64 | #endregion
|
---|
65 |
|
---|
66 | [Storable]
|
---|
67 | public ItemList<Tour> Tours { get; set; }
|
---|
68 |
|
---|
69 | public TourEncoding() {
|
---|
70 | Tours = new ItemList<Tour>();
|
---|
71 | }
|
---|
72 |
|
---|
73 | [StorableConstructor]
|
---|
74 | protected TourEncoding(bool serializing)
|
---|
75 | : base() {
|
---|
76 | }
|
---|
77 |
|
---|
78 | public static void ConvertFrom(IVRPEncoding encoding, TourEncoding solution, ILookupParameter<DoubleMatrix> distanceMatrix) {
|
---|
79 | solution.Tours = new ItemList<Tour>(encoding.GetTours(distanceMatrix));
|
---|
80 | }
|
---|
81 |
|
---|
82 | public static void ConvertFrom(List<int> route, TourEncoding solution) {
|
---|
83 | solution.Tours = new ItemList<Tour>();
|
---|
84 |
|
---|
85 | Tour tour = new Tour();
|
---|
86 | for (int i = 0; i < route.Count; i++) {
|
---|
87 | if (route[i] == 0) {
|
---|
88 | if (tour.Cities.Count > 0) {
|
---|
89 | solution.Tours.Add(tour);
|
---|
90 | tour = new Tour();
|
---|
91 | }
|
---|
92 | } else {
|
---|
93 | tour.Cities.Add(route[i]);
|
---|
94 | }
|
---|
95 | }
|
---|
96 | }
|
---|
97 | }
|
---|
98 | }
|
---|