def generate_PI_ind_tree(max_depth): """ Generate an individual using a given Position Independent subtree initialisation method. :param max_depth: The maximum depth for the initialised subtree. :return: A fully built individual. """ # Initialise an instance of the tree class ind_tree = Tree(str(params['BNF_GRAMMAR'].start_rule["symbol"]), None) # Generate a tree genome, output, nodes, depth = pi_grow(ind_tree, max_depth) # Get remaining individual information phenotype, invalid, used_cod = "".join(output), False, len(genome) if params['BNF_GRAMMAR'].python_mode: # Grammar contains python code phenotype = python_filter(phenotype) # Initialise individual ind = individual.Individual(genome, ind_tree, map_ind=False) # Set individual parameters ind.phenotype, ind.nodes = phenotype, nodes ind.depth, ind.used_codons, ind.invalid = depth, used_cod, invalid # Generate random tail for genome. ind.genome = genome + [randint(0, params['CODON_SIZE']) for _ in range(int(ind.used_codons / 2))] return ind
def generate_new_genome_and_phenotype(): writeLog('Creating new individual values') depths = range(params['BNF_GRAMMAR'].min_ramp + 1, params['MAX_INIT_TREE_DEPTH'] + 1) size = params['POPULATION_SIZE'] if size < len(depths): depths = depths[:int(size)] max_depth = depths[int(len(depths) / 2)] # Initialise an instance of the tree class ind_tree = Tree(str(params['BNF_GRAMMAR'].start_rule["symbol"]), None) # Generate a tree genome, output, nodes, depth = pi_grow(ind_tree, max_depth) # Get remaining individual information phenotype, invalid, used_cod = "".join(output), False, len(genome) return phenotype, nodes, genome, depth, used_cod, invalid