Challenge

Genetic Algorithm

AIM:-

Find the maximum value on a plane.

OBJECTIVES:-

  • Write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function.
  • Clearly expalin the concept of genetic algorithm in your own words and also explain the syntax for ga in MATLAB in your report.

THEORY:-

Genetic algorithm (GA) is a metahuristic inspired by the process of natural selection that belongs to a larger category of evalutionary algorithm (EA). Genetic algorithms are often used to produce high-quality optimization remedies and search problems by relying on environmentally-inspired operators such as mutation, crossover and selection.john holland introduced genetic engineering in 1960 based on Darvin's concept of cleaning.

GA is used for optimization.as at the request of each project the GA is used to obtain the maximum value in the face.In MatLAB GA will only give the lowest value, by using a little logic in the system we can control GA and make it give maximum value.*

Five sections are drawn from the genetic algorithm.
1. First population
2. The work of fitness
3. Choice
4. Crossover
5. Conversion

1. Human Origin
The process begins with a group of people called Population. Each person is the solution to the problem you want to solve.2. The work of fitness
The intensity function determines how good a person is (one’s ability to compete with other people). It provides points of strength for each individual. The probability that a person will be selected for rebirth is based on the number of severity.
3. Choice
The idea of ​​the selection phase is to select the most suitable individuals and let them pass on their species to future generations.
4. Crossover
Crossover is the most important stage in the genetic algorithm. For each pair of parents to marry, a crossover point is randomly selected within the gene.
5. Conversion
For some newly generated offspring, some of their subfamilies can be assigned to possible genes by random randomization. This means that some fragments in the transformed threads can be turned on.
6. Termination
The algorithm disappears when the population is merged (not producing very different offspring from the previous generation). It is afterwards said that the genetic algorithm provides a set of solutions to our problem.

SYNTAX:


1) x = ga(fun,nvars):
It gets a bit of unreadable space, x, for serving purpose, it's fun. nvars is the size (amount of variation in design) of the fun.


2) x = ga (fun, nvars, A, b):
Finds the minimum radius x excitation, depending on the exact inequality of A * x ≤ b. ga analyzes the product of the matrix A * x as if x is passed (A * x ').


3) x = ga (fun, nvars, A, b, Aeq, beq):
Finds the minimum area of ​​x to be happy, depending on the corresponding values ​​of Aeq * x = beq and A * x ≤ b. (Set A = [] and b = [] if no direct inequalities exist.) Ga analyzes the product of the matrix Aeq * x as if x has been transferred (Aeq * x ').


4) x = ga (fun, nvars, A, b, Aeq, beq, lb, ub):
Defines a set of lower and upper bounds in a variable, x, in order to obtain a solution in the range lb ≤ x ≤ ub. (Set Aeq = [] and beq = [] if no exact equations exist.)


5) x = ga (fun, nvars, A, b, Aeq, beq, lb, ub, nonlcon):
Includes a reduction in the pressures described in the nonlcon. The nonlcon function accepts x and returns the veterans C and Ceq, representing linear inequalities and equations respectively. ga decreases the sweetness of C (x) ≤ 0 and Ceq (x) = 0. (Set lb = [] and ub = [] if there are no parameters.)


6) x = ga (fun, nvars, A, b, Aeq, beq, lb, ub, nonlcon, options):
It is reduced by the default default parameters that are replaced by other values ​​with options. (Set nonlcon = [] if there are no nonlinear constraints.) Create options using optimoptions.


7) x = ga (fun, nvars, A, b, [], [], lb, ub, nonlcon, IntCon):
It requires that variables in the IntCon list take numeric values.


8) x = ga (problem):
It detects the least of the problem, where the problem is structural.


9) [x, fval] = ga (___):
In any argument of the previous input, it returns the fval, the value of the fitness function at x.


10) [x, fval, exitflag, output] = ga (___):
It then returns an output, which is an integer that points to why the algorithm is terminated, and as a result, a structure containing output for each generation and other information about the operation of the algorithm.


11) [x, fval, exitflag, output, population, scores] = ga (___):
It also returns the population of the matrix, their rows belong to the last population, and vector scores, the scores of the last people.


HOW IT WORKS Generations:


Algorithm outline:The following list summarizes how the genetic algorithm works:
1. The algorithm starts by building the first population.
2. The algorithm subsequently creates a sequence of new people. In each step, the algorithm uses people in the current generation to create the next value. To create a new population, the algorithm performs the following steps:1. Scores by each member of the current figure by adding its physical value. These values ​​are called raw fitness scores.

2. The algorithm subsequently creates a sequence of new people. In each step, the algorithm uses people in the current generation to create the next value. To create a new population, the algorithm performs the following steps:
1. Scores by each member of the current figure by adding its physical value. These values ​​are called fitness scores.
2. Scale the fitness scores of the converters to convert them into a more useful value range. These computed values ​​are called expected values.
3. To select members, called parents, according to their desire.
4. Some people of the present population with low physical fitness are selected as special. These fictional characters are passed on to the following people.
5. Produce children from parents. Children are produced by making random mutations in a single parent - a genetic mutation - or by combining the input of a parent vector - a crossover.
6. Instead of existing people and children to build the next generation.


3. The algorithm stops when one of the configuration methods is met.

CALCULATION:-

the formula for stalagmite function

MATLAB program:-

clear all
close all
clc
                                              % X and Y values is an array. 
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
                                     % meshgrid was done to X and Y arrays. 
[xx yy]=meshgrid(x,y);

tic
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);
        get_stalagmite(i,j)=f(i,j);
    end
end
                                %program for plotting the surface and plot.
figure(1)
surf(xx,yy,-f)
shading interp
                                                                   %STUDY-1
                                  %study about genetic algorithm function.
[x,b]=ga(@stalagmite,2)
xlabel('X-axis')
ylabel('Y-axis')
zlabel('f')
sim1_time=toc


                                                                   %STUDY-2
num_cases=50
tic
for i=1:num_cases;
    [inputs, fopt(i)]=ga(@stalagmite,2);
    xopt(i)=inputs(1);
    yopt(i)=inputs(2);
        
end
sim2_time=toc
                                %program for plotting the surface and plot.
figure(2)
subplot(2,1,1)
hold on
surf(xx,yy,-f)
shading interp
plot3(xopt,yopt,-fopt,'-o','MarkerSize',5,'MarkerEdgeColor','b', 'MarkerFaceColor','r')
xlabel('x')
ylabel('y')
zlabel('f')

title('unbounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('iterations')
ylabel('function minima')

                                                                   %study-3
tic
for i=1:num_cases;
    [inputs fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
        xopt(i)=inputs(1);
        yopt(i)=inputs(2);
end
sim3_time=toc
                                %program for plotting the surface and plot.
figure(3)
subplot(2,1,1)
hold on
surf(xx,yy,-f)
shading interp
plot3(xopt,yopt,-fopt,'-o','MarkerSize',5,'MarkerEdgeColor','b', 'MarkerFaceColor','r')
xlabel('x')
ylabel('y')
zlabel('f')

title('unbounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('iterations')
ylabel('function minima')
                                                                    %study3

    tic
    options = optimoptions('ga','populationsize',300);
for i=1:num_cases
    
       [inputs fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);
        xopt(i)=inputs(1);
        yopt(i)=inputs(2);
end
figure(4)
                                %program for plotting the surface and plot.
subplot(2,1,1)
hold on
surf(xx,yy,-f)
shading interp
plot3(xopt,yopt,-fopt,'-o','MarkerSize',5,'MarkerEdgeColor','b', 'MarkerFaceColor','r')
xlabel('x')
ylabel('y')
zlabel('f')

title('unbounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('iterations')
ylabel('function minima')

sim4_time=toc
                                       %functional program for main program
function f=stalagmite(input_vector);
x=input_vector(1);
y=input_vector(2);
f1x=(sin(5.1*pi*x+0.5))^6;
f1y=(sin(5.1*pi*y+0.5))^6;
f2x=exp(-4*log(2)*((x-0.0677)^2)/0.64);
f2y=exp(-4*log(2)*((y-0.0677)^2)/0.64);
f =-1*(f1x.*f1y.*f2x.*f2y);
end

Interpretation:

1) Start the program with 'erase everything', 'close all', and the 'clc' commands

2) First, we have to write a Stalagmite activity plan. Solving the genetic algorithm for stalagmite performance gives us a small amount of work. To obtain the maximum value of the function, we need to change the function's sign, to give the sum of the backward value of minima, e.g. Maxima.

3) Now we need to define the search space for finding x & y values ​​in the stalagmite function. The association space is selected to be between 0 to 0,6 at 150 intervals. In this case we have used 'linspace ()'. The genetic Algorithm cannot take. for each time we execute the code we assign different values ​​each time. In order to save the calculation time limit our script to work for only 50. It does not mean that the function will provide the maximum value of 50 iterations. We will process any maximum value up to those 50 times. We have limited our search space. This is none of the news provided by 'N = 50'.

4) Since 'linspace ()' creates a series of nos, x & y will be the column keeper. As an input for stalagmite function, we need to convert a column vector into a line vector. The first step of this transformation is to create a 'meshgrid (x, y)'. Next, we use 'up' conversion.

5) The next step is to obtain the results of the 3 cases of the genetic algorithm in Matab. In the first case, the maximum amount of work is achieved with no limits, no limits, and no limit. The genetic algorithm is free to select a population and then reproduce and change.

6) To get the same amount each time we run a program, we need to limit the system within certain limits ie high bound and low bound. The whole process of organizing this case is the same as the previous one, except GA.

7) Even the results from the arrest cases are not always the same when the program ends. So now we need to maximize GA iterations. We can raise it at any price, choosing it as 300. In this case, the syntax of GA is different than the previous case. The GA should provide this population with an option.

8) The results of this case will be the number one piece of work that goes wrong with no reports.

Result:

Errors:

Many basic syntex errors are there.And also many typing mistakes are there.

STEPS TAKEN TO SOLVE ERRORS:

1. Check in google.

2. Rewatch course videoes multiple time.

References:

1) https://www.mathworks.com/help/gads/how-the-genetic-algorithm-works.html

2) https://www.mathworks.com/discovery/genetic-algorithm.html

3) https://in.mathworks.com/help/gads/examples/genetic-algorithm-options.html

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.