Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2895_PushGP_GenealogyAnalysis/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/NameExpressions.cs @ 16724

Last change on this file since 16724 was 15771, checked in by bburlacu, 7 years ago

#2895: Add solution skeleton for PushGP with genealogy analysis.

File size: 2.9 KB
Line 
1using System.Linq;
2using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
3
4namespace HeuristicLab.Problems.ProgramSynthesis {
5  [StorableClass]
6  public class NameDefineXExecExpression : StatefulExpression<string> {
7    public NameDefineXExecExpression(string state) : base(state) { }
8    [StorableConstructor]
9    protected NameDefineXExecExpression(bool deserializing) : base(deserializing) { }
10
11    public override bool IsNoop(IInternalPushInterpreter interpreter) {
12      return false;
13    }
14
15    public override void Eval(IInternalPushInterpreter interpreter) {
16      Expression expression;
17      if (!interpreter.IsNameQuoteFlagSet &&
18          interpreter.CustomExpressions.TryGetValue(State, out expression)) {
19        interpreter.ExecStack.Push(expression);
20      } else {
21        interpreter.NameStack.Push(State);
22        interpreter.IsNameQuoteFlagSet = false;
23      }
24    }
25  }
26
27  /// <summary>
28  /// Sets a flag indicating that the next State encountered will be pushed onto the NAME stack (and not have its
29  /// associated value pushed onto the EXEC stack), regardless of whether or not it has a definition. Upon encountering such a State and
30  /// pushing it onto the NAME stack the flag will be cleared (whether or not the pushed State had a definition).
31  /// </summary>
32  [PushExpression(
33    StackTypes.Name,
34    "NAME.QUOTE",
35    "Sets a flag indicating that the next State encountered will be pushed onto the NAME stack")]
36  [StorableClass]
37  public class NameQuoteExpression : StatelessExpression {
38    public NameQuoteExpression() { }
39    [StorableConstructor]
40    protected NameQuoteExpression(bool deserializing) : base(deserializing) { }
41
42    public override bool IsNoop(IInternalPushInterpreter interpreter) {
43      return interpreter.IsNameQuoteFlagSet;
44    }
45
46    public override void Eval(IInternalPushInterpreter interpreter) {
47      interpreter.IsNameQuoteFlagSet = true;
48    }
49  }
50
51  /// <summary>
52  /// Pushes a randomly selected NAME that already has a definition.
53  /// </summary>
54  [PushExpression(
55    StackTypes.Name,
56    "NAME.RANDBOUNDNAME",
57    "Pushes a randomly selected NAME that already has a definition.")]
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);
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.