1 | namespace HeuristicLab.Tests.Benchmark.Problem {
|
---|
2 | using System;
|
---|
3 | using System.Collections.Generic;
|
---|
4 | using System.IO;
|
---|
5 | using System.IO.Compression;
|
---|
6 | using System.Linq;
|
---|
7 | using System.Reflection;
|
---|
8 | using System.Text.RegularExpressions;
|
---|
9 | using HeuristicLab.BenchmarkSuite.ProblemData;
|
---|
10 |
|
---|
11 | public class Problem<Tin, Tout> {
|
---|
12 | public const char ExampleSeparator = ',';
|
---|
13 |
|
---|
14 | public const string ArchiveFileName = "BenchmarkExamples.zip";
|
---|
15 |
|
---|
16 | private static readonly string instanceArchiveName = GetResourceName(ArchiveFileName);
|
---|
17 |
|
---|
18 | protected List<Example<Tin, Tout>> Examples { get; set; }
|
---|
19 |
|
---|
20 | protected List<Example<Tin, Tout>> GetExamples(
|
---|
21 | string problemName,
|
---|
22 | Converter<string, Tin> inputConverter,
|
---|
23 | Converter<string, Tout> outputConverter) {
|
---|
24 | using (var file = this.GetType().Assembly.GetManifestResourceStream(instanceArchiveName)) {
|
---|
25 | using (var archive = new ZipArchive(file, ZipArchiveMode.Read)) {
|
---|
26 | var entry = archive.Entries.SingleOrDefault(x => x.Name == problemName);
|
---|
27 |
|
---|
28 | using (var reader = new StreamReader(entry.Open())) {
|
---|
29 | var examples = new List<Example<Tin, Tout>>();
|
---|
30 |
|
---|
31 | // skip training headline
|
---|
32 | if (reader.Peek() >= 0) reader.ReadLine();
|
---|
33 |
|
---|
34 | while (reader.Peek() >= 0) {
|
---|
35 | var line = reader.ReadLine();
|
---|
36 |
|
---|
37 | // return
|
---|
38 | if (line.StartsWith("test")) break;
|
---|
39 |
|
---|
40 | var values = line.Split(ExampleSeparator);
|
---|
41 |
|
---|
42 | examples.Add(
|
---|
43 | new Example<Tin, Tout> { Input = inputConverter(values[0]), Output = outputConverter(values[1]) });
|
---|
44 | }
|
---|
45 |
|
---|
46 | return examples;
|
---|
47 | }
|
---|
48 | }
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 | private static string GetResourceName(string fileName) {
|
---|
53 | return Assembly
|
---|
54 | .GetExecutingAssembly()
|
---|
55 | .GetManifestResourceNames()
|
---|
56 | .SingleOrDefault(x => Regex.Match(x, @".*\.Data\." + fileName).Success);
|
---|
57 | }
|
---|
58 | }
|
---|
59 | } |
---|