Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HiveHiveEngine/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/General/TourEncoding.cs @ 10355

Last change on this file since 10355 was 7259, checked in by swagner, 13 years ago

Updated year of copyrights to 2012 (#1716)

File size: 3.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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 System.Collections.Generic;
23using System.Drawing;
24using System.Text;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace 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 static new Image StaticItemImage {
35      get { return HeuristicLab.Common.Resources.VSImageLibrary.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        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>();
70
71      Tours.ToStringChanged += new System.EventHandler(Tours_ToStringChanged);
72    }
73
74    void Tours_ToStringChanged(object sender, System.EventArgs e) {
75      this.OnToStringChanged();
76    }
77
78    [StorableConstructor]
79    protected TourEncoding(bool serializing)
80      : base() {
81    }
82    protected TourEncoding(TourEncoding original, Cloner cloner)
83      : base(original, cloner) {
84    }
85
86    public static void ConvertFrom(IVRPEncoding encoding, TourEncoding solution, ILookupParameter<DoubleMatrix> distanceMatrix) {
87      solution.Tours = new ItemList<Tour>(encoding.GetTours(distanceMatrix));
88    }
89
90    public static void ConvertFrom(List<int> route, TourEncoding solution) {
91      solution.Tours = new ItemList<Tour>();
92
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    }
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
118      return sb.ToString();
119    }
120  }
121}
Note: See TracBrowser for help on using the repository browser.