Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from Tkinter import *
- import random
- import math
- from random import shuffle
- size_population = 10
- mutation_probability = 0.1
- accuracy = 3
- lower_bound = 17
- upper_bound = 18
- long_string = 0
- number_generation = 10
- intial_population = [] #almacena los cromosomas
- decoded_population = []#almacena el valor de los cromosomas en decimal
- evaluated_in_function = [] #alamacena el valor evaluado en la funcion objetivo
- parents1 = []#almacena el numero del primer padre obtenido en el torneo
- parents2 = []#almacena el numero del segundo padre obtenido en el torneo
- to_next_generation = []#cuando se realiza el cruce aqui se guardaran los hijos y luego la poblacion inicial
- #sera reemplazada por estos
- vacia = [] #para limpiar los valores de las listas y estas vuelvan a llenarse desde el principio
- def long_string():
- return (int) (math.ceil(math.log(abs(upper_bound - lower_bound) * math.pow(10.0, accuracy) ,2)))
- def binary_(bitstring):#funcion deco Paco
- aux = 0
- aux1 = 0
- bitstring.reverse()
- for i in range (0, long_string):
- aux+= math.pow(2,i)*bitstring[i]
- aux1= lower_bound + ((aux*upper_bound)/(math.pow(2,long_string)-1))
- aux1 ="%0.*f"%(accuracy,aux1)
- return (float) (aux1)
- def decode_number(binary):#nueva funcion decodifica
- binary_inside = []
- binary_inside = binary[:]
- binary_inside.reverse()
- decimal_value =0
- for i in range(0,len(binary)):
- decimal_value += math.pow(2,i)*binary_inside[i]
- (int)(decimal_value)
- return (lower_bound+decimal_value*((upper_bound-lower_bound)/(math.pow(2,long_string)-1))) #funcion Paco #nueva funcion decodificar cromosoma
- def random_gene():
- gene=[]
- for i in range(0,long_string):
- if random.random()<0.5: #Uniform population
- gene.append(0)
- else:
- gene.append(1)
- return gene
- def FO1(x):
- return (x + 5* math.sin(3*x) + 8*math.cos(5*x))
- def crossover_2_point(parent1,parent2):
- children1 = []
- children2 = []
- cross1 = 0
- cross2 = 0
- cross1 = (int)(random.random()*long_string)
- cross2 = (int)(random.random()*long_string)
- while( cross1 >= cross2):
- cross1 = (int)(random.random()*long_string)
- cross2 = (int)(random.random()*long_string)
- if( cross2 > cross1):
- break
- #print("cruce 1",cross1,"cruce 2", cross2)
- for i in range (0,cross1):
- children1.append(parent2[i])
- children2.append(parent1[i])
- for i in range (cross1,cross2):
- children1.append(parent1[i])
- children2.append(parent2[i])
- for i in range ( cross2,long_string):
- children1.append(parent2[i])
- children2.append(parent1[i])
- #print(parent1)
- #print(parent2)
- #print("TERMINA PADRES")
- #print(children1)
- #print(children2)
- to_next_generation.append(children1)
- to_next_generation.append(children2)
- def make_pairs(genes):
- rand1 = []
- rand2 = []
- erase = []
- rand1 = range(size_population)
- rand2 = range(size_population)
- shuffle(rand1)
- shuffle(rand2)
- #print(rand1)
- #print(rand2)
- index3 = 1
- index2 = 1
- for i in range(0,size_population,2):
- if(genes[rand1[i]] >= genes[rand1[index2]]):
- parents1.append(rand1[i])
- else:
- parents1.append(rand1[index2])
- index2 = index2 + 2
- for i in range(0,size_population,2):
- if(genes[rand2[i]] >= genes[rand2[index3]]):
- parents2.append(rand2[i])
- else:
- parents2.append(rand2[index3])
- index3 = index3 + 2
- rand1 = erase[:]
- rand2 = erase[:]
- index2 = 1
- index3 = 1
- #print(parents1,"luego",parents2)
- def mutation():
- rand = random.random()
- if( rand < mutation_probability):
- chromosome = (int)(random.random()* size_population)
- gen = (int)(random.random()* long_string)
- #print("Se mutara el chromosoma ",chromosome,"en la posicion ",gen)
- if(intial_population[chromosome][gen] ==1):
- intial_population[chromosome][gen] = 0
- else:
- intial_population[chromosome][gen] = 1
- #PRIMEROS PASOS
- long_string = long_string()
- for i in range(0,size_population): #generamos la poblacion aleatoria
- intial_population.append(random_gene())
- #print(intial_population)
- #REPETICION PARA LAS SIGUIENTES GENERACIONES
- for generacion in range(0,number_generation):
- for j in range(0,size_population): #decodificamos la poblacion inicial
- decoded_population.append(decode_number(intial_population[j]))
- for k in range(0,size_population): #evaluamos en la funcion objetivo FO1
- evaluated_in_function.append(FO1(decoded_population[k]))
- #print(decoded_population)
- make_pairs(evaluated_in_function)#realizamos la seleccion de parejas por torneo
- for l in range(0,len(parents1)): #realizamos la cruza de los padres seleccionados y generamos los dos hijos que se iran a la siguiente
- #generacion
- crossover_2_point(intial_population[parents1[l]],intial_population[parents2[l]])
- #print(intial_population)
- intial_population = to_next_generation[:]
- mutation()
- #print(intial_population)
- for j in range(0,size_population): #decodificamos la poblacion inicial
- decoded_population.append(decode_number(intial_population[j]))
- for k in range(0,size_population): #evaluamos en la funcion objetivo FO1
- evaluated_in_function.append(FO1(decoded_population[k]))
- #evaluated_in_function.sort(reverse= True)
- for p in range(0,size_population):
- print ("%.3f" % ( evaluated_in_function[p]))
- print("end generation number: ",generacion+1)
- funcion_generacion = vacia[:]
- to_next_generation = vacia[:]
- decoded_population = vacia[:]
- evaluated_in_function = vacia[:]
- parents1 = vacia[:]
- decode_numberparents2 = vacia[:]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement