Package ec.gp

# Class ADFStack

java.lang.Object
ec.gp.ADFStack

All Implemented Interfaces:
  `Prototype`, `Setup`, `Serializable`, `Cloneable`

public class ADFStack extends Object implements Prototype
ADFStack is a special data object used to hold ADF data. This object is a weird beast and takes some explaining. It consists of a main stack, a secondary "substack", and a "reserve" area (also implemented as a stack, but it doesn't have to be). The reserve is used to "recycle" objects rather than having to create then new every time.

When an ADF is evaluated, it first evaluates its children, then it calls push() on the ADFstack. push() either creates a new ADFContext, or it fetches one from the reserve if possible. It then pushes the context on the main stack, and also returns the context. The ADF fills the context's arguments with the results of its childrens' evaluation, and sets numargs to the number of arguments, then evaluates the ADF's associated function tree,

When an ADM is evaluated, it calls push() on the ADFstack. The ADM then fills the context's adm node with itself, and sets numargs to the number of children it has. Then it calls the ADM's associated function tree.

In that tree, if an argument terminal of value n is evaluated, the argument terminal calls evaluate(...) on the top context on the ADF stack and returns the result. This method does different things depending on whether the top context represented an ADF or an ADM. If it was an ADF, the context simply sets input to the value of argument n in the context's argument list, and returns input. If it was an ADM, the context pops itself off the stack and pushes itself on the substack (to set up the right context for evaluating an original child of the ADM), then evaluates child n of the ADM, then pops itself off the substack and pushes itself back on the stack to restore the context. Input is set to the evaluated results, and input is returned.

Parameters

base.`context` classname, inherits and != ec.gp.GPContext | (the stack's GPContext class)

Parameters
gp.adf-stack

Parameter bases

base.`context` | (context_proto)

See Also:
  - Serialized Form

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `ADFContext`
  `context_proto`
  `static final int`
  `INITIAL_STACK_SIZE`
  `protected int`
  `inReserve`
  `protected int`
  `onStack`
  `protected int`
  `onSubstack`
  `static final String`
  `P_ADF`
  `static final String`
  `P_ADFSTACK`
  `static final String`
  `P_CONTEXT`
  `protected ADFContext[]`
  `reserve`
  `protected ADFContext[]`
  `stack`
  `protected ADFContext[]`
  `substack`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `ADFStack()`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `Object`
  `clone()`
  Creates a new individual cloned from a prototype, and suitable to begin use in its own evolutionary context.
  `Parameter`
  `defaultBase()`
  Returns the default base for this prototype.
  `final ADFContext`
  `get()`
  Returns an ADFContext from the stack's reserve, or creates one fresh if there are none in reserve.
  `final int`
  `moveFromSubstack(int n)`
  Moves n items onto the stack (popss them off the substack and pushes them onto the stack).
  `final int`
  `moveOntoSubstack(int n)`
  Moves n items onto the substack (pops them off the stack and pushes them onto the substack).
  `final int`
  `pop(int n)`
  Pops off n items from the stack, if possible.
  `final ADFContext`
  `push(ADFContext obj)`
  Pushes an ADFContext onto the main stack.
  `final void`
  `reset()`
  Pops off all items on the stack and the substack.
  `void`
  `setup(EvolutionState state, Parameter base)`
  Sets up the object by reading it from the parameters stored in state, built off of the parameter base base.
  `final ADFContext`
  `top(int n)`
  Returns the nth item in the stack (0-indexed), or null if this goes to the bottom of the stack.

  ### Methods inherited from class java.lang.Object

  `equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Field Details

  - ### P_ADFSTACK

    public static final String P_ADFSTACK
    See Also:
      - Constant Field Values

  - ### P_ADF

    public static final String P_ADF
    See Also:
      - Constant Field Values

  - ### P_CONTEXT

    public static final String P_CONTEXT
    See Also:
      - Constant Field Values

  - ### context_proto

    public ADFContext context_proto

  - ### INITIAL_STACK_SIZE

    public static final int INITIAL_STACK_SIZE
    See Also:
      - Constant Field Values

  - ### onStack

    protected int onStack

  - ### onSubstack

    protected int onSubstack

  - ### inReserve

    protected int inReserve

  - ### stack

    protected ADFContext[] stack

  - ### substack

    protected ADFContext[] substack

  - ### reserve

    protected ADFContext[] reserve

- ## Constructor Details

  - ### ADFStack

    public ADFStack()

- ## Method Details

  - ### defaultBase

    public Parameter defaultBase()
    Description copied from interface: `Prototype`
    Returns the default base for this prototype. This should generally be implemented by building off of the static base() method on the DefaultsForm object for the prototype's package. This should be callable during setup(...).
    Specified by:
      `defaultBase` in interface `Prototype`

  - ### setup

    public void setup(EvolutionState state, Parameter base)
    Description copied from interface: `Prototype`
    Sets up the object by reading it from the parameters stored in state, built off of the parameter base base. If an ancestor implements this method, be sure to call super.setup(state,base); before you do anything else.

    For prototypes, setup(...) is typically called once for the prototype instance; cloned instances do not receive the setup(...) call. setup(...) may be called more than once; the only guarantee is that it will get called at least once on an instance or some "parent" object from which it was ultimately cloned.

    Specified by:
      `setup` in interface `Prototype`
    Specified by:
      `setup` in interface `Setup`

  - ### clone

    public Object clone()
    Description copied from interface: `Prototype`
    Creates a new individual cloned from a prototype, and suitable to begin use in its own evolutionary context.

    Typically this should be a full "deep" clone. However, you may share certain elements with other objects rather than clone hem, depending on the situation:

    - If you hold objects which are shared with other instances, don't clone them.
    - If you hold objects which must be unique, clone them.
    - If you hold objects which were given to you as a gesture of kindness, and aren't owned by you, you probably shouldn't clone them.
    - DON'T attempt to clone: Singletons, Cliques, or Populations, or Subpopulation.
    - Arrays are not cloned automatically; you may need to clone an array if you're not sharing it with other instances. Arrays have the nice feature of being copyable by calling clone() on them.

    Implementations.

    - If no ancestor of yours implements clone(), and you have no need to do clone deeply, and you are abstract, then you should not declare clone().
    - If no ancestor of yours implements clone(), and you have no need to do clone deeply, and you are not abstract, then you should implement it as follows:

      ``

           public Object clone()
               {
               try
                   {
                   return super.clone();
                   }
               catch ((CloneNotSupportedException e)
                   { throw new InternalError(); } // never happens
               }

    - If no ancestor of yours implements clone(), but you need to deep-clone some things, then you should implement it as follows:

      ``

           public Object clone()
               {
               try
                   {
                   MyObject myobj = (MyObject) (super.clone());

                   // put your deep-cloning code here...
                   }
               catch ((CloneNotSupportedException e)
                   { throw new InternalError(); } // never happens
               return myobj;
               }

    - If an ancestor has implemented clone(), and you also need to deep clone some things, then you should implement it as follows:

      ``

           public Object clone()
               {
               MyObject myobj = (MyObject) (super.clone());

               // put your deep-cloning code here...

               return myobj;
               }

    Specified by:
      `clone` in interface `Prototype`
    Overrides:
      `clone` in class `Object`

  - ### get

    public final ADFContext get()
    Returns an ADFContext from the stack's reserve, or creates one fresh if there are none in reserve. While you can throw this ADFContext away if you like, it'd be good if you actually didn't call this function unless you expected to push the context onto the stack with push(ADFContext obj) -- karma!

  - ### push

    public final ADFContext push(ADFContext obj)
    Pushes an ADFContext onto the main stack. The best way to get an ADFContext to push onto the stack is with get(). Returns obj.

  - ### pop

    public final int pop(int n)
    Pops off n items from the stack, if possible. Returns the number of items actually popped off.

  - ### top

    public final ADFContext top(int n)
    Returns the nth item in the stack (0-indexed), or null if this goes to the bottom of the stack.

  - ### moveOntoSubstack

    public final int moveOntoSubstack(int n)
    Moves n items onto the substack (pops them off the stack and pushes them onto the substack). Returns the actual number of items for which this was done.

  - ### moveFromSubstack

    public final int moveFromSubstack(int n)
    Moves n items onto the stack (popss them off the substack and pushes them onto the stack). Returns the actual number of items moved from the Substack onto the main stack

  - ### reset

    public final void reset()
    Pops off all items on the stack and the substack.