- Timestamp:
- 01/25/17 01:53:30 (8 years ago)
- Location:
- branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.BenchmarkSuite
- Files:
-
- 42 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.BenchmarkSuite/BenchmarkSuiteDataDescriptor.cs
r14513 r14602 1 namespace HeuristicLab.BenchmarkSuite 2 { 1 namespace HeuristicLab.BenchmarkSuite { 2 using System; 3 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; 4 9 5 using HeuristicLab.Core; 6 using HeuristicLab.Random; 10 using HeuristicLab.BenchmarkSuite.ProblemData; 7 11 8 public abstract class BenchmarkSuiteDataDescritpor<T> : ArtificialDataDescriptor 9 { 10 protected static IRandom rand = new FastRandom(); 12 using Microsoft.VisualBasic.FileIO; 11 13 12 protected abstract IEnumerable<T> GenerateTraining(); 14 public abstract class BenchmarkSuiteDataDescriptor<TIn, TOut> : IBenchmarkSuiteDataDescriptor { 15 //protected static IRandom rand = new FastRandom(); 13 16 14 protected abstract IEnumerable<T> GenerateTest(); 17 private const string InstanceArchiveName = "HeuristicLab.BenchmarkSuite.Data.BenchmarkExamples.zip"; 18 private const string TestProblemPostfix = "Test.csv"; 19 private const string TrainingProblemPostfix = "Training.csv"; 20 private const string ResourcePath = @".*\.Data\."; 21 private const string ExampleSeparator = ","; 15 22 16 protected abstract IEnumerable<Example> GenerateExamples(IEnumerable<T> trainingAndTest); 23 protected const char ArrayValueSeparator = ' '; 24 protected static readonly char[] ArraySymbolTrim = { '[', ']' }; 17 25 18 protected override IEnumerable<Example> GenerateExamples() 19 { 20 var training = this.GenerateTraining(); 26 public abstract string Name { get; } 27 public abstract string Description { get; } 21 28 22 training = training.Shuffle(rand); 29 protected abstract int InputCount { get; } 30 protected abstract int OutputCount { get; } 23 31 24 var test = this.GenerateTest(); 32 public abstract TIn ConvertInput(string[] input); 33 public abstract TOut ConvertOutput(string[] input); 25 34 26 var all = new List<T>();27 all.AddRange(training);28 all.AddRange(test);35 public IData CreateData() { 36 var training = this.GetTraining(); 37 var test = this.GetTest(); 29 38 30 return this.GenerateExamples(all); 39 return new Data<TIn, TOut>(training, test, 0, double.MaxValue); 40 } 41 42 public IPushData CreatePushData() { 43 var training = this.GetTraining(); 44 var test = this.GetTest(); 45 46 return this.CreatePushData(training, test); 47 } 48 49 private Example<TIn, TOut>[] GetTest() { 50 return this.ParseData(TestProblemPostfix).ToArray(); 51 } 52 53 private Example<TIn, TOut>[] GetTraining() { 54 return this.ParseData(TrainingProblemPostfix)/*.Shuffle(rand)*/.ToArray(); ; 55 } 56 57 public abstract IPushData CreatePushData(Example<TIn, TOut>[] training, Example<TIn, TOut>[] test); 58 59 protected IEnumerable<Example<TIn, TOut>> ParseData(string postfix) { 60 var type = this.GetType(); 61 var problemInstanceName = type.Name + postfix; 62 63 using (var file = this.GetType().Assembly.GetManifestResourceStream(InstanceArchiveName)) 64 using (var archive = new ZipArchive(file, ZipArchiveMode.Read)) { 65 var entry = archive.Entries.SingleOrDefault(x => x.Name == problemInstanceName); 66 67 using (var parser = new TextFieldParser(entry.Open())) { 68 parser.TextFieldType = FieldType.Delimited; 69 parser.SetDelimiters(ExampleSeparator); 70 71 while (!parser.EndOfData) { 72 //Processing row 73 var fields = parser.ReadFields(); 74 75 if (fields.Length != InputCount + OutputCount) 76 throw new InvalidDataException("Number of values do not fit"); 77 78 yield return new Example<TIn, TOut> { 79 Input = ConvertInput(fields.Take(this.InputCount).ToArray()), 80 Output = ConvertOutput(fields.Skip(this.InputCount).ToArray()) 81 }; 82 } 83 } 84 } 85 } 86 87 protected static string GetResourceName(string fileName) { 88 return Assembly 89 .GetExecutingAssembly() 90 .GetManifestResourceNames() 91 .SingleOrDefault(x => Regex.Match(x, ResourcePath + fileName).Success); 92 } 93 94 protected static double[] ConvertDoubles(string input) { 95 return ConvertMultiple(input, double.Parse); 96 } 97 98 protected static double ConvertDouble(string input) { 99 return double.Parse(input); 100 } 101 102 protected static long[] ConvertIntegers(string input) { 103 return ConvertMultiple(input, long.Parse); 104 } 105 106 protected static long ConvertInteger(string input) { 107 108 return string.IsNullOrEmpty(input) ? default(long) : long.Parse(input); 109 } 110 111 protected static T[] ConvertMultiple<T>(string input, Func<string, T> converter) { 112 return input 113 .Trim(ArraySymbolTrim) 114 .Split(ArrayValueSeparator) 115 .Where(s => !string.IsNullOrWhiteSpace(s)) 116 .Select(converter) 117 .ToArray(); 118 } 119 120 protected static IEnumerable<string> SplitByNewLine(string input) { 121 return input.Split(new[] { Environment.NewLine, "\n", "\r\n" }, StringSplitOptions.None); 122 } 123 124 private static string[] trueFormats = new[] { "true", "t", "1" }; 125 private static string[] falseFormats = new[] { "false", "f", "0" }; 126 127 protected static bool ConvertBool(string input) { 128 var str = input.ToLower(); 129 130 if (trueFormats.Contains(str)) return true; 131 if (falseFormats.Contains(str)) return false; 132 throw new InvalidDataException(string.Format("Unable to parse {0} as boolean", str)); 31 133 } 32 134 } -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.BenchmarkSuite/HeuristicLab.BenchmarkSuite.csproj
r14392 r14602 8 8 <AppDesignerFolder>Properties</AppDesignerFolder> 9 9 <RootNamespace>HeuristicLab.BenchmarkSuite</RootNamespace> 10 <AssemblyName>HeuristicLab. BenchmarkSuite</AssemblyName>11 <TargetFrameworkVersion>v4.5 .2</TargetFrameworkVersion>10 <AssemblyName>HeuristicLab.Problems.ProgramSynthesis.BenchmarkSuite</AssemblyName> 11 <TargetFrameworkVersion>v4.5</TargetFrameworkVersion> 12 12 <FileAlignment>512</FileAlignment> 13 13 <ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids> … … 17 17 <IsCodedUITest>False</IsCodedUITest> 18 18 <TestProjectType>UnitTest</TestProjectType> 19 <TargetFrameworkProfile /> 19 20 </PropertyGroup> 20 21 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> … … 22 23 <DebugType>full</DebugType> 23 24 <Optimize>false</Optimize> 24 <OutputPath> bin\Debug\</OutputPath>25 <OutputPath>..\..\..\..\trunk\sources\bin\</OutputPath> 25 26 <DefineConstants>DEBUG;TRACE</DefineConstants> 26 27 <ErrorReport>prompt</ErrorReport> 27 28 <WarningLevel>4</WarningLevel> 29 <LangVersion>5</LangVersion> 28 30 </PropertyGroup> 29 31 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> 30 32 <DebugType>pdbonly</DebugType> 31 33 <Optimize>true</Optimize> 32 <OutputPath> bin\Release\</OutputPath>34 <OutputPath>..\..\..\..\trunk\sources\bin\</OutputPath> 33 35 <DefineConstants>TRACE</DefineConstants> 34 36 <ErrorReport>prompt</ErrorReport> … … 42 44 <HintPath>..\..\..\..\trunk\sources\bin\HeuristicLab.Core-3.3.dll</HintPath> 43 45 </Reference> 46 <Reference Include="HeuristicLab.PluginInfrastructure-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL"> 47 <SpecificVersion>False</SpecificVersion> 48 <HintPath>..\..\..\..\trunk\sources\bin\HeuristicLab.PluginInfrastructure-3.3.dll</HintPath> 49 </Reference> 44 50 <Reference Include="HeuristicLab.Problems.Instances-3.3"> 45 51 <HintPath>..\..\..\..\trunk\sources\bin\HeuristicLab.Problems.Instances-3.3.dll</HintPath> … … 48 54 <HintPath>..\..\..\..\trunk\sources\bin\HeuristicLab.Random-3.3.dll</HintPath> 49 55 </Reference> 56 <Reference Include="Microsoft.VisualBasic" /> 50 57 <Reference Include="System" /> 58 <Reference Include="System.IO.Compression" /> 51 59 </ItemGroup> 52 60 <Choose> … … 63 71 </Choose> 64 72 <ItemGroup> 65 <Compile Include="ArtificialDataDescriptor.cs" />66 73 <Compile Include="BenchmarkSuiteDataDescriptor.cs" /> 67 <Compile Include="Example.cs" /> 74 <Compile Include="BenchmarkSuiteInstanceProvider.cs" /> 75 <Compile Include="BenchmarkSuitePushInstanceProvider.cs" /> 76 <Compile Include="IBenchmarkSuiteDataDescriptor.cs" /> 77 <Compile Include="ProblemData\ReplaceSpaceWithNewlinePushData.cs" /> 78 <Compile Include="ProblemData\PigLatinPushData.cs" /> 79 <Compile Include="ProblemData\NumberIoPushData.cs" /> 80 <Compile Include="ProblemData\NegativeToZeroPushData.cs" /> 81 <Compile Include="ProblemData\MirrorImagePushData.cs" /> 82 <Compile Include="ProblemData\MedianPushData.cs" /> 83 <Compile Include="ProblemData\LastIndexOfZeroPushData.cs" /> 84 <Compile Include="ProblemData\GradePushData.cs" /> 85 <Compile Include="ProblemData\ChecksumPushData.cs" /> 86 <Compile Include="ProblemData\CollatzNumbersPushData.cs" /> 87 <Compile Include="ProblemData\CompareStringLengthsPushData.cs" /> 88 <Compile Include="ProblemData\ForLoopIndexPushData.cs" /> 89 <Compile Include="ProblemData\EvenSquaresPushData.cs" /> 90 <Compile Include="ProblemData\DoubleLettersPushData.cs" /> 91 <Compile Include="ProblemData\DigitsPushData.cs" /> 92 <Compile Include="ProblemData\Data.cs" /> 93 <Compile Include="ProblemData\Example.cs" /> 94 <Compile Include="Plugin.cs" /> 95 <Compile Include="ProblemData\IData.cs" /> 96 <Compile Include="ProblemData\IPushData.cs" /> 97 <Compile Include="ProblemData\CountOddsPushData.cs" /> 98 <Compile Include="Problems\ReplaceSpaceWithNewline.cs" /> 99 <Compile Include="Problems\PigLatin.cs" /> 100 <Compile Include="Problems\NumberIo.cs" /> 101 <Compile Include="Problems\NegativeToZero.cs" /> 102 <Compile Include="Problems\MirrorImage.cs" /> 103 <Compile Include="Problems\Median.cs" /> 104 <Compile Include="Problems\LastIndexOfZero.cs" /> 105 <Compile Include="Problems\Grades.cs" /> 106 <Compile Include="Problems\ForLoopIndex.cs" /> 107 <Compile Include="Problems\EvenSquares.cs" /> 108 <Compile Include="Problems\DoubleLetters.cs" /> 109 <Compile Include="Problems\Checksum.cs" /> 110 <Compile Include="Problems\CollatzNumbers.cs" /> 111 <Compile Include="Problems\CompareStringLengths.cs" /> 112 <Compile Include="Problems\Digits.cs" /> 68 113 <Compile Include="Problems\CountOdds.cs" /> 69 114 <Compile Include="Properties\AssemblyInfo.cs" /> 70 115 </ItemGroup> 71 116 <ItemGroup> 72 <Folder Include="Data\" /> 117 <ProjectReference Include="..\HeuristicLab.Algorithms.PushGP\HeuristicLab.Algorithms.PushGP.csproj"> 118 <Project>{368D1303-470C-4F31-91F2-0FAD379DAA45}</Project> 119 <Name>HeuristicLab.Algorithms.PushGP</Name> 120 </ProjectReference> 121 </ItemGroup> 122 <ItemGroup> 123 <EmbeddedResource Include="Data\BenchmarkExamples.zip" /> 73 124 </ItemGroup> 74 125 <Choose> -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.BenchmarkSuite/HeuristicLab.BenchmarkSuite.csproj.user
r14513 r14602 2 2 <Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> 3 3 <PropertyGroup> 4 <ProjectView> ShowAllFiles</ProjectView>4 <ProjectView>ProjectFiles</ProjectView> 5 5 </PropertyGroup> 6 6 </Project> -
branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.BenchmarkSuite/Problems/CountOdds.cs
r14513 r14602 1 namespace HeuristicLab.BenchmarkSuite.Problems 2 { 3 using System.Collections.Generic; 4 using System.Linq; 1 namespace HeuristicLab.BenchmarkSuite.Problems { 2 using HeuristicLab.BenchmarkSuite.ProblemData; 5 3 6 public class CountOdds : BenchmarkSuiteDataDescri tpor<List<int>>7 {8 p ublic const int MaxOdd = 999;4 public class CountOdds : BenchmarkSuiteDataDescriptor<long[], long> { 5 private const string displayMame = "Count Odds"; 6 private const string description = ""; 9 7 10 public const int MaxEven = 1000; 8 public override string Name { get { return displayMame; } } 9 public override string Description { get { return description; } } 11 10 12 public const int MaxRandom = 1000; 11 protected override int InputCount { get { return 1; } } 12 protected override int OutputCount { get { return 1; } } 13 13 14 public const int MinRandom = 1000; 15 16 public const int MaxVectorLength = 50; 17 18 public override string Description 19 { 20 get 21 { 22 return string.Empty; 23 } 14 public override long[] ConvertInput(string[] input) { 15 return ConvertIntegers(input[0]); 24 16 } 25 17 26 public override string Name 27 { 28 get 29 { 30 return "Count Odds"; 31 } 18 public override long ConvertOutput(string[] output) { 19 return ConvertInteger(output[0]); 32 20 } 33 21 34 protected override IEnumerable<Example> GenerateExamples(IEnumerable<List<int>> inputData) 35 { 36 foreach (var input in inputData) 37 yield return 38 new Example 39 { 40 Input = string.Format("[{0}]", string.Join(", ", input)), 41 Output = input.Count(x => x % 2 == 1).ToString() 42 }; 43 } 44 45 protected override IEnumerable<List<int>> GenerateTest() 46 { 47 foreach (var list in this.GetHardcodedTrainingSamples()) yield return list; 48 49 foreach (var list in this.GetAllOdd(100)) yield return list; 50 51 foreach (var list in this.GetAllEven(100)) yield return list; 52 53 foreach (var list in this.GetRandom(1800)) yield return list; 54 } 55 56 protected override IEnumerable<List<int>> GenerateTraining() 57 { 58 foreach (var list in this.GetHardcodedTrainingSamples()) yield return list; 59 60 foreach (var list in this.GetAllOdd(9)) yield return list; 61 62 foreach (var list in this.GetAllEven(9)) yield return list; 63 64 foreach (var list in this.GetRandom(150)) yield return list; 65 } 66 67 private IEnumerable<List<int>> GetAllOdd(int n) 68 { 69 for (var i = 0; i < n; i++) 70 { 71 var length = rand.Next(0, MaxVectorLength); 72 var vector = new List<int>(length); 73 74 for (var j = 0; j < length; j++) vector.Add(rand.Next(0, MaxOdd) * 2 - MaxOdd); 75 76 yield return vector; 77 } 78 } 79 80 private IEnumerable<List<int>> GetAllEven(int n) 81 { 82 for (var i = 0; i < n; i++) 83 { 84 var length = rand.Next(0, MaxVectorLength); 85 var vector = new List<int>(length); 86 87 for (var j = 0; j < length; j++) vector.Add(rand.Next(0, MaxEven) * 2 - MaxEven); 88 89 yield return vector; 90 } 91 } 92 93 private IEnumerable<List<int>> GetRandom(int n) 94 { 95 for (var i = 0; i < n; i++) 96 { 97 var length = rand.Next(0, MaxVectorLength); 98 var vector = new List<int>(length); 99 100 for (var j = 0; j < length; j++) vector.Add(rand.Next(MinRandom, MaxRandom)); 101 102 yield return vector; 103 } 104 } 105 106 private IEnumerable<List<int>> GetHardcodedTrainingSamples() 107 { 108 yield return new List<int>(); 109 yield return new List<int> { -10 }; 110 yield return new List<int> { -9 }; 111 yield return new List<int> { -8 }; 112 yield return new List<int> { -7 }; 113 yield return new List<int> { -6 }; 114 yield return new List<int> { -5 }; 115 yield return new List<int> { -4 }; 116 yield return new List<int> { -3 }; 117 yield return new List<int> { -2 }; 118 yield return new List<int> { -1 }; 119 yield return new List<int> { -0 }; 120 yield return new List<int> { 1 }; 121 yield return new List<int> { 2 }; 122 yield return new List<int> { 3 }; 123 yield return new List<int> { 4 }; 124 yield return new List<int> { 5 }; 125 yield return new List<int> { 6 }; 126 yield return new List<int> { 7 }; 127 yield return new List<int> { 8 }; 128 yield return new List<int> { 9 }; 129 yield return new List<int> { 10 }; 130 yield return new List<int> { -947 }; 131 yield return new List<int> { -450 }; 132 yield return new List<int> { 303 }; 133 yield return new List<int> { 886 }; 134 yield return new List<int> { 0, 0 }; 135 yield return new List<int> { 0, 1 }; 136 yield return new List<int> { 7, 1 }; 137 yield return new List<int> { -9, -1 }; 138 yield return new List<int> { -11, 40 }; 139 yield return new List<int> { 944, 77 }; 22 public override IPushData CreatePushData(Example<long[], long>[] training, Example<long[], long>[] test) { 23 return new CountOddsPushData(training, test); 140 24 } 141 25 }
Note: See TracChangeset
for help on using the changeset viewer.