def subtree_mutate(ind_tree): """ Creates a list of all nodes and picks one node at random to mutate. Because we have a list of all nodes, we can (but currently don't) choose what kind of nodes to mutate on. Handy. :param ind_tree: The full tree of an individual. :return: The full mutated tree and the associated genome. """ # Find the list of nodes we can mutate from. targets = ind_tree.get_target_nodes( [], target=params['BNF_GRAMMAR'].non_terminals) # Pick a node. new_tree = choice(targets) # Set the depth limits for the new subtree. if params['MAX_TREE_DEPTH']: # Set the limit to the tree depth. max_depth = params['MAX_TREE_DEPTH'] - new_tree.depth else: # There is no limit to tree depth. max_depth = None # Mutate a new subtree. generate_tree(new_tree, [], [], "random", 0, 0, 0, max_depth) return ind_tree
def subtree_mutate(ind_tree): """ Creates a list of all nodes and picks one node at random to mutate. Because we have a list of all nodes, we can (but currently don't) choose what kind of nodes to mutate on. Handy. :param ind_tree: The full tree of an individual. :return: The full mutated tree and the associated genome. """ # Find the list of nodes we can mutate from. targets = ind_tree.get_target_nodes([], target=params[ 'BNF_GRAMMAR'].non_terminals) # Pick a node. new_tree = choice(targets) # Set the depth limits for the new subtree. if params['MAX_TREE_DEPTH']: # Set the limit to the tree depth. max_depth = params['MAX_TREE_DEPTH'] - new_tree.depth else: # There is no limit to tree depth. max_depth = None # Mutate a new subtree. generate_tree(new_tree, [], [], "random", 0, 0, 0, max_depth) return ind_tree
def generate_ind_tree(max_depth, method): """ Generate an individual using a given subtree initialisation method. :param max_depth: The maximum depth for the initialised subtree. :param method: The method of subtree initialisation required. :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 = generate_tree(ind_tree, [], [], method, 0, 0, 0, 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) # import pdb; pdb.set_trace() # 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_ind_tree(max_depth, method): """ Generate an individual using a given subtree initialisation method. :param max_depth: The maximum depth for the initialised subtree. :param method: The method of subtree initialisation required. :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 = generate_tree(ind_tree, [], [], method, 0, 0, 0, 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