Chapter 7

Genetic Algorithms to Constraint Satisfaction Problems

1

Outline 

     

What is a Genetic Algorithm? Components of GA How does GA work? Constraint Handling in Gas GA for 8-Queens Problems GA for Exam Timetabling Problem Memetic Algorithms

2

  

Genetic algorithm is a population-based search method. Genetic algorithms are acknowledged as good solvers for tough problems. However, no standard GA takes constraints into account. This chapter describes how genetic algorithms can be used for solving constraint satisfaction problems.

3

repeat SELECT parents. EVALUATE children. SELECT individuals for the next generation until TERMINATION-CONDITION is satisfied end 4 . MUTATE the resulting children. EVALUATE each candidate.What is a Genetic Algorithm?  The general scheme of a GA: begin INITIALIZE population with random candidate solutions. RECOMBINE pairs of parents.

The general scheme of Genetic Algorithm Parent selection Parents Initialization Recombination Population Mutation Termination Children Survivor selection 5 .

GA is stochastic.The general scheme of Genetic Algorithm (cont. GA has a number of features: GA is population-based GA uses recombination to mix information of candidate solutions into a new one. The evaluation function represents a heuristic estimation of solution quality and the search process is driven by the variation and the selection operator. 6 .)    It¶s clear that this scheme falls in the category of generate-and-test algorithms.

COMPONENTS OF GENETIC ALGORITHMS  The most important components in a GA consist of: representation (definition of individuals) evaluation function (or fitness function) population parent selection mechanism variation operators (crossover and mutation) survivor selection mechanism (replacement) 7 .

8 . The representation step specifies the mapping from the phenotypes onto a set of genotypes.Representation      Objects forming possible solutions within original problem context are called phenotypes. A value of a gene is called an allele. Chromosome. phenotype and individual are used to denote points of the space of possible solutions. Elements of a chromosome are called genes. are called genotypes. and individual can be used for points in the genotye space. Candidate solution. This space is called phenotype space. the individuals within the GA. their encoding.

 In general. mutation is supposed to cause a random unbiased change. It is applied to one genotype and delivers a modified mutant. Mutation Operator  A unary variation operator is called mutation.Variation Operators  The role of variation operators is to create new individuals from old ones. the child or offspring of it. 9 . Variation operators form the implementation of the elementary steps with the search space. Mutation has a theoretical role: it can guarantee that the space is connected.

Crossover Operator  A binary variation operator is called recombination or crossover. and the way these parts are combined. we can produce an offspring which combines both of those features. depend on random drawings. The principle behind crossover is simple: by mating two individuals with different but desirable features. Similarly to mutation. This operator merges information from two parent genotypes into one or two offspring genotypes.   10 . crossover is a stochastic operator: the choice of what parts of each parent are combined.

but positive chance. 11 . otherwise the whole search could become too greedy and get stuck in a local optimum. Low quality individuals are often given a small. High quality individuals get a higher chance to become parents than those with low quality. Parent selection is probabilistic.Parent Selection Mechanism   The role of parent selection (mating selection) is to distinguish among individuals based on their quality to allow the better individuals to become parents of the next generation.

survivor selection is often deterministic. This decision is based on their fitness values. 12 . For instance. the population size is constant. or selection only from the offspring (age-biased). ranking the unified multiset of parents and offspring and selecting the top segment (fitness biased). As opposed to parent selection which is stochastic. thus a choice has to be made on which individuals will be allowed in the next generation. In GA.Survivor Selection Mechanism      The role of survivor selection is to distinguish among individuals based on their quality. favoring those with higher quality.

Termination Condition  GA is stochastic and mostly there are no guarantees to reach an optimum. 13 . the fitness improvement remains under a threshold value the population diversity drops under a given threshold. Whether this step is worth the extra computational effort or not is very much depending on the application.Initialization and Termination Condition Initialization  Initialization is kept simple in most GA applications.  Commonly-used conditions for terminations are the following: the maximally allowed CPU times elapses The total number of fitness evaluations reaches a given limit for a given period of time.

In almost all GA applications. Population  The role of the population is to hold possible solutions. the population size is constant. 14 . A population is a multiset of genotypes.Population  Note: Premature convergence is the well-known effect of loosing population diversity too quickly and getting trapped in a local optimum.

Such an approach is designed to give the GA a good start and speed up the evolutionary process.  Example: A GA for exam timetabling problem in which the GA works only with feasible solutions. 15 .   The initial population must also be made up of feasible solutions.  Some GAs use special techniques to produce a higher quality initial population.HOW DO GENETIC ALGORITHMS WORK ? Initialization  In GA. Then GA is run to improve the fitness of the initial population. an initial population that is generated randomly.

Example 3. 8. we can use a non-binary bit string representation to represent the chromosome because it is easy to understand and represent. 4.e4 If we randomly generate six numbers 3. 6. Six positions represent six exams with each position¶s value as the time slot assigned to the exam. 7 as six timeslots for e1-e6. 8. Day AM PM time1 time2 time3 time4 Day1 e1 e3 Day2 e5 e6 e2.1    In a simple exam timetabling problem. We can generate the population randomly to assign each exam a timeslot.  16 . then the chromosome is 3 8 4 8 6 7.

an initial population can be generated randomly as follows: Index 1 2 3 4 5 Chromosome 384867 737613 535558 767722 174522 Fitness 0.062 0.006 0.If the population size is 5.005 0.040 17 .020 0.

Generational Reproduction  The whole of a population is potentially replaced at each generation.Reproduction There are two kinds of reproduction: generational reproduction and steady-state reproduction. and installs the result back into that population.  The procedure is to loop N/2 times. select two chromosomes each time according to the current selection procedure. performs crossover on them to obtain one or two children. finally producing N new chromosomes. Steady-state Reproduction  This method selects two chromosomes. producing two children from those two parents. where N is the population size. 18 . (perhaps applies mutation as well). the least fit is destroyed.

Parent Selection mechanism  The selection is to return a selected parent. we select the second chromosome 7 3 7 6 1 3 as our first parent and 1 7 4 5 2 2 as our second parent. Fitness-based selection  The standard. The chance of each parent being selected is in some way related to its fitness.  The effect of this method depends on the range of fitness values in the current population.1. 19 . original method for parent selection is Roulette Wheel selection: each chromosome has a chance of selection that is directly proportional to its fitness. Example: if fitness range from 5 to 10.  If we apply fitness-based selection on the population given in example 3. then the fittest chromosome is twice as likely to be selected as a parent than the least fit.

Calculate fitness value f(vi) for each chromosome vi 2. Calculate a cumulative probability qi for each vi i qi = §p j !1 j 20 . Find the total fitness of the population popsize F= § f (v ) i i !1 3.Roulette Wheel Selection 1. Calculate the probability of selection pi cho each vi pi = f(vi)/F 4.

The selection process is based on spinning the roulette wheel pop-size times. each time we select a single chromosome for being a parent in the following way: Generate a random number r from [0. If r < q1 the select v1.1].. otherwise select the i-th chromosome vi ( 2 ” i ” pop-size) such that qi-1 < r ” qi 21 .5.

Tournament-based selection  The tournament selection is to choose K parents at random and returns the fittest one of these. 22 .Parent Selection mechanism Rank-based selection  Selection probabilities are based on a chromosome¶s relative rank or position in the population. rather than absolute fitness.

Crossover is a process yielding recombination of bit strings via an exchange of segments between pairs of chromosomes. One-point crossover Two-point crossover Uniform crossover 23 . There are many kinds of crossover.Crossover Operator   The crossover operator is the most important in GA.

the chromosome length) as the crossover position. Example: With the two parents selected above. Then. we randomly generate a number 2 as the crossover position: Parent1: 7 3| 7 6 1 3 Parent2: 1 7| 4 5 2 2 Then we get two children: Child 1 : 7 3| 4 5 2 2 Child 2 : 1 7| 7 6 1 3 24 . keep the bits before the number unchanged and swap the bits after the crossover position between the two parents.One-point Crossover   This crossover is to randomly generate a number ( ” l.

Two-point Crossover    This crossover is similar to that of one-point crossover except that we must select two positions and only the bits between the two positions are swapped. This crossover method can preserve the first and the last parts of a chromosome and just swap the middle part. we randomly generate two numbers 2 and 4 as the crossover positions: Parent1: 7 3 |7 6 |1 3 Parent2: 1 7 |4 5| 2 2 Then we get two children: Child 1 : 7 3| 4 5| 1 3 Child 2 : 1 7| 7 6| 2 2 25 . Example: With the two parents selected above.

Otherwise. we randomly generate a number between 0 and 1. 0. vice versa. 0.4. Parent1: 7 *3 *7 6 *1 3 Parent2: 1 *7 *4 5 *2 2 Then we get two children: Child 1 : 7 7* 4* 6 2* 3 Child 2 : 1 3* 7* 5 1* 2 26 . for example.Uniform Crossover   The uniform crossover : each gene of the first parent has a 0. Example: For each position. and child2 gets the gene from parent2. 0.5. 0. 0.7.1. then child1 gets the gene from parent1.5 probability of swapping with the corresponding gene of the second parent.9.2.6. 0. If the number generated for a given position is less than 0.

5 and apply the inversion operator. then we get the new string: 3 6 8 4 8 7. If we randomly choose two positions 2. it requires additional overhead. Example: Given a chromosome 3 8 4 8 6 7. While this operator was inspired by a biological process.Inversion   It operates on a single chromosome and inverts the order of the elements between two randomly chosen points on the chromosome.  27 .

We repeat the same procedure for the other bits. The mutation operator can overcome this by simply randomly selecting any bit position in a string and changing it. Assume the mutation rate is 0. and the rest of the bits don¶t mutate. we generate randomly a number between 0 and 1. With crossover and even inversion. and get a number (e. if only the first bit mutates.001. For the first bit 7. If the number is less than the mutation rate (0. Example: Assume that we have already used crossover to get a new string: 7 3 4 5 1 3.Mutation    Mutation has the effect of ensuring that all possible chromosomes are reachable.001). Now the first bit mutates to 2. then we¶ll get a new chromosome: 234513  28 . We generate another number between 1 and the maximum value 8. 2). In our example.g. the search is constrained to alleles which exist in the initial population. then the first bit 7 needs to mutate.

Direct constraint handling means that we leave the constraints as they are and µadapt¶ the GA to enforce them.    Indirect constraint handling: we deal with the problem of satisfying constraints by incorporating them in the fitness function f such that f optimal implies that the constraints are satisfied. in one application we can handle some constraints directly and others indirectly. Direct and indirect constraint handling can be applied in combination. 29 . At the high conceptual level we can distinguish two cases: indirect constraint handling direct constraint handling.e.CONSTRAINT HANDLING IN GENETIC ALGORITHMS  There are many ways to handle constraints in a GA. i.. and use the power of GA to find a solution.

we have to create and maintains feasible chromosomes in the population. mutating one or crossing over two feasible chromosomes can result in infeasible offspring.Direct constraint handling    Treating constraints directly implies that violating them is not reflected in the fitness function. 30 . thus there is no bias towards chromosomes satisfying them. Typical approaches to handle constraints directly are: eliminating infeasible candidates repairing infeasible candidates preserving feasibility by special operators decoding. Therefore.e. transforming the search space. The basic problem: the genetic operators are blind to constraints. i.

    Eliminating infeasible candidates is very inefficient. which can be NP-complete. Repairing infeasible candidates requires a repair procedure that modifies a given chromosome such that it will not violate constraints. Decoding can simplify the problem search space and allow an efficient genetic algorithm.   It requires the creation of a feasible initial population. decoding can be seen as shifting to a search space that is different from the Cartesian product of the domains of the variables in the original problem formulation. Formally. and therefore hardly applicable. This technique is thus problem dependent. The preserving approach amounts to designing and applying problem-specific operators that do preserve the feasibility of parent chromosomes. 31 .

‡ In general penalties are given for violated constraints although some GAs allocate penalties for wrongly instantiated variables or as the distance to a feasible solution.does not work well with sparse problems.loss of information .reduction of the problem to µsimple¶ optimization .generality .Indirect Constraint Handling ‡ The optimization objectives replacing the constraints are viewed penalties for constraint violation hence to be minimized.possibility of embedding user preferences by means of weights. ‡ Advantages: . Disadvantages: . 32 .

4. Example: The permutation g = < 1.8 and a given g = < i1.GA FOR 8-QUEENS PROBLEM  Solution Representation A chromosome is a permutation of the number 1.8> represents a board where the queens are placed along the main diagonal. The solution space is now the set of all permutations of 1. i8> denotes the board configuration where the k-th column contains exactly one queen placed on the ik th row.«.6.«.7.8.2.3.5. 33 .«.

 34 . The representation guarantees ³half´ number of the constraints and what remains to be minimized is the number of diagonal constraint violations.)  By using such chromosome. we restrict the search to board configurations where horizontal constraint violation (two queens on the same column) and vertical constraint violation (two queens on the same row) do not occur.Solution Representation (cont.

35 . do the same for parent1 and child2. the following mechanism will create two child permutations. 2. «. select a random position. 1. crossover point. 4. 7} cut both parents in two segments after this position copy the first segment of parent1 into child1 and the first segment of parent2 into child2 scan parent2 from left to right and fill the second segment of child1 with values from parent2 skipping those that already contained in it. which are two permutations. 3.Crossover Operator (cut and crossfill)  Given two parents. 5. i  {1.

Example of crossover Parent1 Parent2 1 3 5| 7 6 2 4 8 2 1 8| 6 4 3 5 7 1 3 5| 2 8 6 4 7 2 1 8| 3 5 7 6 4 Child1: Child2: Mutation Operator ‡ We select two positions in a given chromosome and swaps the values standing on those positions. 36 . ‡ Mutation will cause a small undirected change and crossover creates children that inherit genetic material from both parents.

then ranks them according to fitness and deletes the worst two.  37 .Parent selection and survivor selection  Parent selection (best 2 out of random 5) choosing 5 individuals randomly from the population and taking the best two as parents that undergone crossover. Survivor selection: (replace worst) after merging the population and offsprings. This ensures a bias towards using parents with relatively high fitness.

Other issues Some other parameters for the GA are as follows:      Recombination probability 100% Mutation probability 80% Population size 100 Initialization random Termination Solution or 10000 evaluations 38 .

A GA for Exam Timetabling Problem   

Burke et al., 1995 [1] proposed a genetic algorithm for solving exam timetabling problem. This algorithm combines direct representation and heuristic crossover operators to ensure that the most fundamental constraints are never violated. Heuristic crossovers are used to propagate the most desirable features of the timetable to produce good quality solutions.

39

Solution Representation  

The most logical approach is to directly encode solutions with events matched to periods. Figure 5.1 shows such an encoding for n events where each gene in the chromosome represents which period in which a particular event is to be scheduled.

Event 1

Event 2

Event n-1

Event n

Period 1

Period 3

Period 2

Period 7

40

The Creation of an Initial Population 

The random sequential graph coloring algorithm is used to generate the starting population. It can create conflict-free graph colorings.

For each population member: Generate a random ordering of exams Take each exam in turn according to that ordering: Find the first period in which the exam may be placed without conflict and so that the number of students does not go above a predefined maximum. Place the exam in that period. 

This algorithm can quickly produce large populations of random feasible exam timetables.
41

It uses on average about twice as many periods as the optimal amount.)  Note: The method allows the length of the timetable to vary. Then the GA evolves new timetables. possibly reducing the length.The Creation of an Initial Population (cont.    42 . This approach guarantees a feasible timetable.

  43 . Respect is the property that if an exam is timetabled to the same period in both parents then it will be scheduled to that period in the child.Crossover Operators  The crossover operator should satisfy the properties of respect and assortment given by Radcliffe. Assortment is the property that the operator can generate a child such that if Exam1 is scheduled to Period 1 in the first parent and Exam2 is scheduled to Period 2 in the second parent then the child may have Exam 1 in Period 1 and Exam 2 in Period 2 providing that these are compatible.

Crossover Operators (cont. Once this is completed. the crossover looks at period two and so on until all exams are placed. 44 .) The crossover operator works for the period i as follows:  The operator starts by looking at the first period. It takes exams scheduled in that period (in both parents) and then uses an algorithm to select other exams so that none clash with those already scheduled and the limit on the number of spaces is not violated.

2. 45 .  (Any unscheduled exams are passed onto period i+1). select extra exams from the exams scheduled in period i in either parent1 or parent2 or left over from period i-1.Heuristic Crossover Operator  Period i of child Timetable: 1. Take those exams schedules in period i in both parents 1 and 2.

Heuristic Crossover Operator 46 .

Some of such operators are as follows. This is closest to the standard uniform crossover. Once an exam is selected. y Largest Degree Exams are selected according to the number of other exams they conflict with. all other exams that clash with it are labeled as unscheduled for that period. The authors construct a number of different crossover operators based on the same framework but using alternative selection algorithms. 47  . y Random Exams are selected at random.

It cannot take any exam and shift it to another period at random.Mutation Operator   Mutation. like crossover. since this may cause a conflict between the moved exams and ones already scheduled. Mutation is included into the crossover algorithm by adding exams to the current search that would otherwise not be considered until a later period.  48 . must also ensure that a timetable remains feasible after its action.

we may focus on two particular common requirements: The length of the timetable The num of conflicts between exams in adjacent periods. fitness function f(p) for p  P measures the quality of a solution p. For example. Note: The quality of a solution p may not vary smoothly as the genes comprising p vary since the genetic operators such as crossover and mutation do not vary the gene values smoothly. Given a space P of candidate solutions to a problem.   49 .Fitness Calculation   The evaluation function can be made up of any timetabling related factors.

one could choose: f(p) = 1/(1 + V(p)) so that the range of f(p) is from 0 to 1. if V(p) is the number of violated soft constraints in candidate p. p is a timetable. For instance.Fitness calculation (cont. If we have n kinds of soft constraints. the penalty associated with constraint-type i is wi.)   It seems reasonable to distinguish between timetables in terms of fitness based on the numbers and kinds of different constraints violated. then the fitness function becomes: n  f(p) = 1/(1 + § wici(p)) i =1 50 . and ci(p) is the number of violations of constraints of type i in p.

51 .Population size = 200 . ‡ The heuristics can be incorporated into the genetic operators mutation and crossover.Generational Reproduction . [1] combines traditional CSP solving heuristics with GAs. ‡ The underlying motivation is to get the best of two worlds.Other Issues  The Genetic algorithm for exam timetabling problem uses: .Rank-based selection mechanism Remarks ‡ Burke et al. The greediness of the heuristics (which can lead to dead-ends) and the blindness of the stochastic genetic search.

the offspring will satisfy them as well. in the fitness function. There is no guarantee that if the parents satisfy some constraints. or in repair mechanisms. 52 . However. genetic algorithms can be effective constraint solvers when knowledge about the constraints is incorporated either into  the genetic operators.Conclusions   Constraint handling is not straightforward in a GA because the search operators such as mutation and recombination are µblind¶ to constraints.

K. 2nd Edition. Weave.References   [1] Burke. 53 . How to Handle Constraint with Evolutionary Algorithms. A Hybrid Genetic Algorithm for Highly Constrained Timetabling Problems. [2] Craenen. and Marchiori.. 341 ± 361. E.G. 1995. Eiben. In L. pp. The Practical Handbook of Genetic Algorithms: Applications. volume 1. Napier University. D.. E. Chapman & Hall/CRC. R. of 6th International Conference on the Practice and Theory of Automated Timetabling.. Proc. A. UK.). W. B. 2001. Chambers (Ed. F. Edinburgh. E.. Elliman. C..

and so on. 54   . then as many as possible of the un-colored vertices with the second color. One strategy for graph coloring is the following ³greedy´ algorithm.Appendix: Randomly Sequential Graph Coloring Algorithm  A coloring of a graph is an assignment of a color to each vertex of the graph so that no two vertices connected by an edge have the same color. Initially we try to color as many as vertices as possible with the first color.

2. we perform the following steps. 55 . If there is no such edge. determine whether it has an edge to any vertex already colored with the new color. For each uncolored vertex. Select some uncolored vertex and color it with the new color. color the present vertex with the new color. Scan the list of uncolored vertices.To color vertices with a new color. 1.

56 .Example In figure having colored vertex 1 red. 3 red red 1 5 2 4 red There are some heuristics on the order of selecting one vertex from the list of uncolored vertices. we can color vertices 3 and 4 red also.

MEMETIC ALGORITHMS    Memetic algorithm introduced by Moscato and Norman in 1992 is an improved variant of genetic algorithm. It takes the concept of evolution as in genetic algorithm. However. but also by adaptation from itself. 57 . while genetic algorithm is based on biological evolution. an idea may be improved not only by recombination from others. In the evolution of ideas. memetic algorithm is based on cultural evolution or idea evolution.

A meme differs from a gene in that as it is passed between individuals. a unit of information in memetic algorithm can be improved by the individual holding it before it is passed on.A meme    A meme. A basic memetic algorithm. 58 . an evolution algorithm incorporated with some local search technique. each individual adapts the meme as it see best whereas genes are passed unchanged. is then.

An Example Memetic Operation Original solution Solution after mutation Solution after mutation and local Mutation and local search 59 .

Local search reduces the search space and reaches to high quality solution faster. this orientation is achieved by local search. 60 . the crossover operator orients seeking the best solution from the genes in the population. In memetic algorithm. In genetic algorithm.Memetic Algorithm    Normally. the local search technique is hill-climbing and the evolutionary operators are only mutation operators. while the mutation creates new genes for the population.

take each individual in turn: Choose a mutation method Apply mutation operator to chosen individuals Apply hill-climbing to individual just created. select a half of them to reduce the population to its original size.Outline of a Memetic Algorithm   create initial population repeat 1. until termination condition is true 61 . 2. Insert it into the population.

Master your semester with Scribd & The New York Times

Special offer for students: Only $4.99/month.

Master your semester with Scribd & The New York Times

Cancel anytime.