1 | namespace HeuristicLab.Problems.ProgramSynthesis.Push.Configuration {
|
---|
2 | using System;
|
---|
3 | using System.Collections.Generic;
|
---|
4 | using System.Linq;
|
---|
5 |
|
---|
6 | using HeuristicLab.Common;
|
---|
7 | using HeuristicLab.Core;
|
---|
8 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
9 | using HeuristicLab.Problems.ProgramSynthesis.Push.Attributes;
|
---|
10 | using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions;
|
---|
11 | using HeuristicLab.Problems.ProgramSynthesis.Push.Stack;
|
---|
12 |
|
---|
13 | [StorableClass]
|
---|
14 | public class PushGpConfiguration : NamedItem, IReadonlyPushGpConfiguration, IEnabledExpressionsConfiguration {
|
---|
15 | private static readonly string itemName = "PushGP Configuration";
|
---|
16 |
|
---|
17 | public PushGpConfiguration() {
|
---|
18 | this.EvalPushLimit = 4096;
|
---|
19 | this.MaxPointsInProgram = 128;
|
---|
20 | this.MaxPointsInRandomExpression = 64;
|
---|
21 |
|
---|
22 | this.TopLevelPushCode = true;
|
---|
23 | this.TopLevelPopCode = false;
|
---|
24 |
|
---|
25 | this.MinRandomInteger = -128;
|
---|
26 | this.MaxRandomInteger = 128;
|
---|
27 | this.MinRandomFloat = -128D;
|
---|
28 | this.MaxRandomFloat = 128D;
|
---|
29 | this.NewErcNameProbability = 0.5;
|
---|
30 | this.ErcProbability = 0.2;
|
---|
31 |
|
---|
32 | this.EnabledExpressions = ExpressionTable.ExpressionNames.ToList();
|
---|
33 | }
|
---|
34 |
|
---|
35 | [StorableConstructor]
|
---|
36 | public PushGpConfiguration(bool deserializing) : base(deserializing) { }
|
---|
37 |
|
---|
38 | public PushGpConfiguration(PushGpConfiguration origin, Cloner cloner) : base(origin, cloner) {
|
---|
39 | this.EnabledExpressions = origin.EnabledExpressions.ToList();
|
---|
40 |
|
---|
41 | this.EvalPushLimit = origin.EvalPushLimit;
|
---|
42 | this.MaxPointsInProgram = origin.MaxPointsInProgram;
|
---|
43 | this.MaxPointsInRandomExpression = origin.MaxPointsInRandomExpression;
|
---|
44 |
|
---|
45 | this.TopLevelPushCode = origin.TopLevelPushCode;
|
---|
46 | this.TopLevelPopCode = origin.TopLevelPopCode;
|
---|
47 |
|
---|
48 | this.MinRandomInteger = origin.MinRandomInteger;
|
---|
49 | this.MaxRandomInteger = origin.MaxRandomInteger;
|
---|
50 | this.MinRandomFloat = origin.MinRandomFloat;
|
---|
51 | this.MaxRandomFloat = origin.MaxRandomFloat;
|
---|
52 | this.NewErcNameProbability = origin.NewErcNameProbability;
|
---|
53 | this.ErcProbability = origin.ErcProbability;
|
---|
54 |
|
---|
55 | this.isBooleanStackEnabled = origin.IsBooleanStackEnabled;
|
---|
56 | this.isIntegerStackEnabled = origin.IsIntegerStackEnabled;
|
---|
57 | this.isFloatStackEnabled = origin.IsFloatStackEnabled;
|
---|
58 | this.isNameStackEnabled = origin.IsNameStackEnabled;
|
---|
59 | this.isCodeStackEnabled = origin.IsCodeStackEnabled;
|
---|
60 | this.isExecStackEnabled = origin.IsExecStackEnable;
|
---|
61 | }
|
---|
62 |
|
---|
63 | public event EventHandler<EnabledExpressionsChangedEventArgs> EnabledExpressionsChanged;
|
---|
64 |
|
---|
65 | [Storable]
|
---|
66 | public IList<string> EnabledExpressions { get; private set; }
|
---|
67 |
|
---|
68 | IReadOnlyList<string> IReadonlyPushGpConfiguration.EnabledExpressions { get { return this.EnabledExpressions as IReadOnlyList<string>; } }
|
---|
69 |
|
---|
70 | [Storable]
|
---|
71 | private bool isBooleanStackEnabled = true;
|
---|
72 |
|
---|
73 | public bool IsBooleanStackEnabled
|
---|
74 | {
|
---|
75 | get
|
---|
76 | {
|
---|
77 | return this.isBooleanStackEnabled;
|
---|
78 | }
|
---|
79 |
|
---|
80 | set
|
---|
81 | {
|
---|
82 | if (this.isBooleanStackEnabled == value) return;
|
---|
83 |
|
---|
84 | this.isBooleanStackEnabled = value;
|
---|
85 |
|
---|
86 | if (this.isBooleanStackEnabled) EnableExpressionOfStack(StackType.Boolean);
|
---|
87 | else DisableExpressionOfStack(StackType.Boolean);
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | [Storable]
|
---|
92 | private bool isIntegerStackEnabled = true;
|
---|
93 |
|
---|
94 | public bool IsIntegerStackEnabled
|
---|
95 | {
|
---|
96 | get
|
---|
97 | {
|
---|
98 | return this.isIntegerStackEnabled;
|
---|
99 | }
|
---|
100 |
|
---|
101 | set
|
---|
102 | {
|
---|
103 | if (this.isIntegerStackEnabled == value) return;
|
---|
104 |
|
---|
105 | this.isIntegerStackEnabled = value;
|
---|
106 |
|
---|
107 | if (this.isIntegerStackEnabled) EnableExpressionOfStack(StackType.Integer);
|
---|
108 | else DisableExpressionOfStack(StackType.Integer);
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|
112 | [Storable]
|
---|
113 | private bool isFloatStackEnabled = true;
|
---|
114 |
|
---|
115 | public bool IsFloatStackEnabled
|
---|
116 | {
|
---|
117 | get
|
---|
118 | {
|
---|
119 | return this.isFloatStackEnabled;
|
---|
120 | }
|
---|
121 |
|
---|
122 | set
|
---|
123 | {
|
---|
124 | if (this.isFloatStackEnabled == value) return;
|
---|
125 |
|
---|
126 | this.isFloatStackEnabled = value;
|
---|
127 |
|
---|
128 | if (this.isFloatStackEnabled) EnableExpressionOfStack(StackType.Float);
|
---|
129 | else DisableExpressionOfStack(StackType.Float);
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | [Storable]
|
---|
134 | private bool isCodeStackEnabled = true;
|
---|
135 |
|
---|
136 | public bool IsCodeStackEnabled
|
---|
137 | {
|
---|
138 | get
|
---|
139 | {
|
---|
140 | return this.isCodeStackEnabled;
|
---|
141 | }
|
---|
142 |
|
---|
143 | set
|
---|
144 | {
|
---|
145 | if (this.isCodeStackEnabled == value) return;
|
---|
146 |
|
---|
147 | this.isCodeStackEnabled = value;
|
---|
148 |
|
---|
149 | if (this.isCodeStackEnabled) EnableExpressionOfStack(StackType.Code);
|
---|
150 | else DisableExpressionOfStack(StackType.Code);
|
---|
151 | }
|
---|
152 | }
|
---|
153 |
|
---|
154 | [Storable]
|
---|
155 | private bool isExecStackEnabled = true;
|
---|
156 |
|
---|
157 | public bool IsExecStackEnable
|
---|
158 | {
|
---|
159 | get
|
---|
160 | {
|
---|
161 | return this.isExecStackEnabled;
|
---|
162 | }
|
---|
163 |
|
---|
164 | set
|
---|
165 | {
|
---|
166 | if (this.isExecStackEnabled == value) return;
|
---|
167 |
|
---|
168 | this.isExecStackEnabled = value;
|
---|
169 |
|
---|
170 | if (this.isExecStackEnabled) EnableExpressionOfStack(StackType.Exec);
|
---|
171 | else DisableExpressionOfStack(StackType.Exec);
|
---|
172 | }
|
---|
173 | }
|
---|
174 |
|
---|
175 | [Storable]
|
---|
176 | private bool isNameStackEnabled = true;
|
---|
177 |
|
---|
178 | public bool IsNameStackEnabled
|
---|
179 | {
|
---|
180 | get
|
---|
181 | {
|
---|
182 | return this.isNameStackEnabled;
|
---|
183 | }
|
---|
184 |
|
---|
185 | set
|
---|
186 | {
|
---|
187 | if (this.isNameStackEnabled == value) return;
|
---|
188 |
|
---|
189 | this.isNameStackEnabled = value;
|
---|
190 |
|
---|
191 | if (this.isNameStackEnabled) EnableExpressionOfStack(StackType.Name);
|
---|
192 | else DisableExpressionOfStack(StackType.Name);
|
---|
193 | }
|
---|
194 | }
|
---|
195 |
|
---|
196 | [Storable]
|
---|
197 | public double ErcProbability { get; set; }
|
---|
198 |
|
---|
199 | /// <summary>
|
---|
200 | /// This is the maximum allowed number of "executions" in a single top-level call to the interpreter.
|
---|
201 | /// The execution of a single Push instruction counts as one execution, as does the processing of a single literal,
|
---|
202 | /// as does the descent into one layer of parentheses (that is, the processing of the "(" counts as one execution).
|
---|
203 | /// When this limit is exceeded the interpreter aborts immediately, leaving its stacks in the states they were in prior
|
---|
204 | /// to the abort (so they may still be examined by a calling program). Whether or not this counts as an "abnormal"
|
---|
205 | /// termination
|
---|
206 | /// is up to the calling program.
|
---|
207 | /// </summary>
|
---|
208 | [Storable]
|
---|
209 | public int EvalPushLimit { get; set; }
|
---|
210 |
|
---|
211 | /// <summary>
|
---|
212 | /// This is the maximum of depth a push program can have. Expressions, which lead to exceed this limit are interpreted as NOOP.
|
---|
213 | /// </summary>
|
---|
214 | [Storable]
|
---|
215 | public int MaxDepth { get; set; }
|
---|
216 |
|
---|
217 | /// <summary>
|
---|
218 | /// This is the maximum size of an item on the CODE stack, expressed as a number of points.
|
---|
219 | /// A point is an instruction, a literal, or a pair of parentheses. Any instruction that would cause this limit to be
|
---|
220 | /// exceeded
|
---|
221 | /// should instead act as a NOOP, leaving all stacks in the states that they were in before the execution of the
|
---|
222 | /// instruction.
|
---|
223 | /// </summary>
|
---|
224 | [Storable]
|
---|
225 | public int MaxPointsInProgram { get; set; }
|
---|
226 |
|
---|
227 | /// <summary>
|
---|
228 | /// The maximum number of points in an expression produced by the CODE.RAND instruction.
|
---|
229 | /// </summary>
|
---|
230 | [Storable]
|
---|
231 | public int MaxPointsInRandomExpression { get; set; }
|
---|
232 |
|
---|
233 | /// <summary>
|
---|
234 | /// When TRUE (which is the default), code passed to the top level of the interpreter
|
---|
235 | /// will be pushed onto the CODE stack prior to execution.
|
---|
236 | /// </summary>
|
---|
237 | [Storable]
|
---|
238 | public bool TopLevelPushCode { get; set; }
|
---|
239 |
|
---|
240 | /// <summary>
|
---|
241 | /// When TRUE, the CODE stack will be popped at the end of top level calls to the interpreter. The default is FALSE.
|
---|
242 | /// </summary>
|
---|
243 | [Storable]
|
---|
244 | public bool TopLevelPopCode { get; set; }
|
---|
245 |
|
---|
246 | /// <summary>
|
---|
247 | /// The minimum INTEGER that will be produced as an ephemeral random INTEGER constant or from a call to INTEGER.RAND.
|
---|
248 | /// </summary>
|
---|
249 | [Storable]
|
---|
250 | public int MinRandomInteger { get; set; }
|
---|
251 |
|
---|
252 | /// <summary>
|
---|
253 | /// The maximum INTEGER that will be produced as an ephemeral random INTEGER constant or from a call to INTEGER.RAND.
|
---|
254 | /// </summary>
|
---|
255 | [Storable]
|
---|
256 | public int MaxRandomInteger { get; set; }
|
---|
257 |
|
---|
258 | /// <summary>
|
---|
259 | /// The minimum FLOAT that will be produced as an ephemeral random FLOAT constant or from a call to FLOAT.RAND.
|
---|
260 | /// </summary>
|
---|
261 | [Storable]
|
---|
262 | public double MinRandomFloat { get; set; }
|
---|
263 |
|
---|
264 | /// <summary>
|
---|
265 | /// The maximum FLOAT that will be produced as an ephemeral random FLOAT constant or from a call to FLOAT.RAND.
|
---|
266 | /// </summary>
|
---|
267 | [Storable]
|
---|
268 | public double MaxRandomFloat { get; set; }
|
---|
269 |
|
---|
270 | /// <summary>
|
---|
271 | /// The probability that the selection of the ephemeral
|
---|
272 | /// random NAME constant for inclusion in randomly generated code will produce a new name
|
---|
273 | /// (rather than a name that was previously generated).
|
---|
274 | /// </summary>
|
---|
275 | [Storable]
|
---|
276 | public double NewErcNameProbability { get; set; }
|
---|
277 |
|
---|
278 | public void EnableExpressionOfStack(StackType type) {
|
---|
279 | foreach (var name in ExpressionTable.StackTypeToNamesTable[type]) {
|
---|
280 | EnableExpression(name);
|
---|
281 | }
|
---|
282 | }
|
---|
283 |
|
---|
284 | public void DisableExpressionOfStack(StackType type) {
|
---|
285 | foreach (var name in ExpressionTable.StackTypeToNamesTable[type]) {
|
---|
286 | DisableExpression(name);
|
---|
287 | }
|
---|
288 | }
|
---|
289 |
|
---|
290 | public void EnableExpression(string name) {
|
---|
291 | if (EnabledExpressions.Contains(name)) return;
|
---|
292 |
|
---|
293 | EnabledExpressions.Add(name);
|
---|
294 |
|
---|
295 | if (EnabledExpressionsChanged != null) {
|
---|
296 | EnabledExpressionsChanged.Invoke(this, new EnabledExpressionsChangedEventArgs(
|
---|
297 | new[] { name },
|
---|
298 | new string[0]));
|
---|
299 | }
|
---|
300 | }
|
---|
301 |
|
---|
302 | public void DisableExpression(string name) {
|
---|
303 | if (!EnabledExpressions.Contains(name)) return;
|
---|
304 |
|
---|
305 | EnabledExpressions.Remove(name);
|
---|
306 |
|
---|
307 | if (EnabledExpressionsChanged != null) {
|
---|
308 | EnabledExpressionsChanged.Invoke(this, new EnabledExpressionsChangedEventArgs(
|
---|
309 | new string[0],
|
---|
310 | new[] { name }));
|
---|
311 | }
|
---|
312 | }
|
---|
313 |
|
---|
314 | public void EnableExpression<T>() where T : Expression {
|
---|
315 | var attribute = (PushExpressionAttribute)Attribute.GetCustomAttribute(typeof(T), typeof(PushExpressionAttribute));
|
---|
316 | EnableExpression(attribute.ExpressionName);
|
---|
317 | }
|
---|
318 |
|
---|
319 | public void DisableExpression<T>() where T : Expression {
|
---|
320 | var attribute = (PushExpressionAttribute)Attribute.GetCustomAttribute(typeof(T), typeof(PushExpressionAttribute));
|
---|
321 | DisableExpression(attribute.ExpressionName);
|
---|
322 | }
|
---|
323 |
|
---|
324 | public void EnableStack(StackType type) {
|
---|
325 | SetStack(type, true);
|
---|
326 | }
|
---|
327 |
|
---|
328 | public void DisableStack(StackType type) {
|
---|
329 | SetStack(type, false);
|
---|
330 | }
|
---|
331 |
|
---|
332 | private void SetStack(StackType type, bool value) {
|
---|
333 | switch (type) {
|
---|
334 | case StackType.Boolean:
|
---|
335 | IsBooleanStackEnabled = value;
|
---|
336 | break;
|
---|
337 | case StackType.Integer:
|
---|
338 | IsIntegerStackEnabled = value;
|
---|
339 | break;
|
---|
340 | case StackType.Float:
|
---|
341 | IsFloatStackEnabled = value;
|
---|
342 | break;
|
---|
343 | case StackType.Name:
|
---|
344 | IsNameStackEnabled = value;
|
---|
345 | break;
|
---|
346 | case StackType.Code:
|
---|
347 | IsCodeStackEnabled = value;
|
---|
348 | break;
|
---|
349 | default: throw new InvalidOperationException("Stacktype unkown");
|
---|
350 | }
|
---|
351 | }
|
---|
352 |
|
---|
353 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
354 | return new PushGpConfiguration(this, cloner);
|
---|
355 | }
|
---|
356 | }
|
---|
357 | } |
---|