Implementation of Teaching Learning Based Optimization

Teaching Learning Based Optimization (TLBO) is a population-based metaheuristic algorithm inspired by the teaching-learning process in a classroom. The algorithm mimics the interaction between a teacher and students, where knowledge is transferred through teaching and peer learning phases.

What is TLBO?

TLBO considers a population (class) with learners who gain knowledge through two distinct phases:

  • Teaching Phase ? The teacher shares knowledge with all learners

  • Learning Phase ? Learners interact among themselves to improve their knowledge

Each learner represents a potential solution, and their "knowledge" corresponds to the fitness value of the optimization problem.

Algorithm Phases

Teaching Phase

The teacher (best solution) attempts to improve the mean knowledge of the class. The new solution is calculated as ?

X_new = X_old + r × (X_teacher - TF × X_mean) where TF is teaching factor (1 or 2), r is random number [0,1]

Learning Phase

Learners interact randomly with each other. If one learner has better knowledge, others learn from them ?

If f(X_i) < f(X_j): X_new = X_i + r × (X_i - X_j) Otherwise: X_new = X_i + r × (X_j - X_i)

Test Functions

Common benchmark functions used to evaluate TLBO performance include ?

Sphere Function ? Simple unimodal function for basic testing:

$$f(x_1, x_2, ..., x_n) = \sum_{i=1}^{n} x_i^2$$

Rastrigin Function ? Multimodal function with many local optima:

$$f(x_1, x_2, ..., x_n) = 10n + \sum_{i=1}^{n} [x_i^2 - 10\cos(2\pi x_i)]$$

Implementation Example

Here's a simple TLBO implementation optimizing the Easom function ?

import numpy as np
import matplotlib.pyplot as plt

# Easom function - has global minimum at (?, ?)
def easom_function(x):
    x1, x2 = x[0], x[1]
    return -np.cos(x1) * np.cos(x2) * np.exp(-(x1 - np.pi)**2 - (x2 - np.pi)**2)

# Simple TLBO implementation
def tlbo_optimize(func, bounds, pop_size=20, max_iter=100):
    # Initialize population randomly
    dim = len(bounds)
    population = np.random.uniform([b[0] for b in bounds], [b[1] for b in bounds], 
                                   (pop_size, dim))
    
    best_fitness = float('inf')
    best_solution = None
    
    for iteration in range(max_iter):
        # Evaluate fitness
        fitness = [func(ind) for ind in population]
        
        # Find teacher (best solution)
        teacher_idx = np.argmin(fitness)
        teacher = population[teacher_idx].copy()
        
        if fitness[teacher_idx] < best_fitness:
            best_fitness = fitness[teacher_idx]
            best_solution = teacher.copy()
        
        # Teaching Phase
        mean_population = np.mean(population, axis=0)
        teaching_factor = np.random.choice([1, 2])
        
        for i in range(pop_size):
            r = np.random.random()
            new_solution = population[i] + r * (teacher - teaching_factor * mean_population)
            
            # Apply bounds
            new_solution = np.clip(new_solution, [b[0] for b in bounds], [b[1] for b in bounds])
            
            if func(new_solution) < fitness[i]:
                population[i] = new_solution
        
        # Learning Phase
        for i in range(pop_size):
            j = np.random.randint(0, pop_size)
            while j == i:
                j = np.random.randint(0, pop_size)
            
            r = np.random.random()
            if func(population[i]) < func(population[j]):
                new_solution = population[i] + r * (population[i] - population[j])
            else:
                new_solution = population[i] + r * (population[j] - population[i])
            
            # Apply bounds
            new_solution = np.clip(new_solution, [b[0] for b in bounds], [b[1] for b in bounds])
            
            if func(new_solution) < func(population[i]):
                population[i] = new_solution
        
        if iteration % 20 == 0:
            print(f"Generation {iteration}: f(x) = {best_fitness:.6f}")
    
    return best_solution, best_fitness

# Run optimization
bounds = [(-5, 5), (-5, 5)]
solution, fitness = tlbo_optimize(easom_function, bounds)

print(f"\nOptimal solution: [{solution[0]:.5f}, {solution[1]:.5f}]")
print(f"Optimal fitness: {fitness:.6f}")
print(f"Expected optimum: [3.14159, 3.14159] with fitness ? -1.0")
Generation 0: f(x) = -0.000002
Generation 20: f(x) = -0.863421
Generation 40: f(x) = -0.999998
Generation 60: f(x) = -1.000000
Generation 80: f(x) = -1.000000

Optimal solution: [3.14159, 3.14159]
Optimal fitness: -1.000000
Expected optimum: [3.14159, 3.14159] with fitness ? -1.0

Advantages and Disadvantages

Advantages Disadvantages
Parameter-free (only population size and iterations) Can be computationally expensive
No derivative information required May converge slowly on complex problems
Simple implementation Limited exploration in later iterations
Good balance of exploration and exploitation Performance depends on population diversity

Conclusion

TLBO is an effective parameter-free optimization algorithm inspired by classroom learning dynamics. Its simplicity and good performance make it suitable for various optimization problems, though it may require careful tuning of population size for complex landscapes.

Updated on: 2026-03-27T00:36:38+05:30

791 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements