Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2665 Fixed VectorExpression errors, Fixed BenchmarkSuite Problem Data View issues

File size: 11.6 KB
Line 
1namespace HeuristicLab.Problems.ProgramSynthesis.Push.Configuration {
2  using System;
3  using System.Collections.Generic;
4  using System.Linq;
5  using Base.Erc;
6  using Common;
7  using Core;
8  using Expressions;
9  using Persistence.Default.CompositeSerializers.Storable;
10  using Stack;
11
12  [StorableClass]
13  public class PushConfiguration : ParameterizedNamedItem, IReadOnlyPushConfiguration, IEnabledExpressionsConfiguration {
14    public PushConfiguration() {
15      Name = "Push Configuration";
16
17      enabledExpressions = ExpressionTable.ExpressionNames.ToList();
18      enabledStacks = new Dictionary<StackTypes, bool>();
19
20      ErcOptions = new ErcOptions();
21      EvalPushLimit = 1024;
22      MinPointsInProgram = 25;
23      MaxPointsInProgram = 200;
24      TopLevelPushCode = true;
25      TopLevelPopCode = false;
26      MaxPointsInRandomExpression = 64;
27      MaxStringLength = 128;
28      MaxVectorLength = 64;
29      MaxDepth = 32;
30
31      InitEnabledStacks();
32    }
33
34    private void InitEnabledStacks(bool state = true) {
35      foreach (StackTypes type in Enum.GetValues(typeof(StackTypes))) {
36        if (!enabledStacks.ContainsKey(type) && type != StackTypes.None)
37          enabledStacks.Add(type, state);
38      }
39    }
40
41    [StorableConstructor]
42    public PushConfiguration(bool deserializing)
43      : base(deserializing) {
44    }
45
46    public PushConfiguration(PushConfiguration origin, Cloner cloner) : base(origin, cloner) {
47      enabledExpressions = origin.EnabledExpressions.ToList();
48      enabledStacks = origin.EnabledStacks.ToDictionary(x => x.Key, x => x.Value);
49
50      ErcOptions = cloner.Clone(origin.ErcOptions);
51      EvalPushLimit = origin.EvalPushLimit;
52      MinPointsInProgram = origin.MinPointsInProgram;
53      MaxPointsInProgram = origin.MaxPointsInProgram;
54      MaxPointsInRandomExpression = origin.MaxPointsInRandomExpression;
55      TopLevelPushCode = origin.TopLevelPushCode;
56      TopLevelPopCode = origin.TopLevelPopCode;
57      MaxStringLength = origin.MaxStringLength;
58      MaxVectorLength = origin.MaxVectorLength;
59      MaxDepth = origin.MaxDepth;
60    }
61
62    [StorableHook(HookType.AfterDeserialization)]
63    // ReSharper disable once UnusedMember.Local
64    private void AfterDeserialization() {
65      // Ensures that all types added after last serialization are available in enabledStacks
66      InitEnabledStacks(false);
67    }
68
69
70    [Storable]
71    private readonly Dictionary<StackTypes, bool> enabledStacks;
72
73    public IReadOnlyDictionary<StackTypes, bool> EnabledStacks { get { return enabledStacks; } }
74
75    public event EventHandler<EnabledExpressionsChangedEventArgs> EnabledExpressionsChanged;
76
77    [Storable]
78    private readonly List<string> enabledExpressions;
79
80    public IList<string> EnabledExpressions
81    {
82      get { return enabledExpressions; }
83      set
84      {
85        var removedExpressions = enabledExpressions.ToArray();
86        enabledExpressions.Clear();
87        enabledExpressions.AddRange(value);
88
89        if (EnabledExpressionsChanged != null) {
90          EnabledExpressionsChanged(this, new EnabledExpressionsChangedEventArgs(value, removedExpressions));
91        }
92      }
93    }
94
95    IReadOnlyList<string> IReadOnlyPushConfiguration.EnabledExpressions { get { return enabledExpressions; } }
96
97    [Storable]
98    public ErcOptions ErcOptions { get; set; }
99
100    IReadOnlyErcOptions IReadOnlyPushConfiguration.ErcOptions { get { return ErcOptions; } }
101
102    /// <summary>
103    ///     This is the maximum allowed number of "executions" in a single top-level call to the interpreter.
104    ///     The execution of a single Push instruction counts as one execution, as does the processing of a single literal,
105    ///     as does the descent into one layer of parentheses (that is, the processing of the "(" counts as one execution).
106    ///     When this limit is exceeded the interpreter aborts immediately, leaving its stacks in the states they were in prior
107    ///     to the abort (so they may still be examined by a calling program). Whether or not this counts as an "abnormal"
108    ///     termination
109    ///     is up to the calling program.
110    /// </summary>
111    [Storable]
112    public int EvalPushLimit { get; set; }
113
114    /// <summary>
115    /// This is the maximum of depth a push program can have. Expressions, which lead to exceed this limit are interpreted as NOOP.
116    /// </summary>
117    [Storable]
118    public int MaxDepth { get; set; }
119
120    /// <summary>
121    ///     This is the minimum size of an item on the CODE stack, expressed as a number of points.
122    ///     A point is an instruction, a literal, or a pair of parentheses. Any instruction that would cause this limit to be
123    ///     exceeded should instead act as a NOOP, leaving all stacks in the states that they were in before the execution of the
124    ///     instruction.
125    /// </summary>
126    [Storable]
127    public int MinPointsInProgram { get; set; }
128
129    /// <summary>
130    ///     This is the maximum size of an item on the CODE stack, expressed as a number of points.
131    ///     A point is an instruction, a literal, or a pair of parentheses. Any instruction that would cause this limit to be
132    ///     exceeded should instead act as a NOOP, leaving all stacks in the states that they were in before the execution of the
133    ///     instruction.
134    /// </summary>
135    [Storable]
136    public int MaxPointsInProgram { get; set; }
137
138    /// <summary>
139    ///     The maximum number of points in an expression produced by the CODE.RAND instruction.
140    /// </summary>
141    [Storable]
142    public int MaxPointsInRandomExpression { get; set; }
143
144    /// <summary>
145    ///     When TRUE (which is the default), code passed to the top level of the interpreter
146    ///     will be pushed onto the CODE stack prior to execution.
147    /// </summary>
148    [Storable]
149    public bool TopLevelPushCode { get; set; }
150
151    /// <summary>
152    ///     When TRUE, the CODE stack will be popped at the end of top level calls to the interpreter. The default is FALSE.
153    /// </summary>
154    [Storable]
155    public bool TopLevelPopCode { get; set; }
156
157    [Storable]
158    public int MaxStringLength { get; set; }
159
160    [Storable]
161    public int MaxVectorLength { get; set; }
162
163
164    public void SetEnabledStacks(StackTypes types) {
165      // Disable all
166      EnabledExpressions.Clear();
167
168      foreach (StackTypes type in Enum.GetValues(types.GetType())) {
169        if (type == StackTypes.None) continue;
170        enabledStacks[type] = false;
171      }
172
173      foreach (var pair in ExpressionTable.StackDependencyToNamesTable) {
174        if (types.HasFlag(pair.Key)) {
175          foreach (var name in pair.Value) {
176            EnableExpression(name, true);
177          }
178        }
179      }
180    }
181
182    public void EnableExpressionOfStack(StackTypes types) {
183      EnableExpressions(ExpressionTable.StackTypeToNamesTable[types]);
184    }
185
186    public void EnableExpressionDependentOnStack(StackTypes types) {
187      var names = ExpressionTable.StackDependencyToNamesTable[types].Where(
188        name => {
189          var type = ExpressionTable.NameToTypeTable[name];
190          var attribute = ExpressionTable.TypeToAttributeTable[type];
191
192          return (attribute.StackType | attribute.AdditionalStackDependencies)
193            .ToEnumerable()
194            .All(st => enabledStacks.ContainsKey(st) && enabledStacks[st]);
195        });
196
197      EnableExpressions(names);
198    }
199
200    private void EnableExpressions(IEnumerable<string> names) {
201      foreach (var name in names.Except(EnabledExpressions))
202        EnabledExpressions.Add(name);
203
204      if (EnabledExpressionsChanged != null) {
205        EnabledExpressionsChanged(this, new EnabledExpressionsChangedEventArgs(names, new string[0]));
206      }
207    }
208
209    public void DisableExpressionsOfStack(StackTypes types) {
210      DisableExpressions(ExpressionTable.StackTypeToNamesTable[types]);
211    }
212
213    public void DisableExpressionsDependentOnStack(StackTypes types) {
214      var names = ExpressionTable.StackDependencyToNamesTable
215          .Where(pair => pair.Key.HasFlag(types))
216          .SelectMany(pair => pair.Value);
217
218      DisableExpressions(names);
219    }
220
221    private void DisableExpressions(IEnumerable<string> names) {
222      foreach (var name in names.Intersect(EnabledExpressions))
223        EnabledExpressions.Remove(name);
224
225      if (EnabledExpressionsChanged != null) {
226        EnabledExpressionsChanged(this, new EnabledExpressionsChangedEventArgs(new string[0], names));
227      }
228    }
229
230    public void EnableExpression(string name, bool enableStackIfDisabled = false) {
231      if (EnabledExpressions.Contains(name)) return;
232
233      EnabledExpressions.Add(name);
234
235      if (enableStackIfDisabled) {
236        var type = ExpressionTable.TypeToNameTable.Single(x => x.Value == name).Key;
237        var attribute = ExpressionTable.TypeToAttributeTable[type];
238        enabledStacks[attribute.StackType] = true;
239      }
240
241      if (EnabledExpressionsChanged != null) {
242        EnabledExpressionsChanged(this, new EnabledExpressionsChangedEventArgs(
243          new[] { name },
244          new string[0]));
245      }
246    }
247
248    public void DisableExpression(string name, bool disableStackIfEnabled = false) {
249      if (!EnabledExpressions.Contains(name)) return;
250
251      EnabledExpressions.Remove(name);
252
253      if (disableStackIfEnabled) {
254        var type = ExpressionTable.TypeToNameTable.Single(x => x.Value == name).Key;
255        var attribute = ExpressionTable.TypeToAttributeTable[type];
256        enabledStacks[attribute.StackType] = false;
257      }
258
259      if (EnabledExpressionsChanged != null) {
260        EnabledExpressionsChanged(this, new EnabledExpressionsChangedEventArgs(
261          new string[0],
262          new[] { name }));
263      }
264    }
265
266    public void EnableExpression<T>(bool enableStackIfDisabled = false) where T : Expression {
267      var attribute = ExpressionTable.TypeToAttributeTable[typeof(T)];
268      EnableExpression(attribute.ExpressionName, enableStackIfDisabled);
269    }
270
271    public void DisableExpression<T>(bool disableStackIfEnabled = false) where T : Expression {
272      var attribute = ExpressionTable.TypeToAttributeTable[typeof(T)];
273      DisableExpression(attribute.ExpressionName, disableStackIfEnabled);
274    }
275
276    public void SetExpression(string name, bool state, bool cascadeForStack = false) {
277      if (state) EnableExpression(name, cascadeForStack);
278      else DisableExpression(name, cascadeForStack);
279    }
280
281    public void SetExpression<T>(bool state, bool cascadeForStack = false) where T : Expression {
282      if (state) EnableExpression<T>(cascadeForStack);
283      else DisableExpression<T>(cascadeForStack);
284    }
285
286    public void EnableStack(StackTypes type, bool enableExpressions = true, bool enableDependencies = true) {
287      enabledStacks[type] = true;
288
289      if (enableExpressions) EnableExpressionOfStack(type);
290      if (enableDependencies) EnableExpressionDependentOnStack(type);
291    }
292
293    public void DisableStack(StackTypes type, bool disableExpressions = true, bool disableDependencies = true) {
294      enabledStacks[type] = false;
295
296      if (disableExpressions) DisableExpressionsOfStack(type);
297      if (disableDependencies) DisableExpressionsDependentOnStack(type);
298    }
299
300    public void SetStack(StackTypes type, bool state, bool setExpressions = true, bool setDependencies = true) {
301      if (state) EnableStack(type, setExpressions, setDependencies);
302      else DisableStack(type, setExpressions, setDependencies);
303    }
304
305    public override IDeepCloneable Clone(Cloner cloner) {
306      return new PushConfiguration(this, cloner);
307    }
308  }
309}
Note: See TracBrowser for help on using the repository browser.