Challenge

Week 4.1 - Genetic Algorithm

REPORT:
main code

defining two 1-dimentional arrays x,y with n value of 100.
making 1-dimentional to 2-dimentional with meshgrid command.

defining a code as a function 'stalagmite' for the equation given.
f=stalagmite(input_vector).

writing for loop to creat stalagmite crust and trufs by using the function defined.

calculation of time for no of iterations by TIC AND toc commands.

 TIC - used to start timer.
 TOC - used to stop timer

taking num_cases=50 set to use as counter for iterations.
 
by taking for loop writing a code calculate the no of iterations by using defalt 'GA'function
[inputs,fval]=ga(@fan,nvbls).
inputs = x,y for which the function has the least value (fval).
nvbls= the number of design variables.
the smallest minimum value of function for each iteration outputed and stored sepetate variables x,y.
TOC commend is used to stop the timer of iteration of study_time1.

ploting:
by figure command making figure 1
subplot(2,1,1) this is used to partation the plot window,in order to plot more plots than one.  
surfc command is used to plot counter with x and y value from the meshgrid as input and negitive 
optimum value is used because we need to maximise the function.
shading interp is used to smoothen the function plot and remove the grid lines.
HOLD ON command is use to prevent plot from being removed.
asining x,y axis by xlabel,ylabel.
graph titel is selected. 

plot3 command is used to plot a point in 3 dimentional where it plot -fval maximum function value.

subplot(1,2,1) 
plot command is used to plot -fval in subplot
assining x,y axis.
x=no of iteration
y =f max.

study_time2
the same study is made but the time is bounded by using upper and lower limit UB,LB.
Then requires the same commands and same steps to evalate the ga command.
but the santax of ga was inserted by LB,UB,A,b,Aeq,beq

[input,fval(i)]= ga(@stalagmite,nvbls,[],[],[],[],LB,UB);

study_time3
the same study is made but the time is bounded by using upper and lower limit UB,LB.
Then requires the same commends and same steps to evalate the ga command.
but the santax of ga was inserted by LB,UB,A,b,Aeq,beq,nonlcon,intcon,options
options-optimazation options,specified as the output uptimoptions or a structure.

[input,fval(i)]= ga(@stalagmite,nvbls,[],[],[],[],LB,UB,[],[],options);

CONCLUSION:
comparing study_time1,2,3 the f max is improved or optimused in study_time3 to single point.
the GA gives more acruate results in the case of bounded inputs by incressing no of iteration and population size.
Optimization techniques are the techniques used to discover the best solution out of all the possible solutions available under the constraints present. The genetic algorithm is one such optimization algorithm built based on the natural evolutionary process of our nature. The idea of Natural Selection and Genetic Inheritance is used here. Unlike other algorithms, it uses guided random search, i.e., finding the optimal solution by starting with a random initial cost function and then searching only in the space with the least cost (in the guided direction). Suitable when you are working with huge and complex datasets.

 Introduction to Optimization:

Optimization is the process of making something better. In any process, we have a set of inputs and a set of outputs as shown in the following figure1.


Optimization:

Optimization refers to finding the values of inputs in such a way that we get the “best” output values. The definition of “best” varies from problem to problem, but in mathematical terms, it refers to maximizing or minimizing one or more objective functions, by varying the input parameters.

The set of all possible solutions or values which the inputs can take make up the search space. In this search space, lies a point or a set of points which gives the optimal solution. The aim of optimization is to find that point or set of points in the search space. 

 What are Genetic Algorithms?

Nature has always been a great source of inspiration to all mankind. Genetic Algorithms (GAs) are search based algorithms based on the concepts of natural selection and genetics. GAs are a subset of a much larger branch of computation known as Evolutionary Computation.

GAs were developed by John Holland and his students and colleagues at the University of Michigan, most notably David E. Goldberg and has since been tried on various optimization problems with a high degree of success.

In GAs, we have a pool or a population of possible solutions to the given problem. These solutions then undergo recombination and mutation (like in natural genetics), producing new children, and the process is repeated over various generations. Each individual (or candidate solution) is assigned a fitness value (based on its objective function value) and the fitter individuals are given a higher chance to mate and yield more “fitter” individuals. This is in line with the Darwinian Theory of “Survival of the Fittest”.

In this way we keep “evolving” better individuals or solutions over generations, till we reach a stopping criterion.

Genetic Algorithms are sufficiently randomized in nature, but they perform much better than random local search (in which we just try various random solutions, keeping track of the best so far), as they exploit historical information as well.                  

       Advantages of GAs:

GAs have various advantages which have made them immensely popular. These include −

Does not require any derivative information (which may not be available for many real-world problems).

Is faster and more efficient as compared to the traditional methods.

Has very good parallel capabilities.

Optimizes both continuous and discrete functions and also multi-objective problems.

Provides a list of “good” solutions and not just a single solution.

Always gets an answer to the problem, which gets better over the time.

Useful when the search space is very large and there are a large number of parameters involved.




    Limitations of GAs:

Like any technique, GAs also suffer from a few limitations. These include −

GAs are not suited for all problems, especially problems which are simple and for which derivative information is available.

Fitness value is calculated repeatedly which might be computationally expensive for some problems.

Being stochastic, there are no guarantees on the optimality or the quality of the solution.

If not implemented properly, the GA may not converge to the optimal solution.

    GA – Motivation:

Genetic Algorithms have the ability to deliver a “good-enough” solution “fast-enough”. This makes genetic algorithms attractive for use in solving optimization problems. The reasons why GAs are needed are as follows −

1)Solving Difficult Problems

In computer science, there is a large set of problems, which are NP-Hard. What this essentially means is that, even the most powerful computing systems take a very long time (even years!) to solve that problem. In such a scenario, GAs prove to be an efficient tool to provide usable near-optimal solutions in a short amount of time.

2)Failure of Gradient Based Methods

Traditional calculus based methods work by starting at a random point and by moving in the direction of the gradient, till we reach the top of the hill. This technique is efficient and works very well for single-peaked objective functions like the cost function in linear regression. But, in most real-world situations, we have a very complex problem called as landscapes, which are made of many peaks and many valleys, which causes such methods to fail, as they suffer from an inherent tendency of getting stuck at the local optima as shown in the following figure 2.

    GA Motivation:

Getting a Good Solution Fast
Some difficult problems like the Travelling Salesperson Problem (TSP), have real-world applications like path finding and VLSI Design. Now imagine that you are using your GPS Navigation system, and it takes a few minutes (or even a few hours) to compute the “optimal” path from the source to destination. Delay in such real world applications is not acceptable and therefore a “good-enough” solution, which is delivered “fast” is what is required.                                                                                              
function f = stalagmite(input_vector)
x= input_vector(1);
y= input_vector(2);

f1 = (sin(5.1*pi*x+0.5)).^6;

f2 = exp(-4*log(2)*((x-0.0667).^2)./0.64);

f4 = exp(-4*log(2)*((y-0.0667).^2)./0.64);

f3 = (sin(5.1*pi*y+0.5)).^6;

f = -(f1*f2*f3*f4);

end
close all
clear all
clc
%program to predict global maxima of a stalagmite function

% 1-dimensional array of x and y

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

% 2-dimensional array of x,y

[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(input_vector);
    end
end

%calculation of time for no of iterations
tic

num_cases=50;
for i=1:num_cases
    [input,fval(i)]=ga(@stalagmite,2);%genitic algortham syntax
    x_out(i)= input(1);
    y_out(i)= input(2);
end

study_time1=toc

%ploting
figure(1)
subplot(2,1,1)
surfc(x,y,-f)
shading interp
xlabel('xaxis')
ylabel('yaxis')
hold on
plot3(x_out,y_out,-fval,'Marker','o','MarkerSize',4,'MarkerFaceColor','r')

subplot(2,1,2)
plot(-fval)
xlabel('iteration')
ylabel('fmax')

% study_time2
%there are many maximua,to reduse we go  with different cases
% by setting upper and lower bounses to the code
LB=[0;0];
UB=[0.6;0.6];
nvbls=2;
% calculation of time for iteration
tic
for i=1:num_cases
    [input,fval(i)]= ga(@stalagmite,nvbls,[],[],[],[],LB,UB);
    x_out(i)=input(1);
    y_out(i)=input(2);
end

study_time2=toc;

%plotting
figure(2)
subplot(2,1,1)
surfc(xx,yy,-f)
shading interp
xlabel('xaxis')
ylabel('yaxis')
title('study_time2 bounded with LB UB')
hold on

plot3(x_out,y_out,-fval,'Marker','o','MarkerSize',5,'MarkerFaceColor','r')
subplot(2,1,2)
plot(-fval)
xlabel('iteration')
ylabel('fmax')

%study_time3
%incressing the value of ga
options=optimoptions('ga');
options= optimoptions(options,'PopulationSize',200);


% calculation of time for iteration
tic
for i=1:num_cases
    [input,fval(i)]= ga(@stalagmite,nvbls,[],[],[],[],LB,UB,[],[],options);
    x_out(i)=input(1);
    y_out(i)=input(2);
end

study_time3=toc;

%plotting
figure(3)
subplot(2,1,1)
surfc(xx,yy,-f)
shading interp
xlabel('xaxis')
ylabel('yaxis')
title('study_time3 bounded with incress in ga value')
hold on

plot3(x_out,y_out,-fval,'Marker','o','MarkerSize',5,'MarkerFaceColor','r')
subplot(2,1,2)
plot(-fval)
xlabel('iteration')
ylabel('fmax')



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.