using System; namespace HeuristicLab.Problems.ProgramSynthesis.Push.SolutionCreator { using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Operators; using HeuristicLab.Optimization; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Problems.ProgramSynthesis.Base.Erc; using HeuristicLab.Problems.ProgramSynthesis.Push.Configuration; using HeuristicLab.Problems.ProgramSynthesis.Push.Encoding; using HeuristicLab.Problems.ProgramSynthesis.Push.Extensions; using HeuristicLab.Problems.ProgramSynthesis.Push.Generators.CodeGenerator; [Item("PlushCreator", "An operator which creates a new random plush vector with each element uniformly distributed")] [StorableClass] public class PlushCreator : InstrumentedOperator, IPlushCreator, IStochasticOperator { public PlushCreator() { Parameters.Add(new LookupParameter("Random", "The pseudo random number generator which should be used for stochastic manipulation operators.")); Parameters.Add(new LookupParameter("PlushVector", "The vector which should be manipulated.")); Parameters.Add(new ValueLookupParameter("MinLength", "The min length of the vector.")); Parameters.Add(new ValueLookupParameter("MaxLength", "The max length of the vector.")); Parameters.Add(new ValueLookupParameter("MaxClose", "The max close variable for a plush genome entry.")); Parameters.Add(new ValueLookupParameter("CloseBiasLevel", "Determines how strongly the random close variable is biased towards 0.")); Parameters.Add(new ValueLookupParameter("Instructions", "The enabled instructions")); Parameters.Add(new ValueLookupParameter("ErcOptions", "ERC options")); Parameters.Add(new ValueLookupParameter("InInstructionProbability", "The probability of IN Instructions")); } [StorableConstructor] public PlushCreator(bool deserializing) : base(deserializing) { } public PlushCreator(PlushCreator origin, Cloner cloner) : base(origin, cloner) { } public override bool CanChangeName { get { return false; } } public ILookupParameter RandomParameter { get { return (LookupParameter)Parameters["Random"]; } } public ILookupParameter PlushVectorParameter { get { return (ILookupParameter)Parameters["PlushVector"]; } } public IValueLookupParameter MinLengthParameter { get { return (IValueLookupParameter)Parameters["MinLength"]; } } public IValueLookupParameter MaxLengthParameter { get { return (IValueLookupParameter)Parameters["MaxLength"]; } } public IValueLookupParameter InstructionsParameter { get { return (IValueLookupParameter)Parameters["Instructions"]; } } public IValueLookupParameter ErcOptionsParameter { get { return (IValueLookupParameter)Parameters["ErcOptions"]; } } public IValueLookupParameter InInstructionProbabilityParameter { get { return (IValueLookupParameter)Parameters["InInstructionProbability"]; } } public IValueLookupParameter MaxCloseParameter { get { return (IValueLookupParameter)Parameters["MaxClose"]; } } public IValueLookupParameter CloseBiasLevelParameter { get { return (IValueLookupParameter)Parameters["CloseBiasLevel"]; } } public override IDeepCloneable Clone(Cloner cloner) { return new PlushCreator(this, cloner); } public sealed override IOperation InstrumentedApply() { PlushVectorParameter.ActualValue = Create(); return base.InstrumentedApply(); } private PlushVector Create() { var random = RandomParameter.ActualValue; var minLength = MinLengthParameter.ActualValue.Value; var maxLength = MaxLengthParameter.ActualValue.Value; var ercOptions = ErcOptionsParameter.ActualValue; var instructions = InstructionsParameter.ActualValue; var inInstructionProbability = InInstructionProbabilityParameter.ActualValue.Value; var maxClose = MaxCloseParameter.ActualValue.Value; var biasLevel = CloseBiasLevelParameter.ActualValue.Value; if (minLength > maxLength) throw new InvalidOperationException("MinLength > MaxLength"); var length = random.Next(minLength, maxLength); var result = new PlushVector(length); for (var i = 0; i < length; i++) { var expression = CodeGeneratorUtils.GetRandomExpression( random, ercOptions, instructions, inInstructionProbability); var close = maxClose == 0 ? 1 : random.NextBiased(0, maxClose, biasLevel); result.Add(new PlushEntry { Close = close, Instruction = expression, }); } return result; } } }