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 StringRandomErc : WeightedErcItem<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 StringRandomErc() : this(true, 1d) { }
|
---|
30 |
|
---|
31 | public StringRandomErc(bool isEnabled, double weight = 1d) : base(isEnabled, weight) {
|
---|
32 | Name = "String random";
|
---|
33 | Parameters.Add(new FixedValueParameter<BoolValue>(AllowLowercaseLettersParameterName, new BoolValue(true)));
|
---|
34 | Parameters.Add(new FixedValueParameter<BoolValue>(AllowUppercaseLettersParameterName, new BoolValue(true)));
|
---|
35 | Parameters.Add(new FixedValueParameter<BoolValue>(AllowDigitsParameterName, new BoolValue(true)));
|
---|
36 | Parameters.Add(new FixedValueParameter<BoolValue>(AllowSymbolsParameterName, new BoolValue(true)));
|
---|
37 | Parameters.Add(new FixedValueParameter<BoolValue>(AllowSpaceParameterName, new BoolValue(false)));
|
---|
38 | Parameters.Add(new FixedValueParameter<IntValue>(StringLengthParameterName, new IntValue(DefaultStringLength)));
|
---|
39 | Parameters.Add(new FixedValueParameter<PercentValue>(SpaceProbabilityParameterName));
|
---|
40 | Parameters.Add(new FixedValueParameter<PercentValue>(VowelProbabilityParameterName));
|
---|
41 | Parameters.Add(new FixedValueParameter<PercentValue>(NewStringProbabilityParameterName, NewStringProbabilityParameterDescription));
|
---|
42 | }
|
---|
43 |
|
---|
44 | [StorableConstructor]
|
---|
45 | public StringRandomErc(bool deserializing) : base(deserializing) { }
|
---|
46 |
|
---|
47 | public StringRandomErc(StringRandomErc origin, Cloner cloner) : base(origin, cloner) {
|
---|
48 | }
|
---|
49 |
|
---|
50 | public override string GetErcValue(IRandom random) {
|
---|
51 | if (random.NextDouble() < NewStringProbability || generatedValues.Count == 0) {
|
---|
52 | var value = StringGenerator.RandomString(
|
---|
53 | AllowLowercaseLetters,
|
---|
54 | AllowUppercaseLetters,
|
---|
55 | AllowSpace,
|
---|
56 | SpaceProbability,
|
---|
57 | StringLength,
|
---|
58 | random);
|
---|
59 |
|
---|
60 | generatedValues.Add(value);
|
---|
61 | return value;
|
---|
62 | }
|
---|
63 |
|
---|
64 | var idx = random.Next(generatedValues.Count);
|
---|
65 | return generatedValues[idx];
|
---|
66 | }
|
---|
67 |
|
---|
68 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
69 | return new StringRandomErc(this, cloner);
|
---|
70 | }
|
---|
71 |
|
---|
72 | public IValueParameter<BoolValue> AllowLowercaseLettersParameter
|
---|
73 | {
|
---|
74 | get { return (IValueParameter<BoolValue>)Parameters[AllowLowercaseLettersParameterName]; }
|
---|
75 | }
|
---|
76 |
|
---|
77 | public bool AllowLowercaseLetters
|
---|
78 | {
|
---|
79 | get { return AllowLowercaseLettersParameter.Value.Value; }
|
---|
80 | set { AllowLowercaseLettersParameter.Value.Value = value; }
|
---|
81 | }
|
---|
82 |
|
---|
83 | public IValueParameter<BoolValue> AllowUppercaseLettersParameter
|
---|
84 | {
|
---|
85 | get { return (IValueParameter<BoolValue>)Parameters[AllowUppercaseLettersParameterName]; }
|
---|
86 | }
|
---|
87 |
|
---|
88 | public bool AllowUppercaseLetters
|
---|
89 | {
|
---|
90 | get { return AllowUppercaseLettersParameter.Value.Value; }
|
---|
91 | set { AllowUppercaseLettersParameter.Value.Value = value; }
|
---|
92 | }
|
---|
93 |
|
---|
94 | public IValueParameter<BoolValue> AllowDigitsParameter
|
---|
95 | {
|
---|
96 | get { return (IValueParameter<BoolValue>)Parameters[AllowDigitsParameterName]; }
|
---|
97 | }
|
---|
98 |
|
---|
99 | public bool AllowDigits
|
---|
100 | {
|
---|
101 | get { return AllowDigitsParameter.Value.Value; }
|
---|
102 | set { AllowDigitsParameter.Value.Value = value; }
|
---|
103 | }
|
---|
104 |
|
---|
105 | public IValueParameter<BoolValue> AllowSymbolsParameter
|
---|
106 | {
|
---|
107 | get { return (IValueParameter<BoolValue>)Parameters[AllowSymbolsParameterName]; }
|
---|
108 | }
|
---|
109 |
|
---|
110 | public bool AllowSymbols
|
---|
111 | {
|
---|
112 | get { return AllowSymbolsParameter.Value.Value; }
|
---|
113 | set { AllowSymbolsParameter.Value.Value = value; }
|
---|
114 | }
|
---|
115 |
|
---|
116 | public IValueParameter<BoolValue> AllowSpaceParameter
|
---|
117 | {
|
---|
118 | get { return (IValueParameter<BoolValue>)Parameters[AllowSpaceParameterName]; }
|
---|
119 | }
|
---|
120 |
|
---|
121 | public bool AllowSpace
|
---|
122 | {
|
---|
123 | get { return AllowSpaceParameter.Value.Value; }
|
---|
124 | set { AllowSpaceParameter.Value.Value = value; }
|
---|
125 | }
|
---|
126 |
|
---|
127 | public IValueParameter<PercentValue> SpaceProbabilityParameter
|
---|
128 | {
|
---|
129 | get { return (IValueParameter<PercentValue>)Parameters[SpaceProbabilityParameterName]; }
|
---|
130 | }
|
---|
131 |
|
---|
132 | public double SpaceProbability
|
---|
133 | {
|
---|
134 | get { return SpaceProbabilityParameter.Value.Value; }
|
---|
135 | set { SpaceProbabilityParameter.Value.Value = value; }
|
---|
136 | }
|
---|
137 |
|
---|
138 | public IValueParameter<PercentValue> VowelProbabilityParameter
|
---|
139 | {
|
---|
140 | get { return (IValueParameter<PercentValue>)Parameters[VowelProbabilityParameterName]; }
|
---|
141 | }
|
---|
142 |
|
---|
143 | public double VowelProbability
|
---|
144 | {
|
---|
145 | get { return VowelProbabilityParameter.Value.Value; }
|
---|
146 | set { VowelProbabilityParameter.Value.Value = value; }
|
---|
147 | }
|
---|
148 |
|
---|
149 | public IValueParameter<IntValue> StringLengthParameter
|
---|
150 | {
|
---|
151 | get { return (IValueParameter<IntValue>)Parameters[StringLengthParameterName]; }
|
---|
152 | }
|
---|
153 |
|
---|
154 | public IValueParameter<PercentValue> NewStringProbabilityParameter
|
---|
155 | {
|
---|
156 | get { return (IValueParameter<PercentValue>)Parameters[NewStringProbabilityParameterName]; }
|
---|
157 | }
|
---|
158 |
|
---|
159 | public double NewStringProbability
|
---|
160 | {
|
---|
161 | get { return NewStringProbabilityParameter.Value.Value; }
|
---|
162 | set { NewStringProbabilityParameter.Value.Value = value; }
|
---|
163 | }
|
---|
164 |
|
---|
165 | public int StringLength
|
---|
166 | {
|
---|
167 | get { return StringLengthParameter.Value.Value; }
|
---|
168 | set { StringLengthParameter.Value.Value = value; }
|
---|
169 | }
|
---|
170 | }
|
---|
171 | }
|
---|