Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2665 simplifier, push solution results view, performance improvements, small bug fixes, ui fixes

File size: 3.6 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  using HeuristicLab.BenchmarkSuite.ERC;
10  using HeuristicLab.BenchmarkSuite.Problems;
11
12  using Microsoft.VisualBasic.FileIO;
13
14
15  public abstract class BenchmarkSuiteDataDescriptor : IBenchmarkSuiteDataDescriptor {
16    private const string InstanceArchiveName = "HeuristicLab.BenchmarkSuite.Data.BenchmarkExamples.zip";
17    private const string ResourcePath = @".*\.Data\.";
18    private const string ExampleSeparator = ",";
19
20    private Example[] examples;
21    public Example[] Examples
22    {
23      get
24      {
25        return this.examples ?? (this.examples = this.ParseData().ToArray());
26      }
27    }
28
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; }
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; }
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; }
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
81            if (fields.Length != InputArgumentTypes.Length + OutputArgumentTypes.Length)
82              throw new InvalidDataException("Number of values do not fit");
83
84            var input = fields.Take(InputArgumentTypes.Length).ToArray();
85            var output = fields.Skip(OutputArgumentTypes.Length).ToArray();
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  }
100}
Note: See TracBrowser for help on using the repository browser.