1. A Hybrid Approach Using Particle Swarm
Optimization and Simulated Annealing for N-queen
Problem
Vahid Mohammadi Saffarzadeh, Pourya Jafarzadeh, Masoud Mazloom
Abstract—This paper presents a hybrid approach for solving n-
queen problem by combination of PSO and SA. PSO is a population
based heuristic method that sometimes traps in local maximum. To
solve this problem we can use SA. Although SA suffer from many
iterations and long time convergence for solving some problems, By
good adjusting initial parameters such as temperature and the length
of temperature stages SA guarantees convergence. In this article we
use discrete PSO (due to nature of n-queen problem) to achieve a
good local maximum. Then we use SA to escape from local
maximum. The experimental results show that our hybrid method in
comparison of SA method converges to result faster, especially for
high dimensions n-queen problems.
Keywords—PSO, SA, N-queen, CSP
I. INTRODUCTION
CSP is defined by a set of variables and a set of
constraints. Variable has a nonempty domain of possible
values. Each constraint involves some subset of the
variables and specifies the allowable combination of values for
the subset. A state of the problem is defined by an assignment
of values to some or all variables. A complete assignment is
one in which every variable is mentioned, and solution to CSP
is a complete assignment that satisfies all the constraints. Some
CSPs also require a solution that maximize an objective
function [1].
The n-queens problem is a CSP that consists of placing n
queens on an N by N chess board, so that they do not attack
each other, i.e. on every row, column or diagonal, there is only
one queen, Its complexity is O(n!) [2][3]. There are many
heuristics for solving n-queen problem, some of these
heuristics cooperate better with some search methods than the
others. The complexity of heuristic used in this paper is O(n).
There are several search strategies for n-queen problem such as
Depth First Search, Beam Search, Branch and Bound, local
search methods and Evolutionary algorithms (EA).
The sections of the papers are as follows: Section II reviews
the basic and discrete forms of PSO. Section III reviews the SA
algorithms. In section IV we described our hybrid algorithm,
and section V summarizes experimental results. Finally,
conclusions and future works are presented in VI.
II. PARTICLE SWARM OPTIMIZTION
A. Basic PSO
The Particle Swarm Optimization (PSO) [Kennedy and
Eberhart, 1995, Eberhart et al.,2001] evolved from an analogy
drawn with the collective behavior of the animal displacements
[4].
The system is initialized with a population of random
solutions and searches for optima by updating potential
solution over generation. In PSO, the potential solutions, called
particles, ”fly” through the problem space by following the
current better performing particle [2]. The solution updating
can be represented by the concept of velocity [5]. By
definition, a velocity is a vector or, more precisely, an operator,
which, applied to a position (solution), will give another
position (solution). It is in fact a displacement, called velocity
because the time increment of the iteration is always implicitly
regarded as equal to 1 [6]. Velocity of each particle can be
modified by the following equation:
(1)
Where is velocity of particle at iteration , is weighting
function, is weighting coefficients, rand is random number
between 0 and 1, is the current position of particle at
iteration , is the best state of particle and is
the best state among the neighbors of particle (until iteration
) [5]. A general flowchart of basic PSO is shown in figure 1.
B. Disceret PSO
The basic PSO treats nonlinear optimization problem with
continuous variables. However, practical engineering problems
are often formulated as combinatorial optimization problems.
Kennedy and Eberhart developed a discrete binary version of
PSO for these problems. They proposed a model wherein the
probability of an agent’s (particle) deciding yes or no, true or
false and 0 or 1 by the following factors:
Fig. 1. A general flowchart of PSO 5 . Agent is the same particle.
A
World Academy of Science, Engineering and Technology
Vol:4 2010-07-24
754
InternationalScienceIndexVol:4,No:7,2010waset.org/Publication/6488
World Academy of Science, Engineering and Technology
International Journal of Mathematical, Computational, Physical, Electrical and Computer Engineering Vol:4, No:7, 2010
947International Scholarly and Scientific Research & Innovation 4(7) 2010 scholar.waset.org/1999.7/6488
InternationalScienceIndex,MathematicalandComputationalSciencesVol:4,No:7,2010waset.org/Publication/6488
2. 1 , , , (2)
The parameter , a particle’s tendency to make one or the
other choice, will determine a probability threshold. If is
higher, the particle is more likely to choose 1, and lower values
favor 0 choice. Such a threshold requires staying in the range
[0, 1]. The proper function for this feature is the sigmoid
function:
(3)
Like the basic continues version, the formula for the binary
(discrete) version of PSO can be describe as follows [5]:
(4)
1;
0; (5)
Where rand and are random numbers in 0,1 . The entire
algorithm of the binary version of PSO is almost the same as
that of the basic continous version (figure 1) except for the
above equations[5].
III. SIMULATED ANNEALING (SA)
In statistical mechanics, a physical process called annealing
is often perform in order to relax the system to a state with
minimum free energy [8]. The idea to use annealing technique
in order to deal with optimization problems gave rise to the
simulated annealing technique. It consists in introducing a
control parameter in optimization, which plays the rule of
temperature. The “temperature” of the system to be optimized
most have the same effect as the temperature of the physical
system: it must condition the number of accessible states and
lead to the optimal state, if the temperature is lowered
gradually in a slow and well controlled manner (as in the
annealing technique) and towards a local minimum if the
temperature is lowered abruptly. The flowchart of the
algorithm is shown in figure 2 [9].
If ∆ 0 (i.e. new state is better than the current state) the
modification will be accepted, when ∆ 0 if the probability
exp ∆ ⁄ is greater than a random number drawn
uniformly between 0 and 1, the modification, making the state
worse, also will be accepted (Metropolis rule [9]).
By repeatedly observing the rule of acceptance, described
above, a sequence of configurations (states) is generated,
which constitutes a Markov chain (in a sense that each
configuration depends on only that one which immediately
precedes it) [9].
IV. THE HYBRID ALGORITHM
According to the diagram of our method (figure 3), PSO
and SA algorithms were implemented consecutively. The
evaluation functions of two algorithms are the same. Diagram
shows that PSO works with four particles, then SA takes the
state of best particle from PSO and after generating the initial
temperature, performs sufficient iterations (Markov chains)
toward a global maximum. In the following paragraphs the
important parameters and steps of algorithm are represented.
A. Representaiton of problem states
NO
YES
STOP
YES
frozen system?
NO thermodynamic
equilibrium?
ANNEALING
PROGRAM
Slow decrease of T
Metropolis ACCEPTATION RULE
-if ∆E 0 : modification accepted
-if ∆E 0 : modification accepted with
probability exp(-∆E/T)
INITIAL CONFIGURATION
INITIAL TEMPERATURE T
elementary MODIFICATION
energy variation ∆E
World Academy of Science, Engineering and Technology
Vol:4 2010-07-24
755
InternationalScienceIndexVol:4,No:7,2010waset.org/Publication/6488
Fig. 2. Flowchart of SA [10].
World Academy of Science, Engineering and Technology
International Journal of Mathematical, Computational, Physical, Electrical and Computer Engineering Vol:4, No:7, 2010
948International Scholarly and Scientific Research & Innovation 4(7) 2010 scholar.waset.org/1999.7/6488
InternationalScienceIndex,MathematicalandComputationalSciencesVol:4,No:7,2010waset.org/Publication/6488
3. For
were
repre
acco
algo
one
boar
num
boar
repre
upda
trans
proc
B. N
T
1
2
C. M
A
temp
equi
state
temp
of t
dime
stage
the s
adaptation be
e represented
esentation is u
ording to form
rithm these st
dimensional,
. In matrix
rd) each colum
mber that is th
rd. For examp
esented like 2
ating time, a
sformed sche
cedure.
a
Fig.
Number of par
The number of
1. We don’t
want to ac
conflicts)
2. If the num
be require
Markov chains
According to f
perature stage
librium (i.e. n
e) in that tem
perature decre
temperature s
ension of the
e (Markov len
stage index (fi
Fig. 3
etween the pro
d by binary
used only wh
mulas (4) and
tates represen
due to a fun
(that is a
mn shows th
e position of
le the 4 4 ch
2 4 matrix
and the state
ema of figu
rticles
f particles in P
want to solv
chieve a good
for the next st
mber of partic
ed to achieve t
s
figure 2 the m
e should be su
no move can
mperature. Aft
eases by a coe
stages (Mark
problem, and
ngth) is multip
igure 4).
3. The operation
0 0
1 0
oblem and di
y matrices.
hen we want t
(5), whereas
ntation will be
nction that ha
matr
e binary repr
queen on tha
hess board in f
shown in fig
e shown in
ure (4.a), us
ition representa
PSO is low bec
ve the problem
local maximu
tep of algorith
cles is high, m
the local maxi
modifications o
ufficient to re
decrease the
ter the therma
efficient (e.g. 0
kov chains)
d the number
ple of problem
n schema of the
b
1 1
1 0
iscrete PSO, s
Notice that
to update the
for other par
e transformed
as a complexit
rix for c
resentation of
at column in c
figure (4.a) wi
gure (4.b), use
figure (4.c),
ed in evalu
ation.
cause of:
m by PSO, we
um (low numb
hm (SA).
more iterations
mum.
of the states in
eceive the the
evaluation o
al equilibrium
0.99). The num
is a function
of moves in
m’s dimension
e algorithm.
c
1 0 3
states
this
state
rts of
d to a
ty of
chess
f row
chess
ill be
ed in
, the
uation
e just
ber of
s will
each
ermal
of the
m the
mber
n of
each
n and
N
T
begi
the b
T
dive
D. D
the
part
E. S
t
2
N = dimension
for n=0…N×N
for m=0…(n
Modify s
…..
}
T = 0.99 × T
The initial te
inning of SA
best state, the
T=(0
The coefficien
ergence of SA
Determining
The algorithm
evaluation of
icle.
Steps of algor
The algorith
total steps are
1. Generate
velocitie
2. Perform
particle;
3. If the lo
otherwis
4. Select th
5. Start SA
6. Generate
7. Make mo
to the ac
8. If the ev
has ach
otherwis
temperat
go to ste
9. Decrease
10. If Mark
go to 7;
11. End of p
n ( number of
N/2{
n+1)×N {//Ma
state to neighb
T;
udo code of usin
emperature, a
step, is gener
formula is as
0.001)×(bestEv
nts are appro
, i.e. the state
the local max
m traps in loc
f ,
rithm
hm consists o
as follows:
e four particle
es;
the updating
cal maximum
se go to step 4
he particle with
A with the sele
e the initial tem
odification on
cceptation rule
valuation is ze
hieved to the
se if the M
ture stage is n
ep 9;
e the temperat
kov chains loo
procedure.
f Markov chain
arkov chain len
bor configurat
}
ng Markov chai
after the PSO
rated by using
follow [11]:
valuation)/(-lo
opriate to avo
won’t go to ve
ximum of PSO
cal maximum
and state a
of two parts (
es with random
g procedures
m is not happe
4;
h best state;
ction result of
mperature (
n the state, acc
e (Metropolis r
ero it means
e final state,
Markov length
not finished g
ture;
op is finished,
n);
ngth
tion;
in in SA step
O step and in
g the evaluatio
og(0.15))
id from the i
ery bad situati
O
in PSO step w
are equal for
(PSO & SA)
m initial state
(figure 6) of
ened, go to st
f the previous
with (6);
cept that acco
rule);
that the algor
then go to
h for the cu
go to step 7 if
go to step 11
n the
on of
(6)
initial
ions.
when
each
, and
s and
each
tep 2;
step;
rding
rithm
o 11,
urrent
f not,
, else
World Academy of Science, Engineering and Technology
Vol:4 2010-07-24
756
InternationalScienceIndexVol:4,No:7,2010waset.org/Publication/6488
4 Queens pos
Fig. 5. Pseu
World Academy of Science, Engineering and Technology
International Journal of Mathematical, Computational, Physical, Electrical and Computer Engineering Vol:4, No:7, 2010
949International Scholarly and Scientific Research & Innovation 4(7) 2010 scholar.waset.org/1999.7/6488
InternationalScienceIndex,MathematicalandComputationalSciencesVol:4,No:7,2010waset.org/Publication/6488
4. Fig. 6. Pseudo code of the updating procedure.
V. EXPERIMENTAL RESULTS
The algorithm is implemented in Java language and tested
by chess boards with different dimensions. The machine used
was Intel Core 2 Duo CPU 2.40 GHz, 2GB of memory,
running Windows 7 Ultimate and jdk1.7.0. The results are
shown in tables 1 and 2 and figures 7 and 8.
Table 1 shows the number of iterations required to achieve a
good local maximum in PSO step.
TABLE I THE NUMBER OF ITERATIONS THAT PSO PERFORMS TO TRAP IN LOCAL
MAXIMUM (THE RESULTS ARE THE AVERAGE OF 10 RUNNING OF ALGORITHM)
Dimension 20 200 500 700 1000
PSO
iteration
353 2489 5784 8078 11189
Following table is a comparison of the hybrid approach and
SA method, for solving n-queen problem for 5 different
dimensions (number of queens), according to their running
times for finding solution. Afterward you can see diagrams of
this comparison that are obtained from running algorithms
with more different dimensions.
TABLE II COMPARISON OF SA AND HYBRID ALGORITHM FOR 5 DIFFERENT
DIMENSION OF CHESS BOARDS, ACCORDING THE DURATION OF RUNTIMES IN
MILISECOND
Dim
Algorithm
20 200 500 700 1000
Hybrid
PSO&SA
57.6 5545.7 53601.6 131050.6 345855.3
SA 3.1 1911 63451.5 203503.9 776577.8
Fig. 7. Comparison of hybrid algorithm and SA run times for solving
different dimension n-queen problems.
Fig. 8. Column chart of the diagram in figure 7.
Another result is that the majority of time consumes when
the state receives to low conflicts (in range 10 to 1) situations
(that is because of the nature of SA algorithm).
VI. CONCOLUSION
N-queen is a good NP-complete problem to test the new
heuristic algorithms and compare them with old ones. The
results of this paper show that the combination of PSO and SA
has a capacity to improve the performance of each one in
solving this problem, separately. The results also show that n-
queen problem can be solved in a reasonable time by this
hybridization and this idea is better than SA (the same SA
used in the hybrid approach) by an increasingly ratio for
higher dimensions, that means when dimension becomes
larger the hybrid algorithm receives to global maximum faster.
The hybrid algorithm is capable to be implemented in
parallel approach, with parallel PSO it is possible to increase
the number of particles and improve reliability of the
algorithm. In another parallelism approach if we can give
0.000576
0.002933
0.012621
0.055457
0.163535
0.308365
0.536016
0.942252
1.310506
1.882145
3.458553
0.000031
0.000468
0.003916
0.01911
0.1074
0.32365
0.634515
1.484701
2.035039
4.261137
7.765778
0
1
2
3
4
5
6
7
8
9
20 50 100 200 300 400 500 600 700 800 1000
Time(millisecond)x100000
Dimension (number of queens)
Hybrid SA
1
2 ;
. ? 1 0;
;
0 …
0 … log
;
;
World Academy of Science, Engineering and Technology
Vol:4 2010-07-24
757
InternationalScienceIndexVol:4,No:7,2010waset.org/Publication/6488
World Academy of Science, Engineering and Technology
International Journal of Mathematical, Computational, Physical, Electrical and Computer Engineering Vol:4, No:7, 2010
950International Scholarly and Scientific Research & Innovation 4(7) 2010 scholar.waset.org/1999.7/6488
InternationalScienceIndex,MathematicalandComputationalSciencesVol:4,No:7,2010waset.org/Publication/6488
5. more states of PSO (best states) to SA in parallel it is possible
to achieve global maximum with a better speed. We think that
this algorithm also can be useful in solving other CSPs (e.g.
Graph Coloring, Time Scheduling, etc).
Described algorithm has many parameters (e.g. number of
particles, number of PSO iterations, initial temperature, length
of temperature stages, decreasing rate of temperature, etc) to
be adjusted. Here, these parameters are determined by
statistical tests, this work (adjusting parameters) can be done
more precisely by neural networks.
REFERENCES
[1] Stuart Russell, Peter Norvig, “Artificial Inteligence: A Modern
Approach,” Constraint Satisfaction Problems, 2nd ed., Pearson
Education, Inc, Upper Saddle River, New Jersey, 2003,1995, page: 137.
[2] Xiaohui Hu, Russell C. Eberhart, Yuhui Shi, “Swarm Inteligence for
Permutation Optimization: A case Study of n-Queen Problem".
[3] Marko Božikovic, Marin Golub, Leo Budin, “Solving n-Queen problem
using global parallel genetic algorithm”.
[4] J. Dr´eo, A. P´etrowski, P.Siarry, E.Taillard, “Metaheuristics for Hard
Optimization,” Some Other Metaheuristics, Springer-Verlag Berlin
Heidelberg 2006, pp. 162-166.
[5] Kwang Y.Lee, Mohamed Al-Sharkawi, “Modern Heuristic Optimization
Techniques: Theory And Application To Power Systems,”
Fundamentals of Particle Swarm Optimization Techniques, Willey-
Interscience, Hoboken, 2008, pp. 72-79.
[6] Maurice Clerc, “Particle Swarm Optimization,” First Formulations,
ISTE, United States, 2006, page: 39.
[7] M. Young, The Technical Writer's Handbook. Mill Valley, CA:
University Science, 1989.
[8] Kwang Y.Lee, Mohamed Al-Sharkawi, “Modern Heuristic Optimization
Techniques: Theory And Application To Power Systems,” Preface,
Willey-Interscience, Hoboken, 2008, page: xxiv.
[9] J. Dr´eo, A. P´etrowski, P.Siarry, E.Taillard, “Metaheuristics for Hard
Optimization,” Simulated Annealing, Springer-Verlag Berlin Heidelberg
2006, pp. 25-31.
[10] J. Dr´eo, A. P´etrowski, P.Siarry, E.Taillard, “Metaheuristics for Hard
Optimization,” Introduction, Springer-Verlag Berlin Heidelberg 2006,
page: 8.
[11] Kwang Y.Lee, Mohamed Al-Sharkawi, “Modern Heuristic Optimization
Techniques: Theory And Application To Power Systems,”
Fundamentals of Simulated Annealing, Willey-Interscience, Hoboken,
2008, page(s): 128 and 129.
World Academy of Science, Engineering and Technology
Vol:4 2010-07-24
758
InternationalScienceIndexVol:4,No:7,2010waset.org/Publication/6488
World Academy of Science, Engineering and Technology
International Journal of Mathematical, Computational, Physical, Electrical and Computer Engineering Vol:4, No:7, 2010
951International Scholarly and Scientific Research & Innovation 4(7) 2010 scholar.waset.org/1999.7/6488
InternationalScienceIndex,MathematicalandComputationalSciencesVol:4,No:7,2010waset.org/Publication/6488