21.07.2013 Views

Eight Queens with Evolutionary Computing

Eight Queens with Evolutionary Computing

Eight Queens with Evolutionary Computing

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Eight</strong> <strong>Queens</strong> <strong>with</strong> <strong>Evolutionary</strong> <strong>Computing</strong><br />

Ergun Çoruh, B.S. in E.E., Software Developer<br />

November 2010<br />

Abstract<br />

The objective of this paper is to study and verify fundamental evolutionary<br />

concepts by means of a simple computational example.<br />

1 Introduction<br />

<strong>Evolutionary</strong> computing mimics biological evolution to solve problems on a computer.<br />

The author was inspired by The <strong>Eight</strong> <strong>Queens</strong> Problem given in the book<br />

Introduction to <strong>Evolutionary</strong> <strong>Computing</strong> [1].<br />

In this paper the author shall develop a computer program named evocom<br />

to solve The <strong>Eight</strong> <strong>Queens</strong> Problem and then he shall quantitively analyse and<br />

demonstrate fundamental aspects of biological evolution.<br />

2 What is Evolution<br />

Evolution (also known as biological, genetic or organic evolution) is the change in<br />

the inherited traits of a population of organisms through successive generations<br />

[2].<br />

Figure 1 shows a basic flow diagram of evolution. Population is a pool of<br />

individuals who own certain set of traits (phenotypes) which enable them to<br />

adapt to certain environmental conditions. These traits are made from the<br />

individual’s genetic material (genotypes, or chromosomes). Through a parent<br />

selection mechanism fit parents are selected to reproduce.<br />

The genetic material of offsprings are constructed when both parents’ chromosomes<br />

are paired and recombined (shuffled like a deck of cards). During<br />

recombination copy errors may occur too <strong>with</strong> certain probability. Such errors<br />

cause mutation of recombined genes.<br />

Recombination and mutation make new individuals <strong>with</strong> unique genetic material.<br />

As a result these offsprings are either better or worse fitted for survival in<br />

the environment they were born into. Through a process called survivor selection<br />

(aka natural selection) fit offsprings who adapt better to the environment<br />

would survive and be added into the population. Unfit ones shall not survive.<br />

This cycle repeats <strong>with</strong> more individuals who are better at being adapted to the<br />

environment joining the population in an increasing rate.<br />

1


Therefore in certain sense each species is a solution to fix a certain group<br />

of environmental challenges.<br />

Initialise<br />

Termination<br />

parent selection<br />

Population<br />

survivor selection<br />

Figure 1: Evolution flow diagram<br />

3 <strong>Evolutionary</strong> Algorithms<br />

Parents<br />

Offspring<br />

recombination<br />

mutation<br />

<strong>Evolutionary</strong> algorithms follow the model given in Figure 1. The aspects of<br />

evolutionary algorithms are:<br />

• Representation (definition of individuals)<br />

• Fitness function (evaluation function)<br />

• Population (unit of evolution)<br />

• Parent selection (pushes quality)<br />

• Variation operators (recombination and mutation)<br />

• Survival selection (replacement)<br />

2


Parent selection and survivor selection are selection operators which operate<br />

at population level whereas recombination and mutation are variation operators,<br />

and they operate on individuals.<br />

3.1 Representation<br />

Representation defines the mapping between the genotype space and the phenotype<br />

space (see Figure 2). The entire genotype space of a species is called a<br />

genome.<br />

3.2 Fitness function<br />

<br />

<br />

<br />

<br />

<br />

Figure 2: Evolution flow diagram<br />

Fitness function is a function or procedure that assigns a quality measure to<br />

genotypes. Essentially fitness function defines what improvement means. Fitness<br />

function needs to be applied upon the population during parent selection<br />

and survivor selection in order to determine the top (best) and the bottom<br />

(worst) end of a population [1].<br />

3


3.3 Population<br />

A population is a multi-set of genotypes (i.e. we should assume that there<br />

may be individuals who are identical or very close in their fitness score). The<br />

population forms the unit of evolution (i.e. individuals do not evolve, but the<br />

population does) [1].<br />

3.4 Parent selection<br />

The role of parent selection is to select individuals based on their fitness score.<br />

In nature too parents mate based on their fitness score. In biology this is often<br />

broadly called sexual selection. Together <strong>with</strong> survival selection parent selection<br />

is one of two forces that pushes quality.<br />

3.5 Variation operators<br />

There are two fundamental forces that form the basis of evolutionary systems:<br />

• Variation operators<br />

• Selection<br />

Variation operators (recombination and mutation) create the necessary diversity<br />

and thereby facilitate novelty to deal <strong>with</strong> environmental challenges.<br />

Selection on the other hand acts as a force to push quality [1].<br />

3.6 Purpose<br />

In evolutionary computing algorithms perform stochastically, i.e. for a given set<br />

of inputs number of iterations reaching a solution varies in each run.<br />

There are also other limitations such as it is possible that the program may<br />

climb a local hill rather than a global one, i.e. the program may not be able to<br />

find the best set of solutions in a given run.<br />

Nevertheless these known limitations are irrelevant for our purpose. The objective<br />

of this study is not to maximise performance of evolutionary algorithms<br />

(which may not necessarily perform better against deterministic algorithms) but<br />

to demonstrate certain aspects of evolution, these are:<br />

• <strong>Evolutionary</strong> process makes a given population increasingly better at being<br />

adapted to the environment.<br />

• Effects of mutation on diversity.<br />

• Genetic drift in small populations.<br />

4


3.7 The Algorithm<br />

The pseudo code of the evolutionary algorithm is given as follows:<br />

BEGIN<br />

INITIALISE population <strong>with</strong> random candidate solutions<br />

EVALUATE each candidate<br />

REPEAT UNTIL ( TERMINATION CONDITION ) DO<br />

1 SELECT parents<br />

2 RECOMBINE pairs of parents<br />

3 MUTATE the resulting offspring<br />

4 EVALUATE new candidates<br />

5 SELECT individuals for the next generation<br />

OD<br />

END<br />

4 The <strong>Eight</strong> <strong>Queens</strong> Problem<br />

Problem Definition: Consider a regular 8 X 8 chess board. Place eight<br />

queens such that none of them is able to check (capture) each other using<br />

standard chess queen’s moves. Thus a solution requires that no two queens<br />

share the same row, column, or diagonal.<br />

In chess a queen can check every other piece diagonally, vertically<br />

and horizontally.<br />

4.1 Solutions<br />

There are 4426165368 combinations eight queens can be placed on a chess board<br />

(choose 8 from 64), and 92 of them are solutions [3].<br />

C = n!/k!(n − k)! = 64!/8!(64 − 8)! = 4426165368<br />

Let S be the solution set and C be all possible combinations.<br />

Then:<br />

S ⊂ C<br />

Vast majority of combinations can be eliminated upfront as no two queens<br />

can be placed vertically or horizontally.<br />

Hence we can represent each solution candidate as a vector V :<br />

V = 〈c1, c2, . . . , c8〉<br />

Note that each position in vector V corresponds to a row number i, and each<br />

value ci represents a column number at which a queen is placed. Hence if c2 is<br />

equal to 5 then it follows that a queen is placed at 2 nd row 5 th column.<br />

5


Example: Some valid solutions are given as follows:<br />

V = 〈8, 3, 1, 6, 2, 5, 7, 4〉<br />

V = 〈4, 2, 5, 8, 6, 1, 3, 7〉<br />

It should also be noted that in this scheme number of solution candidates is<br />

reduced to permutations P of 8:<br />

where<br />

and<br />

4.2 Fitness function<br />

#{P} = 8! = 40320<br />

S ⊂ P ⊂ C<br />

#{S} = 92<br />

In Section 4.1 we deduced a formula to represent each distinct solution as a<br />

vector:<br />

V = 〈c1, c2, . . . , c8〉<br />

Each vector can be thought of a complete genotype (genome) to solve the<br />

<strong>Eight</strong> <strong>Queens</strong> Problem. Hence a genome in this sense is equivalent to a solution.<br />

Each element of the vector can be thought as a gene expressing a queen being<br />

present at a given square. The overall genetic combination shall determine the<br />

final fitness score such that no two queens can check each other.<br />

Let number of checks in a given vector be:<br />

0


where s is the interpolated fitness score (the larger the better). Since minimum<br />

number of checks is when k is 0, the fittest individual shall have the score<br />

of 7.<br />

For example the following vector is perfectly fit (has score of 7) as no two<br />

queens can check each other <strong>with</strong> this solution:<br />

V = 〈8, 3, 1, 6, 2, 5, 7, 4〉<br />

We should now develop a function to evaluate the fitness for a given vector.<br />

The function must calculate the number of checks i.e. the value of k.<br />

Let ci is the column index at i th row position where a queen is placed. Then<br />

the check condition for ci must be when the row distance from current position<br />

is equal to the column distance, i.e. when two queens are situated in the corners<br />

of an equilateral right triangle.<br />

Let d be the row distance between cj and ci. It follows that:<br />

Then:<br />

d = j − i : j > i<br />

increment k ⇐⇒ d = cj ± ci<br />

The following code listing shows the fitness function implemented in Python:<br />

def fitness(c):<br />

score = 0<br />

score_min = 7<br />

for i in range(0,7):<br />

for j in range(i+1,8):<br />

d = j-i<br />

if c[j] > c[i]:<br />

if c[i]+d == c[j]:<br />

score += 1<br />

break<br />

else:<br />

if c[i]-d == c[j]:<br />

score += 1<br />

break<br />

return score_min-score<br />

7


<strong>Eight</strong> <strong>Queens</strong> Algorithm Description<br />

Representation Permutations<br />

Recombination ”Cut-and-crossfill” crossover<br />

Recombination probability 100 pc<br />

Mutation Swap<br />

Mutation probability 80 pc<br />

Parent selection Best 2 out of random 5<br />

Survival selection Replace worst<br />

Population size 100<br />

Number of offspring 2<br />

Initialisation Random<br />

Termination condition Solution of 10,000 fitness evaluation<br />

4.3 Evocom<br />

The author has written a computer program called evocom and implemented<br />

the evolutionary algorithm given in Section 3.7. Evocom is a Python command<br />

line application that has various options to adjust configuration parameters.<br />

coruh-imac:evocom ergun$ python evocom.py -h<br />

evocom started.<br />

Usage: evocom.py [options]<br />

Options:<br />

-h, --help show this help message and exit<br />

-p POPULATION, --population=POPULATION<br />

-t TRIALS, --trials=TRIALS<br />

-r RUNS, --runs=RUNS<br />

-b BESTFITS, --bestfits=BESTFITS<br />

-m MUTATION, --mutation=MUTATION<br />

-s SELECTFROM, --selectfrom=SELECTFROM<br />

-v, --verbose<br />

Population: Initial size of the population. Default is 100.<br />

Trials: Maximum iterations to terminate the program in case search fails.<br />

Default is 10000.<br />

Runs: Number of runs. This parameter is needed to find the average performance<br />

as each run finishes in different number of iterations. Default is 10.<br />

Best fits: Number of fittest individuals to find before breaking the iteration.<br />

Default is 5.<br />

Select from: Number of parents randomly chosen from the population to pick<br />

the fittest two to mate. Default is 5.<br />

8


5 Use Cases<br />

5.1 Adaptation Rate<br />

Evolution makes a given population of species increasingly better at being<br />

adapted to their environment unless abrupt catastrophic conditions emerge.<br />

Adaptation is a gradual process since evolution by natural selection takes place<br />

over many generations (iterations).<br />

Using evocom number of iterations vs. best-fits was plotted. When number<br />

of fittest individuals reached the value of best-fits parameter configured <strong>with</strong> -b<br />

command line option data were printed and the program terminated.<br />

The data in Figure 3 clearly demonstrates how adaptation rate increases over<br />

time. The X axis shows number of generations, the Y axis shows the number<br />

of fittest individuals in a population of 100. It takes fewer generations to adapt<br />

to the environment as number of fit individuals increases.<br />

Adaptation Data Series<br />

Best fits (BF) Generations (G)<br />

20 507<br />

40 661<br />

60 748<br />

80 837<br />

100 909<br />

Wolfram input:<br />

Figure 3: Adaptation rate graph<br />

plot{{0,0},{507,20},{661,40},{748,60},{837,80},{909,100}}<br />

9


Sample evocom output:<br />

coruh-imac:evocom ergun$ python evocom.py -r 100 -b 20<br />

evocom started.<br />

===============================<br />

Average iterations = 507<br />

Total failures = 0<br />

population = 100<br />

trials = 10000<br />

runs = 100<br />

best fits = 20<br />

mutation probability= 80%<br />

select from = 5<br />

===============================<br />

5.2 Effects of Mutations<br />

Mutations are necessary to drive diversity in a given population. Recombinations<br />

(crossing-over) of parent genes would not be enough for better adaptation.<br />

It should be noted that the graph given in Figure 4 should not be taken as<br />

a universal benchmark as mutations are complex phenomena <strong>with</strong> many nonlinear<br />

parameters in effect.<br />

Using evocom we recorded number of iterations vs. mutation probabilities<br />

while searching for 10 bets-fit individuals in a population of 100. The mutation<br />

probability was specified <strong>with</strong> -m command line option (-m 80 means 80 percent<br />

mutation probability).<br />

Figure 4: Mutation probability graph<br />

10


Mutation Probability Data Series<br />

Probability (P) Generations (G)<br />

0 1081<br />

5 444<br />

10 308<br />

20 248<br />

40 236<br />

60 268<br />

80 351<br />

100 599<br />

Wolfram input:<br />

Plot{{0,1081},{5,444},{10,308},{20,248},{40,236},{60,268},{80,351},{100,599}}<br />

In Fifure 4 the X axis shows probability of applying mutation on offspring<br />

genes. The Y axis shows generations required to reach to a constant number of<br />

fit individuals (10) in a given population of 100.<br />

It is interesting to note that very low and very high probability of mutations<br />

have adverse effects on adaptation, the optimum range lies in between.<br />

Note also from the command output (see below) when mutation probability<br />

is zero (i.e. no mutation is applied to crossed-over genes) then 8 out of 100 runs<br />

actually failed, i.e. unable to produce 10 fit individuals in 10000 iterations.<br />

This demonstrates the necessity to have small non-zero probability of mutations<br />

to drive diversity in populations.<br />

Samle evocom output:<br />

coruh-imac:evocom ergun$ python evocom.py -r 100 -b 10 -m 0<br />

evocom started.<br />

===============================<br />

Average iterations = 1081<br />

Total failures = 8<br />

population = 100<br />

trials = 10000<br />

runs = 100<br />

best fits = 10<br />

mutation probability= 0%<br />

select from = 5<br />

===============================<br />

5.2.1 The mutation function<br />

In evocom the mutation function simply swaps two genes at random positions<br />

as shown below:<br />

11


’’’ mutate<br />

Mutate an offspring genome<br />

Simply swap two bits in random location<br />

’’’<br />

def mutate(c):<br />

g = [0,1,2,3,4,5,6,7]<br />

# find two random positions and swap<br />

g = random.sample(g,8)<br />

i = g[0]<br />

j = g[1]<br />

m = copy.copy(c)<br />

m[i],m[j] = m[j],m[i]<br />

return m<br />

In the main algorithm mutation itself is applied <strong>with</strong> random probability (80<br />

percent default):<br />

# mutate <strong>with</strong> probability 80%<br />

mp = random.randint(1,100)<br />

if mp


evocom started.<br />

===============================<br />

Average iterations = 1337<br />

Total failures = 11<br />

population = 10<br />

trials = 10000<br />

runs = 100<br />

best fits = 5<br />

mutation probability= 80%<br />

select from = 5<br />

===============================<br />

Genetic Drift Data Series<br />

Population (P) Generations (G)<br />

10 1337<br />

20 271<br />

40 181<br />

60 211<br />

80 219<br />

100 260<br />

Wolfram input:<br />

Figure 5: Adaptation rate graph<br />

ListPlot{{10,1337},{20,271},{40,181},{60,211},{80,219},{100,260}}<br />

13


References<br />

[1] J.E. Smith, 2008. Introduction to <strong>Evolutionary</strong> <strong>Computing</strong>, Springer.<br />

[2] Wikipedia, The Free Encyclopedia, Evolution,<br />

http://en.wikipedia.org/wiki/Evolution<br />

[3] Wikipedia, The Free Encyclopedia, <strong>Eight</strong> <strong>Queens</strong> Puzzle,<br />

http://en.wikipedia.org/wiki/<strong>Eight</strong>_queens_puzzle<br />

[4] Wikipedia, The Free Encyclopedia, Genetic drift,<br />

http://en.wikipedia.org/wiki/Genetic_drift<br />

14

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!