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