Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2665 Renamings due to typos, ManagedPool tests, Skip Noops in Debugger

File size: 4.6 KB
Line 
1namespace HeuristicLab.BenchmarkSuite {
2  using System;
3  using System.Collections.Generic;
4  using System.IO;
5  using System.IO.Compression;
6  using System.Linq;
7  using System.Reflection;
8  using System.Text.RegularExpressions;
9
10  using HeuristicLab.BenchmarkSuite.Problems;
11  using HeuristicLab.Common;
12  using HeuristicLab.Core;
13  using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
14
15  using Microsoft.VisualBasic.FileIO;
16
17
18  [StorableClass]
19  public abstract class BenchmarkSuiteDataDescriptor : NamedItem, IBenchmarkSuiteDataDescriptor {
20    private const string InstanceArchiveName = "HeuristicLab.BenchmarkSuite.Data.BenchmarkExamples.zip";
21    private const string ResourcePath = @".*\.Data\.";
22    private const string ExampleSeparator = ",";
23
24    private const char ArrayValueSeparator = ' ';
25    private static readonly char[] ArraySymbolTrim = { '[', ']' };
26
27    protected BenchmarkSuiteDataDescriptor() { }
28
29    [StorableConstructor]
30    protected BenchmarkSuiteDataDescriptor(bool deserializing) : base(deserializing) { }
31
32
33    [Storable]
34    private Example[] examples;
35    public Example[] Examples
36    {
37      get
38      {
39        return this.examples ?? (this.examples = this.ParseData().ToArray());
40      }
41    }
42
43    public new abstract string Name { get; }
44    protected abstract string FileName { get; }
45    public new abstract string Description { get; }
46    public abstract int OriginalTrainingCount { get; }
47    public abstract int OriginalTestCount { get; }
48    public abstract int BestResult { get; }
49    public abstract int WorstResult { get; }
50    public abstract int InputArgumentCount { get; }
51    public abstract int OutputArgumentCount { get; }
52    public int TotalArgumentCount { get { return InputArgumentCount + OutputArgumentCount; } }
53
54    public abstract Example ParseExample(string[] input, string[] output);
55
56    private IEnumerable<Example> ParseData() {
57      using (var file = this.GetType().Assembly.GetManifestResourceStream(InstanceArchiveName))
58      using (var archive = new ZipArchive(file, ZipArchiveMode.Read)) {
59        var entry = archive.Entries.SingleOrDefault(x => x.Name == FileName);
60
61        using (var parser = new TextFieldParser(entry.Open())) {
62          parser.TextFieldType = FieldType.Delimited;
63          parser.SetDelimiters(ExampleSeparator);
64          parser.TrimWhiteSpace = false;
65
66          //Processing rows
67          while (!parser.EndOfData) {
68            var fields = parser.ReadFields();
69
70            if (fields.Length != TotalArgumentCount)
71              throw new InvalidDataException("Number of values do not fit");
72
73            var input = fields.Take(InputArgumentCount).ToArray();
74            var output = fields.Skip(InputArgumentCount).ToArray();
75
76            yield return ParseExample(input, output);
77          }
78        }
79      }
80    }
81
82    protected static string GetResourceName(string fileName) {
83      return Assembly
84          .GetExecutingAssembly()
85          .GetManifestResourceNames()
86          .SingleOrDefault(x => Regex.Match(x, ResourcePath + fileName).Success);
87    }
88
89    protected static double[] ConvertDoubles(string input) {
90      return ConvertMultiple(input, double.Parse);
91    }
92
93    protected static double ConvertDouble(string input) {
94      return double.Parse(input);
95    }
96
97    protected static long[] ConvertIntegers(string input) {
98      return ConvertMultiple(input, long.Parse);
99    }
100
101    protected static long ConvertInteger(string input) {
102
103      return string.IsNullOrEmpty(input) ? default(long) : long.Parse(input);
104    }
105
106    protected static T[] ConvertMultiple<T>(string input, Func<string, T> converter) {
107      return input
108       .Trim(ArraySymbolTrim)
109       .Split(ArrayValueSeparator)
110       .Where(s => !string.IsNullOrWhiteSpace(s))
111       .Select(converter)
112       .ToArray();
113    }
114
115    protected static IEnumerable<string> SplitByNewLine(string input) {
116      return input.Split(new[] { Environment.NewLine, "\n", "\r\n" }, StringSplitOptions.None);
117    }
118
119    private static string[] trueFormats = new[] { "true", "t", "1" };
120    private static string[] falseFormats = new[] { "false", "f", "0" };
121
122    protected static bool ConvertBool(string input) {
123      var str = input.ToLower();
124
125      if (trueFormats.Contains(str)) return true;
126      if (falseFormats.Contains(str)) return false;
127      throw new InvalidDataException(string.Format("Unable to parse {0} as boolean", str));
128    }
129
130    public override IDeepCloneable Clone(Cloner cloner) {
131      return this;
132    }
133  }
134}
Note: See TracBrowser for help on using the repository browser.