Esempio n. 1
0
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,
Esempio n. 2
0
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)
Esempio n. 3
0
def multi_mutate(individual, expr, pset):
    '''定义变异函数'''
    rand = np.random.uniform(0)
    if rand <= 0.33:
        return gp.mutUniform(individual, expr, pset)
    elif rand <= 0.66:
        return gp.mutShrink(individual)
    else:
        return gp.mutNodeReplacement(individual, pset)
Esempio n. 4
0
 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)
Esempio n. 5
0
def customMut(individual, expr, pset):
    '''To handle multiple mutation operators'''
    r = random.random()
    if r >= 0.4:
        individual = gp.mutUniform(individual, expr, pset)
    else:
        #apply shrink
        individual = gp.mutShrink(individual)
    return individual
def multi_mutate(individual, expr, pset):
    '''
    apply multiple kinds of mutation in a funciton
    '''
    rand = np.random.uniform(0)
    if rand <= 0.4:
        return gp.mutUniform(individual, expr, pset)
    elif rand <= 0.75:
        return gp.mutShrink(individual)
    else:
        return gp.mutNodeReplacement(individual, pset)
Esempio n. 7
0
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,
Esempio n. 8
0
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
Esempio n. 9
0
File: tpot.py Progetto: vsolano/tpot
    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 mutShrink_multiple_gene(individual: MultipleGeneGP):
    mutShrink(individual.random_select())
    return individual,
Esempio n. 11
0
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,
Esempio n. 12
0
def random_mutation(individual, expr, pset):
    return random.choice([gp.mutShrink(individual), gp.mutNodeReplacement(individual, pset), gp.mutUniform(individual, expr, pset), gp.mutEphemeral(individual, "all")])