Project

optimise the stalagmite function and find the global maxima of the function.

objective

To Write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function.

what is optimisation

an act, process, or methodology of making something (such as a design, system, or decision) as fully perfect, functional, or effective as possiblespecifically : the mathematical procedures (such as finding the maximum of a function) involved in this

What is the purpose of optimization?
The purpose of optimization is to achieve the “best” design relative to a set of prioritized criteria or constraints. These include maximizing factors such as productivity, strength, reliability, longevity, efficiency, and utilization.
Mathematical optimization

Mathematical optimization (alternatively spelled optimisation) or mathematical programming is the selection of a best element (with regard to some criterion) from some set of available alternatives.[1] Optimization problems of sorts arise in all quantitative disciplines from computer science and engineering to operations research and economics, and the development of solution methods has been of interest in mathematics for centuries.[2]

In the simplest case, an optimization problem consists of maximizing or minimizing a real function by systematically choosing input values from within an allowed set and computing the value of the function. The generalization of optimization theory and techniques to other formulations constitutes a large area of applied mathematics. More generally, optimization includes finding "best available" values of some objective function given a defined domain (or input), including a variety of different types of objective functions and different types of domains.

what is stalagmite function

it is a function which contains the input parameters that to be give plot like stalagmite structure

Calling the function stalagmite that contains the code for evaluates the value of the function using the array input. For the study of the statistical behaviour of Genetic Algorithm, primarily a stopwatch timer is started

it contain the following formulas to generaate data that causes to be plot look so

f = (f1x*f1y*f2x*f2y)


f1y = (sin(5.1*pi*y1+0.5))^6


f2x = exp(-4*log(2)*((x1-0.0667).^2)/0.64)


f2y = exp(-4*log(2)*((y1-0.0667).^2)/0.64)

stalagmite function generated plot

genetic algorithm

A genetic algorithm is a search heuristic that is inspired by Charles Darwin’s theory of natural evolution. This algorithm reflects the process of natural selection where the fittest individuals are selected for reproduction in order to produce offspring of the next generation.

process of natural selection

The process of natural selection starts with the selection of fittest individuals from a population. They produce offspring which inherit the characteristics of the parents and will be added to the next generation. If parents have better fitness, their offspring will be better than parents and have a better chance at surviving. This process keeps on iterating and at the end, a generation with the fittest individuals will be found.

This notion can be applied for a search problem. We consider a set of solutions for a problem and select the set of best ones out of them.

Five phases are considered in a genetic algorithm.

  1. Initial population
  2. Fitness function
  3. Selection
  4. Crossover
  5. Mutation

initial population

The process begins with a set of individuals which is called a Population. Each individual is a solution to the problem you want to solve.

An individual is characterized by a set of parameters (variables) known as Genes. Genes are joined into a string to form a Chromosome (solution).

In a genetic algorithm, the set of genes of an individual is represented using a string, in terms of an alphabet. Usually, binary values are used (string of 1s and 0s). We say that we encode the genes in a chromosome.

fitness function

The fitness function determines how fit an individual is (the ability of an individual to compete with other individuals). It gives a fitness score to each individual. The probability that an individual will be selected for reproduction is based on its fitness score.

selection

The idea of selection phase is to select the fittest individuals and let them pass their genes to the next generation.

Two pairs of individuals (parents) are selected based on their fitness scores. Individuals with high fitness have more chance to be selected for reproduction.

Crossover

Crossover is the most significant phase in a genetic algorithm. For each pair of parents to be mated, a crossover point is chosen at random from within the genes.

For example, consider the crossover point to be 3 as shown below.

Offspring are created by exchanging the genes of parents among themselves until the crossover point is reached.

mutation

In certain new offspring formed, some of their genes can be subjected to a mutation with a low random probability. This implies that some of the bits in the bit string can be flipped.

Mutation occurs to maintain diversity within the population and prevent premature convergence.

termination

The algorithm terminates if the population has converged (does not produce offspring which are significantly different from the previous generation). Then it is said that the genetic algorithm has provided a set of solutions to our problem.

The population has a fixed size. As new generations are formed, individuals with least fitness die, providing space for new offspring.

The sequence of phases is repeated to produce individuals in each new generation which are better than the previous generation.

ga as function of matlab

ga

Find minimum of function using genetic algorithm

collapse all in page

Syntax

Description

x = ga(fun,nvars) finds a local unconstrained minimum, x, to the objective function, fun. nvars is the dimension (number of design variables) of fun.

Note

Passing Extra Parameters (Optimization Toolbox) explains how to pass extra parameters to the objective function and nonlinear constraint functions, if necessary.

x = ga(fun,nvars,A,b) finds a local minimum x to fun, subject to the linear inequalities A*x ≤ b. ga evaluates the matrix product A*x as if x is transposed (A*x').

x = ga(fun,nvars,A,b,Aeq,beq) finds a local minimum x to fun, subject to the linear equalities Aeq*x = beq and A*x ≤ b. (Set A=[] and b=[] if no linear inequalities exist.) ga evaluates the matrix product Aeq*x as if x is transposed (Aeq*x').

x = ga(fun,nvars,A,b,Aeq,beq,lb,ub) defines a set of lower and upper bounds on the design variables, x, so that a solution is found in the range lb ≤ x ≤ ub. (Set Aeq=[] and beq=[] if no linear equalities exist.)

x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon) subjects the minimization to the constraints defined in nonlcon. The function nonlcon accepts x and returns vectors C and Ceq, representing the nonlinear inequalities and equalities respectively. ga minimizes the fun such that C(x) ≤ 0 and Ceq(x) = 0. (Set lb=[] and ub=[] if no bounds exist.)

x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,options) minimizes with the default optimization parameters replaced by values in options. (Set nonlcon=[] if no nonlinear constraints exist.) Create options using optimoptions.

x = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon) or x = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon,options) requires that the variables listed in IntCon take integer values.

Note

When there are integer constraints, ga does not accept linear or nonlinear equality constraints, only inequality constraints.

x = ga(problem) finds the minimum for problem, where problem is a structure.

[x,fval] = ga(___), for any previous input arguments, also returns fval, the value of the fitness function at x.

[x,fval,exitflag,output] = ga(___) also returns exitflag, an integer identifying the reason the algorithm terminated, and output, a structure that contains output from each generation and other information about the performance of the algorithm.

[x,fval,exitflag,output,population,scores] = ga(___) also returns a matrix population, whose rows are the final population, and a vector scores, the scores of the final population

MATLAB code to optimise the stalagmite function and find the global maxima of the function.

close all
clear all
clc

% genotype space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
% creating 2d 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);
        f(i,j) = stalagmite_func(input_vector);
    end
end
% surfc(x,y,f)
% shading interp
% [x, f] = ga(@stalagmite_func,2)
% study 1
n_iteration = 70
tic
for i = 1:n_iteration
    [input1,fopt1(i)] = ga(@stalagmite_func,2);
    xopt1(i) = input1(1);
    yopt1(i) = input1(2);
end
study_time1 = toc
% plotting
figure(1)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
plot3(xopt1,yopt1,-fopt1,'marker','o','markersize',5,'markerfacecolor','r')
title('unbounded points')
subplot(2,1,2)
plot(fopt1)
xlabel('iteration')
ylabel('function minima')
% study 2
n_iteration = 70
tic
for i = 1:n_iteration
    [input2,fopt2(i)] = ga(@stalagmite_func,2,[ ],[ ],[ ],[ ],[0,0],[1,1]);
    xopt2(i) = input2(1);
    yopt2(i) = input2(2);
end
study_time2 = toc
% plotting
figure(2)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
plot3(xopt2,yopt2,-fopt2,'marker','o','markersize',5,'markerfacecolor','r')
title('bounded points')
subplot(2,1,2)
plot(fopt2)
xlabel('iteration')
ylabel('function minima')
% study 3
options = optimoptions('ga');
options = optimoptions(options,'populationsize',300)
tic
n_iteration = 70
for i = 1:n_iteration
    [input3,fopt3(i)] = ga(@stalagmite_func,2,[ ],[ ],[ ],[ ],[0 ,0],[1,1],[ ],[ ],options);
    xopt3(i) = input3(1);
    yopt3(i) = input3(2);
end
study_time3 = toc
% plotting
figure(3)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
plot3(xopt3,yopt3,-fopt3,'marker','o','markersize',5,'markerfacecolor','r')
title('bounded points')
subplot(2,1,2)
plot(fopt2)
xlabel('iteration')
ylabel('function minima')

MATLAB code for stalagmite function

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

results

study1 output

study 2 output

study 3 output

the maximum point on the highest mountain is called globel maxima

explanation to the code

  1. first i gave the limits of search space also called genotype in genetic algorithm
  2. specified every node within the search space using meshgrid command
  3. to found optimised value created stalagmite function seperately with 2 variables of x,y
  4. then called the ga function to solve stalagmite function
  5. to get maximum value we used negative sign in function because ga only provides min value
  6. then run the program with different iteration in three seperate studies
  7. then ploted necessary plots and figures

error incurred during coding

made syntax error while calling the ga function

conclusion

the globel maxima of function has calculated and identified using matlab successfully with neccessary result

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.