Ejemplo n.º 1
0
def main():

    max_iteration = 100

    bin_mutation_par = {}
    #    bin_mutation_par['steps'] = array([0.1, 0.2, 0.3, 0.7])*max_iteration
    #    bin_mutation_par['steps'] = bin_mutation_par['steps'].astype('int')
    #    bin_mutation_par['probs'] = [0.5, 0.2, 0.01, 0.005, 0.001]
    bin_mutation_par["initial_prob"] = 0.5
    bin_mutation_par['exp_coef'] = 0.001
    bin_mutation_par['min_prob'] = 0.001

    # Instantiating genetic operators
    binmut_op = binmut.PunctualExpBinaryMutation(bin_mutation_par)
    #binmut_op = binmut.PunctualStepBinaryMutation(bin_mutation_par)
    cross_op = bincross.OnePointCrossover()
    selection_op = selec.ElitistTournamentSelection(0.6)

    # Population size
    N = 30

    # Generating population
    init_sol = random.rand(N, GRID_SIZE**2) > 0.5

    population = concatenate([init_sol, zeros((init_sol.shape[0], 1))], axis=1)
    new_population = zeros((population.shape[0] * 2, population.shape[1]))

    # Creating fitness tracking
    fitness_tracking = zeros((max_iteration, 3))
    for i in arange(max_iteration):

        # Population fitness evaluation
        population[:, -1] = objective_fun(population)

        # Population mutation
        population = binmut_op.mutate(population, i)

        # Population crossover
        new_population, idx_sons = cross_op.cross(population)

        # Fitness evaluation
        new_population[:, -1] = objective_fun(new_population)

        # Selecting individuals
        population = selection_op.select(new_population)

        fitness_tracking[i, 0] = max(population[:, -1])
        fitness_tracking[i, 1] = min(population[:, -1])
        fitness_tracking[i, 2] = mean(population[:, -1])

        print 'Max %s, Min %s, Mean %s, It %s' % (max(
            population[:, -1]), min(population[:, -1]), mean(
                population[:, -1]), i)

    idx_sort_fitness = argmax(population[:, -1])
    best_one = population[idx_sort_fitness, :-1]
    print float(
        sum(A == reshape(best_one, (GRID_SIZE, GRID_SIZE)))) / GRID_SIZE**2
    savetxt('fitness_evol', fitness_tracking, fmt='%1.4f')
    savetxt('best_sol', population[argmax(population[:, -1])], fmt='%1.4f')
Ejemplo n.º 2
0
def main():
        
    objects = loadtxt('knapsack.txt')
    max_capacity = 6404180
    max_iteration = 200

    bin_mutation_par = {}
    bin_mutation_par['steps'] = array([0.1, 0.2, 0.3, 0.4, 0.8])*max_iteration
    bin_mutation_par['steps'] = bin_mutation_par['steps'].astype('int')
    bin_mutation_par['probs'] = [0.5, 0.3, 0.2, 0.2, 0.15, 0.08]

    # Instantiating genetic operators
    binmut_op = binmut.ElitistPunctualStepBinaryMutation(bin_mutation_par)
    cross_op = bincross.ElitistTwoPointCrossover()    
    selection_op = selec.ElitistTournamentSelection(0.45)

    N = 50

    # Generating population
    init_sol = zeros((N, objects.shape[0]))
    i = 0
    while i < N:
       init_sol[i] = random.rand(1, objects.shape[0]) > 0.5
       if(sum(init_sol[i])/objects.shape[0] > 0.6 or sum(init_sol[i])/objects.shape[0] < 0.4):
           continue
       else:
          i += 1
    population = concatenate([init_sol, zeros((init_sol.shape[0], 1))], axis = 1)
    new_population = zeros((population.shape[0]*2, population.shape[1]))

    # Creating fitness tracking
    fitness_tracking = zeros((max_iteration, 3))
    
    for i in arange(max_iteration):

        # Population fitness evaluation
        population[:, -1] = objective_fun(objects, max_capacity, population)

        # Double the population thought mutation in populatipopulation.shape[0]on
        keyboard;
        new_population[0:population.shape[0], :] = population
        new_population[population.shape[0]:, :] = binmut_op.mutate(population, i)


        # Fitness evaluation
        new_population[:, -1] = objective_fun(objects, max_capacity, new_population)

        # Selecting individuals                
        population = selection_op.select(new_population)   

        fitness_tracking[i, 0] = max(population[:, -1])
        fitness_tracking[i, 1] = min(population[:, -1])
        fitness_tracking[i, 2] = mean(population[:, -1])
        
#        print 'Max %s, Min %s, Mean %s, It %s' % (max(population[:, -1]), min(population[:, -1]), mean(population[:, -1]), i)

    savetxt('fitness_evol', fitness_tracking, fmt='%1.4f')
    savetxt('best_sol', population[argmax(population[:, -1])], fmt='%1.4f')
Ejemplo n.º 3
0
def main():

    dist_matrix = loadtxt('dist.txt')
    max_iteration = 2000

    # Instantiating genetic operators
    binmut_op = permut.ElitistPercentReverseMutation(1)
    cross_op = permcross.ElitistPMXCrossover()
    selection_op = selec.ElitistTournamentSelection(0.5)

    # Population size
    N = 50

    # Generating population
    init_sol = zeros((N, dist_matrix.shape[1]))
    for i in arange(0, N):
        init_sol[i, :] = random.permutation(dist_matrix.shape[1])

    population = concatenate([init_sol, zeros((init_sol.shape[0], 1))], axis=1)
    new_population = zeros((population.shape[0] * 2, population.shape[1]))

    # Creating fitness tracking
    fitness_tracking = zeros((max_iteration, 3))

    for i in arange(max_iteration):

        # Population fitness evaluation
        population[:, -1] = objective_fun(dist_matrix, population)

        # Population mutation
        population = binmut_op.mutate(population, i)

        # Population crossover
        new_population, idx_sons = cross_op.cross(population)

        # Fitness evaluation
        new_population[:, -1] = objective_fun(dist_matrix, new_population)

        # Selecting individuals
        population = selection_op.select(new_population)

        fitness_tracking[i, 0] = max(population[:, -1])
        fitness_tracking[i, 1] = min(population[:, -1])
        fitness_tracking[i, 2] = mean(population[:, -1])

        print 'Max %s, Min %s, Mean %s, It %s' % (max(
            population[:, -1]), min(population[:, -1]), mean(
                population[:, -1]), i)

    savetxt('fitness_evol', fitness_tracking, fmt='%1.4f')
    savetxt('best_sol', population[argmax(population[:, -1])], fmt='%1.4f')
Ejemplo n.º 4
0
def main():

    objects = loadtxt('sum_diff_order.txt')
    max_iteration = 400

    bin_mutation_par = {}
    bin_mutation_par['steps'] = array([0.1, 0.2, 0.3, 0.4, 0.8
                                       ]) * max_iteration
    bin_mutation_par['steps'] = bin_mutation_par['steps'].astype('int')
    bin_mutation_par['probs'] = [0.3, 0.2, 0.08, 0.025, 0.015, 0.01]

    # Instantiating genetic operators
    binmut_op = binmut.PunctualStepBinaryMutation(bin_mutation_par)
    cross_op = bincross.OnePointCrossover()
    selection_op = selec.ElitistTournamentSelection(0.6)

    # Population size
    N = 200

    # Population option
    op = 1

    # Generating population
    if op == 1:
        init_sol = random.rand(N, objects.shape[0]) > 0.5
    else:
        init_sol = zeros((N, objects.shape[0]))
        i = 0
        while i < N:
            init_sol[i] = random.rand(1, objects.shape[0]) > 0.5
            if (sum(init_sol[i]) / objects.shape[0] > 0.6
                    or sum(init_sol[i]) / objects.shape[0] < 0.4):
                continue
            else:
                i += 1
    population = concatenate([init_sol, zeros((init_sol.shape[0], 1))], axis=1)
    new_population = zeros((population.shape[0] * 2, population.shape[1]))

    # Creating fitness tracking
    fitness_tracking = zeros((max_iteration, 3))

    for i in arange(max_iteration):

        # Population fitness evaluation
        population[:, -1] = objective_fun(objects, population)

        # Population mutation
        population = binmut_op.mutate(population, i)

        # Population crossover
        new_population, idx_sons = cross_op.cross(population)

        # Fitness evaluation
        new_population[:, -1] = objective_fun(objects, new_population)

        # Selecting individuals
        population = selection_op.select(new_population)

        fitness_tracking[i, 0] = max(population[:, -1])
        fitness_tracking[i, 1] = min(population[:, -1])
        fitness_tracking[i, 2] = mean(population[:, -1])

        print 'Max %s, Min %s, Mean %s, It %s' % (max(
            population[:, -1]), min(population[:, -1]), mean(
                population[:, -1]), i)

    savetxt('fitness_evol', fitness_tracking, fmt='%1.4f')
    savetxt('best_sol', population[argmax(population[:, -1])], fmt='%1.4f')