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