Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
ai:lab2 [2024/10/08 14:02] – created tami | ai:lab2 [2024/10/08 19:10] (current) – tami | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== AI Lab 2 ====== | ====== AI Lab 2 ====== | ||
+ | <WRAP column 30%> | ||
+ | The following script produces the plot: | ||
+ | |||
+ | {{ : | ||
+ | |||
+ | X axis: Generations, | ||
+ | </ | ||
+ | |||
+ | <file matlab main.m> | ||
+ | population_size = 100; | ||
+ | chromosome_length = 500; | ||
+ | generations = 500; | ||
+ | mutation_rate = 0.001; | ||
+ | |||
+ | best_fitness_hist = []; | ||
+ | |||
+ | %create inital population | ||
+ | population = initialization(population_size, | ||
+ | |||
+ | for g = 1: | ||
+ | %eval fitness | ||
+ | fitness = sum(population, | ||
+ | %select individuals for tournament selection | ||
+ | selected = tournament_selection(population, | ||
+ | %crossover the individuals | ||
+ | offspring = crossover(selected); | ||
+ | %mutation | ||
+ | mutated_offspring = mutate(offspring, | ||
+ | |||
+ | %create new population | ||
+ | population = mutated_offspring; | ||
+ | |||
+ | %display best fitness of this generation | ||
+ | best_fitness = max(fitness); | ||
+ | |||
+ | if best_fitness == chromosome_length | ||
+ | break | ||
+ | end | ||
+ | |||
+ | best_fitness_hist = [best_fitness_hist; | ||
+ | fprintf(' | ||
+ | end | ||
+ | |||
+ | plot(best_fitness_hist) | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | <file matlab initialization.m> | ||
+ | function population = initialization(size, | ||
+ | population = randi([0, | ||
+ | end | ||
+ | </ | ||
+ | |||
+ | <file matlab tournament_selection.m> | ||
+ | function selected_parents = tournament_selection(population, | ||
+ | selected_parents = zeros(n, size(population, | ||
+ | for i = 1:n | ||
+ | %randomly select 2 indiviuals | ||
+ | idx = randi([1, size(population, | ||
+ | if fitness(idx(1)) > fitness(idx(2)) | ||
+ | selected_parents(i, | ||
+ | else | ||
+ | selected_parents(i, | ||
+ | end | ||
+ | end | ||
+ | end | ||
+ | </ | ||
+ | |||
+ | <file matlab crossover.m> | ||
+ | function offspring = crossover(parents) | ||
+ | [num_parents, | ||
+ | offspring = zeros(num_parents, | ||
+ | | ||
+ | %pick every 2, use -1 so you can select the next one i.e. i+1 without | ||
+ | %running out of hte index bounds | ||
+ | for i = 1: | ||
+ | crossover_point = randi([1, string_length-1]); | ||
+ | offspring(i, | ||
+ | parents(i, 1: | ||
+ | parents(i+1, | ||
+ | ]; | ||
+ | offspring(i+1, | ||
+ | parents(i+1, | ||
+ | parents(i, crossover_point+1: | ||
+ | ]; | ||
+ | end | ||
+ | |||
+ | if mod(num_parents, | ||
+ | %Carry over last parent if odd | ||
+ | offspring(end, | ||
+ | end | ||
+ | end | ||
+ | </ | ||
+ | |||
+ | <file matlab mutate.m> | ||
+ | function mutated_offspring = mutate(offspring, | ||
+ | mutated_offspring = offspring; | ||
+ | |||
+ | for i = 1: | ||
+ | for j = 1: | ||
+ | if rand < mutation_rate | ||
+ | % flip the bits | ||
+ | mutated_offspring(i, | ||
+ | end | ||
+ | end | ||
+ | end | ||
+ | end | ||
+ | </ |