Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2582 created branch for Hive Web Job Manager

File size: 4.1 KB
RevLine 
[4365]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[4365]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
[8053]22using System.Collections.Generic;
[4365]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 {
[8053]32
[13656]33
[4365]34    #region IVRPEncoding Members
[6851]35    public virtual void Repair() {
[6752]36      List<Tour> toBeRemoved = new List<Tour>();
37      foreach (Tour tour in Tours) {
38        if (tour.Stops.Count == 0)
39          toBeRemoved.Add(tour);
[5127]40      }
41
[6752]42      foreach (Tour tour in toBeRemoved)
43        Tours.Remove(tour);
[4365]44
[6752]45      while (Tours.Count > ProblemInstance.Vehicles.Value) {
46        Tour tour = Tours[Tours.Count - 1];
47        Tours[Tours.Count - 2].Stops.AddRange(tour.Stops);
[4365]48
[6752]49        Tours.Remove(tour);
[8053]50      }
[4365]51    }
52
[8053]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);
[6752]57
[8053]58      return result;
[6752]59    }
60
[6851]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
[13656]78    public int Cities
79    {
80      get
81      {
[4365]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)
[4376]107      : base(serializing) {
[4365]108    }
[4376]109
[4752]110    protected TourEncoding(TourEncoding original, Cloner cloner)
111      : base(original, cloner) {
[8053]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;
[4752]117    }
118
[4376]119    public static void ConvertFrom(IVRPEncoding encoding, TourEncoding solution, IVRPProblemInstance problemInstance) {
120      solution.Tours = new ItemList<Tour>(encoding.GetTours());
[6837]121      solution.Repair();
[4376]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++) {
[7543]129        if (route[i] <= 0) {
[4376]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      }
[6837]138
139      solution.Repair();
[4376]140    }
[4365]141  }
142}
Note: See TracBrowser for help on using the repository browser.