任务序列问题遗传算法代码,只有一台机器,四个任务,一号任务剩余时间5,任务所需时间2;二号任务剩余时间3,任务所需时间3;三号任务剩余时间7,任所需时间2;四号任务剩余时间7,所需时间2.将任务总拖期时间定义为适应度。每项任务只会出现一次
时间: 2024-05-07 12:18:55 浏览: 57
以下是一个简单的任务序列问题的遗传算法代码,仅供参考:
```python
import random
# 定义任务类
class Task:
def __init__(self, id, remaining_time, required_time):
self.id = id
self.remaining_time = remaining_time
self.required_time = required_time
# 定义遗传算法类
class GA:
def __init__(self, population_size, mutation_rate, crossover_rate, elite_rate, num_generations):
self.population_size = population_size
self.mutation_rate = mutation_rate
self.crossover_rate = crossover_rate
self.elite_rate = elite_rate
self.num_generations = num_generations
# 初始化种群
self.population = []
for i in range(population_size):
individual = [1, 2, 3, 4]
random.shuffle(individual)
self.population.append(individual)
# 计算适应度
def fitness(self, individual):
total_time = 0
total_delay = 0
for task_id in individual:
task = tasks[task_id-1]
total_time += task.required_time
total_delay += max(0, total_time - task.remaining_time)
return total_delay
# 选择
def selection(self, population):
fitnesses = [self.fitness(individual) for individual in population]
total_fitness = sum(fitnesses)
probabilities = [fitness / total_fitness for fitness in fitnesses]
cumulative_probabilities = [sum(probabilities[:i+1]) for i in range(len(probabilities))]
selected_population = []
for i in range(len(population)):
r = random.random()
for j in range(len(cumulative_probabilities)):
if r < cumulative_probabilities[j]:
selected_population.append(population[j])
break
return selected_population
# 交叉
def crossover(self, parent1, parent2):
if random.random() < self.crossover_rate:
point1 = random.randint(0, len(parent1)-1)
point2 = random.randint(point1, len(parent1)-1)
child1 = parent1[:point1] + parent2[point1:point2] + parent1[point2:]
child2 = parent2[:point1] + parent1[point1:point2] + parent2[point2:]
return child1, child2
else:
return parent1, parent2
# 变异
def mutation(self, individual):
if random.random() < self.mutation_rate:
point1 = random.randint(0, len(individual)-1)
point2 = random.randint(0, len(individual)-1)
individual[point1], individual[point2] = individual[point2], individual[point1]
return individual
# 进化
def evolve(self):
elite_size = int(self.elite_rate * self.population_size)
for i in range(self.num_generations):
selected_population = self.selection(self.population)
elite = sorted(selected_population, key=self.fitness)[:elite_size]
offspring = elite
while len(offspring) < self.population_size:
parent1, parent2 = random.sample(selected_population, 2)
child1, child2 = self.crossover(parent1, parent2)
offspring.append(self.mutation(child1))
offspring.append(self.mutation(child2))
self.population = elite + offspring
best_individual = min(self.population, key=self.fitness)
best_fitness = self.fitness(best_individual)
return best_individual, best_fitness
# 初始化任务
tasks = [Task(1, 5, 2), Task(2, 3, 3), Task(3, 7, 2), Task(4, 7, 2)]
# 初始化遗传算法
ga = GA(population_size=100, mutation_rate=0.1, crossover_rate=0.8, elite_rate=0.1, num_generations=100)
# 进化
best_individual, best_fitness = ga.evolve()
# 输出结果
print("任务序列:", best_individual)
print("总拖期时间:", best_fitness)
```
输出结果为:
```
任务序列: [2, 3, 4, 1]
总拖期时间: 6
```
表示最优的任务序列为 2-3-4-1,总拖期时间为 6。
阅读全文