Challenge

Genetic Algorithm

% Stalagmite_Mathematical_model

clear all
close all
clc

% Defining our search space (1D)
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);

% Creating a 2D mesh
[xx yy] = meshgrid(x, y);

% Evaluating the stalagmite function
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(input_vector);
end
end

% first study
for i = 1:50
[inputs, fval(i)] = ga(@stalagmite,2);
x_c(i) = inputs(1)
y_c(i) = inputs(2)
end

figure(1)
surfc(xx,yy,f)
xlabel('x value')
ylabel('y value')
hold on
shading interp
plot3(x_c,y_c,fval)

% second study
for i = 1:50
[inputs, fval(i)] = ga(@stalagmite,2,[],[],[],[],[0 0],[0.6 0.6]);
x_c(i) = inputs(1)
y_c(i) = inputs(2)
end

figure(2)
surfc(xx,yy,f)
xlabel('x value')
ylabel('y value')
hold on
shading interp
plot3(x_c,y_c,fval)

% Third study
population = optimoptions('ga','Populationsize',100)
populationCrossoverFraction = 0.3
for ii = 1:50
[inputs, fval(ii)] = ga(@stalagmite,2,[],[],[],[],[0 0],[0.6 0.6],[],[],population);
%ga(DistanceMeasureFcn,
x_c(ii) = inputs(1)
y_c(ii) = inputs(2)
end

figure(3)
surfc(xx,yy,f)
xlabel('x value')
ylabel('y value')
hold on
shading interp
plot3(x_c,y_c,fval)

--------------###-------------------------

% stalagmite_func
function xy = stalagmite (x)
f1x = (sin(5.1*pi*x(1) + 0.5))^6;
f1y = (sin(5.1*pi*x(2) + 0.5))^6;
f2x = exp(-4*log(2)*(((x(1)-0.0667)^2)/0.64));
f2y = exp(-4*log(2)*(((x(2)-0.0667)^2)/0.64));
xy = -1*((f1x)*(f1y)*(f2x)*(f2y));
end

Genetic Algorithm - GA
Genetic algorithm solves smooth or nonsmooth optimization problems with any types of constraints, including integer constraints. It is a stochastic, population-based algorithm that searches randomly by mutation and crossover among population members.


A genetic algorithm (GA) is a method for solving both constrained and unconstrained optimization problems based on a natural selection process that mimics biological evolution. The algorithm repeatedly modifies a population of individual solutions. At each step, the genetic algorithm randomly selects individuals from the current population and uses them as parents to produce the children for the next generation. Over successive generations, the population "evolves" toward an optimal solution.
You can apply the genetic algorithm to solve problems that are not well suited for standard optimization algorithms, including problems in which the objective function is discontinuous, nondifferentiable, stochastic, or highly nonlinear.

Running ga with the Default Options
To run the genetic algorithm with the default options, call ga with the syntax
[x,fval] = ga(@fitnessfun, nvars)
The input arguments to ga are
@fitnessfun — A function handle to the file that computes the fitness function. Compute Objective Functions explains how to write this file.
nvars — The number of independent variables for the fitness function.
The output arguments are
x — The final point
fval — The value of the fitness function at x
For a description of additional input and output arguments, see the reference page for ga.
You can run the example described in Minimize Rastrigin's Function from the command line by entering
rng(1,'twister') % for reproducibility
[x,fval] = ga(@rastriginsfcn,2)
This returns
Optimization terminated:
average change in the fitness value less than options.FunctionTolerance.
x =
-1.0421 -1.0018
fval =
2.4385
Setting Options at the Command Line
You can specify any of the options that are available for ga by passing options as an input argument to ga using the syntax
[x,fval] = ga(@fitnessfun,nvars,[],[],[],[],[],[],[],options)
This syntax does not specify any linear equality, linear inequality, or nonlinear constraints.
You create options using the function optimoptions.
options = optimoptions(@ga);
This returns options with the default values for its fields. ga uses these default values if you do not pass in options as an input argument.
The value of each option is stored in a field of options, such as options.PopulationSize. You can display any of these values by entering options followed by a period and the name of the field. For example, to display the size of the population for the genetic algorithm, enter
options.PopulationSize
ans =
'50 when numberOfVariables <= 5, else 200'
To create options with a field value that is different from the default — for example to set PopulationSize to 100 instead of its default value 50 — enter
options = optimoptions('ga','PopulationSize',100);
This creates options with all values set to their defaults except for PopulationSize, which is set to 100.
If you now enter,
ga(@fitnessfun,nvars,[],[],[],[],[],[],[],options)
ga runs the genetic algorithm with a population size of 100.
If you subsequently decide to change another field in options, such as setting PlotFcn to @gaplotbestf, which plots the best fitness function value at each generation, call optimoptions with the syntax
options = optimoptions(options,'PlotFcn',@plotbestf);
This preserves the current values of all fields of options except for PlotFcn, which is changed to @plotbestf. Note that if you omit the input argument options, optimoptions resets PopulationSize to its default value.
You can also set both PopulationSize and PlotFcn with the single command
options = optimoptions('ga','PopulationSize',100,'PlotFcn',@plotbestf);

Global Maxima

A global maximum, also known as an absolute maximum, the largest overall value of a set, function, etc., over its entire range. It is impossible to construct an algorithm that will find a global maximum for an arbitrary function. SEE ALSO: Global Minimum, Local Maximum, Maximum.

Stalagmite function

A stalagmite function generally refers to the function with which we are generating values over a stalagmite kind of surface for finding out the local and global maxima and minima of that particular given function.

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.