Challenge

Week 4.1 - Genetic Algorithm

Explanation,

salagmite function was mentioned with appropriare argument vectors.the program having three subdivition are,

1. first study with unbounded inputs

2. second study with bounded inputs

3. third study with bounded inputs in alter size

finally plot all the studies have displayed and get the clear result.

SYNTAX for ga,

ga evalutes the matrix product aeq*x as if x is transposed (aeq*x').x=ga(fun,nvars,A,b,aeq,beq,lb,ub)

defines a set of lower and upper bounds on the design variables,x,so that a solution is found in the range lb<eqal x<eqal ub.

Genetic algorithm,

it is a metod for solving both constrained and unconstrained optimization problems based on the natural selection process that mimics biological evolution. the algrothim repeatedly modifies a population of individual solutions.

you can apply the genetic algorithm to solve problem that are not well suited for standard optimized algorithms,including problem in which the objective function discontinuous,nondifferentiable.

ERROR facing,

while calling the salagmite sub function shoeing error, after that change the file name and location error can be short out.

while typeing the code due to wrong order show erroe in 'surfc' after that coded correct order.

some spilling mistake in plot graph not generated, after corrected that number of interation graph generated.

GLOBAL MAXIMA,

global maxima of the function is 0.99999 or 1.

clear all
close all
clc

% define our search space
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);

% iteration count
num_cases = 200;

% creating a 2 dimensional mesh
[X,Y]=meshgrid(x,y);

% evaluating the stalagmite function
for i= 1:length(X)
    for j= 1:length(Y)
input_vector(1)=X(i,j);
input_vector(2)=Y(i,j);
f(i,j)= stalagmite_trail(input_vector);
    end
end
 
surfc(x,y,-f);
shading interp

for i = 1:num_cases
[inputs,fopt(i)] = ga(@stalagmite_trail,2);
opt_x(i) = inputs(1);
opt_y(i) = inputs(2);
end
figure(1)
subplot(2,1,1)
surfc(x,y,-f);
hold on
shading interp

plot3(opt_x,opt_y,-fopt,'marker','o','MarkerSize',3,'MarkerFaceColor','r');
xlabel('x value')
ylabel('y value')
grid on
title('unbounded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('number of iterations')


clear all
close all
clc
% second study of bounded inputs
% define our search space
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);

% iteration count
num_cases = 500;

% creating a 2 dimensional mesh
[X,Y]=meshgrid(x,y);


 for i= 1:length(X)
    for j= 1:length(Y)
input_vector(1)=X(i,j);
input_vector(2)=Y(i,j);
f(i,j)= stalagmite_trail(input_vector);
    end
 end
% second study of bounded inputs
for i = 1:num_cases
[inputs,fval(i)] = ga(@stalagmite_trail,2,[],[],[],[],[0;0],[1;1]);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end

figure(2)
subplot(2,1,1)
hold on
surfc(x,y,-f);
shading interp
plot3(xopt,yopt,-fval,'marker','o','MarkerSize',3,'markerfacecolor','r');
xlabel('x value')
ylabel('y value')
grid on
title('bounded inputs')
subplot(2,1,2)
plot(-fval)
xlabel('number of iterations')
ylabel('f maximum')
legend('bounded inputs')


clear all
close all
clc
% define our search space
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);

% iteration count
num_cases = 500;

% creating a 2 dimensional mesh
[X,Y]=meshgrid(x,y);


 for i= 1:length(X)
    for j= 1:length(Y)
input_vector(1)=X(i,j);
input_vector(2)=Y(i,j);
f(i,j)= stalagmite_trail(input_vector);
    end
 end
 % bounded inputs with alter size
options = optimoptions('ga');
options = optimoptions(options,'PopulationSize',200);
for i = 1:num_cases
[inputs,fval(i)] = ga(@stalagmite_trail,2,[],[],[],[],[0;0],[1;1],[],[],options)
opt_x(i) = inputs(1);
opt_y(i) = inputs(2);
end
figure(3)
subplot(2,1,1)
surfc(x,y,-f);
xlabel('x value')
ylabel('y value')
hold on
shading interp
plot3(opt_x,opt_y,-fval,'marker','*','MarkerSize',3)
title('bounded inputs with alter size')
subplot(2,1,2)
plot(-fval)
xlabel('number of iterations')
ylabel('f maximum')
legend('bounded inputs with alter size')



function [f] = stalagmite_trail(input_vector)

x = input_vector(1);
y = input_vector(2);

f1_x=(sin(5.1*pi*x + 0.5)).^6;
f1_y=(sin(5.1*pi*y + 0.5)).^6;

t1 = ((x-0.0667).^2)/(0.64);
f2_x = exp(-4.*log(2)*t1);

t2 = ((y-0.0667).^2)/(0.64);
f2_y = exp(-4.*log(2)*t2);

f = -(f1_x.*f2_x*f1_y.*f2_y);
end
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.