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