-
Notifications
You must be signed in to change notification settings - Fork 0
/
genetic_algorithm.py
135 lines (119 loc) · 3.55 KB
/
genetic_algorithm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# -*- coding: utf-8 -*-
"""Tugas01.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1qwg-4BdR1IKfAIB6aPBXUMbC1Auq9VlT
"""
import numpy as np
import random
import math
def generate(tot_pop, bits):
arr = []
for i in range(tot_pop):
chromosome = []
for j in range(bits):
chromosome.append(random.choice([0,1]))
arr.append(chromosome)
return arr
def fungsi(x,y):
return (math.cos(x) * math.sin(y)) - (x / (y**2 + 1))
def fitness(populasi):
b = 1
panjang = len(populasi)
hasil = []
for lop in range(0,panjang):
x1,x2 = decode(populasi[lop])
nilai = fungsi(x1, x2)
hasil.append(b-nilai)
return hasil
def hitung_bawah(len):
hasil = 0
for say in range(1,len):
hasil += 2 ** (-say)
return hasil
def hitung_kanan(kromosom):
panjang = len(kromosom)
hasil = 0
for say in range(1,panjang):
hasil += kromosom[say] * 2 ** (-say)
return hasil
def decode(kromosom):
panjang = len(kromosom)
batas = round(panjang / 2)
x1 = kromosom[:batas] #ngambil array paling bawah sampai batas
x2 = kromosom[batas:] #ngambil array batas sampai paling atas
batas_x1 = 2-(-1)
batas_x2 = 1-(-1)
bawah = hitung_bawah(len(x1))
kanan_x1 = hitung_kanan(x1)
kanan_x2 = hitung_kanan(x2)
nilai_x1 = -1 + batas_x1 * kanan_x1 / bawah
nilai_x2 = -1 + batas_x2 * kanan_x2 / bawah
return [nilai_x1, nilai_x2]
def TournamentSelect(populasi, pitnes):
temp = []
panjang = len(populasi)-1
for aezakmi in range(5):
temp.append(random.randint(0, panjang))
kandidat = []
for you in range(5):
kandidat.append([populasi[temp[you]],pitnes[temp[you]]])
urut = sorted(kandidat, key=lambda id: id[1], reverse=True) #ngurutin kandidat berdasarkan fitness
return urut[0][0], urut[1][0]
def Crossover(ibu, bapak, peluang):
if random.random() <= peluang: # Peluang Cross
acak = random.randint(1, len(ibu)-1)
anak1 = []
anak1.extend(bapak[:acak])
anak1.extend(ibu[acak:])
anak2 = []
anak2.extend(ibu[:acak])
anak2.extend(bapak[acak:])
else:
anak1 = ibu
anak2 = bapak
return anak1, anak2
def mutasi(c1, c2, peluang):
anak1 = c1.copy()
anak2 = c2.copy()
for i in range(len(c1)):
if random.uniform(0,1) <= peluang:
if anak1[i] == 0:
anak1[i] = 1
elif c1[i] == 1:
anak1[i] = 0
if random.uniform(0,1) <= peluang:
if anak2[i] == 0:
anak2[i] = 1
elif c2[i] == 1:
anak2[i] = 0
#buat if acak kalo cocok ganti jadi 0/1
return anak1, anak2
def elitism(populasi, pitnes):
urut = sorted(zip(populasi,pitnes), key=lambda id: id[1], reverse=True) #zip buat nyatuiin populasi sama fitnes jadi satu array
return [urut[0], urut[1]]
banyak = 30
lebar = 50
generasi = 100
peluang_cross = 0.8
peluang_mut = 0.9
populasi = generate(banyak, lebar)
print ('Populasi : ',populasi,'\n')
for jum in range(generasi):
aezakmi = fitness(populasi)
cucubaru = elitism(populasi, aezakmi)
cucubaru = [cucubaru[0][0],cucubaru[1][0]]
while len(cucubaru) < banyak:
ibu, bapak = TournamentSelect(populasi, aezakmi)
anak1, anak2 = Crossover(ibu, bapak, peluang_cross)
anak1, anak2 = mutasi(anak1, anak2, peluang_mut)
cucubaru.extend([anak1, anak2])
populasi = cucubaru
aezakmi = fitness(populasi)
solusi = elitism(populasi, aezakmi)
print(
'Solusi Terbaik = ', solusi[0][0],'\n',
'X1 =', decode(solusi[0][0])[0],'\n',
'X2 = ', decode(solusi[0][0])[1],'\n',
'Nilai Fitnes = ', solusi[0][1]
)