def test_create_and_register(self): lib = PushTypeLibrary(register_core=False) lib.create_and_register("seq", (list, tuple)) assert set(lib.keys()) == {"seq", "exec", "code"} new_type = lib["seq"] assert new_type.is_instance((1, 2, 3)) assert new_type.is_instance([1, 2, 3])
def test_register_core(self): lib = PushTypeLibrary() assert set(lib.keys()) == { "bool", "int", "float", "char", "str", "code", "exec" } assert lib["char"] == PushChar assert lib["int"] == PushInt
def test_create_and_register(self): lib = PushTypeLibrary(register_core=False) lib.create_and_register("seq", (list, tuple)) assert set(lib.keys()) == {"seq", "exec", "code"} new_type = lib["seq"] assert new_type.is_instance((1, 2, 3)) assert new_type.is_instance([1, 2, 3])
def core_instructions(type_library: PushTypeLibrary) -> Sequence[Instruction]: """All instructions definied by pyshgp for the given type library.""" supported_stacks = type_library.supported_stacks() instrucion_modules = [numeric, text, logical, code] basic_instr = list(chain(*[m.instructions() for m in instrucion_modules])) gen_instrs = generic_instructions(type_library) supported_instructions = _supported(basic_instr + gen_instrs, supported_stacks) return supported_instructions
def core_instructions(type_library: PushTypeLibrary) -> Sequence[Instruction]: """All instructions definied by pyshgp for the given type library.""" supported_stacks = type_library.supported_stacks() instrucion_modules = [numeric, text, logical, code] basic_instr = list(chain(*[m.instructions() for m in instrucion_modules])) gen_instrs = generic_instructions(type_library) supported_instructions = _supported(basic_instr + gen_instrs, supported_stacks) return supported_instructions
def __init__(self, type_library: PushTypeLibrary): super().__init__() self.stdout = "" self.inputs = [] self.untyped = deque([]) self.type_library = type_library for name, push_type in type_library.items(): self[name] = PushStack(push_type)
def __init__(self, type_library: PushTypeLibrary, push_config: PushConfig): super().__init__() self.stdout = "" self.inputs = [] self.untyped = deque([]) self.type_library = type_library self.push_config = push_config for name, push_type in type_library.items(): self[name] = PushStack(push_type, push_config)
def __init__(self, type_library: PushTypeLibrary = None, register_core: bool = False, strip_docstrings: bool = True): super().__init__() self.strip_docstrings = strip_docstrings if type_library is None: type_library = PushTypeLibrary() self.type_library = type_library if register_core: self.register_core()
def instructions(type_library: PushTypeLibrary): """Return all core printing instructions.""" i = [] for push_type in type_library.keys(): i.append(SimpleInstruction( "print_{t}".format(t=push_type), lambda x: [str(x)], input_stacks=[push_type], output_stacks=["stdout"], code_blocks=0, docstring="Prints the top {t}.".format(t=push_type) )) return i
def instructions(type_library: PushTypeLibrary): """Return all core printing instructions.""" i = [] for push_type in type_library.keys(): i.append(SimpleInstruction( "print_{t}".format(t=push_type), _wrap, input_stacks=[push_type], output_stacks=["stdout"], code_blocks=0, docstring="Prints the top {t}.".format(t=push_type) )), i.append(SimpleInstruction( "println_{t}".format(t=push_type), _wrap_and_newline, input_stacks=[push_type], output_stacks=["stdout"], code_blocks=0, docstring="Prints the top {t}.".format(t=push_type) )) return i
def instructions(type_library: PushTypeLibrary): """Return all core numeric instructions.""" i = [] for push_type in type_library.keys(): i.append( SimpleInstruction( "{t}_pop".format(t=push_type), _noop, input_stacks=[push_type], output_stacks=[], code_blocks=(1 if push_type == "exec" else 0), docstring="Pops the top {t}.".format(t=push_type))) i.append( SimpleInstruction( "{t}_dup".format(t=push_type), _dup, input_stacks=[push_type], output_stacks=[push_type, push_type], code_blocks=(1 if push_type == "exec" else 0), docstring="Duplicates the top {t}.".format(t=push_type))) i.append( ProducesManyOfTypeInstruction( "{t}_dup_times".format(t=push_type), _dup_times, input_stacks=["int", push_type], output_stack=push_type, code_blocks=(1 if push_type == "exec" else 0), docstring= "Duplicates the top {t} `n` times where `n` is from the int stack." .format(t=push_type))) # Disabled due to performance issues. # i.append(StateToStateInstruction( # "{t}_dup_top_n".format(t=push_type), # _dup_top_n_factory(push_type), # stacks_used=[push_type, "int"], # code_blocks=0, # docstring="Duplicates the top n items on the {t} stack.".format(t=push_type) # )) i.append( SimpleInstruction( "{t}_swap".format(t=push_type), _swap, input_stacks=[push_type, push_type], output_stacks=[push_type, push_type], code_blocks=(2 if push_type == "exec" else 0), docstring="Swaps the top two {t}s.".format(t=push_type))) i.append( SimpleInstruction( "{t}_rot".format(t=push_type), _rot, input_stacks=[push_type] * 3, output_stacks=[push_type] * 3, code_blocks=(3 if push_type == "exec" else 0), docstring="Rotates the top three {t}s.".format(t=push_type))) i.append( StateToStateInstruction( "{t}_flush".format(t=push_type), partial(_flush, type_name=push_type), stacks_used=[push_type], code_blocks=0, docstring="Empties the {t} stack.".format(t=push_type))) i.append( SimpleInstruction( "{t}_eq".format(t=push_type), _eq, input_stacks=[push_type, push_type], output_stacks=["bool"], code_blocks=0, docstring= "Pushes True if the top two {t} are equal. Otherwise pushes False." .format(t=push_type))) i.append( TakesStateInstruction( "{t}_stack_depth".format(t=push_type), partial(_stack_depth, type_name=push_type), output_stacks=["int"], other_stacks=[push_type], code_blocks=0, docstring="Pushes the size of the {t} stack to the int stack.". format(t=push_type))) i.append( StateToStateInstruction( "{t}_yank".format(t=push_type), partial(_yank, type_name=push_type), stacks_used=[push_type, "int"], code_blocks=0, docstring= "Yanks a {t} from deep in the stack based on an index from the int stack and puts it on top." .format(t=push_type))) i.append( StateToStateInstruction( "{t}_yank_dup".format(t=push_type), partial(_yank_dup, type_name=push_type), stacks_used=[push_type, "int"], code_blocks=0, docstring= "Yanks a copy of a {t} deep in the stack based on an index from the int stack and puts it on top." .format(t=push_type))) i.append( StateToStateInstruction( "{t}_shove".format(t=push_type), partial(_shove, type_name=push_type), stacks_used=[push_type, "int"], code_blocks=(1 if push_type == "exec" else 0), docstring= "Shoves the top {t} deep in the stack based on an index from the int stack." .format(t=push_type))) i.append( StateToStateInstruction( "{t}_shove_dup".format(t=push_type), partial(_shove_dup, type_name=push_type), stacks_used=[push_type, "int"], code_blocks=(1 if push_type == "exec" else 0), docstring= "Shoves a copy of the top {t} deep in the stack based on an index from the int stack." .format(t=push_type))) i.append( TakesStateInstruction( "{t}_is_empty".format(t=push_type), partial(_is_empty, type_name=push_type), output_stacks=["bool"], other_stacks=[push_type], code_blocks=0, docstring= "Pushes True if the {t} stack is empty. Pushes False otherwise." .format(t=push_type))) for push_type_name, push_type in type_library.items(): if push_type_name == "code": continue i.append( SimpleInstruction( "code_from_{t}".format(t=push_type_name), partial(_make_code, push_type=push_type), input_stacks=[push_type_name], output_stacks=["code"], code_blocks=(1 if push_type_name == "exec" else 0), docstring="Moves the top {t} to the code stack.".format( t=push_type_name))) return i
import pyshgp from pyshgp.push.atoms import Instruction from pyshgp.push.instruction import ( SimpleInstruction, StateToStateInstruction, TakesStateInstruction, ProducesManyOfTypeInstruction, ) from pyshgp.push.instructions import core_instructions from pyshgp.push.type_library import PushTypeLibrary # @TODO: CLI - Create a way to prepare a release from pyshgp_cli.py CORE_INSTRUCTIONS = core_instructions(PushTypeLibrary()) DOC_DIR = "build/doc/" def _generate_instruction_rst(instr: Instruction) -> str: lines = [instr.name, "=" * len(instr.name)] signature_line = None signature_template = "*Takes: {i} - Produces: {o}*" if isinstance(instr, SimpleInstruction): signature_line = signature_line = signature_template.format( i="[" + ", ".join(instr.input_stacks) + "]", o="[" + ", ".join(instr.output_stacks) + "]") elif isinstance(instr, StateToStateInstruction): signature_line = signature_template.format(i="PushState", o="PushState")
def core_type_lib(): return PushTypeLibrary(register_core=True)
def test_unregister_reserved(self): lib = PushTypeLibrary() with pytest.raises(ValueError): lib.unregister("exec")
def test_register_core(self): lib = PushTypeLibrary() assert set(lib.keys()) == {"bool", "int", "float", "char", "str", "code", "exec"} assert lib["char"] == PushChar assert lib["int"] == PushInt
""" Next, we create a type library that specifies we will be synthesizing programs that manipulate "floats" (built-in to pyshgp) and "rectangles" (custom for this problem). """ class RectangleType(PushType): def __init__(self): super().__init__("rectangle", (Rectangle, )) # override def coerce(self, value): return Rectangle(float(value[0]), float(value[1])) type_library = (PushTypeLibrary( register_core=False).register(PushFloat).register(RectangleType())) """ Next we define out instruction set using the type library and the two instructions we created. Our two custom instructions as well as the input instructions are defined. The instruction set will register all core instructions that can be supported using only exec, code, float, and rectangle types because the only core PushType we registered was "PushInt" For example, the instruction int_from_float will NOT be registered because our type library does not define a type that would support the "int" stack. """ instruction_set = (InstructionSet( type_library=type_library, register_core=True).register(rectangle_areas_instruction).register( rectangle_from_floats_instruction)) print("Stacks: ", instruction_set.required_stacks())
def test_push_type_for_type(self): lib = PushTypeLibrary() assert lib.push_type_for_type(int) == PushInt assert lib.push_type_for_type(bool) == PushBool assert lib.push_type_for_type(str) == PushStr assert lib.push_type_for_type(Char) == PushChar
def test_register_duplicates(self): lib = PushTypeLibrary(register_core=False) lib.create_and_register("char", (int, )) lib.register(PushChar) assert set(lib.keys()) == {"char", "exec", "code"} assert lib["char"] == PushChar
class PointType(PushType): def __init__(self): super().__init__("point", (Point,)) # override def coerce(self, value): return Point(float(value[0]), float(value[1])) # Custom type library that specifies we will be synthesizing programs that # manipulate "floats" (built-in to pyshgp) and "points" (custom for this problem) type_library = ( PushTypeLibrary(register_core=False) .register(PushFloat) .register(PointType()) ) # An instruction set which will register all core instructions that can be supported # using only exec, code, stdout, float, and point types. # # For example, the instruction int_from_float will NOT be registered because # our type library does not define a type that would support the "int" stack. # # Our two custom instructions as well as the input instructions are also defined. instruction_set = ( InstructionSet(type_library=type_library, register_core=True) .register(point_distance_insrt) .register(point_from_floats_instr)
def instructions(type_library: PushTypeLibrary): """Return all core numeric instructions.""" i: List[Instruction] = [] for push_type in set(CORE_VECTOR_PUSH_TYPES).intersection( set(type_library.values())): vec_type_name = push_type.name el_type_name = vec_type_name.replace("vector_", "") i.append( SimpleInstruction( vec_type_name + "_concat", concat, input_stacks=[vec_type_name, vec_type_name], output_stacks=[vec_type_name], code_blocks=0, docstring="Concatenates the top two {vt}.".format( vt=vec_type_name))) i.append( SimpleInstruction( vec_type_name + "_conj", conj, input_stacks=[vec_type_name, el_type_name], output_stacks=[vec_type_name], code_blocks=0, docstring="Appends the top {et} to the top {vt}.".format( vt=vec_type_name, et=el_type_name))) i.append( SimpleInstruction( vec_type_name + "_take", take, input_stacks=[vec_type_name, "int"], output_stacks=[vec_type_name], code_blocks=0, docstring= "Creates a new {vt} from the first N elements of the top {vt}. N is top int." .format(vt=vec_type_name))) i.append( SimpleInstruction( vec_type_name + "_subvec", subvec, input_stacks=[vec_type_name, "int", "int"], output_stacks=[vec_type_name], code_blocks=0, docstring= "Creates a new {vt} from a slice of the top {vt}. Start and end indices are the top two ints." .format(vt=vec_type_name))) i.append( SimpleInstruction( vec_type_name + "_first", first, input_stacks=[vec_type_name], output_stacks=[el_type_name], code_blocks=0, docstring= "Takes the first element of the top {vt} and pushes it to the {et} stack." .format(vt=vec_type_name, et=el_type_name))) i.append( SimpleInstruction( vec_type_name + "_last", last, input_stacks=[vec_type_name], output_stacks=[el_type_name], code_blocks=0, docstring= "Takes the last element of the top {vt} and pushes it to the {et} stack." .format(vt=vec_type_name, et=el_type_name))) i.append( SimpleInstruction( vec_type_name + "_nth", nth, input_stacks=[vec_type_name, "int"], output_stacks=[el_type_name], code_blocks=0, docstring= "Takes the nth element of the top {vt} and pushes it to the {et} stack. N is the top int." .format(vt=vec_type_name, et=el_type_name))) i.append( SimpleInstruction( vec_type_name + "_rest", rest, input_stacks=[vec_type_name], output_stacks=[vec_type_name], code_blocks=0, docstring="Drops the first element of the top {vt}.".format( vt=vec_type_name))) i.append( SimpleInstruction( vec_type_name + "_but_last", but_last, input_stacks=[vec_type_name], output_stacks=[vec_type_name], code_blocks=0, docstring="Drops the last element of the top {vt}.".format( vt=vec_type_name))) i.append( SimpleInstruction( vec_type_name + "_length", length, input_stacks=[vec_type_name], output_stacks=["int"], code_blocks=0, docstring="Pushes the length of the top {vt} to the int stack." .format(vt=vec_type_name))) i.append( SimpleInstruction( vec_type_name + "_reverse", reverse, input_stacks=[vec_type_name], output_stacks=[vec_type_name], code_blocks=0, docstring="Reverses the top {vt}.".format(vt=vec_type_name))) i.append( ProducesManyOfTypeInstruction( vec_type_name + "_push_all", push_all, input_stacks=[vec_type_name], output_stack=el_type_name, code_blocks=0, docstring="Pushes all elements of the top {vt} to the {et}.". format(vt=vec_type_name, et=el_type_name))) i.append( SimpleInstruction( vec_type_name + "_empty_vector", empty_vector, input_stacks=[vec_type_name], output_stacks=["bool"], code_blocks=0, docstring= "Pushes True to the bool stack if the top {vt} is empty. Pushes False otherwise." .format(vt=vec_type_name))) i.append( SimpleInstruction( vec_type_name + "_contains", contains, input_stacks=[vec_type_name, el_type_name], output_stacks=["bool"], code_blocks=0, docstring= "Pushes True to the bool stack if the top {et} is found in the top {vt}. Pushes False otherwise." .format(vt=vec_type_name, et=el_type_name))) i.append( SimpleInstruction( vec_type_name + "_index_of", index_of, input_stacks=[vec_type_name, el_type_name], output_stacks=["int"], code_blocks=0, docstring= "Pushes the index top {et} is top {vt} to int stack. Pushes -1 if not found." .format(vt=vec_type_name, et=el_type_name))) i.append( SimpleInstruction( vec_type_name + "_occurrences_of", occurrences_of, input_stacks=[vec_type_name, el_type_name], output_stacks=["int"], code_blocks=0, docstring= "Pushes the number of time the top {et} is found in the top {vt} to int stack." .format(vt=vec_type_name, et=el_type_name))) i.append( SimpleInstruction( vec_type_name + "_set_nth", set_nth, input_stacks=[vec_type_name, "int", el_type_name], output_stacks=[vec_type_name], code_blocks=0, docstring= "Sets the nth element of the top {vt} to be the top {et}. N is the top int." .format(vt=vec_type_name, et=el_type_name))) i.append( SimpleInstruction( vec_type_name + "_replace", replace, input_stacks=[vec_type_name, el_type_name, el_type_name], output_stacks=[vec_type_name], code_blocks=0, docstring= "Replaces all instances of the top {et} from the top {vt}.". format(vt=vec_type_name, et=el_type_name))) i.append( SimpleInstruction( vec_type_name + "_replace_first", replace_first, input_stacks=[vec_type_name, el_type_name, el_type_name], output_stacks=[vec_type_name], code_blocks=0, docstring= "Replaces the first instance of the top {et} from the top {vt}." .format(vt=vec_type_name, et=el_type_name))) i.append( SimpleInstruction( vec_type_name + "_remove", remove, input_stacks=[vec_type_name, el_type_name], output_stacks=[vec_type_name], code_blocks=0, docstring= "Removes all instances of the top {et} from the top {vt}.". format(vt=vec_type_name, et=el_type_name))) i.append( StateToStateInstruction( vec_type_name + "_iterate", partial(iterate, vec_type=type_library[vec_type_name], el_type=type_library[el_type_name]), stacks_used=[vec_type_name, el_type_name, "exec"], code_blocks=1, docstring= "Iterates over the top {vt} using the code on top of the exec stack." .format(vt=vec_type_name))) return i
def test_supported_stacks(self): lib = PushTypeLibrary() assert lib.supported_stacks() == ALL_CORE_TYPE_NAMES
def test_register_core(self): lib = PushTypeLibrary() assert set(lib.keys()) == ALL_CORE_TYPE_NAMES assert lib["char"] == PushChar assert lib["int"] == PushInt
def test_unregister(self): lib = PushTypeLibrary() lib.unregister("char") lib.unregister("float") assert set(lib.keys()) == ALL_CORE_TYPE_NAMES - {"char", "float"}
def test_infer_literal(): lib = PushTypeLibrary() assert infer_literal(5, lib) == Literal(value=5, push_type=PushInt) assert infer_literal(False, lib) == Literal(value=False, push_type=PushBool) assert infer_literal("", lib) == Literal(value="", push_type=PushStr)
def test_register_reserved(self): lib = PushTypeLibrary(register_core=False) with pytest.raises(ValueError): lib.create_and_register("stdout", (list, ))
def test_register(self): lib = PushTypeLibrary(register_core=False) lib.register(PushChar) assert set(lib.keys()) == {"char", "exec", "code"} assert lib["char"] == PushChar
def test_push_type_of(self): lib = PushTypeLibrary() assert lib.push_type_of(7) == PushInt assert lib.push_type_of(True) == PushBool assert lib.push_type_of("ABC") == PushStr assert lib.push_type_of(Char("Z")) == PushChar
def instructions(type_library: PushTypeLibrary): """Return all core numeric instructions.""" i = [] for push_type in type_library.keys(): i.append(SimpleInstruction( "{t}_pop".format(t=push_type), lambda x: [], input_stacks=[push_type], output_stacks=[], code_blocks=(1 if push_type == "exec" else 0), docstring="Pops the top {t}.".format(t=push_type) )) i.append(SimpleInstruction( "{t}_dup".format(t=push_type), lambda x: [x, x], input_stacks=[push_type], output_stacks=[push_type, push_type], code_blocks=(1 if push_type == "exec" else 0), docstring="Duplicates the top {t}.".format(t=push_type) )) i.append(ProducesManyOfTypeInstruction( "{t}_dup_times".format(t=push_type), _dup_times, input_stacks=["int", push_type], output_stack=push_type, code_blocks=(1 if push_type == "exec" else 0), docstring="Duplicates the top {t} `n` times where `n` is from the int stack.".format(t=push_type) )) # Disabled due to performance issues. # i.append(StateToStateInstruction( # "{t}_dup_top_n".format(t=push_type), # _dup_top_n_factory(push_type), # stacks_used=[push_type, "int"], # code_blocks=0, # docstring="Duplicates the top n items on the {t} stack.".format(t=push_type) # )) i.append(SimpleInstruction( "{t}_swap".format(t=push_type), lambda a, b: [a, b], input_stacks=[push_type, push_type], output_stacks=[push_type, push_type], code_blocks=(2 if push_type == "exec" else 0), docstring="Swaps the top two {t}s.".format(t=push_type) )) i.append(SimpleInstruction( "{t}_rot".format(t=push_type), lambda a, b, c: [b, a, c], input_stacks=[push_type] * 3, output_stacks=[push_type] * 3, code_blocks=(3 if push_type == "exec" else 0), docstring="Rotates the top three {t}s.".format(t=push_type) )) i.append(StateToStateInstruction( "{t}_flush".format(t=push_type), _flusher(push_type), stacks_used=[push_type], code_blocks=0, docstring="Empties the {t} stack.".format(t=push_type) )) i.append(SimpleInstruction( "{t}_eq".format(t=push_type), lambda a, b: [a == b], input_stacks=[push_type, push_type], output_stacks=["bool"], code_blocks=0, docstring="Pushes True if the top two {t} are equal. Otherwise pushes False.".format(t=push_type) )) i.append(TakesStateInstruction( "{t}_stack_depth".format(t=push_type), _stack_depther(push_type), output_stacks=["int"], other_stacks=[push_type], code_blocks=0, docstring="Pushes the size of the {t} stack to the int stack.".format(t=push_type) )) i.append(StateToStateInstruction( "{t}_yank".format(t=push_type), _yanker(push_type), stacks_used=[push_type, "int"], code_blocks=0, docstring="Yanks a {t} from deep in the stack based on an index from the int stack and puts it on top.".format(t=push_type) )) i.append(StateToStateInstruction( "{t}_yank_dup".format(t=push_type), _yank_duper(push_type), stacks_used=[push_type, "int"], code_blocks=0, docstring="Yanks a copy of a {t} deep in the stack based on an index from the int stack and puts it on top.".format(t=push_type) )) i.append(StateToStateInstruction( "{t}_shove".format(t=push_type), _shover(push_type), stacks_used=[push_type, "int"], code_blocks=(1 if push_type == "exec" else 0), docstring="Shoves the top {t} deep in the stack based on an index from the int stack.".format(t=push_type) )) i.append(StateToStateInstruction( "{t}_shove_dup".format(t=push_type), _shove_duper(push_type), stacks_used=[push_type, "int"], code_blocks=(1 if push_type == "exec" else 0), docstring="Shoves a copy of the top {t} deep in the stack based on an index from the int stack.".format(t=push_type) )) i.append(TakesStateInstruction( "{t}_is_empty".format(t=push_type), _is_emptyer(push_type), output_stacks=["bool"], other_stacks=[push_type], code_blocks=0, docstring="Pushes True if the {t} stack is empty. Pushes False otherwise.".format(t=push_type) )) for push_type_name, push_type in type_library.items(): if push_type_name == "code": continue i.append(SimpleInstruction( "code_from_{t}".format(t=push_type_name), partial(_make_code, push_type=push_type), input_stacks=[push_type_name], output_stacks=["code"], code_blocks=(1 if push_type_name == "exec" else 0), docstring="Moves the top {t} to the code stack.".format(t=push_type_name) )) return i
def test_register_duplicates(self): lib = PushTypeLibrary(register_core=False) lib.create_and_register("char", (int, )) lib.register(PushChar) assert set(lib.keys()) == {"char", "exec", "code"} assert lib["char"] == PushChar
def point_type_library(to_point_func): return ( PushTypeLibrary(register_core=False) .register(PushFloat) .create_and_register("point", (Point, ), coercion_func=to_point_func) )
def test_supported_stacks(self): lib = PushTypeLibrary() assert lib.supported_stacks() == {"bool", "int", "float", "char", "str", "code", "exec"}
def state(push_config): return PushState(PushTypeLibrary(PushInt, PushBool, PushFloat, PushStr), push_config)
def test_supported_stacks(self): lib = PushTypeLibrary() assert lib.supported_stacks() == { "bool", "int", "float", "char", "str", "code", "exec" }
def test_unregister(self): lib = PushTypeLibrary() lib.unregister("char") lib.unregister("float") assert set(lib.keys()) == {"int", "str", "bool", "exec", "code"}
def test_push_type_of(self): lib = PushTypeLibrary() assert lib.push_type_of(7) == PushInt assert lib.push_type_of(True) == PushBool assert lib.push_type_of("ABC") == PushStr assert lib.push_type_of(Char("Z")) == PushChar
def test_register_reserved(self): lib = PushTypeLibrary(register_core=False) with pytest.raises(ValueError): lib.create_and_register("exec", (list, ))
def test_push_type_for_type(self): lib = PushTypeLibrary() assert lib.push_type_for_type(int) == PushInt assert lib.push_type_for_type(bool) == PushBool assert lib.push_type_for_type(str) == PushStr assert lib.push_type_for_type(Char) == PushChar
def test_unregister(self): lib = PushTypeLibrary() lib.unregister("char") lib.unregister("float") assert set(lib.keys()) == {"int", "str", "bool", "exec", "code"}
def test_register(self): lib = PushTypeLibrary(register_core=False) lib.register(PushChar) assert set(lib.keys()) == {"char", "exec", "code"} assert lib["char"] == PushChar
point_from_floats_instr = SimpleInstruction( "point_from_floats", point_from_floats, ["float", "float"], ["point"], 0 ) # Training data X = [[Point(row[0], row[1]), Point(row[2], row[3])] for row in np.random.rand(20, 4)] y = [[point_distance(x[0], x[1])] for x in X] # Custom type library that specifies we will be synthesizing programs that # manipulate "floats" (built-in to pyshgp) and "points" (custom for this problem) type_library = ( PushTypeLibrary(register_core=False) .register(PushFloat) .create_and_register("point", (Point, ), coercion_func=to_point) ) # An instruction set which will register all core instructions that can be supported # using only exec, code, stdout, float, and point types. # # For example, the instruction int_from_float will NOT be registered because # our type library does not define a type that would support the "int" stack. # # Our two custom instructions as well as the input instructions are also defined. instruction_set = ( InstructionSet(type_library=type_library, register_core=True) .register(point_distance_insrt) .register(point_from_floats_instr)
def test_unregister_reserved(self): lib = PushTypeLibrary() with pytest.raises(ValueError): lib.unregister("untyped")