Ejemplo n.º 1
0
def operate(population, crossover, mutation, pc, pm):
    length = len(population)
    for i in range(length):
        if random() < pc:
            samp = sample(list(population), 2)
            bin_reprs = [i.bin_value for i in samp]
            crossover(bin_reprs[0], bin_reprs[1])
        if random() < pm:
            bin_rep = choice(population).bin_value
            mutation(bin_rep)
Ejemplo n.º 2
0
def basicSelection(population, referenceCromossome, evaluatedPopulation, crossoverType, crossoverProbability, crossoverTasksNumPerc, mutationType, mutationTasksNumPerc, tasksMutationProbability, operatorsMutationProbability, drivenMutation, drivenMutationPart, limitBestFitnessRepetionCount, bestFitnessRepetionCount, drivenMutatedIndividuals, drivenMutatedGenerations, TPweight, precisenessWeight, simplicityWeight, completenessWeight, elitismPerc, sortedEvaluatedPopulation, selectionOp, currentGeneration, completenessAttemptFactor1, completenessAttemptFactor2, numberOfcyclesAfterDrivenMutation, alphabet, log):
    drivenMutationCycle = 0
    auxPopulation = copy.deepcopy(population)
    auxDrivenMutatedIndividuals = copy.deepcopy(drivenMutatedIndividuals)
    for j in range(len(auxDrivenMutatedIndividuals)):
        auxDrivenMutatedIndividuals[j] = 0
    if (drivenMutation == 1) and (currentGeneration > 0) and (bestFitnessRepetionCount > 0) and (divmod(bestFitnessRepetionCount, limitBestFitnessRepetionCount)[1] == 0):
        drivenMutationCycle = 1
    drivenMutatedEvaluatedPopulation = [[], []]
    if drivenMutatedGenerations >= 1:
        drivenMutatedEvaluatedPopulation[0] = 0
        drivenMutatedEvaluatedPopulation[1] = [0 for _ in range(len(drivenMutatedIndividuals))]
        for j in range(len(drivenMutatedIndividuals)):
            if drivenMutatedIndividuals[j] == 1:
                drivenMutatedEvaluatedPopulation[1][j] = 1
                drivenMutatedEvaluatedPopulation[0] = drivenMutatedEvaluatedPopulation[0] + 1
            else:
                drivenMutatedEvaluatedPopulation[1][j] = evaluatedPopulation[1][j][0]
                drivenMutatedEvaluatedPopulation[0] = drivenMutatedEvaluatedPopulation[0] + evaluatedPopulation[1][j][0]
    i = 0
    while i < len(population):
        chosenIndividual1 = op.parentSelection(evaluatedPopulation, sortedEvaluatedPopulation, selectionOp, drivenMutatedIndividuals, drivenMutatedEvaluatedPopulation, drivenMutatedGenerations)
        auxPopulation[i] = copy.deepcopy(population[chosenIndividual1[0]])
        chosenIndividual2 = op.parentSelection(evaluatedPopulation, sortedEvaluatedPopulation, selectionOp, drivenMutatedIndividuals, drivenMutatedEvaluatedPopulation, drivenMutatedGenerations)
        auxPopulation[i + 1] = copy.deepcopy(population[chosenIndividual2[0]])
        if (chosenIndividual1[1] == 1) or (chosenIndividual2[1] == 1):
            auxDrivenMutatedIndividuals[i] = 1
            auxDrivenMutatedIndividuals[i + 1] = 1
        (auxPopulation[i], auxPopulation[i + 1]) = op.crossoverPerProcess(crossoverType, crossoverProbability, crossoverTasksNumPerc, auxPopulation[i], auxPopulation[i+1], i, evaluatedPopulation, alphabet)
        if drivenMutationCycle == 0:
            op.mutation(auxPopulation[i], tasksMutationProbability, operatorsMutationProbability, mutationType, mutationTasksNumPerc, alphabet)
            op.mutation(auxPopulation[i+1], tasksMutationProbability, operatorsMutationProbability, mutationType, mutationTasksNumPerc, alphabet)
        i = i + 2
        if (i + 1 == len(population)) and (len(population) % 2 == 1):
            i = i - 1
    if (elitismPerc > 0) or (drivenMutationCycle == 1):
        evaluatedAuxPopulation = fitn.evaluationPopulation(auxPopulation, referenceCromossome, TPweight, precisenessWeight, simplicityWeight, completenessWeight, completenessAttemptFactor1, completenessAttemptFactor2, selectionOp, alphabet, log)
        sortedEvaluatedAuxPopulation = sorted(evaluatedAuxPopulation[1], reverse=True, key=takeFirst)
    if drivenMutatedGenerations >= 1:
        drivenMutatedGenerations = drivenMutatedGenerations - 1
        if drivenMutatedGenerations == 0:
            for j in range(len(auxDrivenMutatedIndividuals)):
                auxDrivenMutatedIndividuals[j] = 0
    drivenMutatedIndividuals = copy.deepcopy(auxDrivenMutatedIndividuals)
    if drivenMutationCycle == 1:
        drivenMutatedIndividuals = op.drivenMutation(auxPopulation, sortedEvaluatedAuxPopulation, drivenMutationPart, drivenMutatedIndividuals)
        drivenMutatedGenerations = numberOfcyclesAfterDrivenMutation
    if elitismPerc > 0:
        op.elitism(population, elitismPerc, sortedEvaluatedAuxPopulation, sortedEvaluatedPopulation, auxPopulation, drivenMutatedIndividuals)
    evaluatedNewPopulation = fitn.evaluationPopulation(auxPopulation, referenceCromossome, TPweight, precisenessWeight, simplicityWeight, completenessWeight, completenessAttemptFactor1, completenessAttemptFactor2, selectionOp, alphabet, log)
    return (auxPopulation, evaluatedNewPopulation, drivenMutatedIndividuals, drivenMutatedGenerations)
Ejemplo n.º 3
0
def __mutation(pop, p_mutation_pop, p_mutation_specimen):
    new_pop = []

    for specimen in pop:
        if random.random() < p_mutation_pop:
            mutated = operators.mutation(specimen, p_mutation_specimen)
            new_pop.append(mutated)
        else:
            new_pop.append(specimen)

    return new_pop