Sample Code for solving Matlab Genetic Algorithm Optimization Problems, matlab Genetic Algorithm

Source: Internet
Author: User
Tags binary to decimal

Sample Code for solving Matlab Genetic Algorithm Optimization Problems, matlab Genetic Algorithm

The Code is as follows:

Function m_main () clearclcMax_gen = 100; % running algebra pop_size = 100; % population size chromsome = 10; % chromosome length pc = 0.9; % crossover probability pm = 0.25; % mutation probability gen = 0; % statistical algebra % initialize init = 40 * rand (pop_size, chromsome)-20; pop = init; fit = obj_fitness (pop); [max_fit, index_max] = max (fit); maxfit = max_fit; [min_fit, index_min] = min (fit); best_indiv = pop (index_max ,:); % iteration operation while gen <Max_gen gen = gen + 1; bt (gen) = max_fit; if maxfit <max_fit; maxfit = Max_fit; pop (index_min, :) = pop (index_max, :); best_indiv = pop (index_max, :); end best_indiv_tmp (gen) = pop (index_max); newpop = ga (pop, pc, pm, chromsome, fit); fit = obj_fitness (newpop); [max_fit, index_max] = max (fit); [min_fit, index_min] = min (fit ); pop = newpop; trace (1, gen) = max_fit; trace (2, gen) = sum (fit ). /length (fit); end % running result [f_max gen_ct] = max (bt) % calculates the maximum value and algebra maxfitbest_indiv % plot % bth Old onplot (trace (1 ,:),'. g: '); plot (trace (2 ,:),'. r-'); title ('experiment result fig') xlabel ('iterations/'), ylabel ('best fitness (maximum )'); % coordinate annotation plot (gen_ct-1, 0: 0. 1: f_max + 1, 'c-'); % plot the maximum value text (gen_ct, f_max + 1, 'maximal') hold off function [fitness] = obj_fitness (pop) % fitness calculation function [r c] = size (pop); x = pop; fitness = zeros (r, 1); for I = 1: r for j = 1: c fitness (I, 1) = fitness (I, 1) + sin (sqrt (abs (40 * x (I) + 1-abs (x (I )) /20.0; end Function newpop = ga (pop, pc, pm, chromsome, fit) pop_size = size (pop, 1); % select ps = fit/sum (fit ); pscum = cumsum (ps); % size (pscum) r = rand (1, pop_size); qw = pscum * ones (1, pop_size ); selected = sum (pscum * ones (1, pop_size) <ones (pop_size, 1) * r) + 1; newpop = pop (selected, :); % cross if pop_size/2 ~ = 0 pop_size = pop_size-1; end for I = 1: 2: pop_size-1 while pc> rand c_pt = round (8 * rand + 1); pop_tp1 = newpop (I ,:); pop_tp2 = newpop (I + 1, :); newpop (I + 1, 1: c_pt) = pop_tp1 (1, 1: c_pt); newpop (I, c_pt + 1: chromsome) = pop_tp2 (1, c_pt + 1: chromsome); end % variant for I = 1: pop_size if pm> rand m_pt = 1 + round (9 * rand ); newpop (I, m_pt) = 40 * rand-20; end endend



MATLAB genetic algorithm source program

Genetic algorithm example:

I also found it myself. I have corrected a few errors in the original code and passed the debugging.
It is a very useful file for beginners, especially those who have no programming experience.
Genetic algorithm example

% The following example illustrates the genetic algorithm %
% Calculate the maximum value of the following functions %
% F (x) = 10 * sin (5x) + 7 * cos (4x) x ε [0, 10] %
% Represents the value of x in the form of a 10-bit binary value as a binary problem. The resolution provided by a 10-bit binary value is (10-0) /(2 ^ 10-1) ≈ 0.01. %
% The variable field [1023] Is discretization to the binary value field [], x = 0 + 10 * B/, where B is a binary number in. %
%
% Percent %
% Percent %

% Programming
% -----------------------------------------------
% 2.1 initialization (encoding)
The % initpop. m function is used to initialize a group. popsize indicates the group size, and chromlength indicates the length of the chromosome (length of a binary number ),
The length of % depends on the length of the variable's binary code (10 digits in this example ).
% Genetic algorithm subroutine
% Name: initpop. m
% Initialization
Function pop = initpop (popsize, chromlength)
Pop = round (rand (popsize, chromlength); % rand randomly generates a matrix with the number of rows being {0, 1} and the number of columns being chromlength,
% Roud round each unit of the matrix. The initial population.

% 2.2 calculate the target function value
% 2.2.1 convert binary to decimal (1)
% Genetic algorithm subroutine
% Name: decodebinary. m
% Generates the row vector [2 ^ n 2 ^ (n-1)... 1], then sums and converts the binary into decimal.
Function pop2 = decodebinary (pop)
[Px, py] = size (pop); % evaluate the pop row and column number
For I = 1: py
Pop1 (:, I) = 2. ^ (py-I). * pop (:, I );
End
Pop2 = sum (pop1, 2); % calculates the sum of each row of pop1

% 2.2.2 convert binary encoding to decimal number (2)
The % decodechrom. m function is used to convert the chromosome (or binary code) to decimal. The spoint parameter indicates the starting position of the binary string to be decoded.
% (For multiple variables, if there are two variables, expressed as 20, each variable 10 is, the first variable starts from 1, and the other variable starts from 11. In this example, 1 ),
% The 1ength parameter indicates the intercepted length (10 in this example ).
% Genetic algorithm subroutine
% Name: decodechrom. m
% Convert binary encoding to decimal
Function pop2 = decodechrom (pop, spoint, length)
Pop1 = pop (:, spoint: spoint + length-1 );
Pop2 = decodebinary (pop1 );

% 2.2.3 calculate the target function value
% Cal ...... remaining full text>

Code for Path Optimization Problems Based on Genetic Algorithm in matlab

Algorithm writing is related to your constraints.
 

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.