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