Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Configuration/PushConfiguration.cs @ 14777

Last change on this file since 14777 was 14777, checked in by pkimmesw, 7 years ago

#2665 simplifier, push solution results view, performance improvements, small bug fixes, ui fixes

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