Ejemplo n.º 1
0
def set_state(state):
    """
    Given a dictionary representing the state of an evolutionary run, set all
    aspects of the system to re-create that state. The state includes the
    current population, the current random state, the parameters dictionary,
    the stats dictionary, and all lists in the utilities.stats.trackers module.
    
    Sets all aspects of the system and then returns a population of
    individuals at the current generation.
    
    :param state: The complete state of a run.
    :return: A population of individuals.
    """

    from algorithm.parameters import params
    from utilities.algorithm.initialise_run import set_param_imports
    from stats.stats import stats
    from utilities.stats import trackers
    from time import time

    # Set random state.
    random.setstate(state['random_state'])

    # Set stats.
    for stat in state['stats']:
        stats[stat] = state['stats'][stat]

    # Set trackers.
    for tracker in state['trackers']:
        setattr(trackers, tracker, state['trackers'][tracker])

    # Set parameters.
    for param in state['params']:
        params[param] = state['params'][param]

    # Set correct param imports for specified function options, including
    # error metrics and fitness functions.
    set_param_imports()

    # Set time adjustment to account for old time.
    stats['time_adjust'] = time() - state['time']

    return state['individuals']
Ejemplo n.º 2
0
def set_params(command_line_args, create_files=True):
    """
    This function parses all command line arguments specified by the user.
    If certain parameters are not set then defaults are used (e.g. random
    seeds, elite size). Sets the correct imports given command line
    arguments. Sets correct grammar file and fitness function. Also
    initialises save folders and tracker lists in utilities.trackers.

    :param command_line_args: Command line arguments specified by the user.
    :return: Nothing.
    """

    from utilities.algorithm.initialise_run import initialise_run_params
    from utilities.algorithm.initialise_run import set_param_imports
    from utilities.fitness.math_functions import return_one_percent
    from utilities.algorithm.command_line_parser import parse_cmd_args
    from utilities.stats import trackers, clean_stats
    from representation import grammar

    cmd_args, unknown = parse_cmd_args(command_line_args)

    if unknown:
        # We currently do not parse unknown parameters. Raise error.
        s = "algorithm.parameters.set_params\nError: " \
            "unknown parameters: %s\nYou may wish to check the spelling, " \
            "add code to recognise this parameter, or use " \
            "--extra_parameters" % str(unknown)
        raise Exception(s)

    # LOAD PARAMETERS FILE
    # NOTE that the parameters file overwrites all previously set parameters.
    if 'PARAMETERS' in cmd_args:
        load_params(path.join("..", "parameters", cmd_args['PARAMETERS']))

    # Join original params dictionary with command line specified arguments.
    # NOTE that command line arguments overwrite all previously set parameters.
    params.update(cmd_args)

    if params['LOAD_STATE']:
        # Load run from state.
        from utilities.algorithm.state import load_state

        # Load in state information.
        individuals = load_state(params['LOAD_STATE'])

        # Set correct search loop.
        from algorithm.search_loop import search_loop_from_state
        params['SEARCH_LOOP'] = search_loop_from_state

        # Set population.
        setattr(trackers, "state_individuals", individuals)

    else:
        if params['REPLACEMENT'].split(".")[-1] == "steady_state":
            # Set steady state step and replacement.
            params['STEP'] = "steady_state_step"
            params['GENERATION_SIZE'] = 2

        else:
            # Elite size is set to either 1 or 1% of the population size,
            # whichever is bigger if no elite size is previously set.
            if params['ELITE_SIZE'] is None:
                params['ELITE_SIZE'] = return_one_percent(
                    1, params['POPULATION_SIZE'])

            # Set the size of a generation
            params['GENERATION_SIZE'] = params['POPULATION_SIZE'] - \
                                        params['ELITE_SIZE']

        # Initialise run lists and folders before we set imports.r
        initialise_run_params(create_files)

        # Set correct param imports for specified function options, including
        # error metrics and fitness functions.
        set_param_imports()

        # Clean the stats dict to remove unused stats.
        clean_stats.clean_stats()

        # Set GENOME_OPERATIONS automatically for faster linear operations.
        if (params['CROSSOVER'].representation == "subtree"
                or params['MUTATION'].representation == "subtree"):
            params['GENOME_OPERATIONS'] = False
        else:
            params['GENOME_OPERATIONS'] = True

        # Ensure correct operators are used if multiple fitness functions used.
        if hasattr(params['FITNESS_FUNCTION'], 'multi_objective'):

            # Check that multi-objective compatible selection is specified.
            if not hasattr(params['SELECTION'], "multi_objective"):
                s = "algorithm.parameters.set_params\n" \
                    "Error: multi-objective compatible selection " \
                    "operator not specified for use with multiple " \
                    "fitness functions."
                raise Exception(s)

            if not hasattr(params['REPLACEMENT'], "multi_objective"):

                # Check that multi-objective compatible replacement is
                # specified.
                if not hasattr(params['REPLACEMENT'], "multi_objective"):
                    s = "algorithm.parameters.set_params\n" \
                        "Error: multi-objective compatible replacement " \
                        "operator not specified for use with multiple " \
                        "fitness functions."
                    raise Exception(s)

        # Parse grammar file and set grammar class.
        params['BNF_GRAMMAR'] = grammar.Grammar(
            path.join("..", "grammars", params['GRAMMAR_FILE']))

        # Population loading for seeding runs (if specified)
        if params['TARGET_SEED_FOLDER']:

            # Import population loading function.
            from operators.initialisation import load_population

            # A target folder containing seed individuals has been given.
            params['SEED_INDIVIDUALS'] = load_population(
                params['TARGET_SEED_FOLDER'])

        elif params['REVERSE_MAPPING_TARGET']:
            # A single seed phenotype has been given. Parse and run.

            # Import GE LR Parser.
            from scripts import GE_LR_parser

            # Parse seed individual and store in params.
            params['SEED_INDIVIDUALS'] = [GE_LR_parser.main()]
Ejemplo n.º 3
0
def set_params(command_line_args, create_files=True):
    """
    This function parses all command line arguments specified by the user.
    If certain parameters are not set then defaults are used (e.g. random
    seeds, elite size). Sets the correct imports given command line
    arguments. Sets correct grammar file and fitness function. Also
    initialises save folders and tracker lists in utilities.trackers.

    :param command_line_args: Command line arguments specified by the user.
    :return: Nothing.
    """

    from utilities.algorithm.initialise_run import initialise_run_params
    from utilities.algorithm.initialise_run import set_param_imports
    from utilities.fitness.math_functions import return_one_percent
    from utilities.algorithm.command_line_parser import parse_cmd_args
    from utilities.stats import trackers, clean_stats
    from representation import grammar

    cmd_args, unknown = parse_cmd_args(command_line_args)

    if unknown:
        # We currently do not parse unknown parameters. Raise error.
        s = "algorithm.parameters.set_params\nError: " \
            "unknown parameters: %s\nYou may wish to check the spelling, " \
            "add code to recognise this parameter, or use " \
            "--extra_parameters" % str(unknown)
        raise Exception(s)

    # LOAD PARAMETERS FILE
    # NOTE that the parameters file overwrites all previously set parameters.
    if 'PARAMETERS' in cmd_args:
        load_params(path.join("..", "parameters", cmd_args['PARAMETERS']))

    # Join original params dictionary with command line specified arguments.
    # NOTE that command line arguments overwrite all previously set parameters.
    params.update(cmd_args)

    if params['LOAD_STATE']:
        # Load run from state.
        from utilities.algorithm.state import load_state

        # Load in state information.
        individuals = load_state(params['LOAD_STATE'])

        # Set correct search loop.
        from algorithm.search_loop import search_loop_from_state
        params['SEARCH_LOOP'] = search_loop_from_state

        # Set population.
        setattr(trackers, "state_individuals", individuals)

    else:
        if params['REPLACEMENT'].split(".")[-1] == "steady_state":
            # Set steady state step and replacement.
            params['STEP'] = "steady_state_step"
            params['GENERATION_SIZE'] = 2

        else:
            # Elite size is set to either 1 or 1% of the population size,
            # whichever is bigger if no elite size is previously set.
            if params['ELITE_SIZE'] is None:
                params['ELITE_SIZE'] = return_one_percent(1, params[
                    'POPULATION_SIZE'])

            # Set the size of a generation
            params['GENERATION_SIZE'] = params['POPULATION_SIZE'] - \
                                        params['ELITE_SIZE']

        # Initialise run lists and folders before we set imports.r
        initialise_run_params(create_files)

        # Set correct param imports for specified function options, including
        # error metrics and fitness functions.
        set_param_imports()

        # Clean the stats dict to remove unused stats.
        clean_stats.clean_stats()

        # Set GENOME_OPERATIONS automatically for faster linear operations.
        if (params['CROSSOVER'].representation == "subtree" or
            params['MUTATION'].representation == "subtree"):
            params['GENOME_OPERATIONS'] = False
        else:
            params['GENOME_OPERATIONS'] = True

        # Ensure correct operators are used if multiple fitness functions used.
        if hasattr(params['FITNESS_FUNCTION'], 'multi_objective'):

            # Check that multi-objective compatible selection is specified.
            if not hasattr(params['SELECTION'], "multi_objective"):
                s = "algorithm.parameters.set_params\n" \
                    "Error: multi-objective compatible selection " \
                    "operator not specified for use with multiple " \
                    "fitness functions."
                raise Exception(s)

            if not hasattr(params['REPLACEMENT'], "multi_objective"):

                # Check that multi-objective compatible replacement is
                # specified.
                if not hasattr(params['REPLACEMENT'], "multi_objective"):
                    s = "algorithm.parameters.set_params\n" \
                        "Error: multi-objective compatible replacement " \
                        "operator not specified for use with multiple " \
                        "fitness functions."
                    raise Exception(s)

        # Parse grammar file and set grammar class.
        params['BNF_GRAMMAR'] = grammar.Grammar(path.join("..", "grammars",
                                                params['GRAMMAR_FILE']))

        # Population loading for seeding runs (if specified)
        if params['TARGET_SEED_FOLDER']:

            # Import population loading function.
            from operators.initialisation import load_population

            # A target folder containing seed individuals has been given.
            params['SEED_INDIVIDUALS'] = load_population(
                params['TARGET_SEED_FOLDER'])

        elif params['REVERSE_MAPPING_TARGET']:
            # A single seed phenotype has been given. Parse and run.

            # Import GE LR Parser.
            from scripts import GE_LR_parser

            # Parse seed individual and store in params.
            params['SEED_INDIVIDUALS'] = [GE_LR_parser.main()]
Ejemplo n.º 4
0
def set_params(command_line_args, create_files=True):
    """
    This function parses all command line arguments specified by the user.
    If certain parameters are not set then defaults are used (e.g. random
    seeds, elite size). Sets the correct imports given command line
    arguments. Sets correct grammar file and fitness function. Also
    initialises save folders and tracker lists in utilities.trackers.

    :param command_line_args: Command line arguments specified by the user.
    :return: Nothing.
    """

    from utilities.algorithm.initialise_run import initialise_run_params
    from utilities.algorithm.initialise_run import set_param_imports
    from utilities.fitness.math_functions import return_one_percent
    from representation import grammar
    import utilities.algorithm.command_line_parser as parser
    from utilities.stats import trackers, clean_stats

    cmd_args, unknown = parser.parse_cmd_args(command_line_args)

    if unknown:
        # We currently do not parse unknown parameters. Raise error.
        s = "algorithm.parameters.set_params\nError: " \
            "unknown parameters: %s\nYou may wish to check the spelling, " \
            "add code to recognise this parameter, or use " \
            "--extra_parameters" % str(unknown)
        raise Exception(s)

    # LOAD PARAMETERS FILE
    # NOTE that the parameters file overwrites all previously set parameters.
    if 'PARAMETERS' in cmd_args:
        load_params(path.join("..", "parameters", cmd_args['PARAMETERS']))

    # Join original params dictionary with command line specified arguments.
    # NOTE that command line arguments overwrite all previously set parameters.
    params.update(cmd_args)

    if params['LOAD_STATE']:
        # Load run from state.
        from utilities.algorithm.state import load_state

        # Load in state information.
        individuals = load_state(params['LOAD_STATE'])

        # Set correct search loop.
        from algorithm.search_loop import search_loop_from_state
        params['SEARCH_LOOP'] = search_loop_from_state

        # Set population.
        setattr(trackers, "state_individuals", individuals)

    else:
        if params['REPLACEMENT'].split(".")[-1] == "steady_state":
            # Set steady state step and replacement.
            params['STEP'] = "steady_state_step"
            params['GENERATION_SIZE'] = 2

        else:
            # Elite size is set to either 1 or 1% of the population size,
            # whichever is bigger if no elite size is previously set.
            if params['ELITE_SIZE'] is None:
                params['ELITE_SIZE'] = return_one_percent(
                    1, params['POPULATION_SIZE'])

            # Set the size of a generation
            params['GENERATION_SIZE'] = params['POPULATION_SIZE'] - \
                                        params['ELITE_SIZE']

        # Set correct param imports for specified function options, including
        # error metrics and fitness functions.
        set_param_imports()

        # Clean the stats dict to remove unused stats.
        clean_stats.clean_stats()

        # Initialise run lists and folders
        initialise_run_params(create_files)

        # Set GENOME_OPERATIONS automatically for faster linear operations.
        if params['CROSSOVER'].representation == "linear" and \
                params['MUTATION'].representation == "linear":
            params['GENOME_OPERATIONS'] = True
        else:
            params['GENOME_OPERATIONS'] = False

        # Parse grammar file and set grammar class.
        params['BNF_GRAMMAR'] = grammar.Grammar(
            path.join("..", "grammars", params['GRAMMAR_FILE']))
Ejemplo n.º 5
0
def set_params(command_line_args):
    """
    This function parses all command line arguments specified by the user.
    If certain parameters are not set then defaults are used (e.g. random
    seeds, elite size). Sets the correct imports given command line
    arguments. Sets correct grammar file and fitness function. Also
    initialises save folders and tracker lists in utilities.trackers.
    
    :param command_line_args: Command line arguments specified by the user.
    :return: Nothing.
    """

    from utilities.algorithm.initialise_run import initialise_run_params
    from utilities.algorithm.initialise_run import set_param_imports
    from utilities.fitness.math_functions import return_percent
    from utilities.help_message import help_message
    from representation import grammar
    import getopt

    try:
        opts, args = getopt.getopt(command_line_args[1:], "",
                                   ["help", "debug", "population=",
                                    "generations=", "initialisation=",
                                    "max_init_depth=", "genome_init",
                                    "max_tree_depth=", "codon_size=",
                                    "selection=", "selection_proportion=",
                                    "tournament_size=", "crossover=",
                                    "crossover_probability=", "replacement=",
                                    "mutation=", "mutation_events=",
                                    "random_seed=", "bnf_grammar=",
                                    "dataset=", "target_string=",
                                    "verbose", "elite_size=", "save_all",
                                    "save_plots", "cache", "lookup_fitness",
                                    "lookup_bad_fitness", "mutate_duplicates",
                                    "genome_length=",
                                    "invalid_selection", "silent",
                                    "dont_lookup_fitness", "experiment_name=",
                                    "multicore", "cores=", "max_wraps=",
                                    "error_metric=", "fitness_function=",
                                    "parameters=", "step=", "search_loop="])
    except getopt.GetoptError as err:
        print("Most parameters need a value associated with them \n",
              "Run python ponyge.py --help for more info")
        print(str(err))
        exit(2)

    for opt, arg in opts:
        if opt == "--help":
            help_message()
            exit()

        # LOAD PARAMETERS FILE
        elif opt == "--parameters":
            load_params("../parameters/" + arg)

        # LOAD STEP AND SEARCH LOOP FUNCTIONS
        elif opt == "--search_loop":
            params['SEARCH_LOOP'] = arg
        elif opt == "--step":
            params['STEP'] = arg

        # POPULATION OPTIONS
        elif opt == "--population":
            check_int('POPULATION_SIZE', arg)
        elif opt == "--generations":
            check_int('GENERATIONS', arg)

        # INDIVIDUAL SIZE
        elif opt == "--max_tree_depth":
            check_int('MAX_TREE_DEPTH', arg)
        elif opt == "--codon_size":
            check_int('CODON_SIZE', arg)
        elif opt == "--genome_length":
            check_int('GENOME_LENGTH', arg)
        elif opt == "--max_wraps":
            check_int('MAX_WRAPS', arg)

        # INITIALISATION
        elif opt == "--initialisation":
            params['INITIALISATION'] = arg
        elif opt == "--max_init_depth":
            check_int('MAX_INIT_DEPTH', arg)
        elif opt == "--genome_init":
            params['GENOME_INIT'] = True
            params['INITIALISATION'] = "operators.initialisation.random_init"

        # SELECTION
        elif opt == "--selection":
            params['SELECTION'] = arg
        elif opt == "--invalid_selection":
            params['INVALID_SELECTION'] = arg
        elif opt == "--tournament_size":
            check_int('TOURNAMENT_SIZE', arg)
        elif opt == "--selection_proportion":
            check_float('SELECTION_PROPORTION', arg)

        # EVALUATION
        elif opt == "--multicore":
            params['MULTIPCORE'] = True
        elif opt == "--cores":
            check_int('CORES', arg)

        # CROSSOVER
        elif opt == "--crossover":
            params['CROSSOVER'] = arg
        elif opt == "--crossover_probability":
            check_float('CROSSOVER_PROBABILITY', arg)

        # MUTATION
        elif opt == "--mutation":
            params['MUTATION'] = arg
        elif opt == "--mutation_events":
            check_int('MUTATION_EVENTS', arg)
        elif opt == "--mutation_probability":
            check_float('MUTATION_PROBABILITY', arg)

        # REPLACEMENT
        elif opt == "--replacement":
            params['REPLACEMENT'] = arg
        elif opt == "--elite_size":
            check_int('ELITE_SIZE', arg)

        # PROBLEM SPECIFICS
        elif opt == "--bnf_grammar":
            params['GRAMMAR_FILE'] = arg
        elif opt == "--fitness_function":
            params['FITNESS_FUNCTION'] = arg
        elif opt == "--dataset":
            params['DATASET'] = arg
        elif opt == "--target_string":
            params['STRING_MATCH_TARGET'] = arg
        elif opt == "--experiment_name":
            params['EXPERIMENT_NAME'] = arg
        elif opt == "--error_metric":
            params['ERROR_METRIC'] = arg

        # OPTIONS
        elif opt == "--random_seed":
            check_int('RANDOM_SEED', arg)
        elif opt == "--debug":
            params['DEBUG'] = True
        elif opt == "--verbose":
            params['VERBOSE'] = True
        elif opt == "--silent":
            params['SILENT'] = True
        elif opt == "--save_all":
            params['SAVE_ALL'] = True
        elif opt == "--save_plots":
            params['SAVE_PLOTS'] = True

        # CACHING
        elif opt == "--cache":
            params['CACHE'] = True
            params['LOOKUP_FITNESS'] = True
        elif opt == "--dont_lookup_fitness":
            params['LOOKUP_FITNESS'] = False
        elif opt == "--lookup_bad_fitness":
            params['LOOKUP_FITNESS'] = False
            params['LOOKUP_BAD_FITNESS'] = True
        elif opt == "--mutate_duplicates":
            params['LOOKUP_FITNESS'] = False
            params['MUTATE_DUPLICATES'] = True
        else:
            assert False, "Unhandled Option, use --help for available params"

    # Elite size is set to either 1 or 1% of the population size, whichever is
    # bigger if no elite size is previously set.
    if params['ELITE_SIZE'] is None:
        params['ELITE_SIZE'] = return_percent(1, params['POPULATION_SIZE'])

    # Set the size of a generation
    params['GENERATION_SIZE'] = params['POPULATION_SIZE'] - params[
        'ELITE_SIZE']

    # Set GENOME_OPERATIONS automatically for faster linear operations.
    if (params['MUTATION'] == 'operators.mutation.int_flip' or
                params['MUTATION'] == 'int_flip') and \
            (params['CROSSOVER'] == 'operators.crossover.onepoint' or
                     params['CROSSOVER'] == 'onepoint'):
        params['GENOME_OPERATIONS'] = True
    else:
        params['GENOME_OPERATIONS'] = False

    # Initialise run lists and folders
    initialise_run_params()

    # Set correct param imports for specified function options, including
    # error metrics and fitness functions.
    set_param_imports()
    
    # Parse grammar file and set grammar class.
    params['BNF_GRAMMAR'] = grammar.Grammar("../grammars/" +
                                            params['GRAMMAR_FILE'])