1 | namespace HeuristicLab.Problems.ProgramSynthesis.Base.Erc.String {
|
---|
2 | using System.Collections.Generic;
|
---|
3 |
|
---|
4 | using HeuristicLab.Common;
|
---|
5 | using HeuristicLab.Core;
|
---|
6 | using HeuristicLab.Data;
|
---|
7 | using HeuristicLab.Parameters;
|
---|
8 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
9 | using HeuristicLab.Problems.ProgramSynthesis.Push.Generators;
|
---|
10 |
|
---|
11 | [StorableClass]
|
---|
12 | public class StringRandomErcValue : WeightedErcValueItem<string> {
|
---|
13 | private readonly List<string> generatedValues = new List<string>();
|
---|
14 |
|
---|
15 | public const int DefaultStringLength = 10;
|
---|
16 |
|
---|
17 | private const string AllowLowercaseLettersParameterName = "Allow lowercase letters";
|
---|
18 | private const string AllowUppercaseLettersParameterName = "Allow uppercase letters";
|
---|
19 | private const string AllowSpaceParameterName = "Allow space";
|
---|
20 | private const string SpaceProbabilityParameterName = "Space probability";
|
---|
21 | private const string StringLengthParameterName = "String length";
|
---|
22 | private const string AllowDigitsParameterName = "Allow digits";
|
---|
23 | private const string AllowSymbolsParameterName = "Allow symbols";
|
---|
24 | private const string VowelProbabilityParameterName = "Vowel probability";
|
---|
25 |
|
---|
26 | private const string NewStringProbabilityParameterName = "New string probability";
|
---|
27 | 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).";
|
---|
28 |
|
---|
29 | public StringRandomErcValue() {
|
---|
30 | Name = "String random";
|
---|
31 | Parameters.Add(new FixedValueParameter<BoolValue>(AllowLowercaseLettersParameterName, new BoolValue(true)));
|
---|
32 | Parameters.Add(new FixedValueParameter<BoolValue>(AllowUppercaseLettersParameterName, new BoolValue(true)));
|
---|
33 | Parameters.Add(new FixedValueParameter<BoolValue>(AllowDigitsParameterName, new BoolValue(true)));
|
---|
34 | Parameters.Add(new FixedValueParameter<BoolValue>(AllowSymbolsParameterName, new BoolValue(true)));
|
---|
35 | Parameters.Add(new FixedValueParameter<BoolValue>(AllowSpaceParameterName, new BoolValue(false)));
|
---|
36 | Parameters.Add(new FixedValueParameter<IntValue>(StringLengthParameterName, new IntValue(DefaultStringLength)));
|
---|
37 | Parameters.Add(new FixedValueParameter<PercentValue>(SpaceProbabilityParameterName));
|
---|
38 | Parameters.Add(new FixedValueParameter<PercentValue>(VowelProbabilityParameterName));
|
---|
39 | Parameters.Add(new FixedValueParameter<PercentValue>(NewStringProbabilityParameterName, NewStringProbabilityParameterDescription));
|
---|
40 | }
|
---|
41 |
|
---|
42 | [StorableConstructor]
|
---|
43 | public StringRandomErcValue(bool deserializing) : base(deserializing) { }
|
---|
44 |
|
---|
45 | public StringRandomErcValue(StringRandomErcValue origin, Cloner cloner) : base(origin, cloner) {
|
---|
46 | }
|
---|
47 |
|
---|
48 | public override string GetErcValue(IRandom random) {
|
---|
49 | if (random.NextDouble().IsAlmost(NewStringProbability) || generatedValues.Count == 0) {
|
---|
50 | var value = StringGenerator.RandomString(
|
---|
51 | AllowLowercaseLetters,
|
---|
52 | AllowUppercaseLetters,
|
---|
53 | AllowSpace,
|
---|
54 | SpaceProbability,
|
---|
55 | StringLength,
|
---|
56 | random);
|
---|
57 |
|
---|
58 | generatedValues.Add(value);
|
---|
59 | return value;
|
---|
60 | }
|
---|
61 |
|
---|
62 | var idx = random.Next(generatedValues.Count);
|
---|
63 | return generatedValues[idx];
|
---|
64 | }
|
---|
65 |
|
---|
66 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
67 | return new StringRandomErcValue(this, cloner);
|
---|
68 | }
|
---|
69 |
|
---|
70 | public IValueParameter<BoolValue> AllowLowercaseLettersParameter
|
---|
71 | {
|
---|
72 | get { return (IValueParameter<BoolValue>)Parameters[AllowLowercaseLettersParameterName]; }
|
---|
73 | }
|
---|
74 |
|
---|
75 | public bool AllowLowercaseLetters
|
---|
76 | {
|
---|
77 | get { return AllowLowercaseLettersParameter.Value.Value; }
|
---|
78 | set { AllowLowercaseLettersParameter.Value.Value = value; }
|
---|
79 | }
|
---|
80 |
|
---|
81 | public IValueParameter<BoolValue> AllowUppercaseLettersParameter
|
---|
82 | {
|
---|
83 | get { return (IValueParameter<BoolValue>)Parameters[AllowUppercaseLettersParameterName]; }
|
---|
84 | }
|
---|
85 |
|
---|
86 | public bool AllowUppercaseLetters
|
---|
87 | {
|
---|
88 | get { return AllowUppercaseLettersParameter.Value.Value; }
|
---|
89 | set { AllowUppercaseLettersParameter.Value.Value = value; }
|
---|
90 | }
|
---|
91 |
|
---|
92 | public IValueParameter<BoolValue> AllowDigitsParameter
|
---|
93 | {
|
---|
94 | get { return (IValueParameter<BoolValue>)Parameters[AllowDigitsParameterName]; }
|
---|
95 | }
|
---|
96 |
|
---|
97 | public bool AllowDigits
|
---|
98 | {
|
---|
99 | get { return AllowDigitsParameter.Value.Value; }
|
---|
100 | set { AllowDigitsParameter.Value.Value = value; }
|
---|
101 | }
|
---|
102 |
|
---|
103 | public IValueParameter<BoolValue> AllowSymbolsParameter
|
---|
104 | {
|
---|
105 | get { return (IValueParameter<BoolValue>)Parameters[AllowSymbolsParameterName]; }
|
---|
106 | }
|
---|
107 |
|
---|
108 | public bool AllowSymbols
|
---|
109 | {
|
---|
110 | get { return AllowSymbolsParameter.Value.Value; }
|
---|
111 | set { AllowSymbolsParameter.Value.Value = value; }
|
---|
112 | }
|
---|
113 |
|
---|
114 | public IValueParameter<BoolValue> AllowSpaceParameter
|
---|
115 | {
|
---|
116 | get { return (IValueParameter<BoolValue>)Parameters[AllowSpaceParameterName]; }
|
---|
117 | }
|
---|
118 |
|
---|
119 | public bool AllowSpace
|
---|
120 | {
|
---|
121 | get { return AllowSpaceParameter.Value.Value; }
|
---|
122 | set { AllowSpaceParameter.Value.Value = value; }
|
---|
123 | }
|
---|
124 |
|
---|
125 | public IValueParameter<PercentValue> SpaceProbabilityParameter
|
---|
126 | {
|
---|
127 | get { return (IValueParameter<PercentValue>)Parameters[SpaceProbabilityParameterName]; }
|
---|
128 | }
|
---|
129 |
|
---|
130 | public double SpaceProbability
|
---|
131 | {
|
---|
132 | get { return SpaceProbabilityParameter.Value.Value; }
|
---|
133 | set { SpaceProbabilityParameter.Value.Value = value; }
|
---|
134 | }
|
---|
135 |
|
---|
136 | public IValueParameter<PercentValue> VowelProbabilityParameter
|
---|
137 | {
|
---|
138 | get { return (IValueParameter<PercentValue>)Parameters[VowelProbabilityParameterName]; }
|
---|
139 | }
|
---|
140 |
|
---|
141 | public double VowelProbability
|
---|
142 | {
|
---|
143 | get { return VowelProbabilityParameter.Value.Value; }
|
---|
144 | set { VowelProbabilityParameter.Value.Value = value; }
|
---|
145 | }
|
---|
146 |
|
---|
147 | public IValueParameter<IntValue> StringLengthParameter
|
---|
148 | {
|
---|
149 | get { return (IValueParameter<IntValue>)Parameters[StringLengthParameterName]; }
|
---|
150 | }
|
---|
151 |
|
---|
152 | public IValueParameter<PercentValue> NewStringProbabilityParameter
|
---|
153 | {
|
---|
154 | get { return (IValueParameter<PercentValue>)Parameters[NewStringProbabilityParameterName]; }
|
---|
155 | }
|
---|
156 |
|
---|
157 | public double NewStringProbability
|
---|
158 | {
|
---|
159 | get { return NewStringProbabilityParameter.Value.Value; }
|
---|
160 | set { NewStringProbabilityParameter.Value.Value = value; }
|
---|
161 | }
|
---|
162 |
|
---|
163 | public int StringLength
|
---|
164 | {
|
---|
165 | get { return StringLengthParameter.Value.Value; }
|
---|
166 | set { StringLengthParameter.Value.Value = value; }
|
---|
167 | }
|
---|
168 | }
|
---|
169 | }
|
---|