# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import numpy as np
import random


# +
def generateKromosom(uk_krom):
    krom = []
    
    for i in range(uk_krom):
        k = random.randint(0,9)
        krom.append(k)      
    
    return krom #ini 1 kromosom


def decodeKromosom(krom): #ini masih statis buat kromosom 8 gen!!!!!
#     x1_val = -3 + (6 / (9 * (10**-1 + 10**-2 + 10**-3))) * ((krom[0]*10**-1)+(krom[1]*10**-2)+(krom[2]*10**-3))
#     x2_val = -2 + (4 / (9 * (10**-1 + 10**-2 + 10**-3))) * ((krom[3]*10**-1)+(krom[4]*10**-2)+(krom[5]*10**-3))

    x1_val = -3 + (6 / (9 * (10**-1 + 10**-2 + 10**-3 + 10**-4))) * ((krom[0]*10**-1)+(krom[1]*10**-2)+(krom[2]*10**-3)+(krom[3]*10**-4))
    x2_val = -2 + (4 / (9 * (10**-1 + 10**-2 + 10**-3 + 10**-4))) * ((krom[4]*10**-1)+(krom[5]*10**-2)+(krom[6]*10**-3)+(krom[7]*10**-4))
    
    return [x1_val, x2_val]

def generatePopulasi(ukpop):
    pop = []
#     krom_val = []
    for i in range(ukpop):
        kro = generateKromosom(8) #kromosom 6 gen
        pop.append(kro)
        
    return pop


# +
def hitungFitnessAll(pop, ukpop):
    fit_all = []
    
#     krom_val = []
#     for i in range(ukpop):
#         k = decodeKromosom(b[i])
#         krom_val.append(k)
    
#     print(krom_val)
    
    for i in range(ukpop):
        k = decodeKromosom(pop[i])
        fit_func = -((((4 - (2.1 * k[0]**2) + (k[0]**4 / 3)) * k[0]**2) + (k[0]*k[1]) + ((-4 + (4*k[1]**2))*k[1]**2)))
        fit_all.append(fit_func)
        
#         fit_func = -(((4 - (2.1 * pop[i][0]**2) + (pop[i][0]**4 / 3)) * pop[i][0]**2) + (pop[i][0]*pop[i][1]) + ((-4 + (4*pop[i][1]**2))*pop[i][1]**2))
#         fit_all.append(fit_func)
    
    return fit_all


# +
# def totalFitness(fit_all, ukpop):
#     sum_fit = 0
#     for i in range(ukpop):
#         sum_fit = sum_fit + fit_all[i]
#     return sum_fit

def rouletteWheel(pop, fit_all):
    r = random.random()
#     print('random =', r)
    krom = 0
    while (r > 0 and krom < len(pop)-1):
        r = r - (fit_all[krom]/sum(fit_all))
        krom = krom + 1
#     print('yg kepilih: ',krom)   
    return krom


# -

def crossover(par, pc): #0.7
    
    r = random.random()
    if (r < pc):
        point = random.randint(0,7)  #diganti berdasarkan ukuran gen
        for i in range(point):
            par[0][i], par[1][i] = par[1][i], par[0][i]
            
        
    return par


def mutasi(par, pm): #dibawah 0.3, nanti 0.17
    r = random.random()
    if (r < pm):
        rep0 = random.randint(0,9)
        mut0 = random.randint(0,5)
        rep1 = random.randint(0,9)
        mut1 = random.randint(0,5)
        par[0][mut0] = rep0
        par[1][mut1] = rep1
    return par


def getElitisme(fit_all):    
    return fit_all.index(max(fit_all))


# +
ukpop = 50
ukkrom = 8
max_gen = 100
pc = 0.75
pm = 0.18

pop = generatePopulasi(ukpop)
newpop = []
print(pop)
print("HHHHHHHHHHH")
   
    
for i in range(max_gen):
    fit = hitungFitnessAll(pop, ukpop)
#     print(fit)
#     print('jumlah fitness :', sum(fit))
#     print(max(fit))
    best = getElitisme(fit)
#     print(best)
    print('krom best :', pop[best])
    print('value',decodeKromosom(pop[best]))
    newpop.append(pop[best])
    newpop.append(pop[best])
#     print("Populasi baru")
#     print(newpop)
    print()
    print()


    print("PENCARIAN PARENT")
    g = 0
    while (g < ukpop-2):
        par = []
        par.append(pop[rouletteWheel(pop, fit)])
        par.append(pop[rouletteWheel(pop, fit)])
        offspring = crossover(par, pc)
#         print('setelah di crossover :', offspring)
        offspring = mutasi(par, pm)
#         print('setelah di mutasi:', offspring)
        newpop.append(offspring[0])
        newpop.append(offspring[1])
#         print('newpopulasi ke',g,)
#         print(newpop)
        g = g + 1
    pop = newpop

print('FINISh')
#     print(newpop)
#     print(pop)
# -

ukpop = 10
ukkrom = 6
max_gen = 10
pc = 0.75
pm = 0.17

populasi = generatePopulasi(ukpop)
populasi_new = []
par = []
for i in range(max_gen):
    fitness = hitungFitnessAll(populasi, ukpop)
    best = getElitisme(fitness)
    populasi_new.append(populasi[best])
    populasi_new.append(populasi[best])
    g = 0
    while (g < ukpop):
        print(rouletteWheel(populasi, fitness))
        par.append(populasi[rouletteWheel(populasi, fitness)])
        par.append(populasi[rouletteWheel(populasi, fitness)])
        offspring = crossover(par, pc)
        offspring = mutasi(par, pm)
        populasi_new.append(offspring)
        g = g + 1
    print(max(fitness))
    populasi = populasi_new


def generalReplacement(pop, chi):
    


# +
 parent1 = pop[rouletteWheel(populasi, ukpop, fitness, sum_fit)]
        parent2 = pop[rouletteWheel(populasi, ukpop, fitness, sum_fit)]


# x1 = []
# x2 = []
# for i in range(3):
#     x = random.randint(0,9)
#     x1.append(x)
#     y = random.randint(0,9)
#     x2.append(y)
# print(x1)
# print(x1[2])
# print(x2)
# x1_val = -3 + (6 / (9 * (10**-1 + 10**-2 + 10**-3))) * ((x1[0]*10**-1)+(x1[1]*10**-2)+(x1[2]*10**-3))
# x2_val = -2 + (4 / (9 * (10**-1 + 10**-2 + 10**-3))) * ((x2[0]*10**-1)+(x2[1]*10**-2)+(x2[2]*10**-3))
# print(x1_val)
# print(x2_val)

# pop = []
# for i in range(100):
#     kro = generateKromosom()
#     pop.append(kro)
    
# print(pop[2])


# def generateKromosom():
#     x1 = []
#     x2 = []
    
#     for i in range(3):
#         x = random.randint(0,9)
#         x1.append(x)
#         y = random.randint(0,9)
#         x2.append(y)
        
#     x1_val = -3 + (6 / (9 * (10**-1 + 10**-2 + 10**-3))) * ((x1[0]*10**-1)+(x1[1]*10**-2)+(x1[2]*10**-3))
#     x2_val = -2 + (4 / (9 * (10**-1 + 10**-2 + 10**-3))) * ((x2[0]*10**-1)+(x2[1]*10**-2)+(x2[2]*10**-3))
        
    
#     return [x1_val, x2_val] #ini 1 kromosom
# HAPUS AJA GPP



# def rouletteWheel(pop, ukpop, fitness, sum_fit):
#     rand_sel = []
#     pi_all = []
#     pi_temp = []
#     parent = []
    
#     for i in range(ukpop):
#         pi = fitness[i]/sum_fit
#         pi_all.append(pi)
    
#     sum_pi = 0
#     for i in range(ukpop):
#         sum_pi = sum_pi + pi[i]
#         pi_temp.append(sum_pi)
        
        
#     num_rand = random.random()
#     while(num_rand > pi_temp[i]):
#         i = i + 1
#     parent.append(pop[i])

#     num_rand = random.random()
#     krom = 1
#     while (num_rand > 0):
#         r -= fitness[krom]/sum_fit
#         krom++
        
#     for i in range(ukpop/3):
#         i = 0
#         num_rand = random.random()
#         while(num_rand > pi_temp[i]):
#             i = i + 1
#         parent.append(pop[i])
        
#     return parent


x = [[1,2,3],[4,5,6]]
print(x)
print(x[0])
print(x[0][2])

p = 3
for i in range(p,6):
    print(i)
    
    

roulette wheel return individu
terus crossover
a = roulette wheel
b = roulette wheel
return anak baru



b = generatePopulasi(20)
print(b)
print("halo")
print(b[1])
print("kkkk")

kro = [8, 0, 9, 8, 4, 6]
print(kro)
print(decodeKromosom(kro))

# +
import numpy
my = [1, 2, 3, 100, 5]
x = numpy.argsort(my)

# vowels list
vowels = [1, 2, 3, 100, 5]
y = max(my)
# element 'e' is searched
index = vowels.index(y)

# index of 'e' is printed
print('The index of e:', index)

# +


pop = generatePopulasi(10)
print('Populasi ')
print(pop)
print()
fit = hitungFitnessAll(pop, 10)
print('Fitness')
print(fit)
print()
newpop = []
bestt = getElitisme(fit)
print('krom best :', pop[best])
print('value',hitungFitness(pop[bestt]))
newpop.append(pop[bestt])
newpop.append(pop[bestt])
print("PENCARIAN PARENT")
t = 0
while (t < 8):
    par = []
    print('Parent ke-',t)
    par.append(tournamentSelection(pop, 3, 10))
    par.append(tournamentSelection(pop, 3, 10))
    print('Terpilih')
    print(par)
    print()
    offspring = crossover(par, pc)
    print('Crossover')
    print(offspring)
    print()
    offspring = mutasi(offspring, pm)
    print('Mutasi')
    print(offspring)
    print()
    print()
    newpop.append(offspring)
    t = t + 2
print('Populasi')
print(newpop)
print()



# +
IN<NAME>

pop = generatePopulasi(10)
print('Populasi ')
print(pop)
print()
fit = hitungFitnessAll(pop, 10)
print('Fitness')
print(fit)
print()
newpop = []
bestt = getElitisme(fit)
print('krom best :', pop[bestt])
print('value',hitungFitness(pop[bestt]))
newpop.append(pop[bestt])
newpop.append(pop[bestt])
print("PENCARIAN PARENT")
t = 0
while (t < 8):
    print('Parent ke-',t)
    par1 = tournamentSelection(pop, fit, 3, 10)
    par2 = tournamentSelection(pop, fit, 3, 10)
    while (par1 == par2):
        par2 = tournamentSelection(pop, fit, 3, 10)
    print('Terpilih')
    print('Parent 1:', par1)
    print('Indeks Parent 1:', pop.index(par1))
    print('Parent 2:', par2)
    print('Indeks Parent 2:', pop.index(par2))
    print()
    offspring = crossover(par1, par2, 0.7)
    print('Crossover')
    print(offspring)
    print()
    offspring = mutasi(offspring[0], offspring[1], 0.13)
    print('Mutasi')
    print(offspring)
    print()
    print()
    newpop += offspring
    t = t + 2
print('Populasi')
print(newpop)
print()

fit = hitungFitnessAll(pop, 10)
print('Fitness')
print(fit)
print()
newpop = []
bestt = getElitisme(fit)
print('krom best :', pop[bestt])
print('value',hitungFitness(pop[bestt]))

# +
# n_pop = 50
# n_krom = 8
# n_tour = n_pop//3
# max_gen = 100
# pc = 0.6
# pm = 0.13

# populasi = generatePopulasi(n_pop)
    
# for i in range(max_gen):
#     newpop = []
#     fitness = hitungFitnessAll(populasi, n_pop)
    
#     best = getElitisme(fitness)
#     print('krom best :', populasi[best])
#     print('value',hitungFitness(populasi[best]))
#     newpop.append(populasi[best])
#     newpop.append(populasi[best])
#     print()
#     print()
    
#     for i in range(n_pop-2//2):
#         par = []
#         par.append(tournamentSelection(populasi, n_tour, n_pop))
#         par.append(tournamentSelection(populasi, n_tour, n_pop))
#         offspring = crossover(par, pc)
#         offspring = mutasi(offspring, pm)
#         newpop.append(offspring[0])
#         newpop.append(offspring[1])
#     populasi = newpop

# print('FINISH')

# +
# n_pop = 20
# n_krom = 8
# n_tour = 2*n_pop
# max_gen = 100
# pc = 0.7
# pm = 0.13

# populasi = generatePopulasi(n_pop)
# newpop = []
    
# print(populasi)
# best = getElitisme(fitness)
# print('krom best :', populasi[best])
# print('value',hitungFitness(populasi[best]))
# newpop.append(populasi[best])
# newpop.append(populasi[best])
# print()
# print()

# print("<NAME>")
# t = 0
# while (t < n_pop//2):
#     par = []
#     par.append(tournamentSelection(populasi, n_tour, n_pop))
#     par.append(tournamentSelection(populasi, n_tour, n_pop))
#     offspring = crossover(par, pc)
#     offspring = mutasi(offspring, pm)
#     newpop.append(offspring[0])
#     newpop.append(offspring[1])
#     t = t + 1
# populasi = newpop

# print('FINISH')
# print(newpop)
# fitness = hitungFitnessAll(newpop, n_pop)
# best = fitness.index(max(fitness))
# print('krom best :', newpop[best])
# print('value',hitungFitness(newpop[best]),len(newpop))

# +
kpop = 10

pop = generatePopulasi(kpop)
terbaek = []

print('Populasi ')
print(pop)
fit = hitungFitnessAll(pop, kpop)
print('Fitness')
print(fit)
print()
newpop = []

bestt = getElitisme(fit)
print('krom best :', pop[bestt])
bestla = hitungFitness(pop[bestt])
print('value', bestla)
terbaek.append(bestla)
newpop.append(pop[bestt])
newpop.append(pop[bestt])
# print("PENCARIAN PARENT")
t = 0
while (t < kpop-2):
    print('Populasi setelah iterasi ke', t)
    print(pop)
    print()
    print('Parent ke-',t)
    par1 = tournamentSelection(pop, kpop//4, kpop)
    par2 = tournamentSelection(pop, kpop//4, kpop)
    if (par1 == par2):
        idx = pop.index(par1)
        if (idx==0):
            par2 = pop[idx+1]
        else:
            par2 = pop[idx-1]
    print('Terpilih')
    print('Parent 1:', par1)
    print('Indeks Parent 1:', pop.index(par1))
    print('Parent 2:', par2)
    print('Indeks Parent 2:', pop.index(par2))
    print()
    pr1 = copy.deepcopy(par1)
    pr2 = copy.deepcopy(par2)
    offspring = crossover(pr1, pr2, 0.82)
    print('Crossover')
    print(offspring)
    print()
    offspring = mutasi(offspring[0], offspring[1], 0.21)
    print('Mutasi')
    print(offspring)
    print()
    print()
    newpop += offspring
    t = t + 2
print('Populasi')
print(newpop)
print()
pop = newpop
bestt = getElitisme(fit)
print('krom best :', newpop[bestt])
bestla = hitungFitness(newpop[bestt])
print('value', bestla)


# +
# Parent ke- 0
# Terpilih
# Parent 1: [3, 1, 1, 3, 4, 5, 6, 7]
# Indeks Parent 1: 6
# Parent 2: [9, 5, 2, 6, 6, 3, 2, 9]
# Indeks Parent 2: 7

# Crossover
# ([9, 5, 2, 6, 6, 3, 6, 7], [3, 1, 1, 3, 4, 5, 2, 9])
import copy
p = generatePopulasi(5)
print(p)
par = []
par = copy.deepcopy(p)
print(par)
par1 = copy.deepcopy(p[2])
par2 = copy.deepcopy(p[1])
r = random.random()
if (r < 0.99):
    point = random.randint(0,7)
    for i in range(point):
        par1[i], par2[i] = par2[i], par1[i]
print(par1,par2)
print(p)
print(par)


# +
FIX <NAME>

kpop = 10

pop = generatePopulasi(kpop)
terbaek = []

for i in range(300):
    print('Populasi ')
    fit = hitungFitnessAll(pop, kpop)
    # print('Fitness')
    # print(fit)
    # print()
    newpop = []

    bestt = getElitisme(fit)
    print('krom best :', pop[bestt])
    bestla = hitungFitness(pop[bestt])
    print('value', bestla)
    terbaek.append(bestla)
    newpop.append(pop[bestt])
    newpop.append(pop[bestt])
    # print("PENCARIAN PARENT")
    t = 0
    while (t < kpop-2):
    #     print('Parent ke-',t)
        par1 = tournamentSelection(pop, kpop//4, kpop)
        par2 = tournamentSelection(pop, kpop//4, kpop)
        while (par1 == par2):
            par2 = tournamentSelection(pop, kpop//4, kpop)
#         if (par1 == par2):
#             idx = pop.index(par1)
#             if (idx==0):
#                 par2 = pop[idx+1]
#             else:
#                 par2 = pop[idx-1]
    #     print('Terpilih')
    #     print('Parent 1:', par1)
    #     print('Indeks Parent 1:', pop.index(par1))
    #     print('Parent 2:', par2)
    #     print('Indeks Parent 2:', pop.index(par2))
    #     print()
        pr1 = copy.deepcopy(par1)
        pr2 = copy.deepcopy(par2)
        offspring = crossover(pr1, pr2, 0.82)
    #     print('Crossover')
    #     print(offspring)
    #     print()
        offspring = mutasi(offspring[0], offspring[1], 0.13)
    #     print('Mutasi')
    #     print(offspring)
    #     print()
    #     print()
        newpop += offspring
        t = t + 2
    # print('Populasi')
    # print(newpop)
    # print()
    pop = newpop

print(len(terbaek))
print(max(terbaek))

StarcoderdataJupyterScriptsDedupFiltered