Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Tests/Problem/IndividualMapperTests.cs @ 15017

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

#2665 Fixed Benchmark Problem Definition, Converted LoopExpressions to stateless expressions, Added several unit test to ensure funcionality, Fixed UI bugs

File size: 2.8 KB
Line 
1// ReSharper disable AccessToDisposedClosure
2namespace HeuristicLab.Tests.Problem {
3  using System;
4  using System.Diagnostics;
5  using System.Linq;
6
7  using HeuristicLab.Core;
8  using HeuristicLab.Encodings.IntegerVectorEncoding;
9  using HeuristicLab.Problems.ProgramSynthesis.Base.Erc;
10  using HeuristicLab.Problems.ProgramSynthesis.Base.Erc.Integer;
11  using HeuristicLab.Problems.ProgramSynthesis.Push.Configuration;
12  using HeuristicLab.Problems.ProgramSynthesis.Push.Data.Pool;
13  using HeuristicLab.Problems.ProgramSynthesis.Push.Interpreter;
14  using HeuristicLab.Problems.ProgramSynthesis.Push.Problem;
15  using HeuristicLab.Problems.ProgramSynthesis.Push.Stack;
16  using HeuristicLab.Random;
17  using Microsoft.VisualStudio.TestTools.UnitTesting;
18
19  [TestClass]
20  public class IndividualMapperTests {
21
22    [TestMethod]
23    [TestProperty("Time", "Short")]
24    [TestCategory("Individual")]
25    public void TestRandomReset() {
26      var random = new MersenneTwister(1337);
27      var config = new PushConfiguration {
28        ErcOptions = new ErcOptions {
29          ErcProbability = 0.05,
30          IntegerErcOptions = new IntegerErcOptions(
31            new IntegerConstantErc(0),
32            new IntegerRangeErc(-1000, 100))
33        }
34      };
35
36      var vector = new IntegerVector(10000, random, 0, config.EnabledExpressions.Count);
37      var randomPool = new ObjectPool<IRandom>(() => new MersenneTwister());
38      var pool = new PushInterpreterPool(config);
39
40      var programs = Enumerable.Range(0, Environment.ProcessorCount)
41        .AsParallel()
42        .Select(i => vector.ToPushProgram(config, randomPool))
43        .ToArray();
44
45      var referenceProgram = vector.ToPushProgram(config, randomPool);
46      Assert.IsTrue(programs.All(p => p.Equals(referenceProgram)));
47    }
48
49    [TestMethod]
50    [TestProperty("Time", "Short")]
51    [TestCategory("Individual")]
52    public void TestPlushToProgram() {
53      var random = new MersenneTwister();
54      var config = new PushConfiguration {
55        ParenthesesCloseBiasLevel = 4
56      };
57
58      config.DisableStack(StackTypes.Name);
59      config.DisableStack(StackTypes.Float);
60      config.DisableStack(StackTypes.Code);
61      config.DisableStack(StackTypes.Boolean);
62      config.DisableStack(StackTypes.IntegerVector);
63      config.DisableStack(StackTypes.FloatVector);
64      config.DisableStack(StackTypes.StringVector);
65      config.DisableStack(StackTypes.BooleanVector);
66
67      var sw = new Stopwatch();
68      var vector = new IntegerVector(100, random, 0, config.EnabledExpressions.Count);
69
70      sw.Start();
71      var program = vector.ToPushProgram(config, random);
72      sw.Stop();
73
74      Console.WriteLine(@"Duration: {0}ms", sw.ElapsedMilliseconds);
75      Assert.AreEqual(vector.Length, program.TotalExpressionCount);
76    }
77  }
78}
Note: See TracBrowser for help on using the repository browser.