Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 13023 was 13023, checked in by gkronber, 9 years ago

#1966: fixed build configuration

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