-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathGame.py
More file actions
53 lines (42 loc) · 1.46 KB
/
Copy pathGame.py
File metadata and controls
53 lines (42 loc) · 1.46 KB
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
from Map import Map
import argparse
from evolutionary_trainer import EvolutionaryTrainer
from reinforcement_trainer import ReinforcementTrainer
class Game:
evo_auto_play = False
def __init__(self):
self.map = Map()
self.colliders = self.map.collider_lines
self.wall_rects = self.map.wall_rects
self.result_file = '.gif'
self.best = 0
def run_reinfocement(self):
trainer = ReinforcementTrainer(self.map)
trainer.train()
def run_evo(self):
trainer = EvolutionaryTrainer(self.map)
trainer.train()
def str2bool(v):
if isinstance(v, bool):
return v
if v.lower() in ('yes', 'true', 't', 'y', '1'):
return True
elif v.lower() in ('no', 'false', 'f', 'n', '0'):
return False
else:
raise argparse.ArgumentTypeError('Boolean value expected.')
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--rl', type=str2bool, default=False, help='Will train the car with reinforcement learning, \
otherwise will train the car with genetic algorithm')
parser.add_argument('--auto_play', type=str2bool, default=False, help='Will play the gif after each generation automatically, \
only applicable for evolutionary method')
opt = parser.parse_args()
if opt.auto_play:
Game.evo_auto_play = True
if opt.rl:
Game().run_reinfocement()
else:
Game().run_evo()
if __name__ == "__main__":
main()