Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CloningRefactoring/HeuristicLab.Problems.VehicleRouting/3.3/Encodings/General/TourEncoding.cs @ 4689

Last change on this file since 4689 was 4687, checked in by mkommend, 14 years ago

Refactored VRP Encodings.General and Zhu (ticket #922).

File size: 3.0 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 System.Collections.Generic;
23using System.Drawing;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace 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    }
36
37    #region IVRPEncoding Members
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;
49    }
50
51    public int Cities {
52      get {
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    }
75    protected TourEncoding(TourEncoding original, Cloner cloner)
76      : base(original, cloner) {
77    }
78
79    public static void ConvertFrom(IVRPEncoding encoding, TourEncoding solution, ILookupParameter<DoubleMatrix> distanceMatrix) {
80      solution.Tours = new ItemList<Tour>(encoding.GetTours(distanceMatrix));
81    }
82
83    public static void ConvertFrom(List<int> route, TourEncoding solution) {
84      solution.Tours = new ItemList<Tour>();
85
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}
Note: See TracBrowser for help on using the repository browser.