// ReSharper disable AccessToDisposedClosure namespace HeuristicLab.Tests.Problem { using System; using System.Diagnostics; using System.Linq; using HeuristicLab.Core; using HeuristicLab.Encodings.IntegerVectorEncoding; using HeuristicLab.Problems.ProgramSynthesis.Base.Erc; using HeuristicLab.Problems.ProgramSynthesis.Base.Erc.Integer; using HeuristicLab.Problems.ProgramSynthesis.Push.Configuration; using HeuristicLab.Problems.ProgramSynthesis.Push.Data.Pool; using HeuristicLab.Problems.ProgramSynthesis.Push.Interpreter; using HeuristicLab.Problems.ProgramSynthesis.Push.Problem; using HeuristicLab.Problems.ProgramSynthesis.Push.Stack; using HeuristicLab.Random; using Microsoft.VisualStudio.TestTools.UnitTesting; [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, randomPool)) .ToArray(); var referenceProgram = vector.ToPushProgram(config, randomPool); 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 { ParenthesesCloseBiasLevel = 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, random); sw.Stop(); Console.WriteLine(@"Duration: {0}ms", sw.ElapsedMilliseconds); Assert.AreEqual(vector.Length, program.TotalExpressionCount); } } }