1 | // ReSharper disable AccessToDisposedClosure
|
---|
2 | namespace 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.Encoding;
|
---|
14 | using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions;
|
---|
15 | using HeuristicLab.Problems.ProgramSynthesis.Push.Individual;
|
---|
16 | using HeuristicLab.Problems.ProgramSynthesis.Push.Interpreter;
|
---|
17 | using HeuristicLab.Problems.ProgramSynthesis.Push.Parser;
|
---|
18 | using HeuristicLab.Problems.ProgramSynthesis.Push.Stack;
|
---|
19 | using HeuristicLab.Random;
|
---|
20 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
21 |
|
---|
22 | [TestClass]
|
---|
23 | public class IndividualMapperTests {
|
---|
24 |
|
---|
25 | [TestMethod]
|
---|
26 | [TestProperty("Time", "Short")]
|
---|
27 | [TestCategory("Individual")]
|
---|
28 | public void TestRandomReset() {
|
---|
29 | var random = new MersenneTwister(1337);
|
---|
30 | var config = new PushConfiguration {
|
---|
31 | ErcOptions = new ErcOptions {
|
---|
32 | ErcProbability = 0.05,
|
---|
33 | IntegerErcOptions = new IntegerErcOptions(
|
---|
34 | new IntegerConstantErc(0),
|
---|
35 | new IntegerRangeErc(-1000, 100))
|
---|
36 | }
|
---|
37 | };
|
---|
38 |
|
---|
39 | var vector = new IntegerVector(10000, random, 0, config.EnabledExpressions.Count);
|
---|
40 | var randomPool = new ObjectPool<IRandom>(() => new MersenneTwister());
|
---|
41 | var pool = new PushInterpreterPool(config);
|
---|
42 |
|
---|
43 | var programs = Enumerable.Range(0, Environment.ProcessorCount)
|
---|
44 | .AsParallel()
|
---|
45 | .Select(i => vector.ToPushProgram(config))
|
---|
46 | .ToArray();
|
---|
47 |
|
---|
48 | var referenceProgram = vector.ToPushProgram(config);
|
---|
49 | Assert.IsTrue(programs.All(p => p.Equals(referenceProgram)));
|
---|
50 | }
|
---|
51 |
|
---|
52 | [TestMethod]
|
---|
53 | [TestProperty("Time", "Short")]
|
---|
54 | [TestCategory("Individual")]
|
---|
55 | public void TestPlushToProgram() {
|
---|
56 | var random = new MersenneTwister();
|
---|
57 | var config = new PushConfiguration {
|
---|
58 | CloseBiasLevel = 4
|
---|
59 | };
|
---|
60 |
|
---|
61 | config.DisableStack(StackTypes.Name);
|
---|
62 | config.DisableStack(StackTypes.Float);
|
---|
63 | config.DisableStack(StackTypes.Code);
|
---|
64 | config.DisableStack(StackTypes.Boolean);
|
---|
65 | config.DisableStack(StackTypes.IntegerVector);
|
---|
66 | config.DisableStack(StackTypes.FloatVector);
|
---|
67 | config.DisableStack(StackTypes.StringVector);
|
---|
68 | config.DisableStack(StackTypes.BooleanVector);
|
---|
69 |
|
---|
70 | var sw = new Stopwatch();
|
---|
71 | var vector = new IntegerVector(100, random, 0, config.EnabledExpressions.Count);
|
---|
72 |
|
---|
73 | sw.Start();
|
---|
74 | var program = vector.ToPushProgram(config);
|
---|
75 | sw.Stop();
|
---|
76 |
|
---|
77 | Console.WriteLine(@"Duration: {0}ms", sw.ElapsedMilliseconds);
|
---|
78 | Assert.AreEqual(vector.Length, program.TotalInstructionCount);
|
---|
79 | }
|
---|
80 |
|
---|
81 | [TestMethod]
|
---|
82 | [TestProperty("Time", "Short")]
|
---|
83 | [TestCategory("Individual")]
|
---|
84 | public void PlushToPushProgram() {
|
---|
85 | var plushGenome = new PlushVector();
|
---|
86 |
|
---|
87 | plushGenome.Add(new PlushEntry { Instruction = new ExecDoTimesExpression(), Close = 0 });
|
---|
88 | plushGenome.Add(new PlushEntry { Instruction = new IntegerPushExpression(8), Close = 0 });
|
---|
89 | plushGenome.Add(new PlushEntry { Instruction = new IntegerPushExpression(11), Close = 3 });
|
---|
90 | plushGenome.Add(new PlushEntry { Instruction = new IntegerAddExpression(), Close = 3, Silent = true });
|
---|
91 | plushGenome.Add(new PlushEntry { Instruction = new ExecIfExpression(), Close = 1 });
|
---|
92 | plushGenome.Add(new PlushEntry { Instruction = new IntegerPushExpression(17), Close = 0 });
|
---|
93 | plushGenome.Add(new PlushEntry { Instruction = new BooleanPushExpression(false), Close = 0 });
|
---|
94 | plushGenome.Add(new PlushEntry { Instruction = new CodeQuoteExpression(), Close = 0 });
|
---|
95 | plushGenome.Add(new PlushEntry { Instruction = new FloatMultiplyExpression(), Close = 2 });
|
---|
96 | plushGenome.Add(new PlushEntry { Instruction = new ExecRotateExpression(), Close = 0 });
|
---|
97 | plushGenome.Add(new PlushEntry { Instruction = new FloatPushExpression(34.44), Close = 0 });
|
---|
98 |
|
---|
99 | var expected = PushParser.ParseProgram("( EXEC.DO*TIMES ( 8 11 ) EXEC.IF ( ) ( 17 false CODE.QUOTE ( FLOAT.* ) ) EXEC.ROT ( 34.44 ) ( ) ( ) )");
|
---|
100 |
|
---|
101 | Assert.AreEqual(expected, plushGenome.PushProgram);
|
---|
102 | }
|
---|
103 | }
|
---|
104 | }
|
---|