MATLAB genetic algorithm performance test

Source: Internet
Author: User

Genetic algorithms, combined with biological genetic rules, are used to solve the problem's optimal solution and are widely used.

However, because of its uncertainty algorithm, the search performance and the stability of the optimal solution still need to be improved.

Using the Genetic Algorithm of Matlab, write the code as follows:

Function [output_args] = ga_testclear; % solution: x = [0, 0,...] % nvar = 30% dims: [-30, 30] function fitness = sphere (Vals) Prod = Vals. * Vals; fitness = sum (prod, 2); end % f (x) = 1/4000 * sum ^ N_1 (x_ I) ^ 2-prod ^ N_1 * Cos (x_ I/SQRT (I) + 1% f * = 0, x * = [0, 0,...]; % nvar = 30% dims: [-600,600] function fitness = griewank (Vals) [H w] = size (Vals); P1 = Vals. ^ 2; P1 = 1/4000. * sum (P1, 2); t = SQRT ([1: W]); P2 = Vals. /repmat (T, H, 1); P2 = cos (P2); P2 = prod (P2, 2); fitness = p1-P2 + 1; end % solution: x * = [1, 1, 1,...] % nvar: 30 function fitness = rosenbroek (Vals) [k N] = size (Vals); vals1 = Vals (:, 1: N-1); vals2 = Vals (:, 2: n); TMP = vals2-vals1. * vals1; TMP = 100 * TMP. * TMP; tmp1 = (vals1-1 ). ^ 2; fitness = sum (TMP, 2) + sum (tmp1, 2); end % many local minimum values, optimal solution: x = [0, 0], fixed 2 variables % nvar = 2 function fitness = rastrigin (x) V = x. ^ 2-10. * Cos (2 * pi. * x) + 10; fitness = sum (v, 2); end % parameter popsize = 50; % group scale run = 4; % test round option = gaoptimset ('populationsize ', popsize); %, 'useparallel', true); ans ={}; for r = 1: Run % Round func = R; % test the fitness function switch func case 1 func = @ sphere xmin =-100; xmax = 100; nvar = 30; Case 2 func = @ griewank xmin =-600; xmax = 600; nvar = 30; Case 3 nvar = 30; func = @ rosenbroek xmin =-100; xmax = 100; Case 4 nvar = 2; func = @ rastrigin xmin =-5.12; xmax = 5.12; end; TIC; [ans {r }. bestgene, ANS {r }. bestfit] = Ga (func, nvar, [], [], [], [], repmat (xmin, nvar, 1), repmat (xmax, nvar, 1 ), [], option); costt = toc ans {r }. costtime = costt; end % can run 30 rounds, view the result for r = 1: Run fprintf ('fit = % F, costtime = % F (s) \ n ', ans {r }. bestfit, ANS {r }. costtime); % ans {r }. bestgeneend; End
Running result:

Func =


@ Ga_test/sphere


Optimization terminated: average change in the fitness value less than options. tolfun.


Costt =


4.2468




Func =


@ Ga_test/griewank


Optimization terminated: average change in the fitness value less than options. tolfun.


Costt =


4.2952




Func =


@ Ga_test/rosenbroek


Optimization terminated: Maximum number of generations exceeded.


Costt =


14.4236




Func =


@ Ga_test/rastrigin


Optimization terminated: average change in the fitness value less than options. tolfun.


Costt =


0.2880


Fit = 0.000100, costtime = 4.246814 (s)
Fit = 0.000150, costtime = 4.295222 (s)
Fit = 1.511176, costtime = 14.423554 (s)
Fit = 0.000000, costtime = 0.288025 (s)


In terms of running time, the algorithm performance is poor.

Therefore, new mechanisms and algorithms need to be proposed to solve such problems, such as genetic algorithms and differential evolution.

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.