Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2839_HiveProjectManagement/HeuristicLab.Problems.BinPacking/3.3/3D/Instances/RealWorldContainerPackingInstanceProvider.cs

Last change on this file was 16057, checked in by jkarder, 6 years ago

#2839:

File size: 5.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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.IO.Compression;
26using System.Linq;
27using System.Reflection;
28using System.Text.RegularExpressions;
29using HeuristicLab.Problems.Instances;
30
31namespace HeuristicLab.Problems.BinPacking3D {
32  public sealed class RealWorldContainerPackingInstanceProvider : ProblemInstanceProvider<BPPData> {
33    private string FileName { get { return "ContainerPackingInstances"; } }
34   
35    public override string Name {
36      get { return "Real-world Container Packing"; }
37    }
38
39    public override string Description {
40      get { return "Problem instances derived from real-world container packing data."; }
41    }
42
43    public override Uri WebLink {
44      get { return null; }
45    }
46
47    public override string ReferencePublication {
48      get { return null; }
49    }
50
51    public RealWorldContainerPackingInstanceProvider() : base() { }
52
53    public override IEnumerable<IDataDescriptor> GetDataDescriptors() {
54      var instanceArchiveName = GetResourceName(FileName + @"\.zip");
55      if (String.IsNullOrEmpty(instanceArchiveName)) yield break;
56
57      using (var instanceStream = new ZipArchive(GetType().Assembly.GetManifestResourceStream(instanceArchiveName), ZipArchiveMode.Read)) {
58        foreach (var entry in instanceStream.Entries.Select(x => x.Name).OrderBy(x => x)) {
59          yield return new ThreeDInstanceDescriptor(Path.GetFileNameWithoutExtension(entry), GetDescription(), entry);
60        }
61      }
62    }
63
64    private string GetResourceName(string fileName) {
65      return Assembly.GetExecutingAssembly().GetManifestResourceNames()
66              .Where(x => Regex.Match(x, @".*\.Instances\." + fileName).Success).SingleOrDefault();
67    }
68
69    private string GetDescription() {
70      return "Embedded instance of plugin version " + Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).Cast<AssemblyFileVersionAttribute>().First().Version + ".";
71    }
72
73    public override BPPData LoadData(IDataDescriptor dd) {
74      var desc = dd as ThreeDInstanceDescriptor;
75      if (desc == null) throw new NotSupportedException("Cannot load data descriptor " + dd);
76      var instanceArchiveName = GetResourceName(FileName + @"\.zip");
77      using (
78        var instancesZipFile = new ZipArchive(GetType().Assembly.GetManifestResourceStream(instanceArchiveName),
79          ZipArchiveMode.Read)) {
80        var entry = instancesZipFile.GetEntry(desc.InstanceIdentifier);
81
82        using (var stream = entry.Open()) {
83          var parser = new ThreeDInstanceParser();
84          parser.Parse(stream);
85
86          return new BPPData() {
87            Name = desc.Name,
88            Description = desc.Description,
89            BinShape = parser.Bin,
90            Items = parser.Items.ToArray()
91          };
92        }
93      }
94    }
95
96    public override bool CanImportData {
97      get { return true; }
98    }
99
100    public override BPPData ImportData(string path) {
101      using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) {
102        var parser = new ThreeDInstanceParser();
103        parser.Parse(stream);
104
105        return new BPPData() {
106          Name = Path.GetFileNameWithoutExtension(path),
107          Description = "Imported instance from " + path,
108          BinShape = parser.Bin,
109          Items = parser.Items.ToArray()
110        };
111      }
112    }
113
114    public override bool CanExportData {
115      get { return true; }
116    }
117
118    public override void ExportData(BPPData instance, string file) {
119      using (Stream stream = new FileStream(file, FileMode.OpenOrCreate, FileAccess.Write)) {
120        Export(instance, stream);
121      }
122    }
123    public static void Export(BPPData instance, Stream stream) {
124
125      using (var writer = new StreamWriter(stream)) {
126        writer.WriteLine(String.Format("{0,-5} {1,-5} {2,-5}   WBIN,HBIN,DBIN", instance.BinShape.Width, instance.BinShape.Height, instance.BinShape.Depth));
127        for (int i = 0; i < instance.NumItems; i++) {
128          if (i == 0)
129            writer.WriteLine("{0,-5} {1,-5} {2,-5}   W(I),H(I),D(I),I=1,...,N", instance.Items[i].Width, instance.Items[i].Height, instance.Items[i].Depth);
130          else
131            writer.WriteLine("{0,-5} {1,-5} {2,-5}", instance.Items[i].Width, instance.Items[i].Height, instance.Items[i].Depth);
132
133        }
134        writer.Flush();
135      }
136    }
137
138  }
139}
Note: See TracBrowser for help on using the repository browser.