Challenge

Week 4.1 - Genetic Algorithm

optimization refers to finding the values of inputsin such a way that we get the 'best' output values. The parameters of 'best' varies from problem to problem, but in the mathematical term,it refers to the maximizing or minimizing one or more objective function by varying the input function.

for all this we need a optimization algorithem to perform the optimization process. So, Genetic Algorithem is one of the optimization process.

Genetic Algorithem are commonly used to genrate High-quality solutions to optimization and by search problems by relying on biologically inspired operator such as mutation, crosss-over and selection.

The Genetic Algorithm GA is the method of solving constrained and unconstrained optimizations problems which are based on natural selection , this process drive from biological evolution.

1. The search space of the stalagmite function is defined using linspace.

2. 2-dimensional array is created using meshgrid.

3. Convert array to matrixes.

4. Now the values in the different matrixes created can be put as an input into the function that we have created by calling it. by this we can get the value of the function at various values x and y.

5. Now since we have get the value of the function at various values, we now need to calculate the maximum value, which can be calculated using the 'ga' , as in-built function.

6. So, for our first study we will use the for loop and iterate it 50 times.

we will using the following syntax :

[input,fout] = ga(function, number of variables)

here the number of variable are 2 i.e x,y

These inputs can be stored and we can use the surf command to create a surface to visualize the function

Here we are having 3 functions given below :-

a. First, Running the function with no boundaries and minimum input values

b. Secondly, Running the function with boundaries.

c. lastly, Running the Function with specified intial population.

On the other hand, error occured once due to the program file and function program file not in the same folder, so in order to remove this error put these both files in the same folder.

function z = stalagmite (x)

    x1 = ( sin((5.1*pi*x(1)) +0.5 ) )^6;
    x2 = exp( -4*log(2)* ( ( (x(2)-0.0667)^2 ) / 0.64 ) );
    y1 = ( sin((5.1*pi*x(2)) +0.5 ) )^6;
    y2 = exp( -4*log(2)* ( ( (x(1)-0.0667)^2 ) / 0.64 ) );
    
    z = -1* x1 * x2 * y1 * y2;
    

end
clear all 
close all
clc


%our search space
x = linspace(0,0.5,300);
y = linspace(0,0.5,300);

% 2Dimensional array
[xx, yy] = meshgrid(x,y);

for i = 1:length(xx)
    
    for  j = 1:length(yy)
        input_vector(1) = xx(i,j);
        input_vector(2) = yy(i,j);
       z(i,j) = stalagmite([xx(i,j), yy(i,j)]);
        
    end
end
 
%%
% Running the function with no boundaries and minimum input values

for i =1:50
    
    [x_y ,val(i)] = ga(@stalagmite,2); 
    x_x(i) = x_y(1);
    y_y(i) = x_y(2);
    
end
figure(1)
subplot(2,1,1);
surf(x, y, z);
xlabel('x');
ylabel('y');
hold on
shading interp;
plot3(x_x,y_y,val);
subplot(2,1,2);
plot(val);

%%
% Running the function with boundaries
for i =1:50
    
    [x_y, val(i)] = ga(@stalagmite,2,[],[],[],[],[0 0],[0.6 0.6]);
    x_x(i) = x_y(1);
    y_y(i) = x_y(2);
    
end
figure(2)
subplot(2,1,1);
surf(x, y, z);
xlabel('x');
ylabel('y');
hold on
shading interp;
plot3(x_x,y_y,val)
subplot(2,1,2);
plot(val);


%%
% Running the Function with specified intial population

options = optimoptions('ga', 'Populationsize', 100);
% options.crossoverfraction = 0.3;
x_x = 0;
y_y = 0;
val = 0;
for i = 1:30
    
    [x_y, val(i)] = ga(@stalagmite,2,[],[],[],[],[0 0],[0.6 0.6],[],[],options);
    x_x(i) = x_y(1);
    y_y(i) = x_y(2);

end

figure(3);
subplot(2,1,1)
surf(x,y,z);
xlabel('x')
ylabel('y')
hold on;
shading interp;
plot3(x_x,y_y,val);
subplot(2,1,2);
plot(val);
xlabel('iterations')
ylabel('minimum value obtaibed')

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.