def get_variation_operator(name: str, **kwargs) -> VariationOperator: """Get the variaton operator class with the given name.""" name_to_cls = { "deletion": DeletionMutation, "addition": AdditionMutation, "alternation": Alternation, "genesis": Genesis, "cloning": Cloning, # UMAD citation: https://dl.acm.org/citation.cfm?id=3205455.3205603 "umad": VariationPipeline([AdditionMutation(0.09), DeletionMutation(0.0826)]), "umad-shrink": VariationPipeline([AdditionMutation(0.09), DeletionMutation(0.1)]), "umad-grow": VariationPipeline([AdditionMutation(0.09), DeletionMutation(0.0652)]) } op = name_to_cls.get(name, None) if op is None: raise ValueError( "No varition operator '{nm}'. Supported names: {lst}.".format( nm=name, lst=list(name_to_cls.keys()))) if isinstance(op, type): op = instantiate_using(op, kwargs) return op
def test_instantiate_using_optional(): args = { "this": True } tmt = instantiate_using(ThisMaybeThat, args) assert tmt.this assert tmt.that == "default"
def test_instantiate_using_exact(): args = { "a": 1, "b": 2 } p = instantiate_using(Pair, args) assert p.a == 1 assert p.b == 2
def test_instantiate_using_extra(): args = { "a": 1, "b": 2, "c": 3 } p = instantiate_using(Pair, args) assert p.a == 1 assert p.b == 2
def get_search_algo(name: str, **kwargs) -> SearchAlgorithm: """Return the search algorithm class with the given name.""" name_to_cls = { "GA": GeneticAlgorithm, "SA": SimulatedAnnealing, # "ES": EvolutionaryStrategy, } _cls = name_to_cls.get(name, None) if _cls is None: raise ValueError( "No search algo '{nm}'. Supported names: {lst}.".format( nm=name, lst=list(name_to_cls.keys()))) return instantiate_using(_cls, kwargs)
def get_selector(name: str, **kwargs) -> Selector: """Get the selector class with the given name.""" name_to_cls = { "roulette": FitnessProportionate, "tournament": Tournament, "lexicase": Lexicase, "elite": Elite, } _cls = name_to_cls.get(name, None) if _cls is None: raise ValueError("No selector '{nm}'. Supported names: {lst}.".format( nm=name, lst=list(name_to_cls.keys()))) return instantiate_using(_cls, kwargs)
def get_search_algo(name: str, **kwargs) -> SearchAlgorithm: """Return the search algorithm class with the given name.""" name_to_cls = { "GA": GeneticAlgorithm, "SA": SimulatedAnnealing, # "ES": EvolutionaryStrategy, } _cls = name_to_cls.get(name, None) if _cls is None: raise ValueError("No search algo '{nm}'. Supported names: {lst}.".format( nm=name, lst=list(name_to_cls.keys()) )) return instantiate_using(_cls, kwargs)
def get_selector(name: str, **kwargs) -> Selector: """Get the selector class with the given name.""" name_to_cls = { "roulette": FitnessProportionate, "tournament": Tournament, "lexicase": Lexicase, "epsilon-lexicase": Lexicase(epsilon=True), "elite": Elite, } selector = name_to_cls.get(name, None) if selector is None: raise ValueError("No selector '{nm}'. Supported names: {lst}.".format( nm=name, lst=list(name_to_cls.keys()) )) if isinstance(selector, type): selector = instantiate_using(selector, kwargs) return selector
def test_instantiate_using_optional(): args = {"this": True} tmt = instantiate_using(ThisMaybeThat, args) assert tmt.this assert tmt.that == "default"
def test_instantiate_using_insufficient(): args = {"b": 2} with pytest.raises(TypeError): p = instantiate_using(Pair, args)
def test_instantiate_using_extra(): args = {"a": 1, "b": 2, "c": 3} p = instantiate_using(Pair, args) assert p.a == 1 assert p.b == 2
def test_instantiate_using_exact(): args = {"a": 1, "b": 2} p = instantiate_using(Pair, args) assert p.a == 1 assert p.b == 2
def test_instantiate_using_insufficient(): args = { "b": 2 } with pytest.raises(TypeError): p = instantiate_using(Pair, args)