Examples of genetic algorithms (MATLAB Implementation)

Source: Internet
Author: User
Tags abs rand sin

Genetic algorithm optimization function y=10*sin (5*x) +7*abs (x-5) +10, this function image is:

Here's a look at the code:

(1) First look at the main function

function main () clear;clc;% population size popsize=100;% binary encoded length chromlength=10;% crossover probability pc = 0.6;% mutation probability pm = 0.001;% initial population pop = Initpop ( Popsize,chromlength); For i = 1:100    % Calculation of fitness value (function value)    ObjValue = Cal_objvalue (pop);    Fitvalue = ObjValue;    % selection operation    Newpop = Selection (pop,fitvalue);    % crossover operation    Newpop = Crossover (NEWPOP,PC);    % variant operation    Newpop = mutation (NEWPOP,PM);    % update population    pops = Newpop;    % search for optimal solution    [bestindividual,bestfit] = Best (pop,fitvalue);    x2 = Binary2decimal (bestindividual);    X1 = Binary2decimal (newpop);    Y1 = Cal_objvalue (newpop);    If mod (i,10) = = 0 Figure        ;        Fplot (' 10*sin (5*x) +7*abs (x-5) +10 ', [0]);        Hold on;        Plot (x1,y1, ' * ');        Title ([' Iteration number is n= ' num2str (i)]);        %plot (x1,y1, ' * ');    endendfprintf (' The best X was--->>%5.2f\n ', x2); fprintf (' The best Y is--->>%5.2f\n ', bestfit);

(2) The method of binary population generation is shown below

% initialized population size% input variable:%popsize: Population size%chromlength: Chromosome length-->> conversion binary length% output variable:%pop: Population function Pop=initpop (popsize, chromlength) pop = round (rand (POPSIZE,CHROMLENGTH));%rand (3,4) generates 3 rows 4 columns of 0-1 random number% rand (3,4)% ans =%%     0.8147    0.9134    0.2785    0.9649%     0.9058    0.6324    0.5469    0.1576%    0.1270 0.0975 0.9575    0.9706%round is rounding% round (rand (3,4)) =% 1 1 0 1% 1 1 1 0% 0 0 1 1% So the returned population is each row is an individual, the number of columns is the chromosome length

(3) See below How to return the binary to the corresponding decimal

% binary converted to decimal function% input variable:% binary population% output variable% decimal value function Pop2 = Binary2decimal (POP) [Px,py]=size (POP); for i = 1:py    pop1 (:, i) = 2.^ (py-i). *pop (:, i); End%sum (., 2) Sums the rows, gets the column vector temp = SUM (pop1,2);p op2 = temp*10/1023;

(4) The calculation of fitness function:

% calculation function target value% input variable: Binary value% output variable: target function value function [ObjValue] = Cal_objvalue (pop) x = Binary2decimal (pop);% The conversion binary number is the x variable of the change domain range of the numerical objvalue=10*sin (5*x) +7*abs (x-5) +10;

(5) How to choose a new individual

All of the above individual function values are calculated, there is objvalue, at this time it is not also 100 sets of Y-value ah, well, then for the existing randomly generated 100 x, how to choose 100 new groups of better x? Here we put the choice between the crossover and the mutation, both can, how to choose, the probability of the construction of the roulette, who is the probability of large, is not the choice of individuals will be more? That is, the choice is now 100 in 100, the last is enough to be the previous 100 of the best X has a word, after the choice, may become 5 this x, the extra 4 is not equivalent to replace the previous bad 4 x value, so as to achieve the total number of X 100 unchanged AH.

% How to select a new individual% input variable: Pop binary population, fitvalue: Fitness value% output variable: Newpop Select binary population function [Newpop] = Selection (pop,fitvalue)% construction Roulette [px,py ] = size (POP); totalfit = SUM (fitvalue);p _fitvalue = Fitvalue/totalfit;p_fitvalue = Cumsum (p_fitvalue);% probability summation sort ms = Sort ( Rand (px,1));% from small to large arrange Fitin = 1;newin = 1;while newin<=px    if (MS (Newin)) <p_fitvalue (Fitin)        Newpop (Newin, :) =pop (Fitin,:);        Newin = newin+1;    else        fitin=fitin+1;    EndEnd

(6) How to cross

% Cross transform% input variable: pop: Binary parent population number, PC: Crossover probability% output variable: newpop: Number of population after crossing function [Newpop] = Crossover (POP,PC) [px,py] = size (POP); Newpop = ones (size (pop)); for i = 1:2:px-1    if (rand<pc)        CPoint = round (rand*py);        Newpop (i,:) = [Pop (i,1:cpoint), Pop (i+1,cpoint+1:py)];        Newpop (i+1,:) = [Pop (i+1,1:cpoint), Pop (i,cpoint+1:py)];    else        newpop (i,:) = Pop (i,:);        Newpop (i+1,:) = Pop (i+1,:);    EndEnd

(7) How to Mutate

% about compiling% function description% input variable: pop: Binary population, pm: Variance probability% OUTPUT variable: Newpop mutation after population function [Newpop] = mutation (POP,PM) [px,py] = size (POP); Newpop = Ones (Size (pop)); for i = 1:px    if (rand<pm)        mpoint = round (rand*py);        If Mpoint <= 0;            Mpoint = 1;        End        Newpop (i,:) = Pop (i,:);        If Newpop (i,mpoint) = = 0            newpop (i,mpoint) = 1;        else Newpop (i,mpoint) = = 1            newpop (i,mpoint) = 0;        End    Else Newpop (i,:) = Pop (i,:);    EndEnd

(8) Select the best individual

% optimal fitness function% input variable: Pop: Population, fitvalue: Population fitness% output variable: bestindividual: Best Individual, Bestfit: Optimal fitness value function [bestindividual bestfit] = Best (Pop,fitvalue) [px,py] = size (pop), bestindividual = POPs (1,:); bestfit = Fitvalue (1); For i = 2:px    if Fitvalue (i) & Gt;bestfit        bestindividual = Pop (i,:);        Bestfit = Fitvalue (i);    EndEnd

The genetic algorithm program is over here.

See Partial picture results:

Cross

The crossover operator in the basic genetic Algorithm (SGA) employs a single-point crossover operator.

Single Point crossover operation

Variation

Ext.: https://www.cnblogs.com/LoganChen/p/7509702.html

Examples of genetic algorithms (MATLAB Implementation)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.