namespace HeuristicLab.Problems.ProgramSynthesis.Push.SolutionCreator { using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Encodings.IntegerVectorEncoding; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Problems.ProgramSynthesis.Base.Erc; using HeuristicLab.Problems.ProgramSynthesis.Push.Configuration; /// /// Generates a new random integer vector with each element uniformly distributed in a specified range. /// [Item("PushSolutionCreator", "An operator which creates a new random int vector with each element uniformly distributed in a specified range and considering Push specific details")] [StorableClass] public class PushSolutionCreator : UniformRandomIntegerVectorCreator { private const string ERC_OPTIONS_PARAMETER_NAME = "ERC options"; private const string MIN_LENGTH_PARAMETER_NAME = "Min. Length"; private const string MAX_LENGTH_PARAMETER_NAME = "Max. Length"; private const string IN_INSTRUCTION_PROBABILITY = "IN Instruction Probability"; public PushSolutionCreator() { if (!Parameters.ContainsKey(ERC_OPTIONS_PARAMETER_NAME)) Parameters.Add(new ValueParameter(ERC_OPTIONS_PARAMETER_NAME)); if (!Parameters.ContainsKey(MIN_LENGTH_PARAMETER_NAME)) Parameters.Add(new FixedValueParameter(MIN_LENGTH_PARAMETER_NAME, new IntValue(20))); //if (!Parameters.ContainsKey(MAX_LENGTH_PARAMETER_NAME)) // Parameters.Add(new FixedValueParameter(MAX_LENGTH_PARAMETER_NAME, new IntValue(100))); if (!Parameters.ContainsKey(IN_INSTRUCTION_PROBABILITY)) Parameters.Add(new FixedValueParameter(IN_INSTRUCTION_PROBABILITY, new PercentValue(0.05))); Parameters.Add(new ValueLookupParameter("Instructions", "The enabled instructions")); } [StorableConstructor] public PushSolutionCreator(bool deserializing) : base(deserializing) { } public PushSolutionCreator(PushSolutionCreator origin, Cloner cloner) : base(origin, cloner) { } public IValueParameter ErcOptionsParameter { get { return (IValueParameter)Parameters[ERC_OPTIONS_PARAMETER_NAME]; } } public ErcOptions ErcOptions { get { return ErcOptionsParameter.Value; } set { ErcOptionsParameter.Value = value; } } public IValueParameter MinLengthParameter { get { return (IValueParameter)Parameters[MIN_LENGTH_PARAMETER_NAME]; } } public int MinLength { get { return MinLengthParameter.Value.Value; } set { MinLengthParameter.Value.Value = value; } } public IValueLookupParameter InstructionsParameter { get { return (IValueLookupParameter)Parameters["Instructions"]; } } //public IValueParameter MaxLengthParameter //{ // get { return LengthParameter; } //(IValueParameter)Parameters[MAX_LENGTH_PARAMETER_NAME]; } //} //public int MaxLength //{ // get { return MaxLengthParameter.Value.Value; } // set { MaxLengthParameter.Value.Value = value; } //} public IValueParameter InInstructionProbabilityParameter { get { return (IValueParameter)Parameters[IN_INSTRUCTION_PROBABILITY]; } } public double InInstructionProbability { get { return InInstructionProbabilityParameter.Value.Value; } set { InInstructionProbabilityParameter.Value.Value = value; } } protected override IntegerVector Create(IRandom random, IntValue length, IntMatrix bounds) { var len = length.Value; var result = new IntegerVector(len); var contentLength = random.Next(MinLength, len); var lowerBound = bounds[0, 0]; var upperBound = bounds[0, 1]; //var inInstructionRelativeProbability = ErcOptions.ErcProbability + InInstructionProbability; //var instructions = InstructionsParameter.ActualValue; for (var i = 0; i < contentLength; i++) { //var x = random.NextDouble(); //if (ErcOptions.ErcProbability > 0 && x <= ErcOptions.ErcProbability) { // result[i] = PushSolutionEncoding.Erc; //} else if (InInstructionProbability > 0 && x <= inInstructionRelativeProbability && instructions.InExpressionCount > 0) { // var index = random.Next(0, instructions.InExpressionCount); // var expression = ExpressionTable.InExpressionTable[index]; // instructions.EnabledExpressions // //result[i] = PushSolutionEncoding.In; //} else { result[i] = random.Next(lowerBound, upperBound); //} } if (contentLength < len) { result[contentLength] = PushSolutionEncoding.End; for (var i = contentLength + 1; i < len; i++) { result[i] = PushSolutionEncoding.Noop; } } return result; } } }