import numpy as np
import random
from typing import List, Tuple, Dict, Optional
import matplotlib.pyplot as plt
from dataclasses import dataclass
import copy
from collections import defaultdict
import seaborn as sns
# ============================================================================
# DATA STRUCTURES
# ============================================================================
@dataclass
class Operation:
"""Represents a single operation in job shop"""
job_id: int
operation_id: int
machine_id: int
duration: int
@dataclass
class Job:
"""Represents a job with sequence of operations"""
job_id: int
operations: List[Operation]
class JobShopInstance:
"""Job shop scheduling problem instance"""
def __init__(self, jobs: List[Job], num_machines: int):
self.jobs = jobs
self.num_machines = num_machines
self.num_jobs = len(jobs)
self.total_operations = sum(len(job.operations) for job in jobs)
self._build_operation_index()
def _build_operation_index(self):
"""Build index of all operations"""
self.all_operations = []
self.operation_map = {} # (job_id, op_id) -> operation
for job in self.jobs:
for op_id, op in enumerate(job.operations):
self.all_operations.append(op)
self.operation_map[(job.job_id, op_id)] = op
@staticmethod
def create_benchmark(num_jobs: int, num_machines: int,
max_duration: int = 100, seed: int = 42) -> 'JobShopInstance':
"""Create random job shop instance"""
random.seed(seed)
np.random.seed(seed)
jobs = []
for job_id in range(num_jobs):
operations = []
# Random permutation of machines for each job
machine_sequence = np.random.permutation(num_machines)
for op_id, machine_id in enumerate(machine_sequence):
duration = random.randint(1, max_duration)
op = Operation(
job_id=job_id,
operation_id=op_id,
machine_id=int(machine_id),
duration=duration
)
operations.append(op)
job = Job(job_id=job_id, operations=operations)
jobs.append(job)
return JobShopInstance(jobs, num_machines)
@staticmethod
def create_ft6() -> 'JobShopInstance':
"""Create FT06 benchmark instance (6 jobs, 6 machines)"""
durations = [
[1, 3, 6, 7, 3, 6],
[8, 5, 10, 10, 10, 4],
[5, 4, 8, 9, 1, 7],
[5, 5, 5, 3, 8, 9],
[9, 3, 5, 4, 3, 1],
[3, 3, 9, 10, 4, 9]
]
machines = [
[2, 0, 1, 3, 4, 5],
[1, 2, 4, 5, 0, 3],
[2, 3, 5, 0, 1, 4],
[1, 0, 2, 3, 5, 4],
[2, 1, 4, 5, 0, 3],
[0, 1, 3, 2, 5, 4]
]
jobs = []
for job_id in range(6):
operations = []
for op_id in range(6):
op = Operation(
job_id=job_id,
operation_id=op_id,
machine_id=machines[job_id][op_id],
duration=durations[job_id][op_id]
)
operations.append(op)
jobs.append(Job(job_id=job_id, operations=operations))
return JobShopInstance(jobs, 6)
# ============================================================================
# SCHEDULE REPRESENTATION & EVALUATION
# ============================================================================
class Schedule:
"""Represents a schedule solution"""
def __init__(self, job_shop: JobShopInstance, operation_sequence: List[int]):
self.job_shop = job_shop
self.operation_sequence = operation_sequence
self.makespan = None
self.fitness = None
self._schedule_cache = None
def evaluate(self) -> float:
"""
Decode schedule from operation sequence and compute makespan.
The sequence represents the order in which operations are scheduled.
We respect job precedence (operations of same job in order).
"""
if self.fitness is not None:
return self.fitness
job_shop = self.job_shop
operation_sequence = self.operation_sequence
# Track when each job's operations can start (precedence)
job_next_free_time = [0] * job_shop.num_jobs
# Track when each machine becomes free
machine_free_time = [0] * job_shop.num_machines
# Track scheduled operations for cache
operation_start_times = {}
operation_end_times = {}
# Process operations in sequence
operation_count = defaultdict(int) # Count operations scheduled for each job
for op_idx in operation_sequence:
operation = job_shop.all_operations[op_idx]
job_id = operation.job_id
machine_id = operation.machine_id
duration = operation.duration
# Operation count for this job (which operation of the job)
op_number = operation_count[job_id]
operation_count[job_id] += 1
# Check precedence: all previous operations of job must be done
job_ready_time = job_next_free_time[job_id]
# Machine must be free
machine_ready_time = machine_free_time[machine_id]
# Actual start time
start_time = max(job_ready_time, machine_ready_time)
end_time = start_time + duration
# Record operation schedule
operation_start_times[op_idx] = start_time
operation_end_times[op_idx] = end_time
# Update resource availability
job_next_free_time[job_id] = end_time
machine_free_time[machine_id] = end_time
self.makespan = max(job_next_free_time)
self._schedule_cache = {
'start_times': operation_start_times,
'end_times': operation_end_times,
'job_free_time': job_next_free_time,
'machine_free_time': machine_free_time
}
# Fitness: smaller makespan is better
self.fitness = 1.0 / self.makespan
return self.fitness
def get_makespan(self) -> float:
"""Get makespan (total completion time)"""
if self.makespan is None:
self.evaluate()
return self.makespan
def copy(self) -> 'Schedule':
"""Create a deep copy"""
new_schedule = Schedule(self.job_shop, self.operation_sequence.copy())
new_schedule.makespan = self.makespan
new_schedule.fitness = self.fitness
if self._schedule_cache:
new_schedule._schedule_cache = copy.deepcopy(self._schedule_cache)
return new_schedule
# ============================================================================
# CROSSOVER OPERATORS
# ============================================================================
class CrossoverOperators:
"""Collection of crossover operators"""
@staticmethod
def order_crossover_ox1(parent1_seq: List[int], parent2_seq: List[int]) -> List[int]:
"""
Order Crossover (OX1) - Preserves relative order of elements.
Key principle: Job precedence is implicitly maintained because each job's
operations appear in order in the sequence, and we preserve relative ordering.
Algorithm:
1. Select two crossover points
2. Copy segment from parent1 to child
3. Fill remaining positions from parent2, maintaining parent2's order
Args:
parent1_seq: Sequence from parent 1
parent2_seq: Sequence from parent 2
Returns:
Child sequence
"""
length = len(parent1_seq)
# Select crossover points
point1 = random.randint(0, length - 1)
point2 = random.randint(point1, length - 1)
# Initialize child with -1 (empty)
child = [-1] * length
# Copy segment from parent1
child[point1:point2 + 1] = parent1_seq[point1:point2 + 1]
# Items already in child
used_items = set(child[point1:point2 + 1])
# Fill remaining positions from parent2, maintaining order
# Start from point2+1 and wrap around
fill_positions = list(range(point2 + 1, length)) + list(range(0, point1))
parent2_items = [item for item in parent2_seq if item not in used_items]
for i, pos in enumerate(fill_positions):
child[pos] = parent2_items[i]
return child
@staticmethod
def position_based_crossover(parent1_seq: List[int], parent2_seq: List[int]) -> List[int]:
"""
Position-Based Crossover (PBX).
Randomly select positions and take values from parent1,
fill remaining from parent2 maintaining order.
"""
length = len(parent1_seq)
# Randomly select positions from parent1 (about 50%)
selected_positions = random.sample(range(length), length // 2)
selected_set = set(selected_positions)
child = [-1] * length
# Copy selected positions from parent1
for pos in selected_positions:
child[pos] = parent1_seq[pos]
used_items = set(child[pos] for pos in selected_positions if child[pos] != -1)
# Fill remaining from parent2
parent2_items = [item for item in parent2_seq if item not in used_items]
fill_idx = 0
for i in range(length):
if child[i] == -1:
child[i] = parent2_items[fill_idx]
fill_idx += 1
return child
@staticmethod
def cycle_crossover(parent1_seq: List[int], parent2_seq: List[int]) -> List[int]:
"""
Cycle Crossover (CX).
Follows cycles of values between parents.
"""
length = len(parent1_seq)
child = [-1] * length
# Create position mapping for parent2
parent2_pos = {val: idx for idx, val in enumerate(parent2_seq)}
# Trace cycles
visited = [False] * length
cycle_num = 0
for i in range(length):
if not visited[i]:
# Start new cycle
pos = i
take_from_parent1 = (cycle_num % 2 == 0)
while not visited[pos]:
visited[pos] = True
if take_from_parent1:
child[pos] = parent1_seq[pos]
# Follow to next position in cycle
next_val = parent1_seq[pos]
pos = parent2_pos[next_val]
else:
pos = parent2_pos[parent1_seq[pos]]
cycle_num += 1
# Fill any remaining -1 values from parent2
for i in range(length):
if child[i] == -1:
for val in parent2_seq:
if val not in child:
child[i] = val
break
return child
# ============================================================================
# MUTATION OPERATORS
# ============================================================================
class MutationOperators:
"""Collection of mutation operators"""
@staticmethod
def swap_mutation(sequence: List[int], rate: float = 0.1) -> List[int]:
"""Swap two random operations"""
mutated = sequence.copy()
length = len(mutated)
num_swaps = max(1, int(length * rate))
for _ in range(num_swaps):
if random.random() < rate:
i, j = random.sample(range(length), 2)
mutated[i], mutated[j] = mutated[j], mutated[i]
return mutated
@staticmethod
def insert_mutation(sequence: List[int]) -> List[int]:
"""Remove and reinsert operation"""
mutated = sequence.copy()
length = len(mutated)
if length < 2:
return mutated
# Remove from random position
pos1 = random.randint(0, length - 1)
op = mutated.pop(pos1)
# Insert at different random position
pos2 = random.randint(0, len(mutated))
mutated.insert(pos2, op)
return mutated
@staticmethod
def inversion_mutation(sequence: List[int]) -> List[int]:
"""Reverse a random segment"""
mutated = sequence.copy()
length = len(mutated)
if length < 2:
return mutated
i, j = sorted(random.sample(range(length), 2))
mutated[i:j + 1] = reversed(mutated[i:j + 1])
return mutated
@staticmethod
def job_swap_mutation(sequence: List[int], job_shop: JobShopInstance) -> List[int]:
"""
Swap operations between two random jobs.
More intelligent mutation that respects job structure.
"""
mutated = sequence.copy()
# Select two random jobs
job1_id, job2_id = random.sample(range(job_shop.num_jobs), 2)
# Find positions of operations for these jobs
job1_ops = [i for i, op_idx in enumerate(mutated)
if job_shop.all_operations[op_idx].job_id == job1_id]
job2_ops = [i for i, op_idx in enumerate(mutated)
if job_shop.all_operations[op_idx].job_id == job2_id]
if job1_ops and job2_ops:
# Swap random operations from each job
pos1 = random.choice(job1_ops)
pos2 = random.choice(job2_ops)
mutated[pos1], mutated[pos2] = mutated[pos2], mutated[pos1]
return mutated
# ============================================================================
# GENETIC ALGORITHM
# ============================================================================
class GeneticAlgorithm:
"""Main GA for Job Shop Scheduling"""
def __init__(self, job_shop: JobShopInstance,
population_size: int = 100,
generations: int = 100,
mutation_rate: float = 0.1,
crossover_rate: float = 0.8,
elitism_rate: float = 0.01,
tournament_size: int = 3,
seed: int = 42):
"""
Initialize GA
Args:
job_shop: Job shop instance
population_size: Number of individuals
generations: Number of generations
mutation_rate: Probability of mutation
crossover_rate: Probability of crossover
elitism_rate: Proportion of top individuals to preserve (e.g., 0.01 = 1%)
tournament_size: Tournament selection size
seed: Random seed
"""
self.job_shop = job_shop
self.population_size = population_size
self.generations = generations
self.mutation_rate = mutation_rate
self.crossover_rate = crossover_rate
self.elitism_count = max(1, int(population_size * elitism_rate))
self.tournament_size = tournament_size
random.seed(seed)
np.random.seed(seed)
self.population: List[Schedule] = []
self.best_schedule: Optional[Schedule] = None
# Statistics
self.best_fitness_history = []
self.avg_fitness_history = []
self.worst_fitness_history = []
self.best_makespan_history = []
self.generation_best = []
def initialize_population(self) -> List[Schedule]:
"""Create initial population"""
population = []
total_ops = self.job_shop.total_operations
# Add some diversity in initialization
for i in range(self.population_size):
if i == 0:
# One fully random
seq = list(np.random.permutation(total_ops))
elif i < self.population_size // 4:
# Some with job-order preference
seq = self._create_job_order_sequence()
else:
# Random shuffles
seq = list(np.random.permutation(total_ops))
schedule = Schedule(self.job_shop, seq)
schedule.evaluate()
population.append(schedule)
return population
def _create_job_order_sequence(self) -> List[int]:
"""Create sequence respecting job order (heuristic initialization)"""
sequence = []
job_queue = [list(range(len(job.operations)))
for job in self.job_shop.jobs]
# Append operations in round-robin fashion
while any(job_queue):
for job_ops in job_queue:
if job_ops:
# Get first operation index of this job
job_op_idx = job_ops.pop(0)
# Find global operation index
for i, op in enumerate(self.job_shop.all_operations):
if (op.job_id ==
list(self.job_shop.jobs).index(
[j for j in self.job_shop.jobs
if len(j.operations) > job_op_idx][0])
and op.operation_id == job_op_idx):
sequence.append(i)
break
return sequence
def tournament_selection(self, population: List[Schedule],
size: Optional[int] = None) -> Schedule:
"""Tournament selection"""
if size is None:
size = self.tournament_size
tournament = random.sample(population, min(size, len(population)))
return max(tournament, key=lambda x: x.fitness)
def selection_roulette_wheel(self, population: List[Schedule]) -> Schedule:
"""Roulette wheel selection (fitness proportionate)"""
fitnesses = np.array([s.fitness for s in population])
# Normalize fitnesses
min_fitness = fitnesses.min()
max_fitness = fitnesses.max()
if max_fitness == min_fitness:
# All same fitness
return random.choice(population)
normalized_fitness = (fitnesses - min_fitness) / (max_fitness - min_fitness)
probabilities = normalized_fitness / normalized_fitness.sum()
return np.random.choice(population, p=probabilities)
def crossover(self, parent1: Schedule, parent2: Schedule) -> Schedule:
"""Apply crossover operator"""
if random.random() < self.crossover_rate:
crossover_type = random.choice(['ox1', 'pbx', 'cx'])
if crossover_type == 'ox1':
child_seq = CrossoverOperators.order_crossover_ox1(
parent1.operation_sequence, parent2.operation_sequence)
elif crossover_type == 'pbx':
child_seq = CrossoverOperators.position_based_crossover(
parent1.operation_sequence, parent2.operation_sequence)
else: # cx
child_seq = CrossoverOperators.cycle_crossover(
parent1.operation_sequence, parent2.operation_sequence)
return Schedule(self.job_shop, child_seq)
else:
# Return copy of better parent
return parent1.copy() if parent1.fitness > parent2.fitness else parent2.copy()
def mutate(self, schedule: Schedule) -> Schedule:
"""Apply mutation operator"""
mutated_seq = schedule.operation_sequence.copy()
if random.random() < self.mutation_rate:
mutation_type = random.choice(
['swap', 'insert', 'inversion', 'job_swap'])
if mutation_type == 'swap':
mutated_seq = MutationOperators.swap_mutation(mutated_seq, self.mutation_rate)
elif mutation_type == 'insert':
mutated_seq = MutationOperators.insert_mutation(mutated_seq)
elif mutation_type == 'inversion':
mutated_seq = MutationOperators.inversion_mutation(mutated_seq)
else: # job_swap
mutated_seq = MutationOperators.job_swap_mutation(
mutated_seq, self.job_shop)
return Schedule(self.job_shop, mutated_seq)
def evolve(self, verbose: bool = True) -> Schedule:
"""Run the GA"""
print("\n" + "="*70)
print("GENETIC ALGORITHM INITIALIZATION")
print("="*70)
print(f"Problem: {self.job_shop.num_jobs} jobs × {self.job_shop.num_machines} machines")
print(f"Total operations: {self.job_shop.total_operations}")
print(f"Population size: {self.population_size}")
print(f"Generations: {self.generations}")
print(f"Mutation rate: {self.mutation_rate}")
print(f"Crossover rate: {self.crossover_rate}")
print(f"Elitism (top 1%): {self.elitism_count} individuals")
print("="*70 + "\n")
# Initialize population
self.population = self.initialize_population()
for generation in range(self.generations):
# Sort by fitness (descending)
self.population.sort(key=lambda x: x.fitness, reverse=True)
# Update statistics
fitnesses = [s.fitness for s in self.population]
makespans = [s.get_makespan() for s in self.population]
best_fitness = max(fitnesses)
avg_fitness = np.mean(fitnesses)
worst_fitness = min(fitnesses)
best_makespan = min(makespans)
self.best_fitness_history.append(best_fitness)
self.avg_fitness_history.append(avg_fitness)
self.worst_fitness_history.append(worst_fitness)
self.best_makespan_history.append(best_makespan)
# Update best overall
if self.best_schedule is None or best_fitness > self.best_schedule.fitness:
self.best_schedule = self.population[0].copy()
self.generation_best.append(self.population[0].copy())
if verbose and (generation + 1) % 20 == 0:
print(f"Gen {generation + 1:3d} | Best: {best_makespan:7.2f} | "
f"Avg: {np.mean(makespans):7.2f} | "
f"Worst: {max(makespans):7.2f} | "
f"Fitness: {best_fitness:.6f}")
# ELITISM: Preserve top individuals
elite_individuals = [s.copy() for s in self.population[:self.elitism_count]]
# Create new population
new_population = elite_individuals
# Generate offspring
while len(new_population) < self.population_size:
# Selection
parent1 = self.tournament_selection(self.population)
parent2 = self.tournament_selection(self.population)
# Crossover
child = self.crossover(parent1, parent2)
# Mutation
child = self.mutate(child)
# Evaluate
child.evaluate()
new_population.append(child)
# Trim to population size
self.population = new_population[:self.population_size]
# Final sort
self.population.sort(key=lambda x: x.fitness, reverse=True)
self.best_schedule = self.population[0].copy()
print("\n" + "="*70)
print(f"FINAL RESULT: Best makespan = {self.best_schedule.get_makespan():.2f}")
print("="*70 + "\n")
return self.best_schedule
# ============================================================================
# VISUALIZATION & ANALYSIS
# ============================================================================
class ScheduleVisualizer:
"""Visualization utilities for schedules"""
@staticmethod
def print_schedule(schedule: Schedule):
"""Print detailed schedule information"""
job_shop = schedule.job_shop
schedule.evaluate()
print("\n" + "="*80)
print("SCHEDULE DETAILS")
print("="*80)
print(f"Makespan: {schedule.get_makespan():.2f}")
print(f"Fitness (1/Makespan): {schedule.fitness:.6f}")
print("\nOperation Sequence (indices):")
print(f"{schedule.operation_sequence}\n")
# Decode schedule
job_next_free = [0] * job_shop.num_jobs
machine_free = [0] * job_shop.num_machines
operation_count = defaultdict(int)
print(f"{'Job':<6} {'Op':<6} {'Machine':<10} {'Duration':<10} "
f"{'Start':<10} {'End':<10} {'Slack':<10}")
print("-" * 80)
total_makespan = 0
for op_idx in schedule.operation_sequence:
operation = job_shop.all_operations[op_idx]
job_id = operation.job_id
start = max(job_next_free[job_id], machine_free[operation.machine_id])
end = start + operation.duration
job_next_free[job_id] = end
machine_free[operation.machine_id] = end
total_makespan = max(total_makespan, end)
print(f"{job_id:<6} {operation.operation_id:<6} {operation.machine_id:<10} "
f"{operation.duration:<10} {start:<10.0f} {end:<10.0f} "
f"{0:<10.0f}")
print("-" * 80)
print(f"Total Makespan: {total_makespan:.0f}")
print("="*80 + "\n")
@staticmethod
def gantt_chart(schedule: Schedule, title: str = "", figsize: Tuple = (16, 8)):
"""Create Gantt chart"""
job_shop = schedule.job_shop
schedule.evaluate()
# Decode schedule
job_free = [0] * job_shop.num_jobs
machine_free = [0] * job_shop.num_machines
operations_scheduled = [] # (machine, start, duration, job_id, op_id)
for op_idx in schedule.operation_sequence:
op = job_shop.all_operations[op_idx]
start = max(job_free[op.job_id], machine_free[op.machine_id])
end = start + op.duration
operations_scheduled.append({
'machine': op.machine_id,
'start': start,
'duration': op.duration,
'job': op.job_id,
'op': op.operation_id,
'end': end
})
job_free[op.job_id] = end
machine_free[op.machine_id] = end
# Create figure
fig, ax = plt.subplots(figsize=figsize)
# Colors for jobs
colors = plt.cm.tab20(np.linspace(0, 1, job_shop.num_jobs))
# Draw operations
for op_info in operations_scheduled:
machine = op_info['machine']
start = op_info['start']
duration = op_info['duration']
job = op_info['job']
op_id = op_info['op']
ax.barh(machine, duration, left=start, height=0.7,
color=colors[job], edgecolor='black', linewidth=1.5, alpha=0.8)
# Label
ax.text(start + duration/2, machine,
f"J{job}-O{op_id}",
ha='center', va='center', fontsize=9, fontweight='bold')
# Formatting
ax.set_ylim(-0.5, job_shop.num_machines - 0.5)
ax.set_xlim(0, schedule.get_makespan() * 1.02)
ax.set_xlabel('Time', fontsize=12, fontweight='bold')
ax.set_ylabel('Machine', fontsize=12, fontweight='bold')
title_text = f'Job Shop Schedule{"" if not title else f" - {title}"}'
title_text += f'\nMakespan: {schedule.get_makespan():.0f}'
ax.set_title(title_text, fontsize=14, fontweight='bold')
ax.set_yticks(range(job_shop.num_machines))
ax.set_yticklabels([f'M{i}' for i in range(job_shop.num_machines)])
ax.grid(True, axis='x', alpha=0.3)
# Legend
from matplotlib.patches import Patch
legend_elements = [Patch(facecolor=colors[i], edgecolor='black',
label=f'Job {i}')
for i in range(job_shop.num_jobs)]
ax.legend(handles=legend_elements, loc='upper right', ncol=2, fontsize=10)
plt.tight_layout()
return fig
@staticmethod
def plot_convergence(ga: GeneticAlgorithm, figsize: Tuple = (15, 10)):
"""Plot convergence metrics"""
fig, axes = plt.subplots(2, 2, figsize=figsize)
generations = range(1, len(ga.best_fitness_history) + 1)
# Plot 1: Fitness
ax = axes[0, 0]
ax.plot(generations, ga.best_fitness_history, 'g-', linewidth=2.5, label='Best')
ax.plot(generations, ga.avg_fitness_history, 'b-', linewidth=2, label='Average', alpha=0.7)
ax.plot(generations, ga.worst_fitness_history, 'r--', linewidth=2, label='Worst', alpha=0.7)
ax.set_xlabel('Generation', fontweight='bold')
ax.set_ylabel('Fitness (1/Makespan)', fontweight='bold')
ax.set_title('Fitness Convergence', fontweight='bold', fontsize=12)
ax.legend(fontsize=10)
ax.grid(True, alpha=0.3)
# Plot 2: Makespan
ax = axes[0, 1]
makespans_best = [1.0 / f for f in ga.best_fitness_history]
makespans_avg = [1.0 / f for f in ga.avg_fitness_history]
makespans_worst = [1.0 / f for f in ga.worst_fitness_history]
ax.plot(generations, makespans_best, 'g-', linewidth=2.5, label='Best')
ax.plot(generations, makespans_avg, 'b-', linewidth=2, label='Average', alpha=0.7)
ax.plot(generations, makespans_worst, 'r--', linewidth=2, label='Worst', alpha=0.7)
ax.set_xlabel('Generation', fontweight='bold')
ax.set_ylabel('Makespan', fontweight='bold')
ax.set_title('Makespan Convergence', fontweight='bold', fontsize=12)
ax.legend(fontsize=10)
ax.grid(True, alpha=0.3)
# Plot 3: Improvement
ax = axes[1, 0]
initial_makespan = makespans_worst[0]
improvements = [(initial - m) / initial * 100
for initial, m in zip([initial_makespan]*len(makespans_best), makespans_best)]
ax.fill_between(generations, improvements, alpha=0.3, color='green')
ax.plot(generations, improvements, 'g-', linewidth=2.5)
ax.set_xlabel('Generation', fontweight='bold')
ax.set_ylabel('Improvement (%)', fontweight='bold')
ax.set_title('Improvement from Initial Solution', fontweight='bold', fontsize=12)
ax.grid(True, alpha=0.3)
# Plot 4: Diversity
ax = axes[1, 1]
diversity = [best - worst for best, worst in
zip(ga.best_fitness_history, ga.worst_fitness_history)]
ax.plot(generations, diversity, 'purple', linewidth=2.5)
ax.fill_between(generations, diversity, alpha=0.3, color='purple')
ax.set_xlabel('Generation', fontweight='bold')
ax.set_ylabel('Fitness Range', fontweight='bold')
ax.set_title('Population Diversity', fontweight='bold', fontsize=12)
ax.grid(True, alpha=0.3)
plt.tight_layout()
return fig
@staticmethod
def compare_solutions(schedules: Dict[str, Schedule], figsize: Tuple = (14, 8)):
"""Compare multiple schedules"""
fig, axes = plt.subplots(1, len(schedules), figsize=figsize)
if len(schedules) == 1:
axes = [axes]
for idx, (name, schedule) in enumerate(schedules.items()):
schedule.evaluate()
job_shop = schedule.job_shop
# Decode
job_free = [0] * job_shop.num_jobs
machine_free = [0] * job_shop.num_machines
operations_scheduled = []
for op_idx in schedule.operation_sequence:
op = job_shop.all_operations[op_idx]
start = max(job_free[op.job_id], machine_free[op.machine_id])
end = start + op.duration
operations_scheduled.append({
'machine': op.machine_id,
'start': start,
'duration': op.duration,
'job': op.job_id,
})
job_free[op.job_id] = end
machine_free[op.machine_id] = end
ax = axes[idx]
colors = plt.cm.tab20(np.linspace(0, 1, job_shop.num_jobs))
for op_info in operations_scheduled:
machine = op_info['machine']
start = op_info['start']
duration = op_info['duration']
job = op_info['job']
ax.barh(machine, duration, left=start, height=0.7,
color=colors[job], edgecolor='black', linewidth=1, alpha=0.8)
ax.set_ylim(-0.5, job_shop.num_machines - 0.5)
ax.set_xlim(0, schedule.get_makespan() * 1.02)
ax.set_xlabel('Time', fontweight='bold')
ax.set_title(f'{name}\nMakespan: {schedule.get_makespan():.0f}',
fontweight='bold', fontsize=11)
ax.set_yticks(range(job_shop.num_machines))
ax.set_yticklabels([f'M{i}' for i in range(job_shop.num_machines)])
ax.grid(True, axis='x', alpha=0.3)
plt.tight_layout()
return fig
# ============================================================================
# EXAMPLE USAGE
# ============================================================================
if __name__ == "__main__":
# Test 1: Small random instance
print("\n" + "█"*70)
print("TEST 1: Small Random Job Shop Instance")
print("█"*70)
job_shop_small = JobShopInstance.create_benchmark(
num_jobs=5,
num_machines=3,
max_duration=50,
seed=42
)
ga_small = GeneticAlgorithm(
job_shop=job_shop_small,
population_size=50,
generations=50,
mutation_rate=0.15,
crossover_rate=0.85,
elitism_rate=0.01,
seed=42
)
best_small = ga_small.evolve(verbose=True)
ScheduleVisualizer.print_schedule(best_small)
fig1 = ScheduleVisualizer.gantt_chart(best_small, "GA Solution (Small Instance)")
plt.show()
# Test 2: Larger instance with FT06 benchmark
print("\n" + "█"*70)
print("TEST 2: FT06 Benchmark Instance")
print("█"*70)
job_shop_ft06 = JobShopInstance.create_ft6()
ga_ft06 = GeneticAlgorithm(
job_shop=job_shop_ft06,
population_size=100,
generations=100,
mutation_rate=0.12,
crossover_rate=0.85,
elitism_rate=0.01,
seed=42
)
best_ft06 = ga_ft06.evolve(verbose=True)
ScheduleVisualizer.print_schedule(best_ft06)
# Convergence plot
fig2 = ScheduleVisualizer.plot_convergence(ga_ft06)
plt.show()
# Gantt chart
fig3 = ScheduleVisualizer.gantt_chart(best_ft06, "GA Solution (FT06)")
plt.show()
# Test 3: Compare with random heuristic
print("\n" + "█"*70)
print("TEST 3: Comparison with Heuristics")
print("█"*70)
# Random sequence
random_seq = list(np.random.permutation(job_shop_ft06.total_operations))
random_schedule = Schedule(job_shop_ft06, random_seq)
random_schedule.evaluate()
# Job-order sequence (each job's ops in order, jobs in round-robin)
job_order_seq = []
for _ in range(job_shop_ft06.num_jobs):
for job_id in range(job_shop_ft06.num_jobs):
for op_id in range(len(job_shop_ft06.jobs[job_id].operations)):
for i, op in enumerate(job_shop_ft06.all_operations):
if op.job_id == job_id and op.operation_id == op_id:
if i not in job_order_seq:
job_order_seq.append(i)
break
job_order_schedule = Schedule(job_shop_ft06, job_order_seq[:job_shop_ft06.total_operations])
job_order_schedule.evaluate()
comparison = {
"Random": random_schedule,
"Job Order": job_order_schedule,
"GA (100 gen)": best_ft06
}
print(f"\nComparison of Solutions:")
print(f"{'Method':<20} {'Makespan':<15} {'Improvement':<15}")
print("-" * 50)
random_makespan = random_schedule.get_makespan()
for name, schedule in comparison.items():
makespan = schedule.get_makespan()
improvement = (random_makespan - makespan) / random_makespan * 100
print(f"{name:<20} {makespan:<15.2f} {improvement:<14.2f}%")
fig4 = ScheduleVisualizer.compare_solutions(comparison)
plt.show()
# Elitism analysis
print("\n" + "█"*70)
print("ELITISM EFFECT ANALYSIS")
print("█"*70)
print(f"Elitism count: {ga_ft06.elitism_count} individuals ({ga_ft06.elitism_count/ga_ft06.population_size*100:.1f}%)")
print(f"Population size: {ga_ft06.population_size}")
print(f"Offspring per generation: {ga_ft06.population_size - ga_ft06.elitism_count}")
print(f"\nBenefit: Top 1% of solutions preserved unchanged across generations")
print(f"This accelerates convergence by maintaining high-quality solutions")