The realization of genetic algorithm matlab

Source: Internet
Author: User

The clcclear % parameter a = 0; b = 4; eps = 0.01; lenchrom = Ceil (log2 (b-a)/eps + 1); Sizepop =: Maxgen =;p Cross = 0. 9;p m = 0.05;  fitness = ones (1,sizepop); chrom = zeros (sizepop,lenchrom); nx = Zeros (Sizepop,lenchrom);   % initialization% randomly produces a population for i = 1:sizepop    for j = 1:lenchrom        Chrom (i,j) = round (rand); &N Bsp   end        x = a + (b-a) * (dot (2. ^ (lenchrom-1): -1:0), Chrom (i,:)))/(2 ^ Lenc hrom-1);        Fitness (i) = Fun (x);  end [bestfitness, Bestindex] = max (fitness);  b Estchrom = Chrom (Bestindex,:);  for i = 1:maxgen   %select    sumfitness = SUM (fitness);    fit = fitness./Sumfi Tness;    Tfit = zeros (sizepop);    Tfit (1) = Fit (1);    for j = 2:sizepop        Tfit (j) = Tfit (j-1) + Fit (j);    end    for k =  1:sizepop       pick = Rand;        if pick < fit (1)             Father = 1;        else           for L = 1: (sizepop-1)   &NB Sp           If pick > Tfit (L) && pick < Tfit (L + 1)                 father = L + 1;              end        &NBS P  end        end        mother = ceil (Rand * sizepop);                   %cross        pick = rand;                 If Pcross > pick            Poscross = Randperm (lenchrom,1);            NX (K,1:poscross) = Chrom (Father,1:poscross); &n Bsp           NX (k, (Poscross + 1): lenchrom) = Chrom (mother, (Poscross + 1): Lenchrom);        else      &N Bsp     NX (k,:) = Chrom (father,:);         end       %mutation       index = Randperm (sizepop,1) ;        pick = Rand;      &NBSP;IF pick < pm        &NBSP ; &NBSP;POSM = Randperm (lenchrom,1);           chrom (INDEX,POSM) = ~chrom (INDEX,POSM); &nbsp ;      end    end        Chrom = NX;        for j = 1:sizepop        x = a + (b-a) * (dot (2. ^ (lenchrom-1): -1:0), Chrom (J,:)))/(2 ^ lenchrom-1);        Fitness (j) = Fun (x);    end        [Newbes Tfitness, Newbestindex] = max (fitness);    if newbestfitness > bestfitness  &nbsp     bestfitness = newbestfitness;        Bestindex = newbestindex;      &NB Sp Bestchorm = Chrom (Bestindex,:);     ENDEND&NBSP;BESTX = a + (b-a) * (dot (2. ^ ((lenchrom-1): -1:0), Bestchrom)/(2 ^ lenchrom-1) BESTF = Bestx * sin (BESTX)

The realization of genetic algorithm matlab

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.