def xmut(ind, expr, unipb, shrpb, psetR, psetT): choice = random.random() if choice < unipb: indx1 = gp.mutUniform(ind[0], expr, pset=psetR) ind[0] = indx1[0] indx2 = gp.mutUniform(ind[1], expr, pset=psetT) ind[1] = indx2[0] elif unipb <= choice < unipb + shrpb: indx1 = gp.mutShrink(ind[0]) ind[0] = indx1[0] indx2 = gp.mutShrink(ind[1]) ind[1] = indx2[0] else: indx1 = gp.mutInsert(ind[0], pset=psetR) ind[0] = indx1[0] indx2 = gp.mutInsert(ind[1], pset=psetT) ind[1] = indx2[0] #else: # choice2 = random.random() # if choice2 < 0.5: # indx1 = gp.mutEphemeral(ind[0], "all") # ind[0] = indx1[0] # indx2 = gp.mutEphemeral(ind[1], "all") # ind[1] = indx2[0] #else: # indx1 = gp.mutEphemeral(ind[0], "one") # ind[0] = indx1[0] # indx2 = gp.mutEphemeral(ind[1], "one") #ind[1] = indx2[0] return ind,
def mutate(individual, pset, expr): rand = np.random.rand() if rand <= 0.25: return gp.mutShrink(individual), elif rand <= 0.5: return gp.mutInsert(individual, pset) elif rand <= 0.75: return gp.mutNodeReplacement(individual, pset) return gp.mutUniform(individual, expr, pset)
def _random_mutation_operator(self, individual): """Randomly picks a replacement, insert, or shrink mutation.""" roll = random.random() if roll <= 0.333333: return gp.mutUniform(individual, expr=self.toolbox.expr_mut, pset=self.pset) elif roll <= 0.666666: return gp.mutInsert(individual, pset=self.pset) else: return gp.mutShrink(individual)
def xmut(ind, expr, psetR, psetT, unipb, shrpb, Ngenes): ch = random.randint(0, Ngenes - 1) choice = random.random() #if choice < opti: # ind = deepcopy(ind) # ind = optimize_ind(ind) if choice < unipb: indx1 = gp.mutUniform(ind[0][ch], expr, pset=psetR) ind[0][ch] = indx1[0] indx2 = gp.mutUniform(ind[1][ch], expr, pset=psetT) ind[1][ch] = indx2[0] elif choice < unipb + shrpb: indx1 = gp.mutShrink(ind[0][ch]) ind[0][ch] = indx1[0] indx2 = gp.mutShrink(ind[1][ch]) ind[1][ch] = indx2[0] else: indx1 = gp.mutInsert(ind[0][ch], pset=psetR) ind[0][ch] = indx1[0] indx2 = gp.mutInsert(ind[1][ch], pset=psetT) ind[1][ch] = indx2[0] return ind,
def multiple_mutator(individual, pset): weight_sum = gencome.config.mut_uniform_weight + gencome.config.mut_replacement_weight \ + gencome.config.mut_insert_weight + gencome.config.mut_shrink_weight cur_weight = gencome.config.mut_uniform_weight rng = random.random() * weight_sum if rng < cur_weight: logger.debug( f"Starting mutation: Uniform {str_individual_with_real_feature_names(individual)}" ) new_ind = gp.mutUniform(individual, expr=gencome.config.toolbox.expr_mut, pset=pset) logger.debug( f"Successful mutation: Uniform {str_individual_with_real_feature_names(new_ind[0])}" ) return new_ind cur_weight += gencome.config.mut_replacement_weight if rng < cur_weight: logger.debug( f"Starting mutation: Node Replace {str_individual_with_real_feature_names(individual)}" ) new_ind = gp.mutNodeReplacement(individual, pset=pset) logger.debug( f"Successful mutation: Node Replace {str_individual_with_real_feature_names(new_ind[0])}" ) return new_ind cur_weight += gencome.config.mut_insert_weight if rng < cur_weight: logger.debug( f"Starting mutation Insert {str_individual_with_real_feature_names(individual)}" ) new_ind = gp.mutInsert(individual, pset=pset) logger.debug( f"Successful mutation Insert {str_individual_with_real_feature_names(new_ind[0])}" ) return new_ind logger.debug( f"Starting mutation Shrink {str_individual_with_real_feature_names(individual)}" ) new_ind = gp.mutShrink(individual) logger.debug( f"Successful mutation Shrink {str_individual_with_real_feature_names(new_ind[0])}" ) return new_ind
def _random_mutation_operator(self, individual): """Perform a replacement, insert, or shrink mutation on an individual Parameters ---------- individual: DEAP individual A list of pipeline operators and model parameters that can be compiled by DEAP into a callable function Returns ------- fitness: list Returns the individual with one of the mutations applied to it """ roll = random.random() if roll <= 0.333333: return gp.mutUniform(individual, expr=self.toolbox.expr_mut, pset=self.pset) elif roll <= 0.666666: return gp.mutInsert(individual, pset=self.pset) else: return gp.mutShrink(individual)
def _mutate(individual: DoubleTreeBasedIndividual, cond_expr, val_expr, cond_pset, val_pset): # print('mutating', individual) if _flip_coin(): expr = cond_expr pset = cond_pset prob = prob_mutate_individual_cond trees = individual.cond_trees # trees = individual['CONDITION_TREES'] else: expr = val_expr pset = val_pset prob = prob_mutate_individual_val trees = individual.value_trees # trees = individual['VALUE_TREES'] for i, tree in enumerate(trees): if random() <= prob: # tree, = gp.mutUniform(tree, expr, pset) tree, = gp.mutInsert(tree, pset) trees[i] = tree return individual,
def mutInsert_multiple_gene(individual: MultipleGeneGP, pset): mutInsert(individual.random_select(), pset) return individual,
def combined_mutation(individual, expr, pset): if random.random() > 0.5: return gp.mutInsert(individual, pset) else: return gp.mutEphemeral(individual, "one")
def xmut(ind, expr, unipb, shrpb, inspb, pset, creator): """ Author(s): Francesco Marchetti email: [email protected] From [1] and modified. Added several mutations possibilities. """ choice = random.random() try: if type(ind[0]) == creator.SubIndividual: if hasattr(pset, '__len__'): if choice < unipb: indx1 = gp.mutUniform(ind[0], expr, pset=pset[0]) ind[0] = indx1[0] indx2 = gp.mutUniform(ind[1], expr, pset=pset[1]) ind[1] = indx2[0] elif unipb <= choice < unipb + shrpb: indx1 = gp.mutShrink(ind[0]) ind[0] = indx1[0] indx2 = gp.mutShrink(ind[1]) ind[1] = indx2[0] elif unipb + shrpb <= choice < unipb + shrpb + inspb: indx1 = gp.mutInsert(ind[0], pset=pset[0]) ind[0] = indx1[0] indx2 = gp.mutInsert(ind[1], pset=pset[1]) ind[1] = indx2[0] else: choice2 = random.random() if choice2 < 0.5: indx1 = gp.mutEphemeral(ind[0], "all") ind[0] = indx1[0] indx2 = gp.mutEphemeral(ind[1], "all") ind[1] = indx2[0] else: indx1 = gp.mutEphemeral(ind[0], "one") ind[0] = indx1[0] indx2 = gp.mutEphemeral(ind[1], "one") ind[1] = indx2[0] else: if choice < unipb: indx1 = gp.mutUniform(ind[0], expr, pset=pset) ind[0] = indx1[0] indx2 = gp.mutUniform(ind[1], expr, pset=pset) ind[1] = indx2[0] elif unipb <= choice < unipb + shrpb: indx1 = gp.mutShrink(ind[0]) ind[0] = indx1[0] indx2 = gp.mutShrink(ind[1]) ind[1] = indx2[0] elif unipb + shrpb <= choice < unipb + shrpb + inspb: indx1 = gp.mutInsert(ind[0], pset=pset) ind[0] = indx1[0] indx2 = gp.mutInsert(ind[1], pset=pset) ind[1] = indx2[0] else: choice2 = random.random() if choice2 < 0.5: indx1 = gp.mutEphemeral(ind[0], "all") ind[0] = indx1[0] indx2 = gp.mutEphemeral(ind[1], "all") ind[1] = indx2[0] else: indx1 = gp.mutEphemeral(ind[0], "one") ind[0] = indx1[0] indx2 = gp.mutEphemeral(ind[1], "one") ind[1] = indx2[0] except AttributeError: if choice < unipb: indx1 = gp.mutUniform(ind, expr, pset=pset) ind = indx1[0] elif unipb <= choice < unipb + shrpb: indx1 = gp.mutShrink(ind) ind = indx1[0] elif unipb + shrpb <= choice < unipb + shrpb + inspb: indx1 = gp.mutInsert(ind, pset=pset) ind = indx1[0] else: choice2 = random.random() if choice2 < 0.5: indx1 = gp.mutEphemeral(ind, "all") ind = indx1[0] else: indx1 = gp.mutEphemeral(ind, "one") ind = indx1[0] return ind,