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