def __init__(self, n_inputs: int, instruction_set: Union[InstructionSet, str], literals: Sequence[Any], erc_generators: Sequence[Callable], distribution: DiscreteProbDistrib = "proportional"): self.n_inputs = n_inputs self.erc_generators = erc_generators self.instruction_set = instruction_set if self.instruction_set == "core": self.instruction_set = InstructionSet(register_core=True) self.type_library = self.instruction_set.type_library self.literals = [ lit if isinstance(lit, Literal) else infer_literal( lit, self.type_library) for lit in literals ] if distribution == "proportional": self.distribution = (DiscreteProbDistrib().add( GeneTypes.INPUT, self.n_inputs).add( GeneTypes.INSTRUCTION, len(self.instruction_set)).add( GeneTypes.CLOSE, sum([ i.code_blocks for i in self.instruction_set.values() ])).add(GeneTypes.LITERAL, len(literals)).add(GeneTypes.ERC, len(erc_generators))) else: self.distribution = distribution
def random_erc(self) -> Literal: """Materialize a random ERC generator into a Literal and return it. Returns ------- pushgp.push.atoms.Literal A Literal whose value comes from running a ERC generator function. """ erc_value = np.random.choice(self.erc_generators)() if not isinstance(erc_value, Literal): erc_value = infer_literal(erc_value, self.type_library) return erc_value
def random_literal(self) -> Literal: """Return a random Literal from the set of Literals. Returns ------- pushgp.push.atoms.Literal A randomly selected Literal. """ lit = np.random.choice(self.literals) if not isinstance(lit, Literal): lit = infer_literal(lit, self.type_library) return lit
def random_erc(self) -> Literal: """Materialize a random ERC generator into a Literal and return it. Returns ------- pyshgp.push.atoms.Literal A Literal whose value comes from running a ERC generator function. """ erc_value = np.random.choice(self.erc_generators)() if not isinstance(erc_value, Literal): erc_value = infer_literal(erc_value, self.type_library) return erc_value
def random_literal(self) -> Literal: """Return a random Literal from the set of Literals. Returns ------- pyshgp.push.atoms.Literal A randomly selected Literal. """ lit = np.random.choice(self.literals) if not isinstance(lit, Literal): lit = infer_literal(lit, self.type_library) return lit
def __init__(self, instruction_set: InstructionSet, literals: Sequence[Union[Literal, Any]], erc_generators: Sequence[Callable], distribution: DiscreteProbDistrib = "proportional"): self.instruction_set = instruction_set self.type_library = instruction_set.type_library self.literals = [lit if isinstance(lit, Literal) else infer_literal(lit, self.type_library) for lit in literals] self.erc_generators = erc_generators if distribution == "proportional": self.distribution = ( DiscreteProbDistrib() .add("instruction", len(instruction_set)) .add("close", sum([i.code_blocks for i in instruction_set.values()])) .add("literal", len(literals)) .add("erc", len(erc_generators)) ) else: self.distribution = distribution
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_infer_literal(): lib = PushTypeLibrary() assert infer_literal(5, lib) == Literal(5, PushInt) assert infer_literal(False, lib) == Literal(False, PushBool) assert infer_literal("", lib) == Literal("", PushStr)