[4230] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[5445] | 3 | * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[4230] | 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 |
|
---|
[4722] | 22 | using System.Collections.Generic;
|
---|
| 23 | using System.Drawing;
|
---|
[5287] | 24 | using System.Text;
|
---|
[4230] | 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Data;
|
---|
| 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 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 {
|
---|
[5287] | 35 | get { return HeuristicLab.Common.Resources.VSImageLibrary.Class; }
|
---|
[4230] | 36 | }
|
---|
[4722] | 37 |
|
---|
[4230] | 38 | #region IVRPEncoding Members
|
---|
[4268] | 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;
|
---|
[4230] | 50 | }
|
---|
| 51 |
|
---|
| 52 | public int Cities {
|
---|
[4722] | 53 | get {
|
---|
[4230] | 54 | int cities = 0;
|
---|
| 55 |
|
---|
| 56 | foreach (Tour tour in Tours) {
|
---|
| 57 | cities += tour.Cities.Count;
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | return cities;
|
---|
| 61 | }
|
---|
| 62 | }
|
---|
| 63 | #endregion
|
---|
| 64 |
|
---|
| 65 | [Storable]
|
---|
| 66 | public ItemList<Tour> Tours { get; set; }
|
---|
| 67 |
|
---|
| 68 | public TourEncoding() {
|
---|
| 69 | Tours = new ItemList<Tour>();
|
---|
[4856] | 70 |
|
---|
| 71 | Tours.ToStringChanged += new System.EventHandler(Tours_ToStringChanged);
|
---|
[4230] | 72 | }
|
---|
| 73 |
|
---|
[4856] | 74 | void Tours_ToStringChanged(object sender, System.EventArgs e) {
|
---|
| 75 | this.OnToStringChanged();
|
---|
| 76 | }
|
---|
| 77 |
|
---|
[4230] | 78 | [StorableConstructor]
|
---|
| 79 | protected TourEncoding(bool serializing)
|
---|
| 80 | : base() {
|
---|
| 81 | }
|
---|
[4722] | 82 | protected TourEncoding(TourEncoding original, Cloner cloner)
|
---|
| 83 | : base(original, cloner) {
|
---|
| 84 | }
|
---|
[4268] | 85 |
|
---|
| 86 | public static void ConvertFrom(IVRPEncoding encoding, TourEncoding solution, ILookupParameter<DoubleMatrix> distanceMatrix) {
|
---|
| 87 | solution.Tours = new ItemList<Tour>(encoding.GetTours(distanceMatrix));
|
---|
[4230] | 88 | }
|
---|
| 89 |
|
---|
| 90 | public static void ConvertFrom(List<int> route, TourEncoding solution) {
|
---|
| 91 | solution.Tours = new ItemList<Tour>();
|
---|
[4722] | 92 |
|
---|
[4230] | 93 | Tour tour = new Tour();
|
---|
| 94 | for (int i = 0; i < route.Count; i++) {
|
---|
| 95 | if (route[i] == 0) {
|
---|
| 96 | if (tour.Cities.Count > 0) {
|
---|
| 97 | solution.Tours.Add(tour);
|
---|
| 98 | tour = new Tour();
|
---|
| 99 | }
|
---|
| 100 | } else {
|
---|
| 101 | tour.Cities.Add(route[i]);
|
---|
| 102 | }
|
---|
| 103 | }
|
---|
| 104 | }
|
---|
[4856] | 105 |
|
---|
| 106 | public override string ToString() {
|
---|
| 107 | StringBuilder sb = new StringBuilder();
|
---|
| 108 |
|
---|
| 109 | foreach (Tour tour in Tours) {
|
---|
| 110 | foreach (int city in tour.Cities) {
|
---|
| 111 | sb.Append(city);
|
---|
| 112 | sb.Append(" ");
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | sb.AppendLine();
|
---|
| 116 | }
|
---|
| 117 |
|
---|
[5287] | 118 | return sb.ToString();
|
---|
[4856] | 119 | }
|
---|
[4230] | 120 | }
|
---|
| 121 | }
|
---|