Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/NameExpressions.cs @ 14952

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

#2665 Added IsNoop to Expression, Made Expressions storable, Fixed Debugger, Fixed and improved problem data and result visualisation, Added custom ErcOption view, Added problem difficulty to problem data name

File size: 2.9 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
34  ///     pushed onto the EXEC stack), regardless of whether or not it has a definition. Upon encountering such a State and
35  ///     pushing it
36  ///     onto the NAME stack the flag will be cleared (whether or not the pushed State had a definition).
37  /// </summary>
38  [PushExpression(StackTypes.Name, "NAME.QUOTE")]
39  [StorableClass]
40  public class NameQuoteExpression : StatelessExpression {
41    public NameQuoteExpression() { }
42    [StorableConstructor]
43    protected NameQuoteExpression(bool deserializing) : base(deserializing) { }
44
45    public override bool IsNoop(IInternalPushInterpreter interpreter) {
46      return interpreter.IsNameQuoteFlagSet;
47    }
48
49    public override void Eval(IInternalPushInterpreter interpreter) {
50      interpreter.IsNameQuoteFlagSet = true;
51    }
52  }
53
54  /// <summary>
55  ///     Pushes a randomly selected NAME that already has a definition.
56  /// </summary>
57  [PushExpression(StackTypes.Name, "NAME.RANDBOUNDNAME")]
58  [StorableClass]
59  public class NameRandBoundNameExpression : StatelessExpression {
60    public NameRandBoundNameExpression() { }
61    [StorableConstructor]
62    protected NameRandBoundNameExpression(bool deserializing) : base(deserializing) { }
63
64    public override bool IsNoop(IInternalPushInterpreter interpreter) {
65      return interpreter.CustomExpressions.Count == 0;
66    }
67
68    public override void Eval(IInternalPushInterpreter interpreter) {
69      var index = interpreter.CustomExpressions.Keys.Count == 1
70        ? 0
71        : interpreter.Random.Next(0, interpreter.CustomExpressions.Keys.Count - 1);
72
73      var state = interpreter.CustomExpressions.Keys.ElementAt(index);
74      interpreter.NameStack.Push(state);
75    }
76  }
77}
Note: See TracBrowser for help on using the repository browser.