Genetic Algorithm Exercise

Mini Timetabling Problem with Step-by-Step GA Operations

Complete coverage of selection, crossover, and mutation

Problem Statement

A university needs to schedule 2 courses (C1, C2) into 2 available time slots (T1, T2) and 2 rooms (R1, R2). Each course requires exactly 1 time slot and 1 room.

Constraints:
  • Constraint 1: No two courses can be scheduled in the same room at the same time
  • Constraint 2: C1 must be scheduled in T1 (instructor availability)
Objective:

Maximize feasibility by minimizing constraint violations

Fitness = Number of satisfied constraints (max = 2)

Chromosome Representation

Each chromosome is represented as: $[C1\_slot, C1\_room, C2\_slot, C2\_room]$

T1 R2 T2 R1
Example: C1 in T1,R2 and C2 in T2,R1
Learning Outcomes

By completing this exercise, you will be able to:

  • Encode timetabling problems as genetic algorithm chromosomes
  • Calculate fitness values based on constraint satisfaction
  • Apply GA operators (selection, crossover, mutation) step-by-step
  • Understand GA advantages for complex optimization problems
  • 1. Given the following initial population of 3 chromosomes, compute the fitness for each as the number of satisfied constraints (max = 2):
    Chromosome 1: T1 R1 T2 R2
    Chromosome 2: T1 R2 T1 R1
    Chromosome 3: T2 R1 T2 R2
Fitness Evaluation Solution
1 Chromosome 1: [T1, R1, T2, R2]
C1 schedule: T1, R1
C2 schedule: T2, R2

Constraint 1: No room/time conflict? ✓ YES (Different times and rooms)
Constraint 2: C1 in T1? ✓ YES

Fitness = 2
2 Chromosome 2: [T1, R2, T1, R1]
C1 schedule: T1, R2
C2 schedule: T1, R1

Constraint 1: No room/time conflict? ✓ YES (Same time T1, but different rooms R2≠R1)
Constraint 2: C1 in T1? ✓ YES

Fitness = 2
3 Chromosome 3: [T2, R1, T2, R2]
C1 schedule: T2, R1
C2 schedule: T2, R2

Constraint 1: No room/time conflict? ✓ YES (Same time T2, but different rooms R1≠R2)
Constraint 2: C1 in T1? ✗ NO (C1 is in T2, not T1)

Fitness = 1
Chromosome Schedule Constraint 1 Constraint 2 Total Fitness
1 [T1,R1,T2,R2] 2
2 [T1,R2,T1,R1] 2
3 [T2,R1,T2,R2] 1
Key Insight

Chromosomes 1 and 2 are both fully feasible (fitness = 2), while chromosome 3 violates the instructor availability constraint for C1. This fitness landscape will drive selection toward the feasible solutions.

  • 2. Use roulette wheel selection (fitness-proportionate) to select 2 parents from the initial population. Show the probabilities for each chromosome and identify the chosen parents if the random numbers are $r_1 = 0.2$, $r_2 = 0.7$.
Roulette Wheel Selection Solution
1 Calculate Total Fitness and Probabilities:
Total Fitness = 2 + 2 + 1 = 5

Selection Probabilities:
• Chromosome 1: $\frac{2}{5} = 0.40$ (40%)
• Chromosome 2: $\frac{2}{5} = 0.40$ (40%)
• Chromosome 3: $\frac{1}{5} = 0.20$ (20%)
Roulette Wheel Visualization:
Chromosome 1
Probability: 0.40
Range: [0.0, 0.4)
Chromosome 2
Probability: 0.40
Range: [0.4, 0.8)
Chromosome 3
Probability: 0.20
Range: [0.8, 1.0]
2 Parent Selection with Random Numbers:
First Parent ($r_1 = 0.2$):
$r_1 = 0.2$ falls in range [0.0, 0.4) → Chromosome 1 selected

Second Parent ($r_2 = 0.7$):
$r_2 = 0.7$ falls in range [0.4, 0.8) → Chromosome 2 selected
Selected Parents
Parent 1: T1 R1 T2 R2
Parent 2: T1 R2 T1 R1
Selection Insight

Both selected parents have high fitness (2), demonstrating how roulette wheel selection favors better solutions while still allowing some randomness. The low-fitness chromosome 3 was not selected, but had a 20% chance.

  • 3. Perform one-point crossover between the two selected parents at the cut between positions 2 and 3:
    • Parent1: $[a_1, a_2, a_3, a_4]$
    • Parent2: $[b_1, b_2, b_3, b_4]$
    • Produce two offspring
One-Point Crossover Solution
Crossover Operation:
1 Identify Parents and Crossover Point:
Parent 1: T1 R1 T2 R2
Parent 2: T1 R2 T1 R1
Crossover point between positions 2 and 3 (marked with red border)
CROSSOVER
2 Generate Offspring:
Offspring 1: T1 R1 T1 R1
Parent 1 genes [1,2] + Parent 2 genes [3,4]
Offspring 2: T1 R2 T2 R2
Parent 2 genes [1,2] + Parent 1 genes [3,4]
3 Verify Offspring Fitness:
Offspring Chromosome C1 Schedule C2 Schedule Constraint 1 Constraint 2 Fitness
1 [T1,R1,T1,R1] T1,R1 T1,R1 ✗ Conflict! 1
2 [T1,R2,T2,R2] T1,R2 T2,R2 2
Crossover Analysis

Offspring 1 has both courses scheduled in the same room (R1) at the same time (T1), creating a constraint violation. Offspring 2 maintains full feasibility like both parents.

Key insight: Crossover can sometimes produce worse offspring than parents, but genetic diversity enables the population to explore new regions of the search space.

  • 4. Apply mutation to the first offspring by flipping its C2 time slot (T1 ↔ T2). Calculate the fitness of the mutated chromosome.
Mutation Operation Solution
1 Original Offspring 1:
Before Mutation: T1 R1 T1 R1
Analysis: Both courses in T1,R1 → Room conflict → Fitness = 1
MUTATION (C2 time slot)
2 After Mutation:
Mutated Offspring: T1 R1 T2 R1
C1 Schedule: T1, R1
C2 Schedule: T2, R1 (time slot changed from T1 to T2)

Constraint 1: Room conflict? ✓ NO (Different times: T1 ≠ T2)
Constraint 2: C1 in T1? ✓ YES

New Fitness = 2
Mutation Success!
Before Mutation:
  • Chromosome: [T1,R1,T1,R1]
  • Problem: Room conflict
  • Fitness: 1
After Mutation:
  • Chromosome: [T1,R1,T2,R1]
  • Solution: No conflicts
  • Fitness: 2
Mutation Insight

This mutation demonstrates how small changes can dramatically improve fitness. By changing just one gene (C2's time slot from T1 to T2), we eliminated the room conflict and achieved perfect feasibility. This shows mutation's power to fine-tune solutions and escape local optima.

  • 5. Briefly explain how the GA approach helps solve timetabling problems compared to deterministic methods like hill climbing.
GA vs. Deterministic Methods Analysis
Hill Climbing Limitations
  • Single solution focus - explores only one path
  • Local optima traps - gets stuck at suboptimal solutions
  • No diversity - cannot explore multiple regions simultaneously
  • Deterministic - repeatable but potentially limited
Genetic Algorithm Advantages
  • Population-based - explores multiple solutions in parallel
  • Global search capability - escapes local optima through diversity
  • Recombination power - combines good partial solutions
  • Adaptive exploration - balances exploitation and exploration
1 Population-Based Exploration:

Hill climbing explores only one solution at a time and can get stuck in local optima. Genetic algorithms maintain a population of diverse solutions, allowing parallel exploration of multiple regions in the solution space.

2 Crossover Combines Good Features:

Unlike hill climbing which makes incremental changes, GA's crossover operator can combine beneficial characteristics from different solutions, potentially creating better offspring than either parent.

3 Mutation Provides Diversity:

Mutation introduces random changes that help escape local optima and maintain genetic diversity, preventing premature convergence to suboptimal solutions.

Timetabling-Specific Benefits
  • Complex constraint handling: GA can balance multiple competing constraints simultaneously
  • Partial solution reuse: Crossover can combine feasible partial schedules
  • Robust to constraint changes: Population diversity helps adapt to new requirements
  • Scalability: Approach works well as problem size increases
Key Insight

For timetabling problems, GA's ability to maintain and evolve multiple candidate solutions simultaneously makes it particularly effective at finding high-quality schedules while avoiding the local optima that trap single-solution methods like hill climbing.

GA Operators
  • Fitness Evaluation: Measures solution quality
  • Selection: Chooses parents based on fitness
  • Crossover: Combines parent characteristics
  • Mutation: Introduces beneficial variation
Population Dynamics
  • Diversity: Multiple simultaneous solutions
  • Evolution: Gradual improvement over generations
  • Balance: Exploration vs exploitation
  • Robustness: Avoids single-point failures
Learning Achievements
  • Hands-on GA experience: Applied all core operators step-by-step
  • Constraint handling: Understood fitness-based optimization
  • Selection mechanisms: Mastered roulette wheel selection
  • Genetic operations: Executed crossover and mutation
  • Problem encoding: Converted timetabling to chromosomes
  • Algorithm comparison: GA vs deterministic methods
  • Practical insights: Why GAs work for complex problems
  • Real-world relevance: Timetabling application understanding
Common Mistakes to Avoid
❌ Fitness Calculation:
  • Forgetting to check all constraints
  • Misunderstanding constraint satisfaction
  • Wrong fitness scoring methods
  • Ignoring problem-specific requirements
❌ Operator Application:
  • Incorrect crossover point identification
  • Wrong roulette wheel probability ranges
  • Mutation applied to wrong genes
  • Not verifying offspring feasibility
✅ Pro tip: Always verify each step by checking constraint satisfaction and recalculating fitness!

Continue Learning