def load_population(target): """ Given a target folder, read all files in the folder and load/parse solutions found in each file. :param target: A target folder stored in the "seeds" folder. :return: A list of all parsed individuals stored in the target folder. """ # Set path for seeds folder path_1 = path.join(getcwd(), "..", "seeds") if not path.isdir(path_1): # Seeds folder does not exist. s = "scripts.seed_PonyGE2.load_population\n" \ "Error: `seeds` folder does not exist in root directory." raise Exception(s) path_2 = path.join(path_1, target) if not path.isdir(path_2): # Target folder does not exist. s = "scripts.seed_PonyGE2.load_population\n" \ "Error: target folder " + target + \ " does not exist in seeds directory." raise Exception(s) # Get list of all target individuals in the target folder. target_inds = [i for i in listdir(path_2) if i.endswith(".txt")] # Initialize empty list for seed individuals. seed_inds = [] for ind in target_inds: # Loop over all target individuals. # Get full file path. file_name = path.join(path_2, ind) # Initialise None data for ind info. genotype, phenotype = None, None # Open file. with open(file_name, "r") as f: # Read file. raw_content = f.read() # Read file. content = raw_content.split("\n") # Check if genotype is already saved in file. if "Genotype:" in content: # Get index location of genotype. gen_idx = content.index("Genotype:") + 1 # Get the genotype. try: genotype = eval(content[gen_idx]) except: s = "scripts.seed_PonyGE2.load_population\n" \ "Error: Genotype from file " + file_name + \ " not recognized: " + content[gen_idx] raise Exception(s) # Check if phenotype (target string) is already saved in file. if "Phenotype:" in content: # Get index location of genotype. phen_idx = content.index("Phenotype:") + 1 # Get the phenotype. phenotype = content[phen_idx] # TODO: Current phenotype is read in as single-line only. Split is performed on "\n", meaning phenotypes that span multiple lines will not be parsed correctly. This must be fixed in later editions. elif "Genotype:" not in content: # There is no explicit genotype or phenotype in the target # file, read in entire file as phenotype. phenotype = raw_content if genotype: # Generate individual from genome. ind = Individual(genotype, None) if phenotype and ind.phenotype != phenotype: s = "scripts.seed_PonyGE2.load_population\n" \ "Error: Specified genotype from file " + file_name + \ " doesn't map to same phenotype. Check the specified " \ "grammar to ensure all is correct: " + \ params['GRAMMAR_FILE'] raise Exception(s) else: # Set target for GE LR Parser. params['REVERSE_MAPPING_TARGET'] = phenotype # Parse target phenotype. ind = GE_LR_parser.main() # Add new ind to the list of seed individuals. seed_inds.append(ind) return seed_inds
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()]
from sys import path path.append("../src") from utilities.algorithm.general import check_python_version check_python_version() import sys from scripts import GE_LR_parser from ponyge import mane from algorithm.parameters import params, set_params if __name__ == '__main__': # Set parameters set_params(sys.argv[1:]) # Parse seed individual and store in params. params['SEED_INDIVIDUAL'] = GE_LR_parser.main() # Launch PonyGE2. mane()
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()]