Exemple #1
0
def _crossover(individual1: DoubleTreeBasedIndividual,
               individual2: DoubleTreeBasedIndividual):
    # cond_trees1 = individual1['CONDITION_TREES']
    cond_trees1 = individual1.cond_trees
    cond_trees2 = individual2.cond_trees
    # cond_trees2 = individual2['CONDITION_TREES']
    val_trees1 = individual1.cond_trees
    val_trees2 = individual2.cond_trees
    # val_trees1 = individual1['VALUE_TREES']
    # val_trees2 = individual2['VALUE_TREES']

    if _flip_coin():  # cx cond trees
        for index in range(len(cond_trees1)):
            if random() <= prob_crossover_individual_cond:
                # print('doing cx on cond trees idx %d' %(index,))
                t1 = cond_trees1[index]
                t2 = cond_trees2[index]
                # print('t1 is', t1)
                # print('t2 is', t2)
                t1, t2 = gp.cxOnePoint(t1, t2)
                # print('t1 is', t1)
                # print('t2 is', t2)
                cond_trees1[index] = t1
                cond_trees2[index] = t2
    else:
        for index in range(len(val_trees1)):
            if random() <= prob_crossover_individual_val:
                # print('doing cx on val trees idx %d' %(index,))
                t1 = val_trees1[index]
                t2 = val_trees2[index]
                t1, t2 = gp.cxOnePoint(t1, t2)
                val_trees1[index] = t1
                val_trees2[index] = t2
    # print('done cx')
    return individual1, individual2
def xmate(ind1, ind2, Ngenes):
    choice = random.random()
    ch = random.randint(0, Ngenes-1)
    for i in range(len(ind1)):
        if choice <= 0.7:
            ind1[i][ch], ind2[i][ch] = gp.cxOnePoint(ind1[i][ch], ind2[i][ch])
        else: # 0.7 < choice <= 0.9:
            ch2 = Ngenes-ch-1
            ind1[i][ch], ind2[i][ch2] = gp.cxOnePoint(ind1[i][ch], ind2[i][ch2])
            ind1[i][ch2], ind2[i][ch] = gp.cxOnePoint(ind1[i][ch2], ind2[i][ch])

    return ind1, ind2
def cxOnePoint_multiple_all_gene(ind1: MultipleGeneGP,
                                 ind2: MultipleGeneGP,
                                 permutation=False):
    if permutation:
        a_list = np.random.permutation(np.arange(0, len(ind1.gene)))
        b_list = np.random.permutation(np.arange(0, len(ind2.gene)))
    else:
        a_list = np.arange(0, len(ind1.gene))
        b_list = np.arange(0, len(ind2.gene))
    for a, b in zip(a_list, b_list):
        cxOnePoint(ind1.gene[a], ind2.gene[b])
    return ind1, ind2
def staticLimitCrossover(ind1, ind2, heightLimit): 
    # Store a backup of the original individuals 
    keepInd1, keepInd2 = toolbox.clone(ind1), toolbox.clone(ind2) 

    # Mate the two individuals 
    # The crossover is done in place (see the documentation) 
    # If using STGP (like spambase), replace this line by 
    gp.cxOnePoint(ind1, ind2)
    # If a child is higher than the maximum allowed, then 
    # it is replaced by one of its parent 
    if ind1.height > heightLimit: 
        ind1[:] = keepInd1 
    if ind2.height > heightLimit: 
        ind2[:] = keepInd2 
    return ind1,ind2
Exemple #5
0
def xmate_st(ind1, ind2):
    # if (random.random() < cxpb):
    i1 = random.randrange(min(len(ind1), len(ind2)))
    # deepcopy needed? duplicates?
    ind1[i1], ind2[i1] = gp.cxOnePoint(copy.deepcopy(ind1[i1]),
                                       copy.deepcopy(ind2[i1]))
    return ind1, ind2
 def replaces_features(ind: MultipleGeneGP):
     for i, c in enumerate(ind.coef):
         if (positive and c >= threshold) or (not positive
                                              and c < threshold):
             new_features = cxOnePoint(
                 copy.deepcopy(random.choice(good_features)),
                 copy.deepcopy(random.choice(good_features)))
             ind.gene[i] = random.choice(new_features)
Exemple #7
0
def xmate_bt(ind1, ind2):
    # hmmm
    # if (random.random() < cxpb):
    for i in range(min(len(ind1), len(ind2))):
        ind1[i], ind2[i] = gp.cxOnePoint(ind1[i], ind2[i])
    #    ind1[0], ind2[0] = gp.cxOnePoint(ind1[0], ind2[0])
    # if (random.random() < cxpb):
    #   ind1[1], ind2[1] = gp.cxOnePoint(ind1[1], ind2[1])

    return ind1, ind2
Exemple #8
0
def mate(ind_1, ind_2, creator):
    ind_1_list = [ind for ind in ind_1]
    ind_2_list = [ind for ind in ind_2]
    n_1 = []
    n_2 = []
    for i_1, i_2 in zip(ind_1_list, ind_2_list):
        new_1, new_2 = gp.cxOnePoint(i_1, i_2)
        n_1.append(new_1)
        n_2.append(new_2)

    return creator.Individual(n_1), creator.Individual(n_2)
Exemple #9
0
def xmate(ind1, ind2, Ngenes):
    choice = random.random()
    ch = random.randint(0, Ngenes - 1)
    #if choice <= 0.8:
    #for i in range(len(ind1)):
    #   ind1[i], ind2[i] = gp.cxOnePoint(ind1[i], ind2[i])
    #else:
    #   gene1 = ind1[ch]
    #  w1 = ind1.w[ch]
    # gene2 = ind2[ch]
    #w2 = ind2.w[ch]
    #ind1[ch] = deepcopy(gene2)
    #ind1.w[ch] = copy(w2)
    #ind2[ch] = deepcopy(gene1)
    #ind2.w[ch] = copy(w1)

    if choice <= 0.7:
        ind1[ch], ind2[ch] = gp.cxOnePoint(ind1[ch], ind2[ch])
    else:  # 0.7 < choice <= 0.9:
        ch2 = Ngenes - ch - 1
        ind1[ch], ind2[ch2] = gp.cxOnePoint(ind1[ch], ind2[ch2])
        ind1[ch2], ind2[ch] = gp.cxOnePoint(ind1[ch2], ind2[ch])

    return ind1, ind2
Exemple #10
0
def xmate_maxt(ind1, ind2):
    max_size = max(len(ind1), len(ind2))
    i1 = random.randrange(max_size)
    i2 = random.randrange(max_size)

    if i1 >= len(ind1):
        # add one!
        ind1.append(copy.deepcopy(ind2[i2]))
    elif i2 >= len(ind2):
        # add one!
        ind2.append(copy.deepcopy(ind1[i1]))
    else:
        # normal crossover.
        ind1[i1], ind2[i2] = gp.cxOnePoint(copy.deepcopy(ind1[i1]), copy.deepcopy(ind2[i2]))

    return ind1, ind2
Exemple #11
0
def repeated_crossover(ind1, ind2, existing, toolbox, max_tries=10):
    """
        Repeatedly apply cxOnePoint until the generated individuals are
        unique from the existing originals (or until max_tries is hit).
        Thiw was inspired by tpots _mate_operator.
    :param ind1:
    :param ind2:
    :param existing:
    :param toolbox:
    :param max_tries:
    :return:
    """
    unique_offspring1 = None
    unique_offspring2 = None

    # Try for max_tries, or until we generate a unique individual
    for i in range(max_tries):
        ind1_copy, ind2_copy = toolbox.clone(ind1), toolbox.clone(ind2)

        offspring1, offspring2 = gp.cxOnePoint(ind1_copy, ind2_copy)

        if str(offspring1) not in existing:
            unique_offspring1 = offspring1

        if str(offspring2) not in existing:
            unique_offspring2 = offspring2

        # Only break once both are unique
        if unique_offspring1 and unique_offspring2:
            break

    # If we didnt find a unique, then use the last (repeated) offspring generated
    unique_offspring1 = unique_offspring1 or offspring1
    unique_offspring2 = unique_offspring2 or offspring2

    return unique_offspring1, unique_offspring2
Exemple #12
0
def xmate_aic(ind1, ind2):
    assert len(ind1) == len(ind2)
    for i in range(len(ind1)):
        ind1[i], ind2[i] = gp.cxOnePoint(ind1[i], ind2[i])
    return ind1, ind2
def cxOnePoint_multiple_gene(ind1: MultipleGeneGP, ind2: MultipleGeneGP):
    cxOnePoint(ind1.random_select(), ind2.random_select())
    return ind1, ind2
def cxOnePoint_multiple_gene_deterministic(ind1: MultipleGeneGP,
                                           ind2: MultipleGeneGP):
    cxOnePoint(ind1.deterministic_select(), ind2.deterministic_select())
    return ind1, ind2
Exemple #15
0
def xmate(ind1, ind2):
    """From [1] and modified"""
    ind1[0], ind2[0] = gp.cxOnePoint(ind1[0], ind2[0])
    ind1[1], ind2[1] = gp.cxOnePoint(ind1[1], ind2[1])
    return ind1, ind2
Exemple #16
0
def xmate(ind1, ind2):
    ind1[0], ind2[0] = gp.cxOnePoint(ind1[0], ind2[0])
    ind1[1], ind2[1] = gp.cxOnePoint(ind1[1], ind2[1])
    return ind1, ind2
Exemple #17
0
def xmate_aic(ind1, ind2):
    min_size = min(len(ind1), len(ind2))
    for i in range(min_size):
        ind1[i], ind2[i] = gp.cxOnePoint(copy.deepcopy(ind1[i]),
                                         copy.deepcopy(ind2[i]))
    return ind1, ind2
Exemple #18
0
def xmateSimple(ind1, ind2):
    """From [1] """
    i1 = random.randrange(len(ind1))
    i2 = random.randrange(len(ind2))
    ind1[i1], ind2[i2] = gp.cxOnePoint(ind1[i1], ind2[i2])
    return ind1, ind2
Exemple #19
0
def xmate(ind1, ind2):
    # if (random.random() < cxpb):
    i1 = random.randrange(len(ind1))
    i2 = random.randrange(len(ind2))
    ind1[i1], ind2[i2] = gp.cxOnePoint(ind1[i1], ind2[i2])
    return ind1, ind2
Exemple #20
0
def xmate_sic(ind1, ind2):
    i = random.randrange(len(ind1))
    ind1[i], ind2[i] = gp.cxOnePoint(ind1[i], ind2[i])
    return ind1, ind2
def cxOnePoint_multiple_gene_weight(ind1: MultipleGeneGP,
                                    ind2: MultipleGeneGP):
    cxOnePoint(ind1.weight_select(), ind2.weight_select())
    return ind1, ind2
Exemple #22
0
def xmate_ric(ind1, ind2):
    i1 = random.randrange(len(ind1))
    i2 = random.randrange(len(ind2))
    ind1[i1], ind2[i2] = gp.cxOnePoint(ind1[i1], ind2[i2])
    return ind1, ind2
Exemple #23
0
 def _mate_operator(self, ind1, ind2):
     return gp.cxOnePoint(ind1, ind2)