using System; using HeuristicLab.Problems.ProgramSynthesis; using HeuristicLab.Random; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace HeuristicLab.Tests.Simplifier { [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 = HeuristicLab.Problems.ProgramSynthesis.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 = HeuristicLab.Problems.ProgramSynthesis.Simplifier.Simplify(program, pool.PushConfiguration, evaluator); Console.WriteLine(simplerProgram); Assert.AreEqual(result, simplerProgram); } } }