push

The push sub-package contains an implementation of the Push language. This includes an instruction set, interpreter, and random Push code generation utilities.

The following paragraph has been taken from the Push language homepage.

Push is a programming language designed for evolutionary computation, to be used as the programming language within which evolving programs are expressed. Push features a stack-based execution architecture in which there is a separate stack for each data type. In Push, “code” itself is a data type, allowing programs to manipulate their own code as they run and thereby to implement arbitrary and potentially novel control structures. This expressiveness is combined with syntactic minimality: the only syntax rule is that parentheses must be balanced. It is therefore trivial to generate and transform syntactically valid Push programs.

Instruction

The instruction module provides classes for various kinds of Push instructions that can be handled by the pyshgp Push interpreter.

class pyshgp.push.instruction.InputInstruction(input_index)

A push instruction that will handle input values. Input instructions which are generated based on initial state of the _input stack.

Parameters:

input_index : int

The index in the input stack to get value from.

execute(state)

Executes the input instruction on the given PushState.

Parameters:

state : PushState

The PushState to execute the input instruction on.

class pyshgp.push.instruction.Instruction(name, func, stack_types, parentheses=0)

A instruction for the push language.

Parameters:

nane : str

A string name for the instruction.

func : function

The python function that manipulates a Push state in the desired way.

stack_types : list of str

List of related pyshgp types.

parentheses : int

Specifies number of paren groups. (0, 1, 2, … etc)

execute(state)

Executes the input instruction on the given PushState.

Parameters:

state : PushState

The PushState to execute the input instruction on.

Interpreter

The interpreter module defines the PushInterpreter class which is capable of running Push programs.

class pyshgp.push.interpreter.PushInterpreter

Object that can run Push programs and stores the state of the Push stacks.

Parameters:

inputs : list

A list of all values that should be accessible as inputs.

Attributes

state (PushState) A PushState that hold the current state of all of the stacks.
status (str) Current status of the interpreter. Either ‘_normal’ or some kind of error indicator.
eval_atom(instruction)

Executes a push instruction or literal.

Parameters:

instruction : PushInstruction

The instruction to the executed.

eval_push(print_steps=False)

Executes the contents of the exec stack.

Aborts prematurely if execution limits are exceeded. If execution limits are reached, status will be denoted.

Parameters:

print_steps : bool, optional

Denotes if stack state should be printed.

reset()

Resets the PushInterpreter. Should be called between push program executions.

run(code, inputs, output_types, print_steps=False)

The top level method of the push interpreter.

Calls eval-push between appropriate code/exec pushing/popping.

Parameters:

code : list

The push program to run.

print_steps : bool, optional

Denotes if stack states should be printed.

Returns:

Returns the output structure as a dictionary.

class pyshgp.push.interpreter.PushState

Custom dictionary that holds stacks of Pysh types. Includes methods to help with for push program execution.

from_dict(d)

Sets the state to match the given dictionary.

Warning

This is written to be used in pyshgp tests, NOT as part of push program execution or evolution. There are no checks to confirm that the d can be converted to a valid Push state.

Parameters:

d : dict

Dict that is converted into a Push state.

load_inputs(inputs)

Loads a list of input values onto the PushState intputs.

Parameters:

inputs : list

List of input values.

observe_outputs(output_types)

Returns a list of output values based on the types indicated in output_types. Items are take from the tops of each stack. If multiple occurences of the same type are in output_types, the returned values are taken from progressively deeper in that stack. Does not pop the values off the stacks.

Parameters:

output_types : list

List of strings denoting the pysh types of the returned values.

pretty_print()

Prints state of all stacks in the PushState.

Plush

The plush module contains code pertaining the Plush genenomes.

Plush genomes are linear representations of Push programs. Plush genomes are python lists of plush genes. Plush genes are python objects defined below.

class pyshgp.push.plush.Gene(atom, is_literal, closes=0, is_silent=False)

Attributes

atom ({Instruction, literal}) An instance of the instruction class or a literal
is_literal (bool) Denotes if the gene is holding a literal or an instruction.
closes (int) Denotes how many close parens to place after instruction in program.
is_silent (bool, optional) If true, do not include instruction in translated program.

Spawn

The spawn module defines classes that produce random Plush genomes and random Push programs.

class pyshgp.push.spawn.LinearSpawner(atom_generators)

Spawns new linear push programs and plush genomes that represent linear programs.

Parameters:

atom_generators : list

List of atoms, and functions that produce atoms, to choose from when generating random code.

generate_close_count()

Returns 0 to keep the genomes/programs linear

Returns:0
class pyshgp.push.spawn.Spawner(atom_generators, close_parens_probabilities='default')

Spawns new push programs and plush genomes.

Parameters:

atom_generators : list

List of atoms, and functions that produce atoms, to choose from when generating random code.

close_parens_probabilities : list (optional)

List of probabilities that sum to one. Recomended you leave this as the default.

atom_to_plush_gene(atom)

Converts an atom into a plush gene.

Parameters:

atom : Instruction or literal

The atom (instruction or literal) to convert to a gene.

Returns:

Instance of Gene.

generate_close_count()

Returns a random number of closes based on close_parens_probabilities.

close_parens_probabilities defaults to [0.772, 0.206, 0.021, 0.001]. This is roughly equivalent to each selection coming from a binomial distribution with n=4 and p=1/16. This results in the following probabilities: p(0) = 0.772 p(1) = 0.206 p(2) = 0.021 p(3) = 0.001

Returns:Integer between 0 and 3, inclusive. Denoted number of closes.
random_plush_gene()

Returns a random plush gene given atom_generators and epigenetic-markers.

Returns:A random Plush gene from the atom_generators.
random_plush_genome(max_genome_size)

Returns a random Plush genome with size limited by max_genome_size.

Parameters:

max_genome_size : int

Max number of genes that could be in the genome.

Returns:

List of Plush genes (tuples). This is considered a genome.

random_plush_genome_with_size(genome_size)

Returns a random Plush genome with size genome_size.

Parameters:

genome_size : int

The number of genes to be put in the Plush genome.

Returns:

List of Plush genes. This is considered a genome.

random_push_code(max_points)

Returns a random Push expression with size limited by max_points.

Parameters:

max_points : int

Max number of instructions, literals and parens.

Returns:

A random Push program.

Stack

The stack module defines the PyshStack class which is used to hold values of a certain type in a PyshState object.

class pyshgp.push.stack.PyshStack(pysh_type_str)

Stack that holds elements of a sinlge push type. Extends Python list.

flush()

Empties the stack.

insert(position, value)

Inserts value at position in stack.’

Parameters:

position : int

Position in stack to get item.

value :

Value to insert into stack.

push(value)

Pushes a value to the top of the stack. An alias for append which makes the definition of push instructions more clear.

Parameters:

value :

Value to push onto stack.

ref(position)

Returns the element at a given position.

If stack is empty, returns None. If position < 0 or position > len(self) returns None.

Parameters:

position : int

Position in stack to get item.

Returns:

Element at positon in stack.

top_item()

Returns the top item on the stack, or a None if empty.

Returns:Returns the top element of the stack, or None if empty.

Translation

The translation module provides functions that translate Plush genomes into Push programs.

pyshgp.push.translation.genome_to_program(genome)

Given a Plush genomes, returns the equivalent Push program.

Takes as input of a Plush genome and translates it to the correct Push program with balanced parens. The linear Plush genome is made up of a list of instruction objects. As the linear Plush genome is traversed, each instruction that requires parens will push "_close" and/or "_close_open" onto the paren stack, and will also put an open paren after it in the program. If the end of the program is reached but parens are still needed (as indicated by the paren-stack), parens are added until the paren stack is empty.

Parameters:

genome : list

List of Plush genes to be translated.

Returns:

A Push program.

pyshgp.push.translation.get_matcing_close_index(sequence)

Returns the index of the the matching "_close" to the first "_open".

Parameters:

sequence : list

List with some "_open" and "_close" elements.

Returns:

Int of index to match "_close".

pyshgp.push.translation.open_close_sequence_to_list(sequence)

Converts flat list with "_open" and "_close" elements to nested lists.

Parameters:

sequence : list

List with some "_open" and "_close" elements.

Examples

>>> open_close_sequence_to_list(["_open", 1, "_close", 2]))
[[1], 2]