Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problem.ProgramSynthesis.BenchmarkSuite/BenchmarkSuiteDataDescriptor.cs @ 14897

Last change on this file since 14897 was 14897, checked in by pkimmesw, 7 years ago

#2665 Dynamic ErcValues, Separate Push from BenchmarkSuite Push

File size: 2.5 KB
Line 
1namespace 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  using HeuristicLab.BenchmarkSuite.Problems;
9
10  using Microsoft.VisualBasic.FileIO;
11
12
13  public abstract class BenchmarkSuiteDataDescriptor : IBenchmarkSuiteDataDescriptor {
14    private const string InstanceArchiveName = "HeuristicLab.BenchmarkSuite.Data.BenchmarkExamples.zip";
15    private const string ResourcePath = @".*\.Data\.";
16    private const string ExampleFieldDelimiter = ",";
17
18    private Example[] examples;
19
20    protected Example[] CloneExamples() {
21      if (examples == null) examples = ParseData().ToArray();
22      return examples.Select(e => (Example)e.Clone()).ToArray();
23    }
24
25    public abstract ProblemData CreateProblemData();
26
27    protected abstract string FileName { get; }
28    public abstract string Name { get; }
29    public abstract string Description { get; }
30    protected abstract int InputArgumentCount { get; }
31    protected abstract int OutputArgumentCount { get; }
32
33    protected abstract Example ParseExample(string[] input, string[] output);
34
35    private IEnumerable<Example> ParseData() {
36      using (var file = GetType().Assembly.GetManifestResourceStream(InstanceArchiveName))
37      using (var archive = new ZipArchive(file, ZipArchiveMode.Read)) {
38        var entry = archive.Entries.SingleOrDefault(x => x.Name == FileName);
39
40        using (var parser = new TextFieldParser(entry.Open())) {
41          parser.TextFieldType = FieldType.Delimited;
42          parser.SetDelimiters(ExampleFieldDelimiter);
43          parser.HasFieldsEnclosedInQuotes = true;
44          parser.TrimWhiteSpace = false;
45
46          //Processing rows
47          while (!parser.EndOfData) {
48            var fields = parser.ReadFields();
49
50            if (fields.Length != InputArgumentCount + OutputArgumentCount)
51              throw new InvalidDataException("Number of values do not fit");
52
53            var input = fields.Take(InputArgumentCount).ToArray();
54            var output = fields.Skip(InputArgumentCount).ToArray();
55
56            yield return ParseExample(input, output);
57          }
58        }
59      }
60    }
61
62    protected static string GetResourceName(string fileName) {
63      return Assembly
64          .GetExecutingAssembly()
65          .GetManifestResourceNames()
66          .SingleOrDefault(x => Regex.Match(x, ResourcePath + fileName).Success);
67    }
68  }
69}
Note: See TracBrowser for help on using the repository browser.