0% found this document useful (0 votes)
9 views9 pages

Lec 10

The lecture on Applied Genetic Algorithms aims to guide undergraduate computer science students in implementing a basic GA algorithm for problems like the TSP and QUEENS Problem. Students will learn to explain GA mechanics, identify real-world applications, and apply GA to optimization problems. The lecture includes practical examples, such as solving the QUEENS Problem and TSP, detailing chromosome structure, fitness functions, selection methods, crossover, and mutation processes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views9 pages

Lec 10

The lecture on Applied Genetic Algorithms aims to guide undergraduate computer science students in implementing a basic GA algorithm for problems like the TSP and QUEENS Problem. Students will learn to explain GA mechanics, identify real-world applications, and apply GA to optimization problems. The lecture includes practical examples, such as solving the QUEENS Problem and TSP, detailing chromosome structure, fitness functions, selection methods, crossover, and mutation processes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Kerbala University Machin Learning II

College of Computer Science and Information Technology


Department of Computer Science Dr. Elaf Adil – 3 Lecture

Applied Genetic Algorithm


3h Lecture Dr. Elaf Adil

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:

By the end of the lecture, students should be able to:


✅ Explain how Genetic Algorithms work.

✅ Identify real-world problems where GA can be applied.

✅ Understand the key components of GA implementation.

✅ Apply GA to solve an optimization problem.

The target audience of the lecture:

Undergraduate-stage (3rd stage) students in computer science.


• Lecture duration: 1 lecture.
• Lecture duration: 2 hours.
Kerbala University Machin Learning II
College of Computer Science and Information Technology
Department of Computer Science Dr. Elaf Adil – 3 Lecture

1. THE QUEENS PROBLEM


There are four queens as shown in Figure, we want to find a way to place them on a
board divided into 16 grid units so that no two queens attach. This work uses genetic
algorithms to determine the best location for four queens.

Problem Representation (how to define a facility location):

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.

Generate Population (50 to 100 is reasonable diversity & processing time)


Kerbala University Machin Learning II
College of Computer Science and Information Technology
Department of Computer Science Dr. Elaf Adil – 3 Lecture

Evaluate the Population

Objective function = Maximize site score = Maximum of Σ d.W

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.

P1 Score = 3x10 + 3x10 + 4x10 + 4x10 + 3x10 + 3x10 = 200

P2 Score = 3x10 + 3x10 + 4x10 + 2x-10 + 5x10 + 3x10 = 160

P3 Score = 2x-10 + 3x10 + 5x10 + 3x10 + 5x10 + 2x-10 = 120

Calculate the Merits of Population Members

Merit of P1 = (200 + 160 + 120) / 200 = 2.4 = 2

Merit of P2 = (200 + 160 + 120) / 160 = 3

Merit of P3 = (200 + 160 + 120) / 120 = 4

the sum of merits = 9


Kerbala University Machin Learning II
College of Computer Science and Information Technology
Department of Computer Science Dr. Elaf Adil – 3 Lecture

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.

Calculate the Relative Merits of Population Members

RM of P1 = merit * 100 / Sum of merits = 2 . 100 / 9 = 22.22

RM of P2 = merit * 100 / Sum of merits = 3 . 100 / 9 = 33.33

RM of P3 = merit * 100 / Sum of merits = 5 . 100 / 9 = 55.55 5. 7.

Randomly Select the Operator (Crossover or Mutation)

Crossover rate = 96% (marriage is the main avenue for evolution)

Mutation rate = 4% (genius people are very rare)

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.

Use the Selected Operator (Assume Crossover)

Randomly select two parents P1 and P2 according to their relative merits of Step
5. 6. Figure shows the two parents selected.

Let’s apply crossover to generate an offspring.Figure.13 shows the two offspring’s


obtained.
Kerbala University Machin Learning II
College of Computer Science and Information Technology
Department of Computer Science Dr. Elaf Adil – 3 Lecture

Soft computing consists of several methodologies that work together to solve real-
world problems. The main components include:

Evaluate the Offspring

Offspring1 Score = 3x10 + 3x10 + 4x10 + 2x-10 + 5x10 + 3x10 = 160

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

2. Example: Solving TSP using Genetic Algorithm

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

Step 2: Fitness Function

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 use Roulette Wheel Selection or Tournament Selection to pick parent


chromosomes for crossover.
The probability of selecting a chromosome is proportional to 1 / fitness (since lower
fitness is better).
Fitter solutions are more likely to be chosen for reproduction.
Step 4: Crossover (Recombination)

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

Mutation is applied to maintain diversity and avoid premature convergence.


Common mutation methods:
Swap Mutation (Swap two cities): [A → C → B → D → E] → [A → B → C → D
→ E]
Kerbala University Machin Learning II
College of Computer Science and Information Technology
Department of Computer Science Dr. Elaf Adil – 3 Lecture

Reverse Mutation (Reverse part of the sequence): [A → C → B → D → E] → [A


→ D → B → C → E]
Step 6: Repeat Until Convergence

The GA repeats Selection → Crossover → Mutation for multiple generations until:


• The best solution does not improve for several iterations, or
• A predefined number of generations is reached.
Final Output

After running for 50-100 generations, we find the best solution:


Optimal route: [A → D → E → C → B → A]
Minimum Distance: 85 km (example result)

<Best Regards>

Dr. Elaf Adil

You might also like