Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/General/TourEncoding.cs @ 13656

Last change on this file since 13656 was 13656, checked in by ascheibe, 8 years ago

#2582 created branch for Hive Web Job Manager

File size: 4.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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 HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26using HeuristicLab.Problems.VehicleRouting.Interfaces;
27
28namespace HeuristicLab.Problems.VehicleRouting.Encodings.General {
29  [Item("TourEncoding", "Represents a base class for tour encodings of VRP solutions.")]
30  [StorableClass]
31  public abstract class TourEncoding : Item, IVRPEncoding {
32
33
34    #region IVRPEncoding Members
35    public virtual void Repair() {
36      List<Tour> toBeRemoved = new List<Tour>();
37      foreach (Tour tour in Tours) {
38        if (tour.Stops.Count == 0)
39          toBeRemoved.Add(tour);
40      }
41
42      foreach (Tour tour in toBeRemoved)
43        Tours.Remove(tour);
44
45      while (Tours.Count > ProblemInstance.Vehicles.Value) {
46        Tour tour = Tours[Tours.Count - 1];
47        Tours[Tours.Count - 2].Stops.AddRange(tour.Stops);
48
49        Tours.Remove(tour);
50      }
51    }
52
53    public virtual List<Tour> GetTours() {
54      List<Tour> result = new List<Tour>();
55      foreach (Tour tour in Tours)
56        result.Add(tour.Clone() as Tour);
57
58      return result;
59    }
60
61    public int GetTourIndex(Tour tour) {
62      int index = -1;
63
64      for (int i = 0; i < Tours.Count; i++) {
65        if (Tours[i].IsEqual(tour)) {
66          index = i;
67          break;
68        }
69      }
70
71      return index;
72    }
73
74    public virtual int GetVehicleAssignment(int tour) {
75      return tour;
76    }
77
78    public int Cities
79    {
80      get
81      {
82        int cities = 0;
83
84        foreach (Tour tour in Tours) {
85          cities += tour.Stops.Count;
86        }
87
88        return cities;
89      }
90    }
91    #endregion
92
93    [Storable]
94    public ItemList<Tour> Tours { get; set; }
95
96    [Storable]
97    protected IVRPProblemInstance ProblemInstance { get; set; }
98
99    public TourEncoding(IVRPProblemInstance problemInstance) {
100      Tours = new ItemList<Tour>();
101
102      ProblemInstance = problemInstance;
103    }
104
105    [StorableConstructor]
106    protected TourEncoding(bool serializing)
107      : base(serializing) {
108    }
109
110    protected TourEncoding(TourEncoding original, Cloner cloner)
111      : base(original, cloner) {
112      this.Tours = (ItemList<Tour>)cloner.Clone(original.Tours);
113      if (original.ProblemInstance != null && cloner.ClonedObjectRegistered(original.ProblemInstance))
114        this.ProblemInstance = (IVRPProblemInstance)cloner.Clone(original.ProblemInstance);
115      else
116        this.ProblemInstance = original.ProblemInstance;
117    }
118
119    public static void ConvertFrom(IVRPEncoding encoding, TourEncoding solution, IVRPProblemInstance problemInstance) {
120      solution.Tours = new ItemList<Tour>(encoding.GetTours());
121      solution.Repair();
122    }
123
124    public static void ConvertFrom(List<int> route, TourEncoding solution) {
125      solution.Tours = new ItemList<Tour>();
126
127      Tour tour = new Tour();
128      for (int i = 0; i < route.Count; i++) {
129        if (route[i] <= 0) {
130          if (tour.Stops.Count > 0) {
131            solution.Tours.Add(tour);
132            tour = new Tour();
133          }
134        } else {
135          tour.Stops.Add(route[i]);
136        }
137      }
138
139      solution.Repair();
140    }
141  }
142}
Note: See TracBrowser for help on using the repository browser.