Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2665 Dynamic ErcValues, Separate Push from BenchmarkSuite Push

File size: 2.5 KB
RevLine 
[14727]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
[14777]13  public abstract class BenchmarkSuiteDataDescriptor : IBenchmarkSuiteDataDescriptor {
[14744]14    private const string InstanceArchiveName = "HeuristicLab.BenchmarkSuite.Data.BenchmarkExamples.zip";
15    private const string ResourcePath = @".*\.Data\.";
[14897]16    private const string ExampleFieldDelimiter = ",";
[14727]17
[14744]18    private Example[] examples;
[14727]19
[14834]20    protected Example[] CloneExamples() {
21      if (examples == null) examples = ParseData().ToArray();
22      return examples.Select(e => (Example)e.Clone()).ToArray();
[14777]23    }
24
[14875]25    public abstract ProblemData CreateProblemData();
[14834]26
27    protected abstract string FileName { get; }
[14777]28    public abstract string Name { get; }
29    public abstract string Description { get; }
[14834]30    protected abstract int InputArgumentCount { get; }
31    protected abstract int OutputArgumentCount { get; }
[14727]32
[14834]33    protected abstract Example ParseExample(string[] input, string[] output);
[14727]34
35    private IEnumerable<Example> ParseData() {
[14897]36      using (var file = GetType().Assembly.GetManifestResourceStream(InstanceArchiveName))
[14727]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;
[14897]42          parser.SetDelimiters(ExampleFieldDelimiter);
43          parser.HasFieldsEnclosedInQuotes = true;
[14727]44          parser.TrimWhiteSpace = false;
45
46          //Processing rows
47          while (!parser.EndOfData) {
48            var fields = parser.ReadFields();
49
[14834]50            if (fields.Length != InputArgumentCount + OutputArgumentCount)
[14727]51              throw new InvalidDataException("Number of values do not fit");
52
[14834]53            var input = fields.Take(InputArgumentCount).ToArray();
54            var output = fields.Skip(InputArgumentCount).ToArray();
[14727]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  }
[14744]69}
Note: See TracBrowser for help on using the repository browser.