namespace HeuristicLab.Problems.ProgramSynthesis.Base.Erc.String { using System.Collections.Generic; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Problems.ProgramSynthesis.Push.Generators; [StorableClass] public class StringRandomErcValue : WeightedErcValueItem { private readonly List generatedValues = new List(); public const int DefaultStringLength = 10; private const string AllowLowercaseLettersParameterName = "Allow lowercase letters"; private const string AllowUppercaseLettersParameterName = "Allow uppercase letters"; private const string AllowSpaceParameterName = "Allow space"; private const string SpaceProbabilityParameterName = "Space probability"; private const string StringLengthParameterName = "String length"; private const string AllowDigitsParameterName = "Allow digits"; private const string AllowSymbolsParameterName = "Allow symbols"; private const string VowelProbabilityParameterName = "Vowel probability"; private const string NewStringProbabilityParameterName = "New string probability"; private const string NewStringProbabilityParameterDescription = "The probability that the selection of the ephemeral random string constant for inclusion in randomly generated code will produce a new string (rather than a string that was previously generated)."; public StringRandomErcValue() { Name = "String random"; Parameters.Add(new FixedValueParameter(AllowLowercaseLettersParameterName, new BoolValue(true))); Parameters.Add(new FixedValueParameter(AllowUppercaseLettersParameterName, new BoolValue(true))); Parameters.Add(new FixedValueParameter(AllowDigitsParameterName, new BoolValue(true))); Parameters.Add(new FixedValueParameter(AllowSymbolsParameterName, new BoolValue(true))); Parameters.Add(new FixedValueParameter(AllowSpaceParameterName, new BoolValue(false))); Parameters.Add(new FixedValueParameter(StringLengthParameterName, new IntValue(DefaultStringLength))); Parameters.Add(new FixedValueParameter(SpaceProbabilityParameterName)); Parameters.Add(new FixedValueParameter(VowelProbabilityParameterName)); Parameters.Add(new FixedValueParameter(NewStringProbabilityParameterName, NewStringProbabilityParameterDescription)); } [StorableConstructor] public StringRandomErcValue(bool deserializing) : base(deserializing) { } public StringRandomErcValue(StringRandomErcValue origin, Cloner cloner) : base(origin, cloner) { } public override string GetErcValue(IRandom random) { if (random.NextDouble().IsAlmost(NewStringProbability) || generatedValues.Count == 0) { var value = StringGenerator.RandomString( AllowLowercaseLetters, AllowUppercaseLetters, AllowSpace, SpaceProbability, StringLength, random); generatedValues.Add(value); return value; } var idx = random.Next(generatedValues.Count); return generatedValues[idx]; } public override IDeepCloneable Clone(Cloner cloner) { return new StringRandomErcValue(this, cloner); } public IValueParameter AllowLowercaseLettersParameter { get { return (IValueParameter)Parameters[AllowLowercaseLettersParameterName]; } } public bool AllowLowercaseLetters { get { return AllowLowercaseLettersParameter.Value.Value; } set { AllowLowercaseLettersParameter.Value.Value = value; } } public IValueParameter AllowUppercaseLettersParameter { get { return (IValueParameter)Parameters[AllowUppercaseLettersParameterName]; } } public bool AllowUppercaseLetters { get { return AllowUppercaseLettersParameter.Value.Value; } set { AllowUppercaseLettersParameter.Value.Value = value; } } public IValueParameter AllowDigitsParameter { get { return (IValueParameter)Parameters[AllowDigitsParameterName]; } } public bool AllowDigits { get { return AllowDigitsParameter.Value.Value; } set { AllowDigitsParameter.Value.Value = value; } } public IValueParameter AllowSymbolsParameter { get { return (IValueParameter)Parameters[AllowSymbolsParameterName]; } } public bool AllowSymbols { get { return AllowSymbolsParameter.Value.Value; } set { AllowSymbolsParameter.Value.Value = value; } } public IValueParameter AllowSpaceParameter { get { return (IValueParameter)Parameters[AllowSpaceParameterName]; } } public bool AllowSpace { get { return AllowSpaceParameter.Value.Value; } set { AllowSpaceParameter.Value.Value = value; } } public IValueParameter SpaceProbabilityParameter { get { return (IValueParameter)Parameters[SpaceProbabilityParameterName]; } } public double SpaceProbability { get { return SpaceProbabilityParameter.Value.Value; } set { SpaceProbabilityParameter.Value.Value = value; } } public IValueParameter VowelProbabilityParameter { get { return (IValueParameter)Parameters[VowelProbabilityParameterName]; } } public double VowelProbability { get { return VowelProbabilityParameter.Value.Value; } set { VowelProbabilityParameter.Value.Value = value; } } public IValueParameter StringLengthParameter { get { return (IValueParameter)Parameters[StringLengthParameterName]; } } public IValueParameter NewStringProbabilityParameter { get { return (IValueParameter)Parameters[NewStringProbabilityParameterName]; } } public double NewStringProbability { get { return NewStringProbabilityParameter.Value.Value; } set { NewStringProbabilityParameter.Value.Value = value; } } public int StringLength { get { return StringLengthParameter.Value.Value; } set { StringLengthParameter.Value.Value = value; } } } }