Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceReintegration/HeuristicLab.Problems.VehicleRouting/3.4/Interpreters/VRPInterpreter.cs @ 14929

Last change on this file since 14929 was 14929, checked in by gkronber, 7 years ago

#2520 fixed unit tests for new persistence: loading & storing all samples

File size: 3.0 KB
Line 
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
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;
29using HeuristicLab.Persistence;
30
31namespace 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}
Note: See TracBrowser for help on using the repository browser.