namespace HeuristicLab.Tests.Simplifier { using System; using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions; using HeuristicLab.Problems.ProgramSynthesis.Push.Interpreter; using HeuristicLab.Problems.ProgramSynthesis.Push.Parser; using HeuristicLab.Problems.ProgramSynthesis.Push.Simplifier; using HeuristicLab.Random; using Microsoft.VisualStudio.TestTools.UnitTesting; [TestClass] public class SimplifierTests { [TestMethod] [TestProperty("Time", "Short")] [TestCategory("Simplifier")] public void SimplifySubProgramsTest() { var program = PushParser.ParseProgram("( ( ( ( ( 1 ) ) ) ) )"); var result = PushParser.ParseProgram("( 1 )"); var simplerProgram = Simplifier.SimplifySubPrograms(program); Console.WriteLine(simplerProgram); Assert.AreEqual(result, simplerProgram); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("Simplifier")] public void RemoveUnnecessaryExpressionsTest() { var program = PushParser.ParseProgram("( 5 ( INTEGER.DUP FLOAT.+ FLOAT.- ) ( EXEC.DO ( EXEC.IF ) EXEC.Y ) INTEGER.+ )"); var result = PushParser.ParseProgram("( 5 INTEGER.DUP INTEGER.+ )"); var pool = new PushInterpreterPool(); var random = new MersenneTwister(1337); Func evaluator = p => { using (var interpreter = pool.Create(random)) { interpreter.Run(p); return interpreter.IntegerStack.IsEmpty ? double.MaxValue : Math.Abs(interpreter.IntegerStack.Top - 10); } }; var simplerProgram = Simplifier.Simplify(program, pool.PushConfiguration, evaluator); Console.WriteLine(simplerProgram); Assert.AreEqual(result, simplerProgram); } } }