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