darkor

chatgpt_genetic_algorithm_for_matches

Mar 29th, 2023
544
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.28 KB | None | 0 0
  1. import random
  2.  
  3. # Перелік команд
  4. teams = ['Team 1', 'Team 2', 'Team 3', 'Team 4', 'Team 5', 'Team 6']
  5.  
  6. # Кількість днів на відпочинок між матчами
  7. rest_days = 2
  8.  
  9. # Кількість поколінь генетичного алгоритму
  10. generations = 100
  11.  
  12. # Кількість особин у популяції
  13. population_size = 100
  14.  
  15. # Ймовірність мутації
  16. mutation_rate = 0.1
  17.  
  18.  
  19. # Функція для створення випадкової популяції розкладів
  20. def create_population():
  21.     population = []
  22.     for i in range(population_size):
  23.         schedule = []
  24.         for j in range(len(teams)):
  25.             for k in range(j + 1, len(teams)):
  26.                 match = [teams[j], teams[k], random.randint(0, rest_days)]
  27.                 schedule.append(match)
  28.         random.shuffle(schedule)
  29.         population.append(schedule)
  30.     return population
  31.  
  32.  
  33. # Функція для розрахунку фітнес-функції для розкладу
  34. def calculate_fitness(schedule):
  35.     team_rest_days = {}
  36.     for team in teams:
  37.         team_rest_days[team] = 0
  38.  
  39.     for match in schedule:
  40.         team_rest_days[match[0]] += match[2]
  41.         team_rest_days[match[1]] += match[2]
  42.  
  43.     avg_rest_days = sum(team_rest_days.values()) / len(team_rest_days)
  44.  
  45.     fitness = 1 / (1 + abs(avg_rest_days - max(team_rest_days.values())))
  46.  
  47.     return fitness
  48.  
  49.  
  50. # Функція для селекції найкращих особин з популяції
  51. def selection(population):
  52.     fitness_scores = [(schedule, calculate_fitness(schedule)) for schedule in population]
  53.     fitness_scores.sort(key=lambda x: x[1], reverse=True)
  54.     selected = [schedule for schedule, fitness in fitness_scores[:int(population_size / 2)]]
  55.     return selected
  56.  
  57.  
  58. # Функція для розмноження двох розкладів
  59. def crossover(schedule1, schedule2):
  60.     child = []
  61.     for i in range(len(schedule1)):
  62.         if random.random() < 0.5:
  63.             child.append(schedule1[i])
  64.         else:
  65.             child.append(schedule2[i])
  66.     return child
  67.  
  68.  
  69. # Функція для мутації розкладу
  70. def mutate(schedule):
  71.     for i in range(len(schedule)):
  72.         if random.random() < mutation_rate:
  73.             schedule[i][2] = random.randint(0, rest_days)
  74.     return schedule
  75.  
  76.  
  77. # Генетичний алгоритм для створення розкладу
  78. def genetic_algorithm():
  79.     population = create_population()  # create_population
  80.     for i in range(generations):
  81.         selected = selection(population)  # select the best schedules of the population
  82.         children = []
  83.         for j in range(int(population_size/2)):
  84.             parent1 = random.choice(selected)
  85.             parent2 = random.choice(selected)
  86.             child = crossover(parent1, parent2)  # crossover
  87.             child = mutate(child)  # mutate
  88.             children.append(child)
  89.         population = selected + children
  90.     fitness_scores = [(schedule, calculate_fitness(schedule)) for schedule in population]  # calculate_fitness
  91.     fitness_scores.sort(key=lambda x: x[1], reverse=True)
  92.     best_schedule = fitness_scores[0][0]
  93.     return best_schedule
  94.  
  95.  
  96. print(genetic_algorithm())
  97.  
Advertisement
Add Comment
Please, Sign In to add comment