EAI Problem 2
EAI Problem 2
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)
]
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
#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
# 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]
i, j = random.sample(range(len(solution)), 2)
solution[i], solution[j] = solution[j], solution[i]
return solution[:max_deadline] # Ensure a valid solution
population = next_population
# Execution
best_sequence, max_profit = evolutionary_algorithm(jobs)
print("Best sequence:", best_sequence)
print("Maximum benefit:", max_profit)