예제 #1
0
파일: factory.py 프로젝트: dimdimadi/evogil
def load_problem_config(
    algo: str,
    problem: str,
    config: Dict[str, str],
    all_drivers: List[str] = None,
    driver_pos: int = 0,
):
    """
    Algorithm (with optional sub-drivers) + problem config
        example key: (SPEA2, ackley)
        example key: (SPEA2, zdt1)
        example key: (HGS, ackley)
        example key: (HGS, zdt1)
        example key: (IMGA, ackley)
        example key: (IMGA, zdt1)
        example key: (HGS, SPEA2,           ackley)

        example key: (IMGA, HGS, SPEA2,           ackley)
        example key: (IMGA, HGS, SPEA2,           zdt1)
    """
    all_drivers = [] if all_drivers is None else all_drivers
    with suppress(KeyError):
        key = (algo, *tuple(all_drivers[driver_pos + 1:]), problem)
        logger.debug("Try cust_base[%s]", key)
        update = run_config.cust_base[key]
        logger.debug(
            "Dedicated problem config: %s %s %s: %s %s",
            "| by cust dict key:",
            key,
            "\n    <<",
            ", ".join(update),
            update,
        )
        config.update(update)
        logger.debug("config: %s", show_conf(config))
예제 #2
0
파일: factory.py 프로젝트: dimdimadi/evogil
def custom_init(
    algo: str,
    problem_mod: str,
    config: Dict[str, str],
    all_drivers: List[str] = None,
    driver_pos: int = None,
    problem: str = None,
):
    """
    Custom initialization functions for algorithm (and optional sub-drivers or problems)
        example fun: init_alg___SPEA2
        example fun: init_alg___HGS
        example fun: init_alg___IMGA

        example fun: init_alg___HGS__SPEA2
        example fun: init_alg___IMGA__HGS_SPEA2

        example fun: init_alg___SPEA2____ackley
        example fun: init_alg___HGS____zdt1

        example fun: init_alg___HGS__SPEA2____ackley
        example fun: init_alg___IMGA__HGS_SPEA2____zdt1
    """
    with suppress(AttributeError):
        key = "init_alg___" + algo
        if all_drivers:
            key += "__" + "_".join(all_drivers[driver_pos + 1:])
        if problem:
            key += "____" + problem
        logger.debug("Try %s(…)", key)
        updater = getattr(run_config, key)
        logger.debug("Custom initialization: %s: %s: %s", "| by algo fun:",
                     key, updater)
        updater(config, problem_mod)
        logger.debug("config: %s", show_conf(config))
예제 #3
0
파일: factory.py 프로젝트: dimdimadi/evogil
def load_algorithm_config(algo: str, config: Dict[str, str]):
    """
    Algorithm base config.
        example key: SPEA2
        example key: HGS
        example key: IMGA
    """
    with suppress(KeyError):
        key = algo
        logger.debug("Try algo_base[%s]", key)
        update = run_config.algo_base[key]
        logger.debug(
            "Algorithm config: %s: %s: %s",
            "| by algo dict key:",
            key,
            ", ".join(update),
        )
        config.update(update)
        logger.debug("config: %s", show_conf(config))
예제 #4
0
파일: factory.py 프로젝트: dimdimadi/evogil
def load_algorithm_with_subdrivers_config(algo: str, all_drivers: List[str],
                                          driver_pos: int, config: Dict[str,
                                                                        str]):
    """
    Algorithm + sub-drivers config
        example key: (SPEA2, ()          )
        example key: (HGS,   (SPEA2)     )
        example key: (IMGA,  (HGS, SPEA2))
    """
    with suppress(KeyError):
        key = (algo, tuple(all_drivers[driver_pos + 1:]))
        logger.debug("Try algo_base[%s]", key)
        update = run_config.algo_base[key]
        logger.debug(
            "Algorithms with subdrivers config: %s %s %s: %s %s",
            "| by algo dict key:",
            key,
            "\n    <<",
            ", ".join(update),
            update,
        )
        config.update(update)
        logger.debug("config: %s", show_conf(config))
예제 #5
0
def prepare(algo, problem, driver=None, all_drivers=None, driver_pos=0):
    logger = logging.getLogger(__name__)
    logger.debug("Starting preparation")

    if not all_drivers:
        all_drivers = []

    try:

        logger.debug("Preparing %s for %s", algo, problem)
        logger.debug("driver class:%s", show_partial(driver))

        algo_mod = '.'.join(['algorithms', algo, algo])
        algo_mod = import_module(algo_mod)
        algo_class = getattr(algo_mod, algo)

        problem_mod = '.'.join(['problems', problem, 'problem'])
        problem_mod = import_module(problem_mod)

        # START WITH META-CONFIG
        logger.debug("Starting with config containing meta-parameters")
        config = {
            "__metaconfig__populationsize":
            run_config.metaconfig_populationsize
        }
        logger.debug("config: %s", show_conf(config))

        ################################################################################
        # CUSTOMS FOR PROBLEM
        update = {"dims": problem_mod.dims, "fitnesses": problem_mod.fitnesses}
        logger.debug("Per-problem config: %s", update)
        config.update(update)
        logger.debug("config: %s", show_conf(config))

        ################################################################################
        descr = "DRIVER ASSIGNMENT"
        if driver:
            update = {"driver": driver}
            config.update(update)
            logger.debug("%s : %s", descr, driver)
            config.update(update)
            logger.debug("config: %s", show_conf(config))

        ################################################################################
        descr = "CUSTOMS FOR ALGORITHM"
        # example key: SPEA2
        # example key: HGS
        # example key: IMGA
        with suppress(KeyError):
            key = algo
            logger.debug("Try algo_base[%s]", key)
            update = run_config.algo_base[key]
            logger.debug("%s %s: %s: %s", descr, "| by algo dict key:", key,
                         ', '.join(update))
            config.update(update)
            logger.debug("config: %s", show_conf(config))

        ################################################################################
        descr = "CUSTOMS FOR ALGORITHM + SUBDRIVERS"
        # example key: (SPEA2, ()          )
        # example key: (HGS,   (SPEA2)     )
        # example key: (IMGA,  (HGS, SPEA2))
        with suppress(KeyError):
            key = (algo, tuple(all_drivers[driver_pos + 1:]))
            logger.debug("Try algo_base[%s]", key)
            update = run_config.algo_base[key]
            logger.debug("%s %s %s %s: %s %s", descr, "| by algo dict key:",
                         key, "\n    <<", ', '.join(update), update)
            config.update(update)
            logger.debug("config: %s", show_conf(config))

        ################################################################################
        descr = "CUSTOMS FOR PARENTS + ALGORITHM"
        # example key: ((IMGA, HGS), SPEA2)
        # example key: ((IMGA,),     HGS  )
        # example key: ((),          IMGA )
        with suppress(KeyError):
            key = (tuple(all_drivers[:driver_pos]), algo)
            logger.debug("Try algo_base[%s]", key)
            update = run_config.algo_base[key]
            logger.debug("%s %s %s %s: %s %s", descr, "| by algo dict key:",
                         key, "\n    <<", ', '.join(update), update)
            config.update(update)
            logger.debug("config: %s", show_conf(config))

        ################################################################################
        descr = "CUSTOMS FOR PARENTS + ALGORITHM + SUBDRIVERS"
        # example key: ((IMGA, HGS), SPEA2, ()          )
        # example key: ((IMGA,),     HGS,   (SPEA2)     )
        # example key: ((),          IMGA,  (HGS, SPEA2))
        with suppress(KeyError):
            key = (tuple(all_drivers[:driver_pos]), algo,
                   tuple(all_drivers[driver_pos + 1:]))
            logger.debug("Try algo_base[%s]", key)
            update = run_config.algo_base[key]
            logger.debug("%s %s %s %s: %s %s", descr, "| by algo dict key:",
                         key, "\n    <<", ', '.join(update), update)
            config.update(update)
            logger.debug("config: %s", show_conf(config))

        ################################################################################
        descr = "CUSTOMS (simpl)"
        # example key: (SPEA2, ackley)
        # example key: (SPEA2, zdt1)
        # example key: (HGS, ackley)
        # example key: (HGS, zdt1)
        # example key: (IMGA, ackley)
        # example key: (IMGA, zdt1)
        with suppress(KeyError):
            key = (algo, problem)
            logger.debug("Try cust_base[%s]", key)
            update = run_config.cust_base[key]
            logger.debug("%s %s %s %s: %s %s", descr, "| by cust dict key:",
                         key, "\n    <<", ', '.join(update), update)
            config.update(update)
            logger.debug("config: %s", show_conf(config))

        ################################################################################
        descr = "CUSTOMS"
        # example key: ((IMGA, HGS), SPEA2, (),           ackley)
        # example key: ((IMGA, HGS), SPEA2, (),           zdt1)
        # example key: ((IMGA),      HGS,   (SPEA2),      ackley)
        # example key: ((IMGA),      HGS,   (SPEA2),      zdt1)
        # example key: ((),          IMGA,  (HGS, SPEA2), ackley)
        # example key: ((),          IMGA,  (HGS, SPEA2), zdt1)
        with suppress(KeyError):
            key = (tuple(all_drivers[:driver_pos]), algo,
                   tuple(all_drivers[driver_pos + 1:]), problem)
            logger.debug("Try cust_base[%s]", key)
            update = run_config.cust_base[key]
            logger.debug("%s %s %s %s: %s %s", descr, "| by cust dict key:",
                         key, "\n    <<", ', '.join(update), update)
            config.update(update)
            logger.debug("config: %s", show_conf(config))

        ################################################################################
        descr = "CUSTOMS FOR ALGORITHM"
        # example fun: init_alg___SPEA2
        # example fun: init_alg___HGS
        # example fun: init_alg___IMGA
        with suppress(AttributeError):
            key = "init_alg___" + algo
            logger.debug("Try %s(…)", key)
            updater = getattr(run_config, key)
            logger.debug("%s %s: %s: %s", descr, "| by algo fun:", key,
                         updater)
            updater(config, problem_mod)
            logger.debug("config: %s", show_conf(config))

        ################################################################################
        descr = "CUSTOMS FOR ALGORITHM + SUBDRIVERS"
        # example fun: init_alg___HGS__SPEA2
        # example fun: init_alg___IMGA__HGS_SPEA2
        with suppress(AttributeError):
            key = "init_alg___" + algo + '__' + '_'.join(
                all_drivers[driver_pos + 1:])
            logger.debug("Try %s(…)", key)
            updater = getattr(run_config, key)
            logger.debug("%s %s: %s: %s", descr, "| by algo fun:", key,
                         updater)
            updater(config, problem_mod)
            logger.debug("config: %s", show_conf(config))

        ################################################################################
        descr = "CUSTOMS FOR PARENTS + ALGORITHM"
        # example fun: init_alg_IMGA_HGS___SPEA2
        # example fun: init_alg_IMGA___HGS
        with suppress(AttributeError):
            key = "init_alg_" + '_'.join(
                all_drivers[:driver_pos]) + '___' + algo
            logger.debug("Try %s(…)", key)
            updater = getattr(run_config, key)
            logger.debug("%s %s: %s: %s", descr, "| by algo fun:", key,
                         updater)
            updater(config, problem_mod)
            logger.debug("config: %s", show_conf(config))

        ################################################################################
        descr = "CUSTOMS FOR PARENTS + ALGORITHM + SUBDRIVERS"
        # example fun: init_alg_IMGA___HGS__SPEA2
        with suppress(AttributeError):
            key = "init_alg_" + '_'.join(
                all_drivers[:driver_pos]) + '___' + algo + '__' + '_'.join(
                    all_drivers[driver_pos + 1:])
            logger.debug("Try %s(…)", key)
            updater = getattr(run_config, key)
            logger.debug("%s %s: %s: %s", descr, "| by algo fun:", key,
                         updater)
            updater(config, problem_mod)
            logger.debug("config: %s", show_conf(config))

        ################################################################################
        descr = "CUSTOMS FOR PROBLEM"
        # example key: ackley
        # example key: zdt1
        with suppress(KeyError):
            key = problem
            logger.debug("Try prob_base[%s]", key)
            update = run_config.prob_base[key]
            logger.debug("%s %s: %s", descr, "| by prob dict key:", key,
                         "\n    <<", ', '.join(run_config.prob_base[key]),
                         update)
            config.update(update)
            logger.debug("config: %s", show_conf(config))

        ################################################################################
        descr = "CUSTOMS FOR PROBLEM"
        # example fun: init_prob____ackley
        with suppress(AttributeError):
            key = "init_prob____" + problem
            logger.debug("Try %s(…)", key)
            updater = getattr(run_config, key)
            logger.debug("%s %s: %s: %s", descr, "| by prob fun:", key,
                         updater)
            updater(config, problem_mod)
            logger.debug("config: %s", show_conf(config))

        ################################################################################
        descr = "CUSTOMS"
        # example fun: init_cust___SPEA2____ackley  # configure SPEA2 for ackley
        # example fun: init_cust___SPEA2____zdt1
        # example fun: init_cust_IMGA_HGS___SPEA2____ackley  # configure SPEA2 for ackley when under IMGA+HGS
        # example fun: init_cust_IMGA_HGS___SPEA2____zdt1
        # example fun: init_cust_IMGA___HGS__SPEA2____ackley  # configure HGS for ackley when under IMGA w/ SPEA2 driver
        # example fun: init_cust_IMGA___HGS__SPEA2____zdt1
        # example fun: init_cust___IMGA__HGS_SPEA2____ackley  # configure IMGA for ackley w/ HGS+SPEA2 driver
        # example fun: init_cust___IMGA__HGS_SPEA2____zdt1
        with suppress(AttributeError):
            overdriver = ''.join('_' + x for x in all_drivers[:driver_pos])
            algo_pref = "___" + algo
            subdrivers = ""
            if all_drivers[driver_pos + 1:]:
                subdrivers = "__" + '_'.join(all_drivers[driver_pos + 1:])
            problem_suf = "____" + problem
            key = "init_cust" + overdriver + algo_pref + subdrivers + problem_suf
            logger.debug("Try %s(…)", key)
            updater = getattr(run_config, key)
            logger.debug("%s %s: %s: %s", descr, "| by cust fun:", key,
                         updater)
            updater(config, problem_mod)
            logger.debug("config: %s", show_conf(config))

        ################################################################################
        descr = "GENERATING POPULATION"
        if "population" not in config:
            initial_population = gen_population(64, problem_mod.dims)
            update = {"population": initial_population}
            logger.debug("%s (size: %s, dims: %s): %s", descr,
                         config["__metaconfig__populationsize"],
                         problem_mod.dims, initial_population)
            config.update(update)

        ################################################################################
        # DROPPING TRASH
        if logger.isEnabledFor(logging.DEBUG):
            logger.debug("dropping trash from config: %s", {
                k: v
                for k, v in config.items() if k.startswith('__metaconfig__')
            })
        config = {
            k: v
            for k, v in config.items() if not k.startswith('__metaconfig__')
        }

        try:
            algo_class(**config)
        except Exception as e:
            logger.exception("Class creation error.", exc_info=e)
            raise e
        else:
            logger.debug(
                "Preparing (algo=%s, problem=%s, driver=%s, all_drivers=%s, driver_pos=%d) done, class obj created",
                algo, problem, show_partial(driver), all_drivers, driver_pos)

        instance = partial(algo_class, **config)
        logger.debug("Dropping this dummy obj, returning partial instead: %s",
                     show_partial(instance))
        return instance, problem_mod

    except NotViableConfiguration as e:
        raise e

    except Exception as e:
        logger.exception("Exception while preparing. "
                         "algo={algo} "
                         "problem={problem} "
                         "driver={driver} "
                         "all_drivers={all_drivers} "
                         "driver_pos={driver_pos}".format(
                             algo=algo,
                             problem=problem,
                             driver=show_partial(driver),
                             all_drivers=all_drivers,
                             driver_pos=driver_pos),
                         exc_info=e)
        raise e
예제 #6
0
def prepare(algo, problem, driver=None, all_drivers=None, driver_pos=0):
    logger = logging.getLogger(__name__)
    logger.debug("Starting preparation")

    if not all_drivers:
        all_drivers = []

    try:

        logger.debug("Preparing %s for %s", algo, problem)
        logger.debug("driver class:%s", show_partial(driver))

        algo_mod = '.'.join(['algorithms', algo, algo])
        algo_mod = import_module(algo_mod)
        algo_class = getattr(algo_mod, algo)

        problem_mod = '.'.join(['problems', problem, 'problem'])
        problem_mod = import_module(problem_mod)

        # START WITH META-CONFIG
        logger.debug("Starting with config containing meta-parameters")
        config = {
            "__metaconfig__populationsize": run_config.metaconfig_populationsize
        }
        logger.debug("config: %s", show_conf(config))

        ################################################################################
        # CUSTOMS FOR PROBLEM
        update = {"dims": problem_mod.dims, "fitnesses": problem_mod.fitnesses}
        logger.debug("Per-problem config: %s", update)
        config.update(update)
        logger.debug("config: %s", show_conf(config))

        ################################################################################
        descr = "DRIVER ASSIGNMENT"
        if driver:
            update = {"driver": driver}
            config.update(update)
            logger.debug("%s : %s", descr, driver)
            config.update(update)
            logger.debug("config: %s", show_conf(config))

        ################################################################################
        descr = "CUSTOMS FOR ALGORITHM"
        # example key: SPEA2
        # example key: HGS
        # example key: IMGA
        with suppress(KeyError):
            key = algo
            logger.debug("Try algo_base[%s]", key)
            update = run_config.algo_base[key]
            logger.debug("%s %s: %s: %s",
                         descr,
                         "| by algo dict key:", key, ', '.join(update))
            config.update(update)
            logger.debug("config: %s", show_conf(config))

        ################################################################################
        descr = "CUSTOMS FOR ALGORITHM + SUBDRIVERS"
        # example key: (SPEA2, ()          )
        # example key: (HGS,   (SPEA2)     )
        # example key: (IMGA,  (HGS, SPEA2))
        with suppress(KeyError):
            key = (algo,
                   tuple(all_drivers[driver_pos + 1:])
                   )
            logger.debug("Try algo_base[%s]", key)
            update = run_config.algo_base[key]
            logger.debug("%s %s %s %s: %s %s",
                         descr,
                         "| by algo dict key:", key, "\n    <<", ', '.join(update),
                         update)
            config.update(update)
            logger.debug("config: %s", show_conf(config))

        ################################################################################
        descr = "CUSTOMS FOR PARENTS + ALGORITHM"
        # example key: ((IMGA, HGS), SPEA2)
        # example key: ((IMGA,),     HGS  )
        # example key: ((),          IMGA )
        with suppress(KeyError):
            key = (tuple(all_drivers[:driver_pos]),
                   algo
                   )
            logger.debug("Try algo_base[%s]", key)
            update = run_config.algo_base[key]
            logger.debug("%s %s %s %s: %s %s",
                         descr,
                         "| by algo dict key:", key, "\n    <<", ', '.join(update),
                         update)
            config.update(update)
            logger.debug("config: %s", show_conf(config))

        ################################################################################
        descr = "CUSTOMS FOR PARENTS + ALGORITHM + SUBDRIVERS"
        # example key: ((IMGA, HGS), SPEA2, ()          )
        # example key: ((IMGA,),     HGS,   (SPEA2)     )
        # example key: ((),          IMGA,  (HGS, SPEA2))
        with suppress(KeyError):
            key = (tuple(all_drivers[:driver_pos]),
                   algo,
                   tuple(all_drivers[driver_pos + 1:])
                   )
            logger.debug("Try algo_base[%s]", key)
            update = run_config.algo_base[key]
            logger.debug("%s %s %s %s: %s %s",
                         descr,
                         "| by algo dict key:", key, "\n    <<", ', '.join(update),
                         update)
            config.update(update)
            logger.debug("config: %s", show_conf(config))

        ################################################################################
        descr = "CUSTOMS (simpl)"
        # example key: (SPEA2, ackley)
        # example key: (SPEA2, zdt1)
        # example key: (HGS, ackley)
        # example key: (HGS, zdt1)
        # example key: (IMGA, ackley)
        # example key: (IMGA, zdt1)
        with suppress(KeyError):
            key = (algo,
                   problem
                   )
            logger.debug("Try cust_base[%s]", key)
            update = run_config.cust_base[key]
            logger.debug("%s %s %s %s: %s %s",
                         descr,
                         "| by cust dict key:", key, "\n    <<", ', '.join(update),
                         update)
            config.update(update)
            logger.debug("config: %s", show_conf(config))

        ################################################################################
        descr = "CUSTOMS"
        # example key: ((IMGA, HGS), SPEA2, (),           ackley)
        # example key: ((IMGA, HGS), SPEA2, (),           zdt1)
        # example key: ((IMGA),      HGS,   (SPEA2),      ackley)
        # example key: ((IMGA),      HGS,   (SPEA2),      zdt1)
        # example key: ((),          IMGA,  (HGS, SPEA2), ackley)
        # example key: ((),          IMGA,  (HGS, SPEA2), zdt1)
        with suppress(KeyError):
            key = (tuple(all_drivers[:driver_pos]),
                   algo,
                   tuple(all_drivers[driver_pos + 1:]),
                   problem
                   )
            logger.debug("Try cust_base[%s]", key)
            update = run_config.cust_base[key]
            logger.debug("%s %s %s %s: %s %s",
                         descr,
                         "| by cust dict key:", key, "\n    <<", ', '.join(update),
                         update)
            config.update(update)
            logger.debug("config: %s", show_conf(config))

        ################################################################################
        descr = "CUSTOMS FOR ALGORITHM"
        # example fun: init_alg___SPEA2
        # example fun: init_alg___HGS
        # example fun: init_alg___IMGA
        with suppress(AttributeError):
            key = "init_alg___" + algo
            logger.debug("Try %s(…)", key)
            updater = getattr(run_config, key)
            logger.debug("%s %s: %s: %s",
                         descr,
                         "| by algo fun:",
                         key,
                         updater
                         )
            updater(config, problem_mod)
            logger.debug("config: %s", show_conf(config))

        ################################################################################
        descr = "CUSTOMS FOR ALGORITHM + SUBDRIVERS"
        # example fun: init_alg___HGS__SPEA2
        # example fun: init_alg___IMGA__HGS_SPEA2
        with suppress(AttributeError):
            key = "init_alg___" + algo + '__' + '_'.join(all_drivers[driver_pos + 1:])
            logger.debug("Try %s(…)", key)
            updater = getattr(run_config, key)
            logger.debug("%s %s: %s: %s",
                         descr,
                         "| by algo fun:",
                         key,
                         updater
                         )
            updater(config, problem_mod)
            logger.debug("config: %s", show_conf(config))

        ################################################################################
        descr = "CUSTOMS FOR PARENTS + ALGORITHM"
        # example fun: init_alg_IMGA_HGS___SPEA2
        # example fun: init_alg_IMGA___HGS
        with suppress(AttributeError):
            key = "init_alg_" + '_'.join(all_drivers[:driver_pos]) + '___' + algo
            logger.debug("Try %s(…)", key)
            updater = getattr(run_config, key)
            logger.debug("%s %s: %s: %s",
                         descr,
                         "| by algo fun:",
                         key,
                         updater
                         )
            updater(config, problem_mod)
            logger.debug("config: %s", show_conf(config))

        ################################################################################
        descr = "CUSTOMS FOR PARENTS + ALGORITHM + SUBDRIVERS"
        # example fun: init_alg_IMGA___HGS__SPEA2
        with suppress(AttributeError):
            key = "init_alg_" + '_'.join(all_drivers[:driver_pos]) + '___' + algo + '__' + '_'.join(
                all_drivers[driver_pos + 1:])
            logger.debug("Try %s(…)", key)
            updater = getattr(run_config, key)
            logger.debug("%s %s: %s: %s",
                         descr,
                         "| by algo fun:",
                         key,
                         updater
                         )
            updater(config, problem_mod)
            logger.debug("config: %s", show_conf(config))

        ################################################################################
        descr = "CUSTOMS FOR PROBLEM"
        # example key: ackley
        # example key: zdt1
        with suppress(KeyError):
            key = problem
            logger.debug("Try prob_base[%s]", key)
            update = run_config.prob_base[key]
            logger.debug("%s %s: %s",
                         descr,
                         "| by prob dict key:", key, "\n    <<", ', '.join(run_config.prob_base[key]),
                         update)
            config.update(update)
            logger.debug("config: %s", show_conf(config))

        ################################################################################
        descr = "CUSTOMS FOR PROBLEM"
        # example fun: init_prob____ackley
        with suppress(AttributeError):
            key = "init_prob____" + problem
            logger.debug("Try %s(…)", key)
            updater = getattr(run_config, key)
            logger.debug("%s %s: %s: %s",
                         descr,
                         "| by prob fun:",
                         key,
                         updater
                         )
            updater(config, problem_mod)
            logger.debug("config: %s", show_conf(config))

        ################################################################################
        descr = "CUSTOMS"
        # example fun: init_cust___SPEA2____ackley  # configure SPEA2 for ackley
        # example fun: init_cust___SPEA2____zdt1
        # example fun: init_cust_IMGA_HGS___SPEA2____ackley  # configure SPEA2 for ackley when under IMGA+HGS
        # example fun: init_cust_IMGA_HGS___SPEA2____zdt1
        # example fun: init_cust_IMGA___HGS__SPEA2____ackley  # configure HGS for ackley when under IMGA w/ SPEA2 driver
        # example fun: init_cust_IMGA___HGS__SPEA2____zdt1
        # example fun: init_cust___IMGA__HGS_SPEA2____ackley  # configure IMGA for ackley w/ HGS+SPEA2 driver
        # example fun: init_cust___IMGA__HGS_SPEA2____zdt1
        with suppress(AttributeError):
            overdriver = ''.join('_' + x for x in all_drivers[:driver_pos])
            algo_pref = "___" + algo
            subdrivers = ""
            if all_drivers[driver_pos + 1:]:
                subdrivers = "__" + '_'.join(all_drivers[driver_pos + 1:])
            problem_suf = "____" + problem
            key = "init_cust" + overdriver + algo_pref + subdrivers + problem_suf
            logger.debug("Try %s(…)", key)
            updater = getattr(run_config, key)
            logger.debug("%s %s: %s: %s",
                         descr,
                         "| by cust fun:",
                         key,
                         updater
                         )
            updater(config, problem_mod)
            logger.debug("config: %s", show_conf(config))

        ################################################################################
        descr = "GENERATING POPULATION"
        if "population" not in config:
            initial_population = gen_population(64, problem_mod.dims)
            update = {"population": initial_population}
            logger.debug("%s (size: %s, dims: %s): %s",
                         descr,
                         config["__metaconfig__populationsize"],
                         problem_mod.dims,
                         initial_population)
            config.update(update)

        ################################################################################
        # DROPPING TRASH
        if logger.isEnabledFor(logging.DEBUG):
            logger.debug("dropping trash from config: %s",
                         {k: v
                          for k, v
                          in config.items()
                          if k.startswith('__metaconfig__')
                          })
        config = {k: v
                  for k, v
                  in config.items()
                  if not k.startswith('__metaconfig__')
                  }

        try:
            algo_class(**config)
        except Exception as e:
            logger.exception("Class creation error.", exc_info=e)
            raise e
        else:
            logger.debug(
                "Preparing (algo=%s, problem=%s, driver=%s, all_drivers=%s, driver_pos=%d) done, class obj created",
                algo,
                problem,
                show_partial(driver),
                all_drivers,
                driver_pos)

        instance = partial(algo_class, **config)
        logger.debug("Dropping this dummy obj, returning partial instead: %s", show_partial(instance))
        return instance, problem_mod

    except NotViableConfiguration as e:
        raise e

    except Exception as e:
        logger.exception(
            "Exception while preparing. "
            "algo={algo} "
            "problem={problem} "
            "driver={driver} "
            "all_drivers={all_drivers} "
            "driver_pos={driver_pos}".format(algo=algo,
                                             problem=problem,
                                             driver=show_partial(driver),
                                             all_drivers=all_drivers,
                                             driver_pos=driver_pos),
            exc_info=e)
        raise e
예제 #7
0
파일: factory.py 프로젝트: dimdimadi/evogil
def load_obligatory_problem_parameters(config: Dict[str, str], problem_mod):
    update = {"dims": problem_mod.dims, "fitnesses": problem_mod.fitnesses}
    logger.debug("Per-problem config: %s", update)
    config.update(update)
    logger.debug("config: %s", show_conf(config))
예제 #8
0
파일: factory.py 프로젝트: dimdimadi/evogil
def prepare_with_driver(
    algo: str,
    problem: str,
    driver=None,
    all_drivers: List[str] = None,
    driver_pos: int = 0,
):
    logger.debug("Starting preparation")

    if not all_drivers:
        all_drivers = []

    try:
        logger.debug("Preparing %s for %s", algo, problem)
        logger.debug("driver class:%s", show_partial(driver))

        algo_class = prepare_algorithm_class(algo)

        problem_mod = prepare_problem_class(problem)

        config = {}

        load_obligatory_problem_parameters(config, problem_mod)

        if driver:
            update = {"driver": driver}
            config.update(update)
            logger.debug("Driver assignment: %s", driver)
            config.update(update)
            logger.debug("config: %s", show_conf(config))

            message_adapter_factory = prepare_message_adapter_class(
                algo, all_drivers, driver_pos)
            config.update(
                {"driver_message_adapter_factory": message_adapter_factory})

        load_algorithm_config(algo, config)
        load_algorithm_with_subdrivers_config(algo, all_drivers, driver_pos,
                                              config)

        load_problem_config(algo, problem, config)
        load_problem_config(algo, problem, config, all_drivers, driver_pos)

        custom_init(algo, problem_mod, config)
        custom_init(algo, problem_mod, config, all_drivers, driver_pos)
        custom_init(algo, problem_mod, config, problem=problem)
        custom_init(algo, problem_mod, config, all_drivers, driver_pos,
                    problem)

        load_init_population(problem_mod, config)

        logger.debug(
            "Preparing (algo=%s, problem=%s, driver=%s, all_drivers=%s, driver_pos=%d) done, class obj created",
            algo,
            problem,
            show_partial(driver),
            all_drivers,
            driver_pos,
        )

        instance = partial(algo_class, **config)
        logger.debug(
            "Dropping this dummy obj, returning partial instead: %s",
            show_partial(instance),
        )
        return instance, problem_mod

    except NotViableConfiguration as e:
        raise e

    except Exception as e:
        logger.exception(
            "Exception while preparing. "
            "algo={algo} "
            "problem={problem} "
            "driver={driver} "
            "all_drivers={all_drivers} "
            "driver_pos={driver_pos}".format(
                algo=algo,
                problem=problem,
                driver=show_partial(driver),
                all_drivers=all_drivers,
                driver_pos=driver_pos,
            ),
            exc_info=e,
        )
        raise e