Challenge

Week 4 - Genetic Algorithm

function [f]=stalagmite(input_vector)
x=input_vector(1);
y=input_vector(2);
f1=(sin(((5.1*pi).*x)+0.5)).^6;
f2=exp((((x-0.0667).^2).*(-4*log(2)))./0.64);
f3=(sin(((5.1*pi).*y)+0.5)).^6;
f4=exp((((y-0.0667).^2).*(-4*log(2)))./0.64);
f=-((f1)*(f2)*(f3)*(f4));
end


clear all
close all
clc

% Defining our search space
x=linspace(0,0.6,150); % No of eually spaced points b/w 0 & 150 with difference 0.6
y=linspace(0,0.6,150); %No of eually spaced points b/w 0 & 150 with difference 0.6

% Creating a 2 Dimensional array

[xx yy]=meshgrid(x,y) % To create Matrices from arrays

% Evaluating the stalgmite 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    

surfc(xx,yy,f) % surface plot of input vectors xx and YY using 'f' function

shading interp % hiding the mesh lines from surfc plot

for i=1:100
    [inputs,fval]=ga(@stalagmite,2)% find a local min/maxima to objective function 'Stalagmite' & 2 is dimension of stalagmite function
end    




%% Genetic Algorithm Syntax from matlab source:

Definiton of genetic Algorithm
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.

x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,options)

Fun=function
nvars=number of variables
A=Non linear in equalities , Aeq=[]
b=Non linear in equalities , beq=[]
Aeq=Linear equalities,A=[]
beq=Linear equalities,B=[]
Linear equations  Aeq*x=b,and A*x=b
lb=Lower Bound of design variables
ub=Upper Bound of design variables
nonlcon=function nonlcon accepts x and returns vectors C and Ceq, representing the 
        nonlinear inequalities and equalities respectively.
options=minimizes with the default optimization parameters replaced by values in option
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.