Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking/3.3/Instances/BPPInstanceProvider.cs @ 9348

Last change on this file since 9348 was 9348, checked in by jhelm, 12 years ago

#1966: First working version of bin-packing problems.

File size: 4.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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;
23using System.Collections.Generic;
24using System.IO;
25using System.Linq;
26using System.Reflection;
27using System.Text.RegularExpressions;
28using ICSharpCode.SharpZipLib.Zip;
29
30namespace HeuristicLab.Problems.Instances.BinPacking {
31  public class BPPORLIBInstanceProvider : ProblemInstanceProvider<BPPData> {
32
33    public override string Name {
34      get { return "ORLIB BPP"; }
35    }
36
37    public override string Description {
38      get { return "Bin packing problems from the Operations Research Library."; }
39    }
40
41    public override Uri WebLink {
42      get { return new Uri("http://people.brunel.ac.uk/~mastjjb/jeb/orlib/binpacktwoinfo.html , http://www.diku.dk/~pisinger/new3dbpp/readme.3dbpp"); }
43    }
44
45    public override string ReferencePublication {
46      get { return String.Empty; }
47    }
48
49    public override IEnumerable<IDataDescriptor> GetDataDescriptors() {
50      var instanceArchiveName = GetResourceName("BPPORLIB.zip");
51      if (String.IsNullOrEmpty(instanceArchiveName)) yield break;
52
53      using (var instanceStream = new ZipInputStream(GetType().Assembly.GetManifestResourceStream(instanceArchiveName))) {
54        foreach (var entry in GetZipContents(instanceStream).OrderBy(x => x)) {
55          yield return new BPPORLIBDataDescriptor(Path.GetFileNameWithoutExtension(entry), GetDescription(), entry, null);
56        }
57      }
58    }
59
60    public override BPPData LoadData(IDataDescriptor id) {
61      var descriptor = (BPPORLIBDataDescriptor)id;
62      var instanceArchiveName = GetResourceName("JSSPORLIB.zip");
63      using (var instancesZipFile = new ZipFile(GetType().Assembly.GetManifestResourceStream(instanceArchiveName))) {
64        var entry = instancesZipFile.GetEntry(descriptor.InstanceIdentifier);
65
66        using (var stream = instancesZipFile.GetInputStream(entry)) {
67          var parser = new BPPORLIBParser();
68          parser.Parse(stream);
69          var instance = Load(parser);
70          instance.Name = id.Name;
71          instance.Description = id.Description;
72
73          return instance;
74        }
75      }
76    }
77
78    public override bool CanImportData {
79      get { return true; }
80    }
81    public override BPPData ImportData(string path) {
82      var parser = new BPPORLIBParser();
83      parser.Parse(path);
84      var instance = Load(parser);
85      instance.Name = Path.GetFileName(path);
86      instance.Description = "Loaded from file \"" + path + "\" on " + DateTime.Now.ToString();
87      return instance;
88    }
89
90    private BPPData Load(BPPORLIBParser parser) {
91      var instance = new BPPData {
92        Items = parser.Items,
93        BinMeasures = parser.BinMeasures,
94        ItemMeasures = parser.ItemMeasures
95      };
96      return instance;
97    }
98
99    public override bool CanExportData {
100      get { return true; }
101    }
102
103    public override void ExportData(BPPData instance, string path) {
104      var parser = new BPPORLIBParser {
105        Name = instance.Name,
106        Description = instance.Description,
107        Items = instance.Items,
108        BinMeasures = instance.BinMeasures,
109        ItemMeasures = instance.ItemMeasures
110      };
111      parser.Export(path);
112    }
113
114    private string GetDescription() {
115      return "Embedded instance of plugin version " + Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).Cast<AssemblyFileVersionAttribute>().First().Version + ".";
116    }
117
118    protected virtual string GetResourceName(string fileName) {
119      return Assembly.GetExecutingAssembly().GetManifestResourceNames()
120        .SingleOrDefault(x => Regex.Match(x, @".*\.Data\." + fileName).Success);
121    }
122
123    protected IEnumerable<string> GetZipContents(ZipInputStream zipFile) {
124      ZipEntry entry;
125      while ((entry = zipFile.GetNextEntry()) != null) {
126        yield return entry.Name;
127      }
128    }
129  }
130}
Note: See TracBrowser for help on using the repository browser.