├── GA.m ├── LICENSE.md ├── Main.m ├── README.md ├── _config.yml ├── calculate_fitness.m ├── create_genes.m ├── create_population.m ├── crossover.m ├── evaluation.m ├── logging.m ├── mutation.m ├── regeneration.m └── selection.m /GA.m: -------------------------------------------------------------------------------- 1 | function GA(target,population_size,mutation_rate) 2 | 3 | % menginisialisasi populasi 4 | population = create_population(target, population_size); 5 | 6 | islooping = true; 7 | generation = 0; 8 | while islooping 9 | % evaluation 10 | islooping = evaluation(population); 11 | % display 12 | logging(population,target,generation) 13 | % selection 14 | [parent1,parent2] = selection(population); 15 | % crossover 16 | [child1,child2] = crossover(parent1,parent2); 17 | % mutation 18 | mutant1 = mutation(child1, mutation_rate); 19 | mutant2 = mutation(child2, mutation_rate); 20 | % hitung fitness mutant 21 | mutant1.fitness = calculate_fitness(mutant1.genes,target); 22 | mutant2.fitness = calculate_fitness(mutant2.genes,target); 23 | % membuat new generation 24 | children = [mutant1 mutant2]; 25 | population = regeneration(children,population); 26 | generation = generation + 1; 27 | end 28 | end -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright (C) 2018 2 | -------------------------------------------------------------------------------- /Main.m: -------------------------------------------------------------------------------- 1 | clear 2 | clc 3 | 4 | target = input('target (string) : ','s'); 5 | jumlah_populasi = input('jumlah populasi (integer) : '); 6 | mutation_rate = input('mutation rate (0-1) : '); 7 | 8 | GA(target,jumlah_populasi,mutation_rate); -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SOURCE CODE TUTORIAL ALGORTIMA GENETIKA 2 | 3 | ## Episode 0 - Pendahuluan tentang apa itu algoritma genetika 4 | [![IMAGE ALT TEXT HERE](https://img.youtube.com/vi/2mXcs-CNCB8/0.jpg)](https://www.youtube.com/watch?v=2mXcs-CNCB8) 5 | -------------------------------------------------------------------------------- /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-cayman -------------------------------------------------------------------------------- /calculate_fitness.m: -------------------------------------------------------------------------------- 1 | function fitness = calculate_fitness(genes,target) 2 | 3 | fitness = 100*sum(target == genes)/length(target); 4 | end 5 | 6 | -------------------------------------------------------------------------------- /create_genes.m: -------------------------------------------------------------------------------- 1 | function genes = create_genes(len) 2 | 3 | random_number = randi([32,126],1,len); 4 | genes = char(random_number); 5 | end 6 | 7 | -------------------------------------------------------------------------------- /create_population.m: -------------------------------------------------------------------------------- 1 | function population = create_population(target,population_size) 2 | 3 | population = struct.empty(population_size,0); 4 | for i=1:population_size 5 | genes = create_genes(length(target)); 6 | fitness = calculate_fitness(genes,target); 7 | population(i).genes = genes; 8 | population(i).fitness = fitness; 9 | end 10 | 11 | end -------------------------------------------------------------------------------- /crossover.m: -------------------------------------------------------------------------------- 1 | function [child1,child2] = crossover(parent1,parent2) 2 | 3 | child1 = parent1; 4 | child2 = parent2; 5 | % cp is crossover point 6 | cp = round(length(parent1.genes)/2); 7 | %cp = randi(length(parent1.genes)-1); 8 | 9 | child1.genes(1:cp) = parent2.genes(1:cp); 10 | child2.genes(1:cp) = parent1.genes(1:cp); 11 | 12 | end -------------------------------------------------------------------------------- /evaluation.m: -------------------------------------------------------------------------------- 1 | function islooping = evaluation(population) 2 | 3 | for i=1:length(population) 4 | if population(i).fitness >= 100 5 | islooping = false; 6 | else 7 | islooping = true; 8 | end 9 | end 10 | end -------------------------------------------------------------------------------- /logging.m: -------------------------------------------------------------------------------- 1 | function logging(population,target,generation) 2 | clc 3 | proses = selection(population); 4 | fprintf('target : %s \n',target); 5 | fprintf('proses : %s \n',proses.genes); 6 | fprintf('\n') 7 | fprintf('generation: %d \n', generation); 8 | fprintf('\n'); 9 | 10 | for i=1:length(population) 11 | fprintf('genes: %s ',population(i).genes); 12 | fprintf('Fitness: %.2f ',population(i).fitness); 13 | fprintf('\n'); 14 | end 15 | 16 | 17 | 18 | end -------------------------------------------------------------------------------- /mutation.m: -------------------------------------------------------------------------------- 1 | function[mutant] = mutation(child,mutation_rate) 2 | 3 | mutant = child; 4 | for i=1:length(child.genes) 5 | if rand <= mutation_rate 6 | mutant.genes(i) = char(randi([32,126])); 7 | end 8 | end 9 | 10 | end -------------------------------------------------------------------------------- /regeneration.m: -------------------------------------------------------------------------------- 1 | function new_population = regeneration(children,population) 2 | 3 | fitness = zeros(1,length(population)); 4 | 5 | 6 | for i=1:length(fitness) 7 | fitness(i) = population(i).fitness; 8 | end 9 | 10 | for i=1:length(children) 11 | [~,index] = min(fitness); 12 | %remove member 13 | population(index)=[]; 14 | fitness(index)=[]; 15 | end 16 | 17 | % add new member 18 | for i=1:length(children) 19 | n = length(population) + 1; 20 | population(n) = children(i); 21 | end 22 | new_population = population; 23 | end -------------------------------------------------------------------------------- /selection.m: -------------------------------------------------------------------------------- 1 | function [best1, best2] = selection(population) 2 | 3 | fitness = zeros(1,length(population)); 4 | for i=1:length(population) 5 | fitness(i) = population(i).fitness; 6 | end 7 | 8 | [~,index] = max(fitness); 9 | best1 = population(index); 10 | 11 | population(index) = []; 12 | fitness(index) = []; 13 | 14 | [~,index] = max(fitness); 15 | best2 = population(index); 16 | 17 | end --------------------------------------------------------------------------------