Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking.2D/3.3/Instances/BPPInstanceProvider.cs @ 14053

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

#1966: separated 2d and 3d problem instances using two different providers

File size: 4.7 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.BinPacking2D {
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://people.brunel.ac.uk/~mastjjb/jeb/orlib/binpacktwoinfo.html , 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 parser = new BPPORLIBParser();
77          parser.Parse(stream);
78          var instance = Load(parser);
79          instance.Name = id.Name;
80          instance.Description = id.Description;
81
82          return instance;
83        }
84      }
85    }
86
87    public override bool CanImportData {
88      get { return true; }
89    }
90    public override BPPData ImportData(string path) {
91      var parser = new BPPORLIBParser();
92      parser.Parse(path);
93      var instance = Load(parser);
94      instance.Name = Path.GetFileName(path);
95      instance.Description = "Loaded from file \"" + path + "\" on " + DateTime.Now;
96      return instance;
97    }
98
99    private BPPData Load(BPPORLIBParser parser) {
100      var instance = new BPPData {
101        Items = parser.Items,
102        BinMeasures = parser.BinMeasures,
103        ItemMeasures = parser.ItemMeasures
104      };
105      return instance;
106    }
107
108    public override bool CanExportData {
109      get { return true; }
110    }
111
112    public override void ExportData(BPPData instance, string path) {
113      var parser = new BPPORLIBParser {
114        Name = instance.Name,
115        Description = instance.Description,
116        Items = instance.Items,
117        BinMeasures = instance.BinMeasures,
118        ItemMeasures = instance.ItemMeasures
119      };
120      parser.Export(path);
121    }
122
123    private string GetDescription() {
124      return "Embedded instance of plugin version " + Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).Cast<AssemblyFileVersionAttribute>().First().Version + ".";
125    }
126
127    protected virtual string GetResourceName(string fileName) {
128      return Assembly.GetExecutingAssembly().GetManifestResourceNames()
129        .SingleOrDefault(x => Regex.Match(x, @".*\.Data\." + fileName).Success);
130    }
131  }
132}
Note: See TracBrowser for help on using the repository browser.