-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain.py
146 lines (120 loc) · 3.87 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# To play yourself click enter
# To train car models click with the mouse
# You can move the car with the arrows and brake with space
import pyglet
from pyglet.window import FPSDisplay
from Car import Car
from Car_Map import CarMap
from Collision import Collision
from Keyboard_helper import Keyboard_helper
from ai import Ai
from ai_Tests import Net
import math
import torch
from pyglet.window import mouse
import glob, os
import random
# change car model
car_model = 'i7437'
glob_frame = 0
saved_car_count = 0
red = [255, 0, 0, 0]
green = [0, 255, 0, 0]
window = pyglet.window.Window(width=800,height=600)
label = pyglet.text.Label(str(0),
font_name='Times New Roman',
font_size=36,
x=window.width//2, y=window.height//2 + 30,
anchor_x='center', anchor_y='center')
label2 = pyglet.text.Label(str(0),
font_name='Times New Roman',
font_size=36,
x=window.width//2, y=window.height//2 - 30,
anchor_x='center', anchor_y='center')
def ai_game(frames, ai, net, in_map, out_map):
global car
car = Car()
best_score = 0
for i in range(frames):
f = car.rays(ai, net, in_map, out_map, False)
keys.ai_keys(f)
update_frames(1)
best_score = max(car.score,best_score)
return (best_score + car.score)/2
def net_tests(num_nets,algo):
global net
global saved_car_count
os.chdir("models")
models = glob.glob("*.txt")
for i in range(num_nets):
net = Net()
if algo == 1:
m1 = random.choice(models)
# m1 = models[0]
net.load_state_dict(torch.load(m1, map_location=device))
net.change_weights(random.uniform(0,1))
net.double()
score = ai_game(120 + saved_car_count*3, ai, net, map.in_map, map.out_map)
need_to_save = 1000 + 10 * saved_car_count
# print score
# you can decide minimum score to save the model
if True and score > need_to_save:
name = 'g' + str(score) + '.txt'
torch.save(net.state_dict(), name)
models.append(name)
# print score
saved_car_count+=1
print saved_car_count,i,score,need_to_save
@window.event
def on_mouse_press(x, y, button, modifiers):
# print(net.forward(torch.zeros(13)))
net_tests(2,algo=0)
net_tests(2,algo=1)
@window.event
def on_key_press(symbol, modifiers):
keys.key_press(symbol,car)
@window.event
def on_key_release(symbol, modifiers):
keys.key_release(symbol)
def update_frames(dt):
car.update(keys)
if collision.car_map_collision(car.get_points(),map):
car.red_bool = True
car.score -= 10 + saved_car_count
else:
car.red_bool = False
score_points = map.score_points((car.last_score + 1) % 8)
if collision.car_line_collision(car.get_points(),score_points[0],score_points[1],score_points[2],score_points[3]):
car.score += 500
car.last_score += 1
car.score -= 1
@window.event
def on_draw():
pyglet.gl.glClearColor(0,0,0,0)
window.clear()
pyglet.gl.glClearColor(0, 50, 0, 0)
map.draw(score_activate=(car.last_score+1)%8)
car.draw()
label.text = str(car.score)
label.draw()
global glob_frame
glob_frame+=1
label2.text = str(glob_frame)
label2.draw()
if not car.player_play:
f = car.rays(ai,net,map.in_map,map.out_map,True)
keys.ai_keys(f)
# print f
fps_display.draw()
fps_display = FPSDisplay(window)
map = CarMap()
ai = Ai()
net = Net()
device = torch.device('cpu')
net.load_state_dict(torch.load('models/'+car_model+'.txt', map_location=device))
net.double()
car = Car()
keys = Keyboard_helper()
collision = Collision()
pyglet.clock.schedule_interval(update_frames,1/24.0)
pyglet.app.run()