2024-10-18 09:44:43 +02:00
|
|
|
#/bin/python
|
|
|
|
import random
|
|
|
|
|
|
|
|
tasks = [4, 5, 8, 2, 10, 7]
|
|
|
|
sol1=[1, 0, 1, 1, 0, 1]
|
|
|
|
sol2=[0, 0, 1, 0, 1, 1]
|
|
|
|
|
|
|
|
def generate_initial_population(problem_data, k, popsize):
|
|
|
|
return [[random.randint(0, k-1) for _ in range(len(problem_data))] for _ in range(popsize)]
|
|
|
|
|
|
|
|
def fitness(solution, problem_data, k):
|
|
|
|
return max([sum(problem_data[i] for i in range(len(solution)) if solution[i] == j) for j in range(k)])
|
|
|
|
|
|
|
|
def crossover(solutionA, solutionB):
|
|
|
|
return [solutionA[i] if i % 2 == 0 else solutionB[i] for i in range(len(solutionA))]
|
|
|
|
|
|
|
|
def mutation(solution, k):
|
|
|
|
i = random.randint(0,len(solution)-1)
|
|
|
|
solution[i] = random.choice(list({i for i in range(k)} - {solution[i]}))
|
|
|
|
return solution
|