Lec 10
Lec 10
Learning Objectives
The general objective of the lecture:
Guide students in implementing a basic GA algorithm for a problem like TSP or
QUEENS Problem.
Detailed objectives:
Since each queen must be on a different row and column, we can assume that queen
i is placed in an i-th column. The position of a number in the tuple represents the
queen's column position, while its value represents the queen's row position
(counting from the bottom) Using this representation, the solution space where two
of the constraints (row and column conflicts) are already satisfied should be searched
to eliminate the diagonal conflicts. The figure illustrates 4-tuples for the 4-queen
problem Tolerant to approximations.
Chromosome Structure
Kerbala University Machin Learning II
College of Computer Science and Information Technology
Department of Computer Science Dr. Elaf Adil – 3 Lecture
The variables in the problem are the locations of four queens. Then, the chromosome
structure is shown in Figure. Note that the genes of a chromosome are the problem
variables.
Score = dQ1Q2 . WQ1Q2 + dQ1Q3 . WQ1Q3+ dQ1Q4 . WQ1Q4 + dQ2Q3 . WQ2Q3+ dQ2Q4 .
WQ2Q4 + dQ3Q4 . WQ3Q4
Let’s also consider the distance (d) between two queens as the number of
horizontal and vertical blocks between them.
the smaller score gives higher merit because we are interested in minimization. In
the case of maximization, we use the inverse of the merit calculation.
To select which operator to use in the current cycle, we generate a random number
(from 0 to 100). If the value is between 0 to 96, then crossover, otherwise, mutation.
Randomly select two parents P1 and P2 according to their relative merits of Step
5. 6. Figure shows the two parents selected.
Soft computing consists of several methodologies that work together to solve real-
world problems. The main components include:
Offspring2 Score = 3x10 + 3x10 + 4x10 + 4x10 + 3x10 + 3x10 = 200 5. 10.
Compare the Offspring with the Population (Evolve the Population)
Kerbala University Machin Learning II
College of Computer Science and Information Technology
Department of Computer Science Dr. Elaf Adil – 3 Lecture
Since the offspring 2 score = 200 is better than the worst population member (P3
has a score of 120), then the offspring 2 survives and P3 dies (will be replaced by
the offspring 2). Figure illustrates (P3) chromosome
Problem Statement
A salesman needs to visit 5 cities exactly once and return to the starting city. The
objective is to find the shortest possible route.
Let’s consider the following distance matrix (in kilometers):
Cities A B C D E
A 0 10 15 20 25
B 10 0 35 25 30
C 15 35 0 30 20
D 20 25 30 0 15
E 25 30 20 15 0
The goal is to find the optimal tour (path) that minimizes the total travel distance.
Step 1: Encoding the Chromosomes
Each chromosome represents a possible route in permutation encoding.
For example, a possible solution can be represented as:
Chromosome: [A → C → B → E → D → A]
This means the salesman starts at city A, then goes to C, then B, then E, then D,
and finally returns to A.
Kerbala University Machin Learning II
College of Computer Science and Information Technology
Department of Computer Science Dr. Elaf Adil – 3 Lecture
The fitness function evaluates the total travel distance of a given chromosome.
For the route [A → C → B → E → D → A], the total distance is calculated as:
Since our goal is to minimize the distance, the lower the fitness value, the better
the solution.
Step 3: Selection
We apply Ordered Crossover (OX), which ensures offspring maintain valid city
sequences.
Example:
Parent 1: [A → B → C → D → E]
Parent 2: [D → E → B → A → C]
After crossover, we get a child:
Child: [A → E → B → D → C] (A mix of both parents)
Step 5: Mutation
<Best Regards>