This repository was archived by the owner on Nov 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathga.py
160 lines (122 loc) · 4.26 KB
/
ga.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
import os
import random
# OK representasi genetika
def createGen(target):
return ''.join([chr(random.randint(32, 126)) for o in range(len(target))])
# OK fitness
def calculateFitness(gen, target):
fitness = 0
for i in range(len(target)):
if (gen[i] == target[i]):
fitness += 1
return (fitness / len(target)) * 100
# OK populasi
def createPopulation(target, besarPopulasi):
populasi = dict()
for i in range(besarPopulasi):
temp = dict()
gen = createGen(target)
temp['gen'] = gen
temp['fitness'] = calculateFitness(gen, target)
populasi[str(i+1)] = temp
return populasi
# OK seleksi
def selection(populasi):
result1 = dict()
result2 = dict()
arryGen = []
arryFitness = []
for i in populasi:
arryFitness.append(float(populasi[i]['fitness']))
arryGen.append(populasi[i]['gen'])
indexparent1 = arryFitness.index(max(arryFitness))
result1['gen'] = arryGen[indexparent1]
result1['fitness'] = arryFitness[indexparent1]
arryFitness[indexparent1] = 0.0
indexparent2 = arryFitness.index(max(arryFitness))
if (indexparent1 == indexparent2):
indexparent2 += 1
result2['gen'] = arryGen[indexparent2]
result2['fitness'] = arryFitness[indexparent2]
return result1, result2
# OK crossover
def crossOver(parent1, parent2, point):
p1, p2 = list(parent1), list(parent2)
for i in range(point, len(p1)):
p1[i], p2[i] = p2[i], p1[i]
return ''.join(p1), ''.join(p2)
# OK mutasi
def mutation(child, lajuMutasi):
mutant = list(child)
for i in range(len(mutant)):
if random.uniform(0.0, 1.0) <= lajuMutasi:
mutant[i] = chr(random.randint(32, 126))
return ''.join(mutant)
# OK regenerasi
def regeneration(children, oldPopulasi):
newPopulasi = dict()
templist = []
for i in range(len(oldPopulasi)):
temp = []
temp.append(oldPopulasi[str(i+1)]['gen'])
temp.append(oldPopulasi[str(i+1)]['fitness'])
templist.append(temp)
templist.sort(key=lambda x: x[1])
for j in range(len(templist)):
tempd = dict()
tempd['gen'] = templist[j][0]
tempd['fitness'] = templist[j][1]
newPopulasi[str(j+1)] = tempd
for k in range(len(children)):
newPopulasi[str(k+1)] = children[k]
return newPopulasi
# OK stop
def termination(populasi):
best, none = selection(populasi)
if (int(best['fitness']) == 100):
isLoop = False
else:
isLoop = True
return best['gen'], isLoop
# OK hasil
def logging(populasi, target, solusi, generasi):
os.system('cls' if os.name == 'nt' else 'clear')
print("Target :", target, "\n")
print("Solusi :", solusi)
print("Generasi:", generasi, "\n")
for i in range(len(populasi)):
gen = populasi[str(i+1)]['gen']
fitness = populasi[str(i+1)]['fitness']
print("Gen :", gen, "| Fitness :", fitness)
# Main
if __name__ == "__main__":
target = str(input('Target : '))
besarPopulasi = int(input('Populasi : '))
lajuMutasi = float(input('Mutasi : '))
populasi = createPopulation(target, besarPopulasi)
isLoop = True
generasi = 0
while isLoop:
# individu terbaik / seleksi
parent1, parent2 = selection(populasi)
# crossover
# cp = round(len(parent1['gen'])/2) # -> fixed
cp = random.randint(1, len(parent1['gen'])) # -> random
child1, child2 = crossOver(parent1['gen'], parent2['gen'], cp)
# mutasi
mutasi1 = dict()
mutasi2 = dict()
result1 = mutation(child1, lajuMutasi)
result2 = mutation(child2, lajuMutasi)
mutasi1['gen'] = result1
mutasi2['gen'] = result2
mutasi1['fitness'] = calculateFitness(result1, target)
mutasi2['fitness'] = calculateFitness(result2, target)
# calon anggota / regenerasi
children = [mutasi1, mutasi2]
populasi = regeneration(children, populasi)
# show
generasi += 1
best, isLoop = termination(populasi)
logging(populasi, target, best, generasi)
input("\nFinished")