Algoritmo Evolutivo Python
Algoritmo Evolutivo Python
import random
import pandas as pd
# Ingresar datos
g1 = (1, 5, "DIESEL", 15, 0, 12, 27.6)
g2 = (2, 2, "DIESEL", 1, 0, 20, 43.5)
g3 = (3, 2, "DIESEL", 2, 0, 20, 43.5)
g4 = (4, 2, "CARBON", 1, 0, 76, 14.4)
g5 = (5, 2, "CARBON", 2, 0, 76, 14.4)
g6 = (6, 6, "GAS NATURAL", 7, 0, 100, 23)
g7 = (7, 1, "CARBON", 7, 0, 155, 11.6)
g8 = (8, 1, "CARBON", 15, 0, 155, 11.7)
g9 = (9, 1, "CARBON", 16, 0, 155, 11.7)
g10 = (10, 1, "CARBON", 23, 0, 155, 11.7)
g11 = (11, 6, "GAS NATURAL", 22, 0, 197, 22.1)
g12 = (12, 1, "CARBON", 23, 0, 350, 11.4)
g13 = (13, 1, "NUCLEAR", 21, 0, 400, 6)
g14 = (14, 1, "NUCLEAR", 18, 0, 400, 6)
B = [b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15,
b16, b17, b18, b19, b20, b21, b22, b23, b24]
#Crear arreglos
L = [l1, l2, l3, l4, l5, l6, l7, l8, l9, l10, l11, l12, l13, l14, l15,
l16, l17, l18, l19, l20, l21, l22, l23, l24, l25,
l26, l27, l28, l29, l30, l31, l32, l33]
G = [g1, g2, g3, g4, g5, g6, g7, g8, g9, g10, g11, g12, g13, g14]
G = np.array(G, dtype=dtype)
L = pd.DataFrame(L)
# Ordenar arreglo de menor a mayor costo
GS = np.sort(G, order='Costo')
class DNA():
def __init__(self, mutation_rate, n_individuals, n_selection,
n_generations, verbose=True, data=0, p=0, dataL = 0, dataB = 0):
self.mutation_rate = mutation_rate
self.n_individuals = n_individuals
self.n_selection = n_selection
self.n_generations = n_generations
self.verbose = verbose
self.data = data
self.length = len(data)
self.p = p
self.dataL = dataL
self.dataB = dataB
# Crear listas
def create_list(self, l):
lst = []
for i in range(l):
lst.insert(i, 0)
return lst
# Crear poblacion
def create_population(self):
population = [self.create_individual() for i in
range(self.n_individuals)]
return population
return c
return c
# Cruce
def reproduction(self, population, selected):
for i in range(len(population)):
point = np.random.randint(1, len(population[0]) - 1)
father = random.sample(selected, 2)
population[i][:point] = father[0][:point]
population[i][point:] = father[1][point:]
return population
# Corregir si es menor
def corindmenor(self, individual):
c = 0
for i in individual:
c += i
if c < self.p:
m = self.p - c
for i in range(len(individual)):
max = int(self.data[i][5]) * int(self.data[i][1])
d = max - individual[i]
if d < m:
individual[i] = max
m -= d
elif d > m:
individual[i] = individual[i] + m
m = 0
return individual
# Corregir si es mayor
def corindmayor(self, individual):
c = 0
for i in individual:
c += i
if c > self.p:
n = c - self.p
for i in range(len(individual)):
if n > 0:
if individual[-i - 1] > n:
individual[-i - 1] = individual[-i - 1] - n
break
elif individual[-i - 1] < n:
n -= individual[-i - 1]
individual[-i - 1] = 0
return individual
# Mutar poblacion
def mutation(self, population):
for i in range(len(population)):
if random.random() <= self.mutation_rate:
min = int(self.data[i][4])
max = int(self.data[i][5]) * int(self.data[i][1])
point = random.randint(0, len(population[0]) - 1)
if population[i][point] != 0:
new_value = random.uniform(min, max)
while new_value == population[i][point]:
new_value = random.uniform(min, max)
population[i][point] = new_value
return population
if c > self.p:
individual = self.create_individual()
elif c < self.p:
individual = self.create_individual()
for i in range(len(individual)):
max = int(self.data[i][5]) * int(self.data[i][1])
if individual[i] > max:
de += 1
if de > 0:
individual = self.create_individual()
return individual
# Imprimir individuos
def printpop(self, population):
temp = population[:]
for i in temp:
i = np.array(i)
i = np.round(i, 2)
print(self.reord(i))
#Reordenar
def reord(self, individual):
temp = self.create_list(self.length)
for i in range(len(individual)):
pos = self.data[i][0]-1
temp[pos] = individual[i]
return temp
def run_gen_alg(self):
population = self.create_population()
for i in range(self.n_generations):
if self.verbose:
print('________________________')
print("GENERACION", i)
#print('POBLACION')
#self.printpop(population)
selected = self.selection(population)
population = self.reproduction(population, selected)
population = self.mutation(population)
population = self.corpop(population)
population = self.deletep(population)
print('Flujo DC:', self.fDC(population[0]))
for i in range(len(Bbus)):
c = 0
for j in range(len(Bbus)):
c += Bbus[i][j]
Bbus[i][i] = -c
Bbus = np.round(Bbus, 2)
BbusInv = np.linalg.inv(Bbus)
return BbusInv
for i in range(len(self.dataB)):
mp[i] -= self.dataB[i][1] * self.p
mp = mp/self.p
return mp
return temp