Challenge

Week 4.1 - Genetic Algorithm

close all
clear all
clc

x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
num_cases=50;
[xx yy]=meshgrid(x,y);


% function code:

function [stalagmite]=stalagmite_func(input_vector)
x=input_vector(1);
y=input_vector(2);
f1_x=(sin(5.1*pi*x+0.5)).^6;
f2_y=(sin(5.1*pi*y+0.5)).^6;
f3_x=exp((-4*0.6931).*(x-0.0667)^2/(0.64));
f4_y=exp((-4*0.6931).*(y-0.0667)^2/(0.64));
stalagmite=(f1_x.*f2_y.*f3_x.*f4_y);
end

for i=1:length(xx);
    for j=1:length(yy);
        input_vector(1)=xx(i,j);
        input_vector(2)=yy(i,j);
        f(i,j)=stalagmite_func(input_vector);
    end
end

%study_1 

tic
for i=1:num_cases
    [inputs,fopt(i)]=ga(@stalagmite_func,2);
    xopt(i)=inputs(1);
    yopt(i)=inputs(2);
   
end

study_time_1=toc
figure(1)
subplot(2,1,1);
hold on
surfc(xx,yy,f);
shading interp;
xlabel('x data');
ylabel('y data');

plot3(xopt,yopt,fopt,'marker','o','markersize',4,'MarkerFaceColor','r')
title('unbounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('iterations')
ylabel('max function')


% study_2

tic
for i=1:num_cases
    [inputs,fopt(i)]=ga(@stalagmite_func,2,[],[],[],[],[0:0],[1:1]);
    xopt(i)=inputs(1);
    yopt(i)=inputs(2);
end
study_time_2=toc;
figure(2)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
shading interp
xlabel('x data')
ylabel('y data')
plot3(xopt,yopt,fopt,'marker','o','markersize',4,'MarkerFaceColor','y')
title('bounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('iterations')
ylabel('max function')

%study_3

options=optimoptions('ga')
options=optimoptions(options,'PopulationSize',300);
tic
for i=1:num_cases
    [inputs,fopt(i)]=ga(@stalagmite_func,2,[],[],[],[],[0:0],[1:1],[],[],options);
    xopt(i)=inputs(1);
    yopt(i)=inputs(2);
end
study_time_3=toc

figure(3)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
shading interp
xlabel('x data')
ylabel('y data')
plot3(xopt,yopt,fopt,'marker','o','markersize',4,'MarkerFaceColor','g')
title('bounded inputs wiith population size increased')
subplot(2,1,2)
plot(fopt)
xlabel('iterations')
ylabel('max function')




In the genetic algorithm process is as follows [1]:
Step 1. Determine the number of chromosomes, generation, and mutation rate and crossover
rate value
Step 2. Generate chromosome-chromosome number of the population, and the initialization
value of the genes chromosome-chromosome with a random valueStep 3. Process steps 4-7 until the number of generations is met
Step 4. Evaluation of fitness value of chromosomes by calculating objective function
Step 5. Chromosomes selection
Step 6. Crossover
Step 7. Mutation
Step 8. Solution (Best Chromosomes)

In this program there were 2 errors,

1. parenthesis was not include in calling stalagmate function.

2. toc command was not included in study_3.

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.