-
Notifications
You must be signed in to change notification settings - Fork 1
/
geneticAlgorithm.py
129 lines (114 loc) · 5.64 KB
/
geneticAlgorithm.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
import math
import random
import copy
xLimit = [-1, 2]
yLimit = [-1, 1]
totalGen = 6
totalChromosome = 6
totalPopulation = 250
probability = 0.65
mutationProbability = 0.08
def initiate_chromosome(totalGen): # fungsi untuk mengenerate chromosome
chromosome = [] # inisialisai array untuk chromosome
for _ in range(totalGen): # perulangan untuk generate angka random pada tiap iterasinya
chromosome.append(random.randint(0,9))
return chromosome
def initiate_population(totalChromosome, totalGen): # fungsi untuk mengenerate populasi
population = [] # inisialisasi array untuk populasi
for _ in range (totalChromosome): # perulangan untuk generate random populasi
chromosome = initiate_chromosome(totalGen)
population.append(chromosome)
return population
def decodeChromosome(chromosome, xLimit, yLimit):
divider = 9 * (10**-1 + 10**-2 + 10**-3)
multiplierX = chromosome[0] * (10**-1) + chromosome[1] * (10**-2) + chromosome[2] * (10**-3)
multiplierY = chromosome[3] * (10**-1) + chromosome[4] * (10**-2) + chromosome[5] * (10**-3)
decodeX = xLimit[0] + (((xLimit[1] - xLimit[0]) / divider) * multiplierX)
decodeY = yLimit[0] + (((yLimit[1] - yLimit[0]) / divider) * multiplierY)
return [decodeX, decodeY]
def fitnessChromosome(chromosome, xLimit, yLimit):
decode = decodeChromosome(chromosome, xLimit, yLimit)
fitness = (math.cos(decode[0]**2) * math.sin(decode[1]**2)) + (decode[0] + decode[1])
return fitness
def fitnessPopulation(population, xLimit, yLimit, totalChromosome):
fitnessPop = []
for i in range(totalChromosome):
chrom = fitnessChromosome(population[i], xLimit, yLimit)
fitnessPop.append(chrom)
return fitnessPop
def parentSelection(population, xLimit, yLimit, totalChromosome):
bestChromosome = []
for _ in range(totalChromosome):
chromosome = population[random.randint(0, totalChromosome-1)]
if (bestChromosome == [] or fitnessChromosome(chromosome, xLimit, yLimit) > fitnessChromosome(bestChromosome, xLimit, yLimit)):
bestChromosome = chromosome
return bestChromosome
def crossover(parent1, parent2, probability):
randomPC = random.random() # untuk mencari nilai probabilitas crossover yang random
if (randomPC < probability):
randomPoint = random.randint(1, 5) # untuk mencari nilai probabilitas point yang random
for i in range (randomPoint):
temp = parent1[i]
parent1[i] = parent2[i]
parent2[i] = temp
return [parent1, parent2]
def mutation(chromosome1, chromosome2, mutationProbability):
probRandom = random.random()
if probRandom < mutationProbability:
gen1, gen2 = random.randint(0,5), random.randint(0,5)
genValue1, genValue2 = random.randint(0,9), random.randint(0,9)
while chromosome1[gen1] == genValue1:
genValue1 = random.randint(0,9)
while chromosome2[gen2] == genValue2:
genValue2 = random.randint(0,9)
chromosome1[gen1] = genValue1
chromosome2[gen2] = genValue2
return chromosome1, chromosome2
def elitism(fitnessPopulation):
index1 = fitnessPopulation.index(max(fitnessPopulation))
return index1
# =============================== MAIN ==================================
population = initiate_population(totalChromosome, totalGen)
fitness = fitnessPopulation(population, xLimit, yLimit, totalChromosome)
indexBest = elitism(fitness)
bestChromosome = copy.deepcopy(population[indexBest])
i = 0
bestGeneration = i
print ("Generasi : ", i + 1)
print ("Fitness : ", fitness[indexBest]) # menginput fitness terbaik
print ("Kromosom : ", population[indexBest]) # menginput kromosom terbaik
print ("Dekode (X, Y): ", decodeChromosome(population[indexBest], xLimit, yLimit))
print ("================================================================================")
while i < (totalPopulation - 1) and fitness[indexBest] < 2.31:
new_population = []
bespop = copy.deepcopy(population[indexBest])
new_population.append(bespop)
new_population.append(bespop)
while len(new_population) <= len(population)-2:
parent1 = parentSelection(population, xLimit, yLimit, totalChromosome) # mencari parent 1
parent2 = parentSelection(population, xLimit, yLimit, totalChromosome) # mencari parent 2
while parent1 == parent2:
parent2 = parentSelection(population, xLimit, yLimit, totalChromosome)
offspring = crossover(parent1, parent2, probability)
offspring = mutation(offspring[0], offspring[1], mutationProbability)
new_population.append(offspring[0])
new_population.append(offspring[1])
i += 1
fitness = fitnessPopulation(new_population, xLimit, yLimit, totalChromosome)
indexBest = elitism(fitness)
population = new_population
print (population)
print ("Generasi : ", i + 1)
print ("Fitness : ", fitness[indexBest])
print ("Kromosom : ", new_population[indexBest])
print ("Decode (X, Y): ", decodeChromosome(new_population[indexBest], xLimit, yLimit))
print ("================================================================================")
if fitnessChromosome(bestChromosome, xLimit, yLimit) < fitnessChromosome(new_population[indexBest], xLimit, yLimit):
bestChromosome = copy.deepcopy(new_population[indexBest])
bestGeneration = i
print (" ")
print ("=============================== Hasil Terbaik ==================================")
print ("Generasi : ", bestGeneration + 1)
print ("Fitness : ", fitnessChromosome(bestChromosome, xLimit, yLimit))
print ("Kromosom : ", bestChromosome)
print ("Decode (X, Y): ", decodeChromosome(bestChromosome, xLimit, yLimit))