The convergence speed of this algorithm can also be found within 10,000 generations.
Main program
clear;clc;%%% Eight queen problem, 8x8 on the board, placing 8 queens, so that 22 can not attack the initial state, randomly placed on the board 8 Queens, each column put an n = 8; %8 Queen percent percent% the genetic algorithm is used to calculate the number of individuals randomly, and to form a population of 10 individuals no_of_people = 10; People = Randi (N,[no_of_people,n]); % the H value of each initial population people_h = ones (no_of_people,1); For i = 1:no_of_people People_h (i) = Fun_c (People (I,:)); End% evolved how many generations, by g to count g = 1; G_max = 1e5; PLT = zeros (1,g_max); While prod (people_h) ~=0 && g<=g_max% Elite retention policy, retain the initial population of 1/100 of elite individuals% retain how many elite No_elite = fix (no_ OF_PEOPLE/100); If No_elite = = 0 No_elite = 1; End% Select these elites by H value [~,ind] = sort (people_h); index = IND (1:no_elite); People_elite = People (index,:); % calculates the advantage of each individual in this population, expressed as a percentage, that the sum of all individual advantages is 1 ADV = people_h./sum (PEOPLE_H); % randomly selected from the population of 10 pairs of individuals, according to individual advantages to choose, the greater the advantage, the greater the probability of being selected People_dad = people; People_mom = people; For i=1:no_of_people pick = ones (2,1); While pick (1) ==pick (2) pick = RANDSRC (2,1,[1:no_of_people; Adv ']); End People_dad (i,:) = people (pick (1),:); People_mom (i,:) = people (pick (2),:); End% then cross-breed, chromosome crosses. A couple gave birth to only one child for i=1:no_of_people% randomly generates an intersection point P = Randi (n-1); People (i,:) = [People_dad (i,1:p), People_mom (i,p+1:n)]; End% then generates a random mutation for i=1:no_of_people% at a certain probability to randomly generate a mutation site P_change = rand (1); P_dot = Randi (n); The probability of setting a mutation is 10% if P_change <= 0.1 people (I,p_dot) = Randi (n); End end% Update population h value for i = 1:no_of_people People_h (i) = Fun_c (People (I,:)); End% finds the worst individuals in the offspring of reproduction, number of individuals = number of elites [~,ind] = sort (people_h, ' descend '); Index_bad = IND (1:no_elite); % delete the worst individual people (Index_bad,:) = []; % to join the elite in the population peoplE_tmp = [people; people_elite]; people = people_tmp; % Update population h value for i = 1:no_of_people People_h (i) = Fun_c (People (I,:)); End PLT (G) = min (people_h); g = g + 1; End Plot (PLT (1:g-1)); Axis Auto; Percent if prod (people_h) ==0 disp (' Genetic algorithm convergence '); index = FIND (People_h = = 0); Disp (' Possible solution for '); Disp (People (index,:)); Else disp (' Genetic algorithm does not converge '); End DISP ([' Experienced ', Num2str (G-1), ' Generation of heredity ']);
function [h] = fun_c (state)% evaluates its cost functions h = 0 according to a condition; n = length (state); Percent% of the state of each column, see how many can attack each other, count every 22 attacks for i=1:n count = Length (find (state = = i)); if Count > 1; H = h + nchoosek (count,2); End end percent% converts state to nxn matrix State_full = zeros (n,n); For I=1:n for j=1:n if j = = State (i) state_full (i,j) = 1; End end% percent per left diagonal state, see how many can attack each other, every 22 attacks counted once i=1; J=1; add = 0; While i<n+1 && j<n+1 && i>0 && j>0% calculate left diagonal diagonal How many queens per line are there? count = Fun_calc_le FT (i,j,n,state_full); if Count > 1; H = h + nchoosek (count,2); End If Add = = 0; j = j + 1; ElseIf add = = 1; i = i + 1; End add = ~add; End percent% each right diagonal state, see how many can attack each other, every 22 attacks counted once i=1; J=n; add = 0; While I<n+1 &&J<n+1 && i>0 && j>0% calculates the right diagonal diagonal how many queens count = Fun_calc_right (i,j,n,state_full); if Count > 1; H = h + nchoosek (count,2); End If Add = = 0; j = j-1; ElseIf add = = 1; i = i + 1; End add = ~add; End End
function count = Fun_calc_left (i,j,n,state_full) percent% statistic i,j point, lower left corner count = 0; i_l = i; I_r = i; j_l = j; J_r = j; While i_l>0 && j_l>0 && i_l<n+1 && j_l<n+1 count = Count + state_full (i_l,j_l);
i_l = i_l + 1; j_l = j_l-1; end%% % of upper right corner while i_r>0 && j_r>0 && i_r<n+1 && j_r<n+1 count = Count + state_full (i_r,j_r); I_r = i_r-1; J_r = J_r + 1; End percent% is repeated plus, minus count = Count-state_full (i,j); end
function count = Fun_calc_right (i,j,n,state_full) percent% statistic i,j point, upper left corner count = 0; i_l = i; I_r = i; j_l = j; J_r = j; While i_l>0 && j_l>0 && i_l<n+1 && j_l<n+1 count = Count + state_full (i_l,j_l);
i_l = i_l-1; j_l = j_l-1; end%% % lower right corner number while i_r>0 && j_r>0 && i_r<n+1 && j_r<n+1 count = Count + state_full (i_r,j_r); I_r = I_r + 1; J_r = J_r + 1; End percent% is repeated plus, minus count = Count-state_full (i,j); end
Solving the problem of eight queens with genetic algorithm