Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2665 Fixed Benchmark Problem Definition, Converted LoopExpressions to stateless expressions, Added several unit test to ensure funcionality, Fixed UI bugs

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