[14727] | 1 | namespace HeuristicLab.BenchmarkSuite {
|
---|
| 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 |
|
---|
[14777] | 9 | using HeuristicLab.BenchmarkSuite.ERC;
|
---|
[14727] | 10 | using HeuristicLab.BenchmarkSuite.Problems;
|
---|
| 11 |
|
---|
| 12 | using Microsoft.VisualBasic.FileIO;
|
---|
| 13 |
|
---|
| 14 |
|
---|
[14777] | 15 | public abstract class BenchmarkSuiteDataDescriptor : IBenchmarkSuiteDataDescriptor {
|
---|
[14744] | 16 | private const string InstanceArchiveName = "HeuristicLab.BenchmarkSuite.Data.BenchmarkExamples.zip";
|
---|
| 17 | private const string ResourcePath = @".*\.Data\.";
|
---|
| 18 | private const string ExampleSeparator = ",";
|
---|
[14727] | 19 |
|
---|
[14744] | 20 | private Example[] examples;
|
---|
| 21 | public Example[] Examples
|
---|
| 22 | {
|
---|
| 23 | get
|
---|
| 24 | {
|
---|
| 25 | return this.examples ?? (this.examples = this.ParseData().ToArray());
|
---|
| 26 | }
|
---|
[14727] | 27 | }
|
---|
| 28 |
|
---|
[14777] | 29 | public Data CreateProblemData() {
|
---|
| 30 | return new Data {
|
---|
| 31 | Name = Name,
|
---|
| 32 | Description = Description,
|
---|
| 33 | Examples = Examples.ToArray(),
|
---|
| 34 | BestResult = BestResult,
|
---|
| 35 | WorstResult = WorstResult,
|
---|
| 36 | InputArgumentTypes = InputArgumentTypes,
|
---|
| 37 | OutputArgumentTypes = OutputArgumentTypes,
|
---|
| 38 | OriginalTestCount = OriginalTestCount,
|
---|
| 39 | OriginalTrainingCount = OriginalTrainingCount,
|
---|
| 40 | EnabledDataTypes = EnabledDataTypes,
|
---|
| 41 | EvalLimit = EvalLimit,
|
---|
| 42 | MaxGenerations = MaxGenerations,
|
---|
| 43 | MaxSize = MaxSize,
|
---|
| 44 | ProgEvalBudget = ProgEvalBudget,
|
---|
| 45 | ProblemErcOptions = ProblemErcOptions,
|
---|
| 46 | };
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | public abstract string Name { get; }
|
---|
| 50 | public abstract string Description { get; }
|
---|
[14744] | 51 | protected abstract string FileName { get; }
|
---|
| 52 | public abstract int OriginalTrainingCount { get; }
|
---|
| 53 | public abstract int OriginalTestCount { get; }
|
---|
| 54 | public abstract int BestResult { get; }
|
---|
| 55 | public abstract int WorstResult { get; }
|
---|
[14777] | 56 | public abstract ExampleArgumentType[] InputArgumentTypes { get; }
|
---|
| 57 | public abstract ExampleArgumentType[] OutputArgumentTypes { get; }
|
---|
| 58 | public abstract ProblemErcOptions ProblemErcOptions { get; }
|
---|
| 59 | public abstract DataTypes EnabledDataTypes { get; }
|
---|
| 60 | public abstract int MaxSize { get; }
|
---|
| 61 | public abstract int EvalLimit { get; }
|
---|
| 62 | public abstract int MaxGenerations { get; }
|
---|
| 63 | public abstract int ProgEvalBudget { get; }
|
---|
[14727] | 64 |
|
---|
| 65 | public abstract Example ParseExample(string[] input, string[] output);
|
---|
| 66 |
|
---|
| 67 | private IEnumerable<Example> ParseData() {
|
---|
| 68 | using (var file = this.GetType().Assembly.GetManifestResourceStream(InstanceArchiveName))
|
---|
| 69 | using (var archive = new ZipArchive(file, ZipArchiveMode.Read)) {
|
---|
| 70 | var entry = archive.Entries.SingleOrDefault(x => x.Name == FileName);
|
---|
| 71 |
|
---|
| 72 | using (var parser = new TextFieldParser(entry.Open())) {
|
---|
| 73 | parser.TextFieldType = FieldType.Delimited;
|
---|
| 74 | parser.SetDelimiters(ExampleSeparator);
|
---|
| 75 | parser.TrimWhiteSpace = false;
|
---|
| 76 |
|
---|
| 77 | //Processing rows
|
---|
| 78 | while (!parser.EndOfData) {
|
---|
| 79 | var fields = parser.ReadFields();
|
---|
| 80 |
|
---|
[14777] | 81 | if (fields.Length != InputArgumentTypes.Length + OutputArgumentTypes.Length)
|
---|
[14727] | 82 | throw new InvalidDataException("Number of values do not fit");
|
---|
| 83 |
|
---|
[14777] | 84 | var input = fields.Take(InputArgumentTypes.Length).ToArray();
|
---|
| 85 | var output = fields.Skip(OutputArgumentTypes.Length).ToArray();
|
---|
[14727] | 86 |
|
---|
| 87 | yield return ParseExample(input, output);
|
---|
| 88 | }
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | protected static string GetResourceName(string fileName) {
|
---|
| 94 | return Assembly
|
---|
| 95 | .GetExecutingAssembly()
|
---|
| 96 | .GetManifestResourceNames()
|
---|
| 97 | .SingleOrDefault(x => Regex.Match(x, ResourcePath + fileName).Success);
|
---|
| 98 | }
|
---|
| 99 | }
|
---|
[14744] | 100 | } |
---|