-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAI_GeneticAlgorithm_Searching.py
167 lines (132 loc) · 4.57 KB
/
AI_GeneticAlgorithm_Searching.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import random
import math
import copy
#GA for searching, blom learning
#binary population
#pakai biner
def buat_populasi(ukuran): #inisialisasi populasi
new_pop_baris = [] #banyak gen 8
new_pop_kol = []
i = 0
while (i < ukuran):
j = 0
while (j < 8):
new_pop_kol.append(random.randint(0,1))
j = j+1
new_pop_baris.append(new_pop_kol)
new_pop_kol = []
i = i+1
return new_pop_baris #output populasi
def dekode_kromosom(arr_krom): #dekode kromosom
hasil = [1]
hasil[0] = -3 + 3 - 3 / (math.pow(2, -1) + math.pow(2, -2) + math.pow(2, -3) + math.pow(2, -4)) * ((arr_krom[0] * math.pow(2, -1)) + (arr_krom[1] * math.pow(2, -2)) + (arr_krom[2] * math.pow(2, -3)) + (arr_krom[3] * math.pow(2, -4)))
hasil.append(-2 + 2 - 2 / (math.pow(2, -1) + math.pow(2, -2) + math.pow(2, -3) + math.pow(2, -4)) * ((arr_krom[4] * math.pow(2, -1)) + (arr_krom[5] * math.pow(2, -2)) + (arr_krom[6] * math.pow(2, -3)) + (arr_krom[7] * math.pow(2, -4))))
return hasil #ouput hasil dekode
def f_objektif(hasil_dekode): #nilai fungsi objektif
x1 = hasil_dekode[0]
x2 = hasil_dekode[1]
hasil_f = (4 - 2.1 * math.pow(x1,2) + math.pow(x1,4)/3) * math.pow(x1,2) + x1 * x2 + (-4 + 4 * math.pow(x2,2) * math.pow(x2,2))
return hasil_f #output hasil fungsi objektif
def fitness(arr_pop = []): #input populasi
fit = []
i = 0
while (i < len(arr_pop)):
dekode = dekode_kromosom(arr_pop[i])
obj = f_objektif(dekode)
fit.append(1/obj+0.01)
i = i+1
return fit #output, fitness per individu
def seleksi_parent(arr_pop=[],fitness=[]):
prop = [] #list untuk hitung proporsi
parent = [] # 2 parent
tot_fitness = sum(fitness) #total fitness
i = 0
while (i<len(arr_pop)):
prop.append(fitness[i]/tot_fitness)
i = i+1
j = 0 #dua parent
while (j<2):
random_num = random.uniform(0,1)
current_prop = 0 #total proporsi saat ini
k = 0
while (current_prop<random_num):
current_prop += prop[k]
k = k + 1
parent.append(arr_pop[k-1])
j=j+1
return parent
def crossover(parent=[]):
#probabilitas crossover random
child_baris = []
child_kolom = []
prob_cross = round(random.uniform(0,1),1) #random probabilitas range 0-1, 1 angka dibelakang koma
random_num= random.uniform(0,1)
if (random_num < prob_cross):
child = parent.copy()
titik_potong = random.randint(1,7)
#child_kolom.append(parent.copy())#child 1
#child_kolom.append(parent.copy())#child 2
i = 0
while (i != titik_potong and i < 8):
child[0][titik_potong] = parent[1][titik_potong]
child[1][titik_potong] = parent[0][titik_potong]
print (titik_potong)
i = i+1
return child
else:
return parent
def mutasi(child=[]):
prob_mutasi = round(random.uniform(0,1),1)
i= 0
j=0
while(j<2):
while (i<len(child)):
random_num = random.uniform(0,1)
if (random_num < prob_mutasi):
child[j][i] = random.randint(0,1)
i = i+1
j=j+2
return child
def fit_rendah(fit=[]):#mencari fitness terendah
curr_fit = fit[0]
i = 0
j=0
while (i<len(fit)):
if curr_fit > fit[i]:
curr_fit = fit[i]
j = i
i = i+1
return j
def best_fit(fit=[]):
curr_fit = fit[0]
i = 0
j = 0
while (i < len(fit)):
if curr_fit < fit[i]:
curr_fit = fit[i]
j = i
i = i + 1
return j
def generational_replace(child = [], pop_lama=[],fit=[]):#fitness terendah akan digantikan; child hasil cross
pop_baru = pop_lama.copy()
fit_terendah = fit_rendah(fit)
pop_baru[fit_terendah]= child[0];
return pop_baru
if __name__ == "__main__":
set_populasi = 10
generasi = 1000
i = 0
populasi = buat_populasi(set_populasi)
while (i < generasi):
fit = fitness(populasi)
get_best = best_fit(fit)
parent_select = seleksi_parent(populasi,fit)
child = crossover(parent_select)
populasi = generational_replace(child,populasi,populasi)
i = i + 1
print("generasi ke- ",i)
print(populasi)
f_minimal = dekode_kromosom(populasi[get_best])
hasil_f = f_objektif(f_minimal)
print("X dan Y", f_minimal)
print("Hasil fungsi: ",hasil_f)