-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
63 lines (56 loc) · 1.72 KB
/
main.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
from pprint import pprint
from helpers import check_winners
from helpers import crossover
from helpers import Players
from helpers import populate
from helpers import select
from parser import parser
def play(
players: Players,
fitness_cutoff: int,
mutation_rate: float,
win_percent: float,
max_iter: int,
size: int,
) -> None:
"""
Runs the game.
Checks for winners, and if the win condition is not met, a new generation
will be created and tested again, and this will continue until the win
condition is met or the max_iter is exceeded.
Inputs:
players: List of players for Generation 0.
fitness_cutoff: Top X scoring players to be selected.
mutation_rate: Percent chance that a gene will randomly change to any
other possible value.
win_percent: Percent of players who must be winners in order to end the
game.
max_iter: Maximum number of generations before quitting, even the win
condition is not met.
"""
generation = 0
winners = False
while not winners:
print(f"Generation: {generation}")
pprint(players)
winners = check_winners(players, win_percent)
if winners:
print(f"Generation {generation} wins!")
return
survivors = select(players, fitness_cutoff)
players = crossover(survivors, size, mutation_rate)
generation += 1
if generation > max_iter:
print("Failed :(")
break
if __name__ == "__main__":
args = parser.parse_args()
players = populate(args.size, args.mutation_rate)
play(
players,
args.fitness_cutoff,
args.mutation_rate,
args.win_percent,
args.max_iter,
args.size,
)