Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.VehicleRouting/3.4/Interpreters/VRPInterpreter.cs @ 15589

Last change on this file since 15589 was 15584, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers on stable

File size: 2.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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.IO;
23using HeuristicLab.Data;
24using HeuristicLab.Problems.Instances;
25using HeuristicLab.Problems.VehicleRouting.Encodings.Potvin;
26using HeuristicLab.Problems.VehicleRouting.ProblemInstances;
27using HeuristicLab.Problems.VehicleRouting.Interfaces;
28using System;
29
30namespace HeuristicLab.Problems.VehicleRouting.Interpreters {
31  public abstract class VRPInterpreter : IVRPDataInterpreter<IVRPData> {
32    protected abstract void Interpret(IVRPData data, IVRPProblemInstance problemInstance);
33
34    protected virtual IVRPEncoding GetBestKnowTour(IVRPData data, IVRPProblemInstance problemInstance) {
35      if (data.BestKnownTour != null) {
36        PotvinEncoding solution = new PotvinEncoding(problemInstance);
37
38        for (int i = 0; i < data.BestKnownTour.GetLength(0); i++) {
39          Tour tour = new Tour();
40          solution.Tours.Add(tour);
41
42          foreach (int stop in data.BestKnownTour[i]) {
43            tour.Stops.Add(stop + 1);
44          }
45        }
46
47        if (data.BestKnownTourVehicleAssignment != null) {
48          if (data.BestKnownTourVehicleAssignment.Length != solution.VehicleAssignment.Length)
49            throw new InvalidDataException("Number of vehicles of the best known tour does not match the number vehicles of the instance.");
50          for (int i = 0; i < data.BestKnownTourVehicleAssignment.Length; i++)
51            solution.VehicleAssignment[i] = data.BestKnownTourVehicleAssignment[i];
52        }
53
54        return solution;
55      } else {
56        return null;
57      }
58    }
59
60    protected abstract IVRPProblemInstance CreateProblemInstance();
61
62    public VRPInstanceDescription Interpret(IVRPData data) {
63      VRPInstanceDescription result = new VRPInstanceDescription();
64      result.Name = data.Name;
65      result.Description = data.Description;
66
67      IVRPProblemInstance problem = CreateProblemInstance();
68      Interpret(data, problem);
69      result.ProblemInstance = problem;
70
71      result.BestKnownQuality = data.BestKnownQuality;
72      result.BestKnownSolution = GetBestKnowTour(data, problem);
73
74      return result;
75    }
76  }
77}
Note: See TracBrowser for help on using the repository browser.