Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking.3D/3.3/Instances/BPPInstanceProvider.cs @ 14055

Last change on this file since 14055 was 14055, checked in by gkronber, 8 years ago

#1966: simplified parsers

File size: 4.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 Joseph Helm and 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
23using System;
24using System.Collections.Generic;
25using System.IO;
26using System.IO.Compression;
27using System.Linq;
28using System.Reflection;
29using System.Text.RegularExpressions;
30using HeuristicLab.Problems.Instances;
31
32namespace HeuristicLab.Problems.BinPacking3D {
33  public class BPPORLIBInstanceProvider : ProblemInstanceProvider<BPPData>, IProblemInstanceProvider<BPPData> {
34
35    public override string Name {
36      get { return "ORLIB BPP"; }
37    }
38
39    public override string Description {
40      get { return "Bin packing problems from the Operations Research Library."; }
41    }
42
43    public override Uri WebLink {
44      get { return new Uri("http://www.diku.dk/~pisinger/new3dbpp/readme.3dbpp"); }
45    }
46
47    public override string ReferencePublication {
48      get { return String.Empty; }
49    }
50
51    public override IEnumerable<IDataDescriptor> GetDataDescriptors() {
52      var instanceArchiveName = GetResourceName("BPPORLIB.zip");
53      if (String.IsNullOrEmpty(instanceArchiveName)) yield break;
54
55
56      using (var file = new ZipArchive(GetType().Assembly.GetManifestResourceStream(instanceArchiveName))) {
57
58        foreach (var entry in file.Entries.OrderBy(x => x.Name)) {
59          if (string.IsNullOrWhiteSpace(entry.Name)) continue;
60          yield return new BPPORLIBDataDescriptor(
61            name: Path.GetFileNameWithoutExtension(entry.Name),
62            description: GetDescription(),
63            instanceIdentifier: entry.FullName,
64            solutionIdentifier: null);
65        }
66      }
67    }
68
69    public override BPPData LoadData(IDataDescriptor id) {
70      var descriptor = (BPPORLIBDataDescriptor)id;
71      var instanceArchiveName = GetResourceName("BPPORLIB.zip");
72      using (var instancesZipFile = new ZipArchive(GetType().Assembly.GetManifestResourceStream(instanceArchiveName))) {
73        var entry = instancesZipFile.GetEntry(descriptor.InstanceIdentifier);
74
75        using (var stream = entry.Open()) {
76          var instance = BPPORLIBParser.Parse(stream);
77          instance.Name = id.Name;
78          instance.Description = id.Description;
79
80          return instance;
81        }
82      }
83    }
84
85    public override bool CanImportData {
86      get { return true; }
87    }
88    public override BPPData ImportData(string path) {
89      var instance = BPPORLIBParser.Parse(path);
90      instance.Name = Path.GetFileName(path);
91      instance.Description = "Loaded from file \"" + path + "\" on " + DateTime.Now;
92      return instance;
93    }
94
95    public override bool CanExportData {
96      get { return true; }
97    }
98
99    public override void ExportData(BPPData instance, string path) {
100      BPPORLIBParser.Export(instance, path);
101    }
102
103    private string GetDescription() {
104      return "Embedded instance of plugin version " + Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).Cast<AssemblyFileVersionAttribute>().First().Version + ".";
105    }
106
107    protected virtual string GetResourceName(string fileName) {
108      return Assembly.GetExecutingAssembly().GetManifestResourceNames()
109        .SingleOrDefault(x => Regex.Match(x, @".*\.Data\." + fileName).Success);
110    }
111  }
112}
Note: See TracBrowser for help on using the repository browser.