-
Notifications
You must be signed in to change notification settings - Fork 0
/
genetic_game.py
69 lines (55 loc) · 1.86 KB
/
genetic_game.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import sys
import random
from connect_4 import *
from genetic_game_utils import *
import pyglet as pg
# Initialize the gridcd
grid = Connect4Grid(900, 700)
# Parameters for Genetic Algorithm
population_size = 1000
generations = 1000
crossover_prob = 0.7
mutation_prob = 0.1
# Initialize population
population = initialize_population(population_size)
# Main game loop
generation = 0
best_board = None
best_fitness = 0
optimal_found = False
def update(dt):
global generation, best_board, best_fitness, optimal_found, population
if optimal_found or generation >= generations:
return
# Run one generation of Genetic Algorithm
fitnesses = [fitness(board) for board in population]
if max(fitnesses) == 42:
print("Optimal solution found!")
optimal_found = True
grid.gen_label.text = f"Optimal Solution Found in {generation}th Generation"
return
parents = select_parents(population, fitnesses)
next_population = []
for i in range(0, population_size, 2):
parent1, parent2 = parents[i], parents[i + 1]
if random.random() < crossover_prob:
child1, child2 = crossover(parent1, parent2)
else:
child1, child2 = parent1, parent2
next_population.append(child1)
next_population.append(child2)
population = [mutate(board) if random.random() < mutation_prob else board for board in next_population]
# Find best board in current generation
best_fitness = 0
for board in population:
curr_fitness = fitness(board)
if curr_fitness > best_fitness:
best_fitness = curr_fitness
best_board = board
# Update the grid
grid.update(generation, best_fitness)
grid.draw_grid(best_board)
generation += 1
if __name__ == "__main__":
pg.clock.schedule_interval(update, 1/120.0)
grid.run()