using System; using System.Diagnostics; using System.Linq; using HeuristicLab.Core; using HeuristicLab.Encodings.IntegerVectorEncoding; using HeuristicLab.Problems.ProgramSynthesis; using HeuristicLab.Random; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace HeuristicLab.Tests.Problem { [TestClass] public class IndividualMapperTests { [TestMethod] [TestProperty("Time", "Short")] [TestCategory("Individual")] public void TestRandomReset() { var random = new MersenneTwister(1337); var config = new PushConfiguration { ErcOptions = new ErcOptions { ErcProbability = 0.05, IntegerErcOptions = new IntegerErcOptions( new IntegerConstantErc(0), new IntegerRangeErc(-1000, 100)) } }; var vector = new IntegerVector(10000, random, 0, config.EnabledExpressions.Count); var randomPool = new ObjectPool(() => new MersenneTwister()); var pool = new PushInterpreterPool(config); var programs = Enumerable.Range(0, Environment.ProcessorCount) .AsParallel() .Select(i => vector.ToPushProgram(config)) .ToArray(); var referenceProgram = vector.ToPushProgram(config); Assert.IsTrue(programs.All(p => p.Equals(referenceProgram))); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("Individual")] public void TestPlushToProgram() { var random = new MersenneTwister(); var config = new PushConfiguration { CloseBiasLevel = 4 }; config.DisableStack(StackTypes.Name); config.DisableStack(StackTypes.Float); config.DisableStack(StackTypes.Code); config.DisableStack(StackTypes.Boolean); config.DisableStack(StackTypes.IntegerVector); config.DisableStack(StackTypes.FloatVector); config.DisableStack(StackTypes.StringVector); config.DisableStack(StackTypes.BooleanVector); var sw = new Stopwatch(); var vector = new IntegerVector(100, random, 0, config.EnabledExpressions.Count); sw.Start(); var program = vector.ToPushProgram(config); sw.Stop(); Console.WriteLine(@"Duration: {0}ms", sw.ElapsedMilliseconds); Assert.AreEqual(vector.Length, program.TotalInstructionCount); } [TestMethod] [TestProperty("Time", "Short")] [TestCategory("Individual")] public void PlushToPushProgram() { var plushGenome = new PlushVector(); plushGenome.Add(new PlushEntry { Instruction = new ExecDoTimesExpression(), Close = 0 }); plushGenome.Add(new PlushEntry { Instruction = new IntegerPushExpression(8), Close = 0 }); plushGenome.Add(new PlushEntry { Instruction = new IntegerPushExpression(11), Close = 3 }); plushGenome.Add(new PlushEntry { Instruction = new IntegerAddExpression(), Close = 3, Silent = true }); plushGenome.Add(new PlushEntry { Instruction = new ExecIfExpression(), Close = 1 }); plushGenome.Add(new PlushEntry { Instruction = new IntegerPushExpression(17), Close = 0 }); plushGenome.Add(new PlushEntry { Instruction = new BooleanPushExpression(false), Close = 0 }); plushGenome.Add(new PlushEntry { Instruction = new CodeQuoteExpression(), Close = 0 }); plushGenome.Add(new PlushEntry { Instruction = new FloatMultiplyExpression(), Close = 2 }); plushGenome.Add(new PlushEntry { Instruction = new ExecRotateExpression(), Close = 0 }); plushGenome.Add(new PlushEntry { Instruction = new FloatPushExpression(34.44), Close = 0 }); var expected = PushParser.ParseProgram("( EXEC.DO*TIMES ( 8 11 ) EXEC.IF ( ) ( 17 false CODE.QUOTE ( FLOAT.* ) ) EXEC.ROT ( 34.44 ) ( ) ( ) )"); Assert.AreEqual(expected, plushGenome.PushProgram); } } }