pyshgp.push package¶
pyshgp.push.atoms module¶
The atoms
module defines all types of Push Atoms.
An Atom is a piece of code in the Push language. Some Atoms only exist in Programs and some only exist in Genomes.
A Literal atom is a constant value of one of the supported PushType.
An Instruction atom is a wrapped function that can be used to manipulate a PushState. These atoms are where the logic of our Push programs is stored.
An Input atom is a reference to one of the Push program’s inputs (aka arguments). Inputs are referenced by position.
A CodeBlock is a sequence of other Atoms is used to express nested block of code. They cannot appear in Genomes.
-
class
pyshgp.push.atoms.
Atom
[source]¶ Bases:
object
Base class of all Atoms. The fundamental element of Push programs.
-
class
pyshgp.push.atoms.
Closer
(**kwargs)[source]¶ Bases:
pyshgp.push.atoms.Atom
,pyrsistent._pclass.PClass
An Atom dedicated to denoting the close of a CodeBlock in a Genomes representation.
-
class
pyshgp.push.atoms.
CodeBlock
(initial=())[source]¶ Bases:
pyshgp.push.atoms.Atom
,pyrsistent._checked_types.CheckedPVector
An Atom which holds a sequence of other atoms in order to express a nested code block.
-
code_at_point
(ndx: int) → Optional[pyshgp.push.atoms.Atom][source]¶ Return a nested element of the CodeBlock using depth first traversal.
-
size
(depth: int = 1) → int[source]¶ Return the size of the block and the size of all the nested blocks.
-
with_code_inserted_at_point
(code: pyshgp.push.atoms.Atom, index: int) → pyshgp.push.atoms.CodeBlock[source]¶ Insert an element into the CodeBlock using depth first traversal.
-
-
class
pyshgp.push.atoms.
Input
(**kwargs)[source]¶ Bases:
pyshgp.push.atoms.Atom
,pyrsistent._pclass.PClass
A reference to a positional argument of the PushProgram which the atom is part of.
-
input_index
¶ The position of the associated input to Push program.
- Type
int
-
input_index
¶
-
-
class
pyshgp.push.atoms.
InstructionMeta
(**kwargs)[source]¶ Bases:
pyshgp.push.atoms.Atom
,pyrsistent._pclass.PClass
An identifier of a Push Instruction.
The InstructionMeta is a placeholder atom. When a PushInterpreter evaluates an instruction ID, it searches for an instruction with the given name in its InstructionSet. If found, this instruction is evaluated by the PushInterpreter.
-
name
¶ The name of the Instruction being referenced.
- Type
str
-
code_blocks
¶ The number of code blocks that are expected to follow the instruction.
- Type
int
-
code_blocks
¶
-
name
¶
-
-
class
pyshgp.push.atoms.
Literal
(**kwargs)[source]¶ Bases:
pyshgp.push.atoms.Atom
,pyrsistent._pclass.PClass
An Atom which holds a constant value.
-
value
¶ The value to be stored in Literal.
- Type
Any
-
push_type
¶ The PushType of the Literal. Usually, the PushType’s underlying native type is the same as the type of the Literal’s value. The PushType is used to determine how to route the Literal onto the correct PushStack of the PushState during program execution.
- Type
PushType, optional
-
push_type
¶
-
value
¶
-
pyshgp.push.instruction module¶
Concrete implementations of the Instruction Atom type.
-
class
pyshgp.push.instruction.
Instruction
(name: str, code_blocks: int, docstring='Write me!')[source]¶ Bases:
abc.ABC
A function in the Push language used to modify the PushState.
The Instruction class is the abstract base class for specific implementations that are configured differently. For example, see SimpleInstruction verses TakesStateInstruction.
- Parameters
name (str,) – A unique name for the instruction.
code_blocks (int) – The number of CodeBlocks to open following the instruction in a Genome.
docstring (str, optional) – A string describing in the behavior of the Instruction.
-
name
¶ A unique name for the instruction.
- Type
str,
-
code_blocks
¶ The number of CodeBlocks to open following the instruction in a Genome.
- Type
int
-
docstring
¶ A string describing in the behavior of the Instruction.
- Type
str, optional
-
code_block
¶
-
docstring
¶
-
abstract
evaluate
(push_state: pyshgp.push.state.PushState, push_config: pyshgp.push.config.PushConfig = None) → pyshgp.push.state.PushState[source]¶ Evaluate the instruction on the given PushState.
- Parameters
push_state (pyshgp.push.state.PushState) – The PushState to run the instruction on.
push_config (pyshgp.push.interpreter.PushConfig) – The configuration of the Push language.
- Returns
Return the given state, possibly modified by the Instruction.
- Return type
-
meta
() → pyshgp.push.atoms.InstructionMeta[source]¶ Create an
InstructionMeta
from the instruction object.
-
name
¶
-
class
pyshgp.push.instruction.
ProducesManyOfTypeInstruction
(name: str, f: Callable, input_stacks: Sequence[str], output_stack: str, code_blocks: int, docstring='Write me!')[source]¶ Bases:
pyshgp.push.instruction.Instruction
Instruction that produces arbitrarily many values of a given PushType.
ProducesManyOfTypeInstructions pop their arguments in the same was as SimpleInstructions. Items are popped from the stacks corresponding the types denoted in the
input_stacks
list. If multiple occurences of the same type are ininput_stacks
, items are taken from progressively deeper in that stack. If the stacks of the PushState do not contain a sufficent number of items, the instruction does not modify the PushState.The popped arguments are then passed to the instruction’s function to produce a tuple of outputs. It is crucial that the instruction’s function produce a tuple of outputs, even if it only conains a single element. All elements of the tuple are pushed individually to the stack denoted in
output_stack
.- Parameters
name (str,) – A unique name for the instruction.
f (Callable) – A function whose signature matches input_stacks and output_stacks.
input_stacks (Sequence[str]) – A list of PushType names to use when popping arguments from the PushState.
output_stack (str) – The name of a PushType to use when pushing function results to the PushState. All values returned by the function go to the stack for this type.
code_blocks (int) – The number of CodeBlocks to open following the instruction in a Genome.
docstring (str, optional) – A string describing in the behavior of the Instruction.
-
code_blocks
¶
-
docstring
¶
-
evaluate
(push_state: pyshgp.push.state.PushState, push_config: pyshgp.push.config.PushConfig = None) → pyshgp.push.state.PushState[source]¶ Evaluate the instruction on the given PushState. Return mutated State.
A ProducesManyOfTypeInstruction infers which values to pop from the stack based on input_stacks and pushes each output to the same stack based on output_stack.
-
f
¶
-
input_stacks
¶
-
name
¶
-
output_stack
¶
-
class
pyshgp.push.instruction.
SimpleInstruction
(name: str, f: Callable, input_stacks: Sequence[str], output_stacks: Sequence[str], code_blocks: int, docstring='Write me!')[source]¶ Bases:
pyshgp.push.instruction.Instruction
A simple instruction implementation.
A SimpleInstruction uses a standardized way of manipulating PushStates. In other words, it handles popping its own function arguments and pushing the function return values.
The first step of evaluating a SimpleInstruction is to pop the arguments from the stacks corresponding the instruction’s
input_stacks
list. If multiple occurrences of the same type are ininput_stacks
, items are taken from progressively deeper in that stack. If the stacks of the PushState do not contain a sufficient number of items, the instruction does not modify the PushState.The popped arguments are then passed to the instruction’s function to produce a tuple of outputs. It is crucial that the instruction’s function produce a tuple of outputs, even if it only contains a single element. The elements of the tuple are then routed to the corresponding stacks specified in the instruction’s
output_stacks
.- Parameters
name (str,) – A unique name for the instruction.
f (Callable) – A function whose signature matches input_stacks and output_stacks.
input_stacks (Sequence[str]) – A list of PushType names to use when popping arguments from the PushState.
output_stacks (Sequence[str]) – A list of PushType names to use when pushing function results to the PushState.
code_blocks (int) – The number of CodeBlocks to open following the instruction in a Genome.
docstring (str, optional) – A string describing in the behavior of the Instruction.
-
code_blocks
¶
-
docstring
¶
-
evaluate
(push_state: pyshgp.push.state.PushState, push_config: pyshgp.push.config.PushConfig = None) → pyshgp.push.state.PushState[source]¶ Evaluate the instruction on the given PushState. Return mutated State.
A SimpleInstruction infers which values to pop and push from the stack based on its input_stacks and output_stacks.
-
f
¶
-
input_stacks
¶
-
name
¶
-
output_stacks
¶
-
class
pyshgp.push.instruction.
StateToStateInstruction
(name: str, f: Callable, stacks_used: Sequence[str], code_blocks: int, docstring='Write me!')[source]¶ Bases:
pyshgp.push.instruction.Instruction
Instruction that takes entire PushState and returns entire PushState.
-
code_blocks
¶
-
docstring
¶
-
evaluate
(push_state: pyshgp.push.state.PushState, push_config: pyshgp.push.config.PushConfig = None) → pyshgp.push.state.PushState[source]¶ Evaluate the instruction on the given PushState. Return mutated State.
A SimpleInstruction infers which values to pop and push from the stack based on its input_stacks and output_stacks.
-
f
¶
-
name
¶
-
stacks_used
¶
-
-
class
pyshgp.push.instruction.
TakesStateInstruction
(name: str, f: Callable, output_stacks: Sequence[str], other_stacks: Sequence[str], code_blocks: int, docstring='Write me!')[source]¶ Bases:
pyshgp.push.instruction.Instruction
Instruction that takes entire PushState and returns particular values.
The function of a TakesStateInstruction accepts an entire PushState as input and produces either a
Token.revert
or a tuple of outputs values. It is crucial that the instruction’s function produce a tuple of outputs, even if it only conains a single element.The elements of the tuple are then routed to the corresponding stacks specified in the instruction’s
output_stacks
.Additional PushTypes utilized by the instruction are denoted in
other_stacks
.- Parameters
name (str,) – A unique name for the instruction.
f (Callable) – A function that takes a PushState as input and produces values corresponding to
output_stacks.
output_stacks (Sequence[str]) – A list of PushType names to use when pushing function results to the PushState.
other_stacks (Sequence[str]) – A list of additional PushType names used by the Insturction’s function.
code_blocks (int) – The number of CodeBlocks to open following the instruction in a Genome.
docstring (str, optional) – A string describing in the behavior of the Instruction.
-
code_blocks
¶
-
docstring
¶
-
evaluate
(push_state: pyshgp.push.state.PushState, push_config: pyshgp.push.config.PushConfig = None) → pyshgp.push.state.PushState[source]¶ Evaluate the instruction on the given PushState. Return mutated State.
A SimpleInstruction infers which values to pop and push from the stack based on its input_stacks and output_stacks.
-
f
¶
-
name
¶
-
other_stacks
¶
-
output_stacks
¶
pyshgp.push.instruction_set module¶
The instruction_set
module defines the InstructionSet class.
An InstructionSet is a collection of Instruction objects, stored by name. The InstructionSet has methods to help define and register additional instructions.
-
class
pyshgp.push.instruction_set.
InstructionSet
(type_library: pyshgp.push.type_library.PushTypeLibrary = None, register_core: bool = False, strip_docstrings: bool = True)[source]¶ Bases:
dict
,typing.Generic
A collection of Instruction objects stored by name.
-
type_library
¶ The PushTypeLibrary which denote the PushTypes (and thus stacks) are supported. Default is None, which corresponds to the core set of types.
- Type
PushTypeLibrary, optional
-
register_core
[source]¶ If True, all instructions in the core instruction set will be registered upon instantiation. Default is False.
- Type
bool, optional
-
strip_docstrings
¶ If True, the docstring attribute of registered instructions will be removed to reduce memory footprint. Default is True.
- Type
bool, optional
-
register
(instr: pyshgp.push.instruction.Instruction)[source]¶ Register an Instruction object.
- Parameters
instr – Instruction to register.
- Returns
A reference to the InstructionSet.
- Return type
-
register_core
()[source]¶ Register all core instructions defined in pyshgp.
- Returns
A reference to the InstructionSet.
- Return type
-
register_core_by_name
(name_pattern: str)[source]¶ Register all instructions whose name match the given pattern.
- Parameters
name_pattern – A regex string.
- Returns
A reference to the InstructionSet.
- Return type
-
register_core_by_stack
(include_stacks: Set[str], *, exclude_stacks: Set[str] = None)[source]¶ Register all instructions that make use of the given type name.
- Parameters
include_stacks – List of PushType names.
exclude_stacks – List of PushType names.
- Returns
A reference to the InstructionSet.
- Return type
-
register_list
(instrs: Sequence[pyshgp.push.instruction.Instruction])[source]¶ Register a list of Instruction objects.
- Parameters
instrs – List of Instruction objects to register.
- Returns
A reference to the InstructionSet.
- Return type
-
required_stacks
() → Set[str][source]¶ Return all stack names used throughout the registered instructions.
- Returns
The set of stack names that are used by the registered instructions.
- Return type
Set[str]
-
set_type_library
(type_library: pyshgp.push.type_library.PushTypeLibrary)[source]¶ Set the type library attribute and return self.
- Parameters
type_library – PushTypeLibrary to set.
- Returns
A reference to the InstructionSet.
- Return type
-
pyshgp.push.interpreter module¶
The interpreter
module defines the PushInterpreter
used to run Push programs.
-
class
pyshgp.push.interpreter.
PushInterpreter
(instruction_set: Union[pyshgp.push.instruction_set.InstructionSet, str] = 'core', reset_on_run: bool = True)[source]¶ Bases:
object
An interpreter capable of running Push programs.
- Parameters
instruction_set (Union[InstructionSet, str], optional) – The
InstructionSet
to use for executing programs. Default is “core” which instantiates anInstructionSet
using all the core instructions.
-
instruction_set
¶ The
InstructionSet
to use for executing programs.- Type
-
state
¶ The current
PushState
. Contains one stack for eachPushType
mentioned by the instructions in the instruction set.- Type
-
status
¶ A string denoting if the interpreter has encountered a situation where non-standard termination was required.
-
evaluate_atom
(atom: pyshgp.push.atoms.Atom, config: pyshgp.push.config.PushConfig)[source]¶ Evaluate an
Atom
.- Parameters
atom (Atom) – The Atom (
Literal
,InstructionMeta
,Input
, orCodeBlock
) to evaluate against the currentPushState
.config (PushConfig) – The configuration of the Push program being run.
-
run
(program: pyshgp.push.program.Program, inputs: list, print_trace: bool = False) → list[source]¶ Run a Push
Program
given some inputs and desired outputPushTypes
.- The general flow of this method is:
Create a new push state
Load the program and inputs.
If the exec stack is empty, return the outputs.
Else, pop the exec stack and process the atom.
Return to step 3.
- Parameters
program (Program) – Program to run.
inputs (list) – A sequence of values to use as inputs to the push program.
print_trace (bool) – If True, each step of program execution will be summarized in stdout.
- Returns
A sequence of values pulled from the final push state. May contain pyshgp.utils.Token.no_stack_item if output stacks are empty.
- Return type
Sequence
pyshgp.push.stack module¶
The stack
module defines the PushStack
class.
A PushStack
is used to hold values of a certain PushType
in a PushState
object.
-
class
pyshgp.push.stack.
PushStack
(push_type: pyshgp.push.types.PushType, push_config: pyshgp.push.config.PushConfig)[source]¶ Bases:
list
,typing.Generic
Stack that holds elements of a single
PushType
.- Parameters
push_type (PushType) – The PushType all items of the stack should conform to.
push_config (PushConfig) – The configuration of the Push program being run.
-
push_config
¶ The configuration of the Push program being run.
- Type
PushConfig
-
insert
(position: int, value)[source]¶ Insert value at
position
in stack.- Parameters
position (int) – Position in stack to get item.
value – Value to insert into stack.
-
nth
(position: int)[source]¶ Return the element at a given position.
If stack is empty, returns None. If
position < 0
orposition > len(self)
returns ano_stack_item
token.- Parameters
position (int) – Position in stack to get item.
- Returns
- Return type
Element at
position
in stack.
-
pop
(index: Optional[int] = None)[source]¶ Pop the top item off the stack, or pop the item at some index.
- Parameters
index (int, optional) – Index to pop from the stack. Default of
None
will pop the top item.- Returns
- Return type
Element at
index
in stack.
-
push
(value)[source]¶ Pushes a value to the top of the stack.
A type safe alias for append which makes the definition of push instructions more clear.
- Parameters
value – Value to push onto stack.
-
push_config
¶
-
push_type
¶
-
set_nth
(position: int, value)[source]¶ Overwrite the item in the nth position of the stack with the new value.
- Parameters
position (int) – Position in stack to get item.
value – Value to insert into stack.
pyshgp.push.state module¶
The state module is define the PushState class.
A PushState object holds the PushStacks, stdout string, and collection of input values. The PushState is what controls the setup of all stacks before program manipulation, and the producing of outputs after program execution.
-
class
pyshgp.push.state.
PushState
(type_library: pyshgp.push.type_library.PushTypeLibrary, push_config: pyshgp.push.config.PushConfig)[source]¶ Bases:
dict
A collection of PushStacks used during push program execution.
-
classmethod
from_dict
(d, type_library: pyshgp.push.type_library.PushTypeLibrary, push_config: pyshgp.push.config.PushConfig)[source]¶ Set 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 thed
can be converted to a valid Push state.- Parameters
d (dict) – Dict that is converted into a Push state.
type_library (PushTypeLibrary) – A Push type library.
push_config (PushConfig) – The configuration of the current program being executed.
-
inputs
¶
-
load_code
(program: pyshgp.push.atoms.CodeBlock)[source]¶ Push the given CodeBlock to the execution stack.
-
load_inputs
(inputs)[source]¶ Load a list of input values onto the PushState inputs.
- Parameters
inputs (list) – List of input values.
-
observe_stacks
(types: Sequence[str]) → list[source]¶ Return a list of output values based on the given types indicated.
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
types (list) – List of strings denoting the push types of the returned values.
-
pop_from_stacks
(types: Sequence[str]) → Union[Sequence, pyshgp.utils.Token][source]¶ Pop the top items for each value_type. Return a vector of the values popped from each stack.
-
push_config
¶
-
push_to_stacks
(values: list, types: Sequence[str])[source]¶ Check that all values can be coerced into their expected PushType. Push them onto the correct stack.
-
stdout
¶
-
type_library
¶
-
untyped
¶
-
classmethod
pyshgp.push.type_library module¶
A PushTypeLibrary describes the PushTypes which a given instance of PushGP will support.
-
class
pyshgp.push.type_library.
PushTypeLibrary
(register_core: bool = True, *args)[source]¶ Bases:
dict
A collection of PushTypes which can support a corresponding PushStack.
- Parameters
register_core (bool, optional) – If True, all core types will be registered. Default is True.
*args – A collection of PushTypes to register.
-
register_core
[source]¶ If True, all core types will be registered. Default is True.
- Type
bool, optional
-
\*args
A collection of PushTypes to register.
-
create_and_register
(name: str, python_types: Tuple[type, …], is_collection: bool = False, is_numeric: bool = False, force=False)[source]¶ Create a PushType and register it into the library.
- Parameters
name (str) – A name for the type. Used when referencing the PushType in Instruction definitions and will be the key in the PushState for the corresponding PushStack.
python_types (Tuple[type]) – A tuple of python types that correspond to the underlying native types which the PushType is representing.
is_collection (bool, optional) – Indicates if the PushType is a collection. Default is False.
is_numeric (bool, optional) – Indicates if the PushType is a number. Default is False.
force (bool) – If True, will register the type even if it will overwrite an existing reserved stack typed (eg. exec, stdout, untyped). Default is False. It is not recommended this argument be changed unless you have a very good reason to do so.
- Returns
A reference to the PushTypeLibrary.
- Return type
-
push_type_for_type
(typ: type, error_on_not_found: bool = False) → Optional[pyshgp.push.types.PushType][source]¶ Return the PushType of the given python (or numpy) type.
- Parameters
typ (type) – Any type to try and find the corresponding PushType.
error_on_not_found (bool, optional) – If True, will raise error if no PushType found. Default is False.
- Returns
The corresponding PushType of the given type. If no corresponding type, returns None.
- Return type
Optional[PushType]
-
push_type_of
(thing: Any, error_on_not_found: bool = False) → Optional[pyshgp.push.types.PushType][source]¶ Return the PushType of the given thing.
- Parameters
thing (Any) – Any value to try and find the corresponding PushType.
error_on_not_found (bool, optional) – If True, will raise error if no PushType found. Default is False.
- Returns
The corresponding PushType of the thing. If no corresponding type, returns None.
- Return type
Optional[PushType]
-
register
(push_type: pyshgp.push.types.PushType, _force=False)[source]¶ Register a PushType object.
- Parameters
push_type – PushType to register.
_force (bool, optional) – For internal use only. Default is False.
- Returns
A reference to the
PushTypeLibrary
.- Return type
-
register_core
()[source]¶ Register all core PushTypes defined in pyshgp.
- Returns
A reference to the PushTypeLibrary.
- Return type
-
register_list
(list_of_push_types: Sequence[pyshgp.push.types.PushType])[source]¶ Register a list of PushType ojbects.
- Parameters
list_of_push_types – List of Instruction objects to register.
- Returns
A reference to the PushTypeLibrary.
- Return type
-
supported_stacks
() → Set[str][source]¶ All stack names which the PushTypeLibrary can support.
- Returns
A set of stacks names which the type library can support.
- Return type
set[str]
-
pyshgp.push.type_library.
infer_literal
(val: Any, type_library: pyshgp.push.type_library.PushTypeLibrary) → pyshgp.push.atoms.Literal[source]¶ Make a literal by inferring the PushType of the value.
- Parameters
val (Any) – Any value to try and make a Literal out of.
type_library (PushTypeLibrary) – The library of PushTypes which a Literal can be made of.
- Returns
The Literal object which holds the value and the corresponding PushType.
- Return type
pyshgp.push.types module¶
The types
module contains the core PushTypes and functions to reference them.
A PushType simply is a named collection of Python types. A PushType can be used to determine if multiple items should be considered the same type during Push program execution.
-
class
pyshgp.push.types.
BoolVector
(initial=())[source]¶ Bases:
pyrsistent._checked_types.CheckedPVector
-
class
pyshgp.push.types.
Char
(char)[source]¶ Bases:
str
Holds a string of length 1.
Used to distinguish between string and char literals in Push program interpretation.
-
char
¶ String of length 1.
- Type
str
-
-
class
pyshgp.push.types.
CharVector
(initial=())[source]¶ Bases:
pyrsistent._checked_types.CheckedPVector
-
class
pyshgp.push.types.
FloatVector
(initial=())[source]¶ Bases:
pyrsistent._checked_types.CheckedPVector
-
class
pyshgp.push.types.
IntVector
(initial=())[source]¶ Bases:
pyrsistent._checked_types.CheckedPVector
-
class
pyshgp.push.types.
PushBoolType
[source]¶ Bases:
pyshgp.push.types.PushType
-
class
pyshgp.push.types.
PushCharType
[source]¶ Bases:
pyshgp.push.types.PushType
-
class
pyshgp.push.types.
PushFloatType
[source]¶ Bases:
pyshgp.push.types.PushType
-
class
pyshgp.push.types.
PushIntType
[source]¶ Bases:
pyshgp.push.types.PushType
-
class
pyshgp.push.types.
PushStrType
[source]¶ Bases:
pyshgp.push.types.PushType
-
class
pyshgp.push.types.
PushType
(name: str, python_types: Tuple[type, …], is_collection: bool = False, is_numeric: bool = False)[source]¶ Bases:
object
Type information for values used by Push programs.
-
name
¶ A name for the type. Used when referencing the PushType in Instruction definitions and will be the key in the PushState for the corresponding PushStack.
- Type
str
-
is_collection
¶ Indicates if that the PushType is a collection. Default is False.
- Type
bool, optional
-
is_numeric
¶ Indicates if that the PushType is a numeric type. Default is False.
- Type
bool, optional
-
-
class
pyshgp.push.types.
PushVectorType
(name: str, p_vec_type)[source]¶ Bases:
pyshgp.push.types.PushType
-
class
pyshgp.push.types.
StrVector
(initial=())[source]¶ Bases:
pyrsistent._checked_types.CheckedPVector
-
pyshgp.push.types.
make_vector_type
(scalar_type: pyshgp.push.types.PushType)[source]¶
Module contents¶
The push
sub-package contains an extensible Push interpreter and Push program abstraction.
When running Push programs (either during search or after) the modules in this package are used to perform the stack-based execution of Push code and report output values. For more information on the Push programming language, see the following links