1 | namespace HeuristicLab.Tests.Simplifier {
|
---|
2 | using System;
|
---|
3 | using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions;
|
---|
4 | using HeuristicLab.Problems.ProgramSynthesis.Push.Interpreter;
|
---|
5 | using HeuristicLab.Problems.ProgramSynthesis.Push.Parser;
|
---|
6 | using HeuristicLab.Problems.ProgramSynthesis.Push.Simplifier;
|
---|
7 | using HeuristicLab.Random;
|
---|
8 |
|
---|
9 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
10 |
|
---|
11 | [TestClass]
|
---|
12 | public class SimplifierTests {
|
---|
13 |
|
---|
14 | [TestMethod]
|
---|
15 | [TestProperty("Time", "Short")]
|
---|
16 | [TestCategory("Simplifier")]
|
---|
17 | public void SimplifySubProgramsTest() {
|
---|
18 | var program = PushParser.ParseProgram("( ( ( ( ( 1 ) ) ) ) )");
|
---|
19 | var result = PushParser.ParseProgram("( 1 )");
|
---|
20 | var simplerProgram = Simplifier.SimplifySubPrograms(program);
|
---|
21 |
|
---|
22 | Console.WriteLine(simplerProgram);
|
---|
23 | Assert.AreEqual(result, simplerProgram);
|
---|
24 | }
|
---|
25 |
|
---|
26 | [TestMethod]
|
---|
27 | [TestProperty("Time", "Short")]
|
---|
28 | [TestCategory("Simplifier")]
|
---|
29 | public void RemoveUnnecessaryExpressionsTest() {
|
---|
30 | var program = PushParser.ParseProgram("( 5 ( INTEGER.DUP FLOAT.+ FLOAT.- ) ( EXEC.DO ( EXEC.IF ) EXEC.Y ) INTEGER.+ )");
|
---|
31 | var result = PushParser.ParseProgram("( 5 INTEGER.DUP INTEGER.+ )");
|
---|
32 | var pool = new PushInterpreterPool();
|
---|
33 | var random = new MersenneTwister(1337);
|
---|
34 |
|
---|
35 | Func<PushProgram, double> evaluator = p => {
|
---|
36 | using (var interpreter = pool.Create(random)) {
|
---|
37 | interpreter.Run(p);
|
---|
38 | return interpreter.IntegerStack.IsEmpty
|
---|
39 | ? double.MaxValue
|
---|
40 | : Math.Abs(interpreter.IntegerStack.Top - 10);
|
---|
41 | }
|
---|
42 | };
|
---|
43 |
|
---|
44 | var simplerProgram = Simplifier.Simplify(program, pool.PushConfiguration, evaluator);
|
---|
45 |
|
---|
46 | Console.WriteLine(simplerProgram);
|
---|
47 | Assert.AreEqual(result, simplerProgram);
|
---|
48 | }
|
---|
49 | }
|
---|
50 | }
|
---|