Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Tests/Benchmark/Problem/Problem.cs @ 14392

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

#2665 Full Push 3.0 instruction set and tests; Added first benchmark test (count odds) for random walk tests;

File size: 2.4 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.IO.Compression;
5using System.Linq;
6using System.Reflection;
7using System.Text.RegularExpressions;
8
9namespace HeuristicLab.Tests.Benchmark.Problem
10{
11    public class Problem<Tin, Tout>
12    {
13        public const char ExampleSeparator = ',';
14        public const string ArchiveFileName = "BenchmarkExamples.zip";
15
16        private static string instanceArchiveName = GetResourceName(ArchiveFileName);
17
18        protected List<Example<Tin, Tout>> Examples { get; set; }
19
20        protected List<Example<Tin, Tout>> GetExamples(string problemName, Converter<string, Tin> inputConverter, Converter<string, Tout> outputConverter)
21        {
22            using (var file = GetType().Assembly.GetManifestResourceStream(instanceArchiveName))
23            using (var archive = new ZipArchive(file, ZipArchiveMode.Read))
24            {
25                var entry = archive.Entries.SingleOrDefault(x => x.Name == problemName);
26
27                using (var reader = new StreamReader(entry.Open()))
28                {
29                    var examples = new List<Example<Tin, Tout>>();
30
31                    // skip training headline
32                    if (reader.Peek() >= 0)
33                    {
34                        reader.ReadLine();
35                    }
36
37                    while (reader.Peek() >= 0)
38                    {
39                        var line = reader.ReadLine();
40
41                        // return
42                        if (line.StartsWith("test"))
43                        {
44                            break;
45                        }
46
47                        var values = line.Split(ExampleSeparator);
48
49                        examples.Add(new Example<Tin, Tout>
50                        {
51                            Input = inputConverter(values[0]),
52                            Output = outputConverter(values[1])
53                        });
54                    }
55
56                    return examples;
57                }
58            }
59        }
60
61        private static string GetResourceName(string fileName)
62        {
63            return Assembly
64                .GetExecutingAssembly()
65                .GetManifestResourceNames()
66                .Where(x => Regex.Match(x, @".*\.Data\." + fileName).Success)
67                .SingleOrDefault();
68        }
69    }
70}
Note: See TracBrowser for help on using the repository browser.