Challenge

Week 4.1 - Genetic Algorithm

clear all
close all
clc
%
p=100; % Population size
c=30; % number of pairs of chromosomes to be crossovered
m=30; % number chromosomes to be mutated
tg=250; % Total number of generations
%
figure
title('Blue - Average Red - Maximum');
xlabel('Generation')
ylabel('Objective Funtion Value')
hold on
P=population(p);
k = 0;
[x1 y1]=size(P);
P1 = 0;
for i=1:tg
cr=crossover(p,c);
Mu=mutation(p,m);
p(p+1 : p+2*c,:)=cr;
p(p+2*c+1:p+2*c+m,:)=Mu;
E=evaluation(p);
[p s]=selection(P,E,p);
K(i,1)=sum(s)/p;
k(i,2)=s(1); % best
plot(k(:,1),'b.'); drawnow
hold on
plot(k(:,2),'r.'); drawnow
end
Max_fitness_value=max(k(:,2))
p2 = p(1,:); % Best chromosome
% convert binary to real numbers
A=bi2de(p2(1,1:y1/2));
x=3+A*(3-(-3))/(2^(y1/2)-1);
B=bi2de(p2(1,y1/2+1:y1));
y=-3+B*(3-(-3))/(2^(y1/2)-1);
optimal_solution=[x y]
Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.