Challenge

Week 4 - Genetic Algorithm

Genetic algorithm: optimization technique which is used to solve non-linear or non-differentiable optimization problems, Which is based on evolution theory proposed by Charles darwin. It starts with random no. of inputs and based on those inputs it evaluates the function and after that, it starts natural selection where it takes 2 generations and mutate them and also do a crossover and generates the new generation and the process continues and it gives better results from parents generation

Important Note: Every time we run the GA code it gives a different result to bound that result we again use different methods like

1.increasing the no. of iterations

2.bouding the x-axis and y-axis limits

3.increasing the random sampling population

....................................................................................

stepwise explanation of code:

STUDY 1:

1.input the values i.e., x and y for stalagmite function(it is a mathematical model that helps us to generate local minimal and maximas)

2.Create a mesh grid using above x and y values

3.Assign the different x values to the each of the different y values with the help of for loop and call the stalagmite function which is saved and coded in another script file

4.Stalagmite function coding with using simple Matlab calculation technique and save the file with the above-mentioned name.

5. Now involve the GA function and repeat the function 50 times using for loop

6. Now make subplot(different plots in one fig) surfc and shading interp(removes the grid lines) and plot the all optimum values. Make in another subplot to draw the only maximum value of the function along with iterations

7.label and provide axis names

STUDY 2

1.all steps are similar only change in the calling of a function i.e., including the bounds in order to make a solution within the required limits

STUDY 3

1. Again all steps are similar only change in increasing the population to 250 by following way

calling optimizations('ga')

and providing the required amount of population

ERRORS: Not particularly difficult errors

clear all
close all
clc
%STUDY 1
% inputs for x & y remember these are no the values for random sample of ga
x = linspace(0,0.6,150); 
y = linspace(0,0.6,150);
[xx yy] = meshgrid(x,y); %changing into 2d array
for i = 1:length(xx)
    for j = 1:length(yy)
        inputvector(1) = xx(i,j);
        inputvector(2) = yy(i,j);
        f(i,j) = stalagmite_func(inputvector); %calling the satlagmite function by sending inputvector as x and y values
    end
end
%
for i=1:50 %providing iterations to calculate the foptimum
[inputs,fval(i)]=ga(@stalagmite_func,2);%inbuilt function which take function name and 2 as no. of variables here (x and y)
xopt(i)=inputs(1);%storing the values for further plotting purpose
yopt(i)=inputs(2);
end
subplot(2,1,1)%which shows diff graphs in one figure
hold on 
surfc(xx,yy,-f)%creates surface plots
shading interp %removes grid lines
hold on
plot3(xopt,yopt,fval,'marker','o')
title('unbounded inputs')
subplot(2,1,2)
plot(-fval)
xlabel('iterations')
ylabel('function_min')

%STUDY 2
%with bounds

clear all
close all
clc
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
[xx yy] = meshgrid(x,y);
for i = 1:length(xx)
    for j = 1:length(yy)
        inputvector(1) = xx(i,j);
        inputvector(2) = yy(i,j);
        f(i,j) = stalagmite_func(inputvector);
    end
end

for i=1:50
[inputs,fval(i)]=ga(@stalagmite_func,2,[],[],[],[],[0;0;[1;1]]);%providing upper and lower bounds
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
subplot(2,1,1)
hold on 
surfc(xx,yy,f)
shading interp
hold on
plot3(xopt,yopt,fval,'marker','o')
title('unbounded inputs')
subplot(2,1,2)
plot(fval)
xlabel('iterations')
ylabel('function_min')

%STUDY 3
%with more population

clear all
close all
clc
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
[xx yy] = meshgrid(x,y);
for i = 1:length(xx)
    for j = 1:length(yy)
        inputvector(1) = xx(i,j);
        inputvector(2) = yy(i,j);
        f(i,j) = stalagmite_func(inputvector);
    end
end
options=optimoptions('ga')%providing with more population so calling inbuilt function i.e, optimoptions
options=optimoptions(options,'populationsize',250)%increasing the population size to 250
tic
for i=1:50
[inputs,fval(i)]=ga(@stalagmite_func,2,[],[],[],[],[0;0],[1;1],[],[],options);%onlychange is including the options inisde the fucnction which is assigned with 'optimoptions'
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study_time=toc
subplot(2,1,1)
hold on 
surfc(xx,yy,-f)
shading interp
hold on
plot3(xopt,yopt,-fval,'marker','o')
title('unbounded inputs')
subplot(2,1,2)
plot(-fval)
xlabel('iterations')
ylabel('function_min')
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.