Click here to Skip to main content
15,880,364 members
Articles / General Programming / Algorithms
Tip/Trick

N-Queen Problem using Genetic Algorithms

Rate me:
Please Sign up or sign in to vote.
4.60/5 (4 votes)
7 Jun 2013CPOL1 min read 32.1K   1.5K   4   7
In this article, we describe a solution for solving the 8-queen problem.

Introduction

In this article, we describe a solution for solving the 8-queen problem. My solution is based on Genetic algorithms that is not a good method for solving this type of a problem.

In my code, first initiate the primary population (as chromosomes). For example: a={3 1 4 2} means queen is in row 1 and column 3, next queen is in row 2 and column 1, and etc.

Then select the best chromosomes from the population. In the next step generate some children from the population (crossover) and some of this children mutate. And between children and parents select the best chromosome.

This cycle repeats for n times (for example, n= 100).

Using the code

  1. Before you run the program you should understand the variants that they use for population size, table size, and fitness (for genetic algorithms).
    C++
    fixedsize=100;
    tablesize=8;
    fitness=0; 
  2. First, generate the primary population that shows as a decimal array: for example, (4-queens) : a={3 1 4 2} means queen is in row 1 and column 3, next queen is in row 2 and column 1, and etc.  
    C++
    for i=1:fixedsize
        cromo(i,1:tablesize)=randperm(tablesize);
        cromo(i,tablesize+1)=-1;
    end;
    for i=1:fixedsize
        for j=1:tablesize
            for k=j+1:tablesize
                if (cromo(i,j)==cromo(i,k)) || (abs(cromo(i,j)-abs(cromo(i,k))) == abs(j-k))
                    fitness=fitness+1;
                end;
            end;
        end;
        cromo(i,tablesize+1)=fitness;
        fitness=0;
    end;
  3. Select the best parents to generate the children: 
    C++
    cromo=sortrows(cromo,9);
  4. Generate the children and mutate them. Repeat this step for n times (example, n=1500). 
    C++
     for cnt=1:1500
        if(cromo(1:fixedsize,9)==0)
            level=cnt;
            checkboard(cromo(1,1:tablesize),tablesize,tablesize);
            break;
        end;
        %pm=(1/240)+(0.11375/2^cnt);
        pc=1/cnt ;
        slct=cromo(1:fixedsize,:);
        n=0;
        for i=1:2:fixedsize/2
            u=rand(1);
            if(u<=pc)
                n=n+2;
                child(n-1:n,1:tablesize)=crossover(slct(i:i+9,:),tablesize);
            end;
        end;
        %n=n+1;
        %if(n>0)
        child(:,tablesize+1)=-1;
        
        cromo((fixedsize+1)-length(child):end,:)=[];
        cromo=[cromo;child];
        for i=(fixedsize+1)-length(child):fixedsize
            cromo(i,1:tablesize)=mutation(cromo(i,1:tablesize),tablesize);
        end;
        %end;
        fitness=0;
        for i=1:fixedsize
            for j=1:tablesize
                for k=j+1:tablesize
                    if (cromo(i,j)==cromo(i,k)) || 
                               (abs(cromo(i,j)-abs(cromo(i,k))) == abs(j-k))
                        fitness=fitness+1;
                    end;
                end;
            end;
            cromo(i,tablesize+1)=fitness;
            fitness=0;
        end;
        cromo=sortrows(cromo,9);
    end;

History

  • Version 1.0.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer ARS NET
Iran (Islamic Republic of) Iran (Islamic Republic of)
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
hadi ahmadipour22-Nov-21 9:06
hadi ahmadipour22-Nov-21 9:06 
Questionتوضیح تابع نامبرده Pin
Member 126688659-Aug-16 12:30
Member 126688659-Aug-16 12:30 
QuestionN-Queen Problem using Genetic Algorithms Pin
Member 1126293426-Jun-15 12:02
Member 1126293426-Jun-15 12:02 
AnswerRe: N-Queen Problem using Genetic Algorithms Pin
Hamed Naeemaei20-Jul-15 7:23
Hamed Naeemaei20-Jul-15 7:23 
Questionerros in the code Pin
Member 1131594616-Dec-14 12:02
Member 1131594616-Dec-14 12:02 
AnswerRe: erros in the code Pin
Hamed Naeemaei17-Dec-14 2:46
Hamed Naeemaei17-Dec-14 2:46 
AnswerRe: erros in the code Pin
Hamed Naeemaei14-Mar-15 20:33
Hamed Naeemaei14-Mar-15 20:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.