Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/NameExpressions.cs @ 16958

Last change on this file since 16958 was 15032, checked in by pkimmesw, 8 years ago

#2665 Fixed bias 0 issue, PushExpressionFrequencyAnalyzer, Fixed probability for ERC settings, Fixed enable/disable instructions, Added expression descriptions

File size: 3.1 KB
Line 
1namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
2  using System.Linq;
3
4  using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
5  using HeuristicLab.Problems.ProgramSynthesis.Push.Attributes;
6  using HeuristicLab.Problems.ProgramSynthesis.Push.Stack;
7  using Interpreter;
8
9  [StorableClass]
10  public class NameDefineXExecExpression : StatefulExpression<string> {
11    public NameDefineXExecExpression(string state) : base(state) { }
12    [StorableConstructor]
13    protected NameDefineXExecExpression(bool deserializing) : base(deserializing) { }
14
15    public override bool IsNoop(IInternalPushInterpreter interpreter) {
16      return false;
17    }
18
19    public override void Eval(IInternalPushInterpreter interpreter) {
20      Expression expression;
21      if (!interpreter.IsNameQuoteFlagSet &&
22          interpreter.CustomExpressions.TryGetValue(State, out expression)) {
23        interpreter.ExecStack.Push(expression);
24      } else {
25        interpreter.NameStack.Push(State);
26        interpreter.IsNameQuoteFlagSet = false;
27      }
28    }
29  }
30
31  /// <summary>
32  /// Sets a flag indicating that the next State encountered will be pushed onto the NAME stack (and not have its
33  /// associated value pushed onto the EXEC stack), regardless of whether or not it has a definition. Upon encountering such a State and
34  /// pushing it onto the NAME stack the flag will be cleared (whether or not the pushed State had a definition).
35  /// </summary>
36  [PushExpression(
37    StackTypes.Name,
38    "NAME.QUOTE",
39    "Sets a flag indicating that the next State encountered will be pushed onto the NAME stack")]
40  [StorableClass]
41  public class NameQuoteExpression : StatelessExpression {
42    public NameQuoteExpression() { }
43    [StorableConstructor]
44    protected NameQuoteExpression(bool deserializing) : base(deserializing) { }
45
46    public override bool IsNoop(IInternalPushInterpreter interpreter) {
47      return interpreter.IsNameQuoteFlagSet;
48    }
49
50    public override void Eval(IInternalPushInterpreter interpreter) {
51      interpreter.IsNameQuoteFlagSet = true;
52    }
53  }
54
55  /// <summary>
56  /// Pushes a randomly selected NAME that already has a definition.
57  /// </summary>
58  [PushExpression(
59    StackTypes.Name,
60    "NAME.RANDBOUNDNAME",
61    "Pushes a randomly selected NAME that already has a definition.")]
62  [StorableClass]
63  public class NameRandBoundNameExpression : StatelessExpression {
64    public NameRandBoundNameExpression() { }
65    [StorableConstructor]
66    protected NameRandBoundNameExpression(bool deserializing) : base(deserializing) { }
67
68    public override bool IsNoop(IInternalPushInterpreter interpreter) {
69      return interpreter.CustomExpressions.Count == 0;
70    }
71
72    public override void Eval(IInternalPushInterpreter interpreter) {
73      var index = interpreter.CustomExpressions.Keys.Count == 1
74        ? 0
75        : interpreter.Random.Next(0, interpreter.CustomExpressions.Keys.Count);
76
77      var state = interpreter.CustomExpressions.Keys.ElementAt(index);
78      interpreter.NameStack.Push(state);
79    }
80  }
81}
Note: See TracBrowser for help on using the repository browser.