Project

global maxima

program to find global maxima of the given function.

Genetic algorithm helps solve constrained and unconstrained optimization problems. ga continuously modifies the input values to obtain next generation values and narrowing the values towards optimum values.

syntax:

x = ga(function,nvar,A,b,lb,up,nonlcon,intcon,options)

here, function is the one whose minima needs to be found.

nvar - number of variables in the function

A and b - matrices for inequalities

lb and up - lower and upper bounds for the function

nonclon - non linear constraints. [] if no non linear constraints exist.

intcon - index vector for integer variables

options - options created using gaoptimset

x and y values have been specified which forms the range of values to be considered. using stalagmite function defined in the later section, the values of input vectors have been calculated for every value of x and y and stored in f.

stalagmite function

function [f] = stalagmite(input_vector)
f1x = (sin(5.1*pi*input_vector(1)+0.5)^6);
f1y = (sin(5.1*pi*input_vector(2)+0.5)^6);
f2x = exp(-4*log(2)*(((input_vector(1)-0.0667)^2)/0.64));
f2y = exp(-4*log(2)*(((input_vector(2)-0.0667)^2)/0.64));
f = f1x*f2x*f1y*f2y;
f = 1/(1+f);
end

main program

clear all
close all
clc

x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num = 100;

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

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



% study 1
tic
for i=1:num
    [inputs fopt(i)] = ga(@stalagmite,2);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
end
study1time = toc

figure(1)
subplot(2,1,1)
hold on
surf(x,y,-f);
shading interp
plot3(xopt,yopt,-fopt,\'marker\',\'o\',\'markersize\',5,\'markerfacecolor\',\'b\')
title(\'unbounded inputs\');
subplot(2,1,2)
plot(fopt)
xlabel(\'iterations\')
ylabel(\'function maximum\')

%study 2 upper and lower bound
tic 
for i=1:num
    [inputs fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0,0],[1,1])
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
end
study2time = toc

figure(2)
subplot(2,1,1)
hold on
surfc(x,y,-f);
shading interp
plot3(xopt,yopt,-fopt,\'marker\',\'o\',\'markersize\',5,\'markerfacecolor\',\'r\')
title(\'bounded inputs\');
subplot(2,1,2)
plot(-fopt)
xlabel(\'iterations\')
ylabel(\'function maximum\')


%study 3 - iterations
options = gaoptimset(\'MutationFcn\',{@mutationadaptfeasible});
options = gaoptimset(options,\'Populationsize\',100);

tic
for i=1:num
    [inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options)
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
end
study3time = toc

% plot
figure(3)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
plot3(xopt,yopt,-fopt,\'marker\',\'o\',\'markersize\',5,\'markerfacecolor\',\'g\')
title(\'bounded input with modified population size\');
subplot(2,1,2)
plot(-fopt)
xlabel(\'iterations\')
ylabel(\'function maximum\')

study 1: genetic algorithm is used to find the global maxima in this section but the inputs of ga are unbounded and random. since ga finds minima only, negative value of f have been taken to obtain maxima values. further 3d plot and fopt values have been plotted to see the distribution of values.

here maxima values are not clear and the input values can be seen to go beyond the defined range of x and y.

study 2: to improve the analysis range of inputs, upper and lower bounds have been applied using the syntax of ga function.

from the 3d plot it can be seen that the values of input are constrained to the range mentioned. further all the local maxima have been covered.

study 3: maxima values have been calculated using mutationadaptivefeasible function of gaoptimset here as the ga was not supported by the matlab version owned.

errors:

as the ga function was not supported by the version of matlab owned, this error kept showing. further ga function was replaced by mutationadaptivefeasible function, and output was obtained.

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.