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