Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/20/16 22:57:11 (7 years ago)
Author:
pkimmesw
Message:

#2665 Added Problem.ProgramSynthesis Project, Fixed Expression Issues, Fixed Code Generation

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Tests/Benchmark/Problem/CountOdds.cs

    r14398 r14513  
    1 using System;
    2 using System.Collections.Generic;
    3 using System.Diagnostics;
    4 using System.Linq;
    5 using System.Threading.Tasks;
    6 using HeuristicLab.Algorithms.PushGP.Expressions;
    7 using HeuristicLab.Algorithms.PushGP.Interpreter;
    8 using Microsoft.VisualStudio.TestTools.UnitTesting;
     1namespace HeuristicLab.Tests.Benchmark.Problem {
     2  using System;
     3  using System.Collections.Generic;
     4  using System.Diagnostics;
     5  using System.Linq;
     6  using System.Threading.Tasks;
    97
    10 namespace HeuristicLab.Tests.Benchmark.Problem
    11 {
    12     [TestClass]
    13     public class CountOdds : Problem<long[], long>
    14     {
    15         public const string ProblemFileName = "examples-count-odds.csv";
     8  using HeuristicLab.Algorithms.PushGP.Data.Random;
     9  using HeuristicLab.Algorithms.PushGP.Expressions;
     10  using HeuristicLab.Algorithms.PushGP.Generators;
     11  using HeuristicLab.Algorithms.PushGP.Interpreter;
    1612
    17         public CountOdds()
    18         {
    19             Examples = GetExamples(ProblemFileName, ExampleConverter.Integers, ExampleConverter.Integer);
     13  using Microsoft.VisualStudio.TestTools.UnitTesting;
     14
     15  [TestClass]
     16  public class CountOdds : Problem<long[], long> {
     17    public CountOdds() {
     18      this.Examples = this.GetExamples(
     19        "examples-count-odds.csv",
     20        ExampleConverter.Integers,
     21        ExampleConverter.Integer);
     22    }
     23
     24    [TestMethod]
     25    [TestProperty("Time", "Medium")]
     26    [TestCategory("ProblemTest")]
     27    public void RandomWalk() {
     28      var maxProgramSizeLimit = 100;
     29      var iterations = 4000;
     30      var best = double.MaxValue;
     31      var globalExecCounter = 0;
     32      var lockObj = new object();
     33      var lockCount = new object();
     34      RandomFactory.Seed = 1337;
     35
     36      Expression bestProgram = null;
     37      var config = new Configuration { EvalPushLimit = 500 };
     38      var pool = new PushGpInterpreterPool(config);
     39
     40      Parallel.For(0, iterations, i => {
     41
     42        var execCounter = 0;
     43        var program = CodeGenerator.RandomProgram(maxProgramSizeLimit);
     44        var results = new List<long>();
     45
     46        using (var interpreter = pool.GetInstance()) {
     47          foreach (var example in this.Examples) {
     48            if (example.Input.Length > 0) interpreter.IntegerStack.Push(example.Input);
     49
     50            interpreter.Interpret(program);
     51
     52            if (!interpreter.IntegerStack.IsEmpty) {
     53              var diff = example.Output - interpreter.IntegerStack.Top;
     54              var value = diff == long.MinValue ? 10000 : Math.Min(Math.Abs(diff), 10000);
     55
     56              results.Add(value);
     57            }
     58
     59            execCounter += interpreter.ExecCounter;
     60            interpreter.Clear();
     61          }
    2062        }
    2163
    22         [TestMethod]
    23         [TestProperty("Time", "Medium")]
    24         [TestCategory("ProblemTest")]
    25         public void RandomWalk()
    26         {
    27             var maxProgramSizeLimit = 1000;
    28             var iterations = 20000;
    29             var best = double.MaxValue;
    30             Expression bestProgram = null;
    31             var lockObj = new object();
    32             var globalExecCounter = 0;
     64        lock (lockCount) {
     65          globalExecCounter += execCounter;
     66        }
    3367
     68        if (results.Count == 0) return;
    3469
    35             Parallel.For(0, iterations, async i =>
    36             {
    37                 var interpreter = new PushGPInterpreter(new Configuration
    38                 {
    39                     EvalPushLimit = 10000
    40                 });
     70        var avg = results.Average();
    4171
    42                 var execCounter = 0;
    43                 var program = interpreter.CodeGenerator.RandomProgram(maxProgramSizeLimit);
    44                 var results = new List<long>();
     72        if (avg >= best) return;
    4573
    46                 foreach (var example in Examples)
    47                 {
    48                     if (example.Input.Length > 0)
    49                     {
    50                         interpreter.IntegerStack.Push(example.Input);
    51                     }
     74        lock (lockObj) {
     75          if (avg < best) {
     76            best = avg;
     77            bestProgram = program;
     78          }
     79        }
     80      });
    5281
    53                     await interpreter.InterpretAsync(program);
    54 
    55                     if (!interpreter.IntegerStack.IsEmpty)
    56                     {
    57                         var value = Math.Abs(example.Output - interpreter.IntegerStack.Top);
    58 
    59                         results.Add(Math.Min(value, 1000));
    60                     }
    61 
    62                     execCounter += interpreter.ExecCounter;
    63                     interpreter.Clear();
    64                 }
    65 
    66                 if (!results.Any())
    67                     return;
    68 
    69                 var avg = results.Average();
    70 
    71                 lock (lockObj)
    72                 {
    73                     if (avg < best)
    74                     {
    75                         best = avg;
    76                         bestProgram = program;
    77                         globalExecCounter += execCounter;
    78                     }
    79                 }
    80             });
    81 
    82             Debug.WriteLine("Best: {0}", best);
    83             Debug.WriteLine("Program: {0}", bestProgram);
    84             Debug.WriteLine("ExecCounter: {0}", globalExecCounter);
    85         }
     82      Debug.WriteLine("Best: {0}", best);
     83      Debug.WriteLine("Program: {0}", bestProgram);
     84      Debug.WriteLine("ExecCounter: {0}", globalExecCounter);
    8685    }
     86  }
    8787}
Note: See TracChangeset for help on using the changeset viewer.