MATLAB Report on: - Calculating the global maxima of a stalagmite function.
Objective: - write a code to calculate the global maxima of a stalagmite function.
- Genetic Algorithm solver for mixed integer or continous variable optimization, constrained and unconstrained. The genetic algorithm is a method for solving both constrained and unconstrained optimization problems that is based on natural selection, the process that drives biological evolution.
- We have used stalagmit mathematical model function and we will be writing code to calculate the global maxima.
- To use \'ga\' solver, we need to provide atleast two input arguments, a fitness function and the number of variables in the problem.
- Residuals are the difference between the observed values of the response (dependent) variable and the values that a model predicts.
- polyfit gave me in linear one value and in cubic four value.
- Polyval to use the coefficents in p to predict cp (Specific heat).
- Compute the residual value as vector yresid = cp - predict cp. This formula also used to find Error in curve fit.
- Then we did the residual sum of square.
- Compute the total sum of square of \'y\' by multiplying the variance of \'y\' by the number of observation.
Step 1: First we have wrote a function code to solve equations.
% Programmed by Ravi Thakur
function [x_y] = function_ga(input_vector_value)
x = input_vector_value(1);
y = input_vector_value(2);
func_1x = (sin(5.1*pi*x+0.5)).^6 ;
func_1y = (sin(5.1*pi*y+0.5)).^6 ;
func_2x = exp(-4*log(2).*(((x-0.0677).^2)/0.64));
func_2y = exp(-4*log(2).*(((x-0.0677).^2)/0.64));
x_y = -(func_1x.*func_1y.*func_2x.*func_2y);
end
Step 2: Wrote a code to solve first study of stalagmite.
% Programmed by Ravi Thakur
close all
clear 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)
input_vector_value(1) = xx(i,j);
input_vector_value(2) = yy(i,j);
f(i,j) = function_ga(input_vector_value);
end
end
tic
num_cases = 50;
for i=1:num_cases
[Input, fopt(i)] = ga(@function_ga, 2);
xopt(i) = Input(1);
yopt(i) = Input(2);
end
study_1=toc
figure(1)
subplot(2,1,1)
hold on
surf(xx,yy,-f)
shading interp
plot3(xopt, yopt, -fopt,\'marker\',\'o\',\'markersize\',5,\'markerfacecolor\',\'r\')
title(\'unbound input\')
subplot(2,1,2)
plot(-fopt)
xlabel(\'Iterations\')
ylabel(\'Function minima\')
Step 3: Result of 1st study.
Step 4: Wrote a code to solve second study of stalagmite.
tic
% study 2
for i=1:num_cases
[Input, fopt(i)] = ga(@function_ga,2,[],[],[],[],[0;0],[0.5;0.5]);
xopt(i) = Input(1);
yopt(i) = Input(2);
end
study_2=toc
figure(2)
subplot(2,1,1)
hold on
surf(xx,yy,-f)
shading interp
plot3(xopt, yopt, -fopt,\'marker\',\'o\',\'markersize\',5,\'markerfacecolor\',\'r\')
title(\'unbound input\')
subplot(2,1,2)
plot(fopt)
xlabel(\'Iterations\')
ylabel(\'Function minima\')
Step 5: Results of 2nd study.
Step 6: Wrote a code to solve third study of stalagmite.
tic
% study 3
options = optimoptions(\'ga\');
options = optimoptions(options ,\'populationsize\',500);
for i=1:num_cases
[Input, fopt(i)] = ga(@function_ga, 2, [],[],[],[],[0;0],[0.2;0.2],[],[],options);
xopt(i) = Input(1);
yopt(i) = Input(2);
end
study_3=toc
figure(3)
subplot(2,1,1)
hold on
surf(xx,yy,-f)
shading interp
plot3(xopt, yopt, -fopt,\'marker\',\'o\',\'markersize\',5,\'markerfacecolor\',\'r\')
title(\'unbound input\')
subplot(2,1,2)
plot(-fopt)
xlabel(\'Iterations\')
ylabel(\'Function maxima\')
Step 7: Results of 3rd study.
Step 8: Conclusion
- We got one maxima from best possible solution from genetic algorithm graph.
- Parallel computing has created genetic algorithms even additional appealing as a result of it will alleviate their main drawback: the lack of speed. This allows for more iterations and more total runs, which increases both the chances of finding better solutions and the certainty that the solutions found are the optimal ones.
Step 9: Reference
- Genetic Algorithm - https://in.mathworks.com/help/gads/what-is-the-genetic-algorithm.html
- Global Vs. local minima using ga - https://in.mathworks.com/help/gads/example-global-vs-local-minima-with-ga.html