% This program solvs the 8-queen problem using genetic algorithms % The population is stored in a 3D 8x8 matrix. clear clc D=8; % Dimension of the table is DxD N=8; % Number of individuals in the population A = zeros(D,D,N); Population = PlaceQueens(A,N); % one queen in each column of each population member NumberOfAttacks = FindAttacks(Population,N); StepCounter=0; while min(NumberOfAttacks)~=0 for x=1:N % prikaz inicijalne populacije subplot(3,3,x), imshow((1-Population(:,:,x))) drawnow end % NumberOfAttacks=FindAttacks(Population,N); % The quality of each individual is measured by % the pairs of non-attacking queens. % The solution is found when the number % of pairs ofnon-attacking queens equals 28. % The non-attackingg pairs of queens are % stored in variable NoAttack. % The solution is found when NoAttack reaches 28. NoAttack=28-NumberOfAttacks; SelectedInds=selection(Population,NoAttack,N); Rec=recombination(SelectedInds,N); Mut=mutation(Rec); Population=Mut; NumberOfAttacks = FindAttacks(Population,N); StepCounter=StepCounter+1; end