Professional Documents
Culture Documents
TM14 GA Application
TM14 GA Application
TEKNIK
OPTIMASI
Genetic Algorithm
On Hands
14
Teknik Teknik ELektro Zendi Iklima, ST., S.Kom., M.Sc.
Abstract Kompetensi
Pada mata kuliah ini, mahasiswa belajar Ketepatan dalam menguraikan
mengenai Swarm Intelligence (SI), definisi, konsep dasar Genetic Algorithm (GA)
konsep dasar SI, serta bagaimana
penggunaan SI. Selain itu mahasiswa juga akan Ketepatan dalam memberikan
dikenalkan mengenai Swarm Robotics (SR). contoh aplikasi Genetic Algorithm
Serta mahasiswa akan belajar
mengenai konsep dasar, visualisasi, formulasi,
hingga teknik aplikasi dari beberapa algoritma
antara lain Ant Colony
Optimization (ACO), Bee Colony Optimization
(BCO), Particle Swarm Optimization (PSO),
Genetic Algorithm (GA).
Application 1: GA to Generating Words
import random
# Valid genes
GENES = '''abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP
QRSTUVWXYZ 1234567890, .-;:_!"#%&/()=?@${[]}'''
class Individual(object):
'''
Class representing individual in population
'''
def __init__(self, chromosome):
self.chromosome = chromosome
self.fitness = self.cal_fitness()
@classmethod
def mutated_genes(self):
'''
create random genes for mutation
'''
global GENES
gene = random.choice(GENES)
return gene
@classmethod
def create_gnome(self):
'''
create chromosome or string of genes
'''
global TARGET
gnome_len = len(TARGET)
return [self.mutated_genes() for _ in range(gnome_len)
]
# random probability
prob = random.random()
def cal_fitness(self):
'''
Calculate fittness score, it is the number of
characters in string which differ from target
string.
'''
global TARGET
fitness = 0
for gs, gt in zip(self.chromosome, TARGET):
if gs != gt: fitness+= 1
return fitness
# Driver code
def main():
global POPULATION_SIZE
#current generation
generation = 1
found = False
new_generation = []
population = new_generation
# print("Generation:", generation)
# print("String:", population[0].chromosome)
# print("Fitness:", population[0].fitness)
generation += 1
print("Generation:", generation)
if __name__ == '__main__':
main()
import numpy
for k in range(offspring_size[0]):
# Index of the first parent to mate.
parent1_idx = k%parents.shape[0]
# Index of the second parent to mate.
parent2_idx = (k+1)%parents.shape[0]
# The new offspring will have its first half of its genes taken
from the first parent.
offspring[k, 0:crossover_point] = parents[parent1_idx, 0:crosso
ver_point]
# The new offspring will have its second half of its genes take
n from the second parent.
offspring[k, crossover_point:] = parents[parent2_idx, crossover
_point:]
return offspring
def mutation(offspring_crossover):
# Mutation changes a single gene in each offspring randomly.
for idx in range(offspring_crossover.shape[0]):
# The random value to be added to the gene.
random_value = numpy.random.uniform(-1.0, 1.0, 1)
offspring_crossover[idx, 4] = offspring_crossover[idx, 4] + ran
dom_value
return offspring_crossover
"""
The y=target is to maximize this equation ASAP:
y = w1x1+w2x2+w3x3+w4x4+w5x5+6wx6
where (x1,x2,x3,x4,x5,x6)=(4,-2,3.5,5,-11,-4.7)
What are the best values for the 6 weights w1 to w6?
We are going to use the genetic algorithm for the best possible val
ues after a number of generations.
"""
Data_set_size=20
fig, ax = pyplot.subplots()
grouped = df.groupby('label')
"""
Genetic algorithm parameters:
Mating pool size
Population size
"""
sol_per_pop = 10
num_parents_mating = 5
num_generations = 10
for generation in range(num_generations):
print("Generation : ", generation)
# Measing the fitness of each chromosome in the population.
fitness = cal_pop_fitness(equation_inputs[0], new_population)