Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/25/17 01:53:30 (7 years ago)
Author:
pkimmesw
Message:

#2665 PushGP HL Integration

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 {
     1namespace HeuristicLab.BenchmarkSuite {
     2  using System;
    33  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;
    49
    5   using HeuristicLab.Core;
    6   using HeuristicLab.Random;
     10  using HeuristicLab.BenchmarkSuite.ProblemData;
    711
    8   public abstract class BenchmarkSuiteDataDescritpor<T> : ArtificialDataDescriptor
    9   {
    10     protected static IRandom rand = new FastRandom();
     12  using Microsoft.VisualBasic.FileIO;
    1113
    12     protected abstract IEnumerable<T> GenerateTraining();
     14  public abstract class BenchmarkSuiteDataDescriptor<TIn, TOut> : IBenchmarkSuiteDataDescriptor {
     15    //protected static IRandom rand = new FastRandom();
    1316
    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 = ",";
    1522
    16     protected abstract IEnumerable<Example> GenerateExamples(IEnumerable<T> trainingAndTest);
     23    protected const char ArrayValueSeparator = ' ';
     24    protected static readonly char[] ArraySymbolTrim = { '[', ']' };
    1725
    18     protected override IEnumerable<Example> GenerateExamples()
    19     {
    20       var training = this.GenerateTraining();
     26    public abstract string Name { get; }
     27    public abstract string Description { get; }
    2128
    22       training = training.Shuffle(rand);
     29    protected abstract int InputCount { get; }
     30    protected abstract int OutputCount { get; }
    2331
    24       var test = this.GenerateTest();
     32    public abstract TIn ConvertInput(string[] input);
     33    public abstract TOut ConvertOutput(string[] input);
    2534
    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();
    2938
    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));
    31133    }
    32134  }
  • branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.BenchmarkSuite/HeuristicLab.BenchmarkSuite.csproj

    r14392 r14602  
    88    <AppDesignerFolder>Properties</AppDesignerFolder>
    99    <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>
    1212    <FileAlignment>512</FileAlignment>
    1313    <ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
     
    1717    <IsCodedUITest>False</IsCodedUITest>
    1818    <TestProjectType>UnitTest</TestProjectType>
     19    <TargetFrameworkProfile />
    1920  </PropertyGroup>
    2021  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
     
    2223    <DebugType>full</DebugType>
    2324    <Optimize>false</Optimize>
    24     <OutputPath>bin\Debug\</OutputPath>
     25    <OutputPath>..\..\..\..\trunk\sources\bin\</OutputPath>
    2526    <DefineConstants>DEBUG;TRACE</DefineConstants>
    2627    <ErrorReport>prompt</ErrorReport>
    2728    <WarningLevel>4</WarningLevel>
     29    <LangVersion>5</LangVersion>
    2830  </PropertyGroup>
    2931  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    3032    <DebugType>pdbonly</DebugType>
    3133    <Optimize>true</Optimize>
    32     <OutputPath>bin\Release\</OutputPath>
     34    <OutputPath>..\..\..\..\trunk\sources\bin\</OutputPath>
    3335    <DefineConstants>TRACE</DefineConstants>
    3436    <ErrorReport>prompt</ErrorReport>
     
    4244      <HintPath>..\..\..\..\trunk\sources\bin\HeuristicLab.Core-3.3.dll</HintPath>
    4345    </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>
    4450    <Reference Include="HeuristicLab.Problems.Instances-3.3">
    4551      <HintPath>..\..\..\..\trunk\sources\bin\HeuristicLab.Problems.Instances-3.3.dll</HintPath>
     
    4854      <HintPath>..\..\..\..\trunk\sources\bin\HeuristicLab.Random-3.3.dll</HintPath>
    4955    </Reference>
     56    <Reference Include="Microsoft.VisualBasic" />
    5057    <Reference Include="System" />
     58    <Reference Include="System.IO.Compression" />
    5159  </ItemGroup>
    5260  <Choose>
     
    6371  </Choose>
    6472  <ItemGroup>
    65     <Compile Include="ArtificialDataDescriptor.cs" />
    6673    <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" />
    68113    <Compile Include="Problems\CountOdds.cs" />
    69114    <Compile Include="Properties\AssemblyInfo.cs" />
    70115  </ItemGroup>
    71116  <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" />
    73124  </ItemGroup>
    74125  <Choose>
  • branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.BenchmarkSuite/HeuristicLab.BenchmarkSuite.csproj.user

    r14513 r14602  
    22<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    33  <PropertyGroup>
    4     <ProjectView>ShowAllFiles</ProjectView>
     4    <ProjectView>ProjectFiles</ProjectView>
    55  </PropertyGroup>
    66</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;
     1namespace HeuristicLab.BenchmarkSuite.Problems {
     2  using HeuristicLab.BenchmarkSuite.ProblemData;
    53
    6   public class CountOdds : BenchmarkSuiteDataDescritpor<List<int>>
    7   {
    8     public const int MaxOdd = 999;
     4  public class CountOdds : BenchmarkSuiteDataDescriptor<long[], long> {
     5    private const string displayMame = "Count Odds";
     6    private const string description = "";
    97
    10     public const int MaxEven = 1000;
     8    public override string Name { get { return displayMame; } }
     9    public override string Description { get { return description; } }
    1110
    12     public const int MaxRandom = 1000;
     11    protected override int InputCount { get { return 1; } }
     12    protected override int OutputCount { get { return 1; } }
    1313
    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]);
    2416    }
    2517
    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]);
    3220    }
    3321
    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);
    14024    }
    14125  }
Note: See TracChangeset for help on using the changeset viewer.