Challenge

Week 4.1 - Genetic Algorithm

 code for genetic algorthim for gobel max :

clear all

close all

clc

 x = linspace(0,0.6,150);

y = linspace(0,0.6,150);

numcase  = 50;

% creating the stalagmite 

[xx yy] = meshgrid(x,y);

for i = 1:length(x)

for j = 1:length(y)

inputvector(1) = xx(i,j );

inputvector(2) = yy(i,j);

f(i,j) = stagalmite(inputvector);

end

end

%  study 1 

tic

for i = 1:numspace

[res ,fopt(i)] = ga(@stagalmite,2);

xopt(i) = res(1);

yoptc(i) = res(2);

end

% plot 1

study1time = toc ;

figure(1)

subplot(2,1,1)

surf(xx,yy,-f)

shading interp

hold on

plot3(xopt,yopt,-fopt,marker,'o')

subplot(2,1,2)

xlabel('intertion')

ylabel('functiom max')

 

% study 2

for i = 1:numspace

[res,fotp(i)] = ga(@stalagmite,2,[],[],[],[],[0.6;0.6]);

xopt(i) = res(1);

yopt(i) = res(2);

end

%plot  2

study2time = toc

figure(2)

subplot(2,1,1);

surf(x,y,-f)

shading interp

plot3(xopt,yopt,-fopt,'marker,'0')

subplot(2,1,2)

plot(-fopt)

xlabel('iteration')

ylabel('optimal solution')

 

% study 3

tic

options = optimoptions('ga')

ooptions = optimoptions(options,'populationsize',250);

tic

for i = 1:numcase

[res,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0.6;0.6],[],[],,options);

xopt = res(1);

yopt = res(2);

end

% plot 3

study3time = toc

 figure (3)

subplot(2,1,1)

surf(x,y,-f)

shading interp

hold on

 plot3(xopt,yopt,-fopt'marker','0',)

subplot(2,1,2)

plot(-fopt)

xlabel('interations')

ylabel('function maxmia')

 

stalagmite function code:

function A = staglamite(inputvector)

x = inputvector(1);

y = inputvector(2);

f1  =(sin(5.1*pi*x+0.5)).06;

f2 = exp(-4*log(2)*((x-0.667).^2))/0.64);

f3 = (sin(5.1*pi*y+0.5)).^6;

f4 = exp((-4*log(2))*((y-0.667).^2)/0.64);

A= -(f1*f2*f3*f4);

end

Explantation of the code

  The  genetic algorthim (GA) is a method  for solving both constraine and unconstrained optimization problems based on a natural selection process that mimics bilogical evolution.The algorthim repeatdly modifies a population of individual solutions.At each step,the genetic algrothim randomly selectes individuals from the current population and uses them as parents to produces the children for the next generation 

The genetic algorthim differs from a classical ,derivative-based,opyimazition algorthim in two ways

a. Classical algorthim

 Generates  a single point at each iteration the sequenc of points approaches an aoptimal solution.selects the next point in the sequence by a deterministic computation.

b.Genetic algrothim

 Generates a population of points at each iteration. the best point in population apporaches an optimal solution. selects the nest population by computation  which users random number generators

Outline of the algorthim

 The following outline summeries how genetic algorthim works.

1.The algorthim begins by creting a random intial population

2. the algrothim then creates  a sequence of new populations.At each steps,the algorthim uses the individuals in the current next population. To crate the new populatiion,the algorthim performs the following steps 

a. scales the raw fittness scores to convert  them into a more range of valves.

b. selects members,called parents ,based on their expections

c. some of the individuals in the current population that have lower fittness are choose as elite. hese eloite .these elite  induluals are passed to the next population.

d.produces childern from the parents .childernare proceduces eithe by makingrandom chages to single parent-mutation-or by cobining the vaectorentries of a pair of parents-crossover.

e.Replaces  the curent population with the childern to fromthe next generation.

3. the algorthim stops when one of the stopping ccriteriais met.

4.Thealgorthim takes modified steps for linear and inter constraints.

5.The algorthim is futher modified for non linear constraints.

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.