Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2817-BinPackingSpeedup/HeuristicLab.Problems.BinPacking/3.3/3D/Instances/RealWorldContainerPackingInstanceProvider.cs @ 15473

Last change on this file since 15473 was 15473, checked in by rhanghof, 6 years ago

#2817:
-Added unit tests
-Refactoring of bp 3D

File size: 5.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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;
30using HeuristicLab.Problems.BinPacking3D.Instances;
31
32namespace HeuristicLab.Problems.BinPacking3D {
33  public sealed class RealWorldContainerPackingInstanceProvider : ProblemInstanceProvider<BPPData> {
34    private string FileName { get { return "ContainerPackingInstances"; } }
35   
36    public override string Name {
37      get { return "Real-world Container Packing"; }
38    }
39
40    public override string Description {
41      get { return "Problem instances derived from real-world container packing data."; }
42    }
43
44    public override Uri WebLink {
45      get { return null; }
46    }
47
48    public override string ReferencePublication {
49      get { return null; }
50    }
51
52    public RealWorldContainerPackingInstanceProvider() : base() { }
53
54    public override IEnumerable<IDataDescriptor> GetDataDescriptors() {
55      var instanceArchiveName = GetResourceName(FileName + @"\.zip");
56      if (String.IsNullOrEmpty(instanceArchiveName)) yield break;
57
58      using (var instanceStream = new ZipArchive(GetType().Assembly.GetManifestResourceStream(instanceArchiveName), ZipArchiveMode.Read)) {
59        foreach (var entry in instanceStream.Entries.Select(x => x.Name).OrderBy(x => x)) {
60          yield return new ThreeDInstanceDescriptor(Path.GetFileNameWithoutExtension(entry), GetDescription(), entry);
61        }
62      }
63    }
64
65    private string GetResourceName(string fileName) {
66      return Assembly.GetExecutingAssembly().GetManifestResourceNames()
67              .Where(x => Regex.Match(x, @".*\.Instances\." + fileName).Success).SingleOrDefault();
68    }
69
70    private string GetDescription() {
71      return "Embedded instance of plugin version " + Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).Cast<AssemblyFileVersionAttribute>().First().Version + ".";
72    }
73
74    public override BPPData LoadData(IDataDescriptor dd) {
75      var desc = dd as ThreeDInstanceDescriptor;
76      if (desc == null) throw new NotSupportedException("Cannot load data descriptor " + dd);
77      var instanceArchiveName = GetResourceName(FileName + @"\.zip");
78      using (
79        var instancesZipFile = new ZipArchive(GetType().Assembly.GetManifestResourceStream(instanceArchiveName),
80          ZipArchiveMode.Read)) {
81        var entry = instancesZipFile.GetEntry(desc.InstanceIdentifier);
82
83        using (var stream = entry.Open()) {
84          var parser = new ThreeDInstanceParser();
85          parser.Parse(stream);
86
87          return new BPPData() {
88            Name = desc.Name,
89            Description = desc.Description,
90            BinShape = parser.Bin,
91            Items = parser.Items.ToArray()
92          };
93        }
94      }
95    }
96
97    public override bool CanImportData {
98      get { return true; }
99    }
100
101    public override BPPData ImportData(string path) {
102      using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) {
103        var parser = new ThreeDInstanceParser();
104        parser.Parse(stream);
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.