Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (2 votes)
740 views

Matlab Code Example

The document describes a MATLAB program that uses a genetic algorithm to maximize the function f(x) = x^2 over x values from 0 to 31. The program performs 4 iterations with a population size of 4 in each iteration. It initializes the population in binary, converts to decimal, calculates fitness values, performs selection, crossover and mutation to evolve the population across iterations with the goal of finding the maximum value of f(x).

Uploaded by

ram_drsn
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (2 votes)
740 views

Matlab Code Example

The document describes a MATLAB program that uses a genetic algorithm to maximize the function f(x) = x^2 over x values from 0 to 31. The program performs 4 iterations with a population size of 4 in each iteration. It initializes the population in binary, converts to decimal, calculates fitness values, performs selection, crossover and mutation to evolve the population across iterations with the goal of finding the maximum value of f(x).

Uploaded by

ram_drsn
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

Refer chapter 8 in "Introduction to Genetic Algorithms" By S.N.

Deepa "Chapter 8: Genetic Algorithm Implementation Using Matlab" Introduction to genetic algorithms - S. N. Sivanandam, S. N. Deepa - Google Books Ex in page 224 Problem 1 Write a MATLAB program for maximizing f(x) = x2 using genetic algorithm, where x ranges from 0 to 31. Perform 4 iterations. %program for Genetic algorithm to maximize the function f(x) =xsquare clear all; clc; %x ranges from 0 to 31 2power5 = 32 %five bits are enough to represent x in binary representation n=input(Enter no. of population in each iteration); nit=input(Enter no. of iterations); %Generate the initial population [oldchrom]=initbp(n,5) %The population in binary is converted to integer FieldD=[5;0;31;0;0;1;1] for i=1:nit phen=bindecod(oldchrom,FieldD,3);% phen gives the integer value of the binary population %obtain fitness value sqx=phen. 2; sumsqx=sum(sqx); avsqx=sumsqx/n; hsqx=max(sqx); pselect=sqx./sumsqx; sumpselect=sum(pselect); avpselect=sumpselect/n; hpselect=max(pselect); %apply roulette wheel selection FitnV=sqx; Nsel=4; newchrix=selrws(FitnV, Nsel); newchrom=oldchrom(newchrix,:); %Perform Crossover crossoverrate=1; newchromc=recsp(newchrom,crossoverrate);%new population after crossover %Perform mutation vlub=0:31; mutationrate=0.001; newchromm=mutrandbin(newchromc,vlub,mutationrate);%new population after mutation disp(For iteration); i disp(Population); oldchrom disp(X); phen disp(f(X)); sqx oldchrom=newchromm; end

Output Code:
Enter no. of population in each iteration4 Enter no. of iterations4 oldchrom = 1 0 0 1 0 0 1 0 1 0 0 0 1 1 0 1 1 1 1 0 FieldD = 5 0 31 0 0 1 1 For iteration i = 1 Population oldchrom =1 0 0 1 0 0 1 0 1 0 0 0 1 1 0 1 1 1 1 0 X phen = 18 10 6 30 f(X) sqx = 324 100 36 900 For iteration i = 2 Population oldchrom = 1 1 1 0 0 0 1 1 0 1 0 0 1 1 0 1 0 1 0 1 X phen = 28 13 6 21 f(X) sqx = 784 169 36 441

For iteration i = 3 Population oldchrom = 0 0 0 0 1 0 0 1 1 1 0 0 0 0 1 1 0 1 0 0X phen = 1 7 1 20 f(X) sqx = 1 49 1 400 For iteration i = 4 Population oldchrom = 1 0 0 0 0 1 1 0 1 1 1 0 0 1 1 0 1 1 1 1 X phen = 16 27 19 15 f(X) sqx = 256 729 361 225

You might also like