0% found this document useful (0 votes)
7 views2 pages

EAI Problem 2

The document contains a Python implementation of an evolutionary algorithm to optimize job scheduling based on profit and deadlines. It includes functions for fitness evaluation, population initialization, crossover, mutation, and the main evolutionary algorithm process. The algorithm aims to find the best sequence of jobs that maximizes profit within the given deadlines.

Uploaded by

lawsantamonica
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

EAI Problem 2

The document contains a Python implementation of an evolutionary algorithm to optimize job scheduling based on profit and deadlines. It includes functions for fitness evaluation, population initialization, crossover, mutation, and the main evolutionary algorithm process. The algorithm aims to find the best sequence of jobs that maximizes profit within the given deadlines.

Uploaded by

lawsantamonica
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import random

jobs = [
('0', 2, 2), ('1', 8, 31), ('2', 5, 58), ('3', 2, 59), ('4', 7, 43),
('5', 8, 37), ('6', 7, 94), ('7', 6, 100), ('8', 2, 83), ('9', 6, 4),
('10', 6, 73), ('11', 1, 90), ('12', 5, 40), ('13', 5, 52), ('14', 7, 34),
('15', 7, 34), ('16', 3, 33), ('17', 4, 9), ('18', 5, 14), ('19', 1, 31),
('20', 4, 55), ('21', 6, 25), ('22', 4, 63), ('23', 8, 13), ('24', 2, 81),
('25', 2, 68), ('26', 4, 59), ('27', 2, 98), ('28', 5, 89), ('29', 6, 97),
('30', 1, 91), ('31', 5, 28), ('32', 4, 37), ('33', 8, 100), ('34', 7, 81),
('35', 6, 56), ('36', 8, 18), ('37', 8, 58), ('38', 4, 42), ('39', 4, 36),
('40', 6, 100), ('41', 3, 63), ('42', 1, 31), ('43', 4, 29), ('44', 8, 6),
('45', 2, 95), ('46', 1, 92), ('47', 2, 17), ('48', 2, 22), ('49', 7, 43),
('50', 6, 38), ('51', 6, 91), ('52', 4, 44), ('53', 2, 56), ('54', 2, 32),
('55', 6, 100), ('56', 5, 6), ('57', 7, 50), ('58', 1, 6), ('59', 7, 34),
('60', 2, 71), ('61', 5, 94), ('62', 7, 73), ('63', 7, 36), ('64', 4, 6),
('65', 4, 60), ('66', 3, 44), ('67', 2, 70), ('68', 4, 59), ('69', 4, 45),
('70', 5, 48), ('71', 6, 88), ('72', 4, 4), ('73', 2, 48), ('74', 4, 88),
('75', 4, 55), ('76', 6, 43), ('77', 6, 58), ('78', 8, 8), ('79', 5, 100),
('80', 1, 47), ('81', 1, 25), ('82', 4, 93), ('83', 2, 70), ('84', 1, 72),
('85', 1, 71), ('86', 7, 48), ('87', 4, 10), ('88', 8, 13), ('89', 5, 21),
('90', 6, 27), ('91', 4, 20), ('92', 1, 72), ('93', 5, 64), ('94', 8, 2),
('95', 7, 47), ('96', 8, 56), ('97', 5, 21), ('98', 6, 66), ('99', 1, 29)
]

def fitness(solution, jobs, max_deadline):

#Calculate the maximum benefit of a solution

total_profit = 0
time_slots = [False] * (max_deadline + 1) # Slots available
for job_id in solution:
job = next(job for job in jobs if job[0] == job_id)
for t in range(min(max_deadline, job[1]), 0, -1):
if not time_slots[t]:
time_slots[t] = True
total_profit += job[2]
break
return total_profit

def initialize_population(jobs, population_size, max_deadline):

#Initialize population

population = []
for _ in range(population_size):
# Generate a maximum sequence
sequence = random.sample([job[0] for job in jobs], len(jobs))
population.append(sequence[:max_deadline])
return population

def crossover(parent1, parent2, max_deadline):

# Crossover

size = len(parent1)
start, end = sorted(random.sample(range(size), 2))
child = parent1[start:end+1]
child += [job for job in parent2 if job not in child]
return child[:max_deadline]

def mutate(solution, max_deadline):

i, j = random.sample(range(len(solution)), 2)
solution[i], solution[j] = solution[j], solution[i]
return solution[:max_deadline] # Ensure a valid solution

def evolutionary_algorithm(jobs, population_size=50, generations=100,


mutation_rate=0.2):

max_deadline = max(job[1] for job in jobs) # Maximum number of possible slots


population = initialize_population(jobs, population_size, max_deadline)

for generation in range(generations):


# Evaluate the population
population = sorted(population, key=lambda sol: fitness(sol, jobs,
max_deadline), reverse=True)

# Selection> Keep the best solutions


next_population = population[:population_size // 2]

# Crossover and mutation for the next generation


while len(next_population) < population_size:
parent1, parent2 = sorted(random.sample(next_population, 2))
child = crossover(parent1, parent2, max_deadline)
if random.random() < mutation_rate:
child = mutate(child, max_deadline)
next_population.append(child)

population = next_population

# Return the best solution found


best_solution = max(population, key=lambda sol: fitness(sol, jobs,
max_deadline))
return best_solution, fitness(best_solution, jobs, max_deadline)

# Execution
best_sequence, max_profit = evolutionary_algorithm(jobs)
print("Best sequence:", best_sequence)
print("Maximum benefit:", max_profit)

You might also like