Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2895_PushGP_GenealogyAnalysis/HeuristicLab.Problems.ProgramSynthesis/Push.Base/Erc/String/StringRandomErc.cs @ 16724

Last change on this file since 16724 was 15771, checked in by bburlacu, 7 years ago

#2895: Add solution skeleton for PushGP with genealogy analysis.

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