-
Notifications
You must be signed in to change notification settings - Fork 0
/
ml_play.py
90 lines (76 loc) · 3.02 KB
/
ml_play.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
"""
The template of the script for the machine learning process in game pingpong
"""
# Import the necessary modules and classes
from mlgame.communication import ml as comm
import os
import pickle
import numpy as np
def direction(esti, curPos):
if curPos + 20 < esti - 5: return 1 #right
elif curPos + 20 > esti + 5: return 2 #left
else: return 0
def ml_loop(side: str):
"""
The main loop for the machine learning process
The `side` parameter can be used for switch the code for either of both sides,
so you can write the code for both sides in the same script. Such as:
```python
if side == "1P":
ml_loop_for_1P()
else:
ml_loop_for_2P()
```
@param side The side which this script is executed for. Either "1P" or "2P".
"""
# === Here is the execution order of the loop === #
# 1. Put the initialization code here
ball_served = False
model = []
if side == "1P":
with open(os.path.join(os.path.dirname(__file__), 'save', 'model1'), 'rb') as f:
model = pickle.load(f)
else:
with open(os.path.join(os.path.dirname(__file__), 'save', 'model2'), 'rb') as f:
model = pickle.load(f)
# 2. Inform the game process that ml process is ready
comm.ml_ready()
# 3. Start an endless loop
while True:
# 3.1. Receive the scene information sent from the game process
scene_info = comm.recv_from_game()
feature = [scene_info["ball"][0],
scene_info["ball"][1],
scene_info["ball_speed"][0],
scene_info["ball_speed"][1],
scene_info["blocker"][0]]
feature = np.array(feature).reshape((-1,5))
plt = 0
if side == "1P":
plt = scene_info["platform_1P"][0]
else:
plt = scene_info["platform_2P"][0]
# 3.2. If either of two sides wins the game, do the updating or
# resetting stuff and inform the game process when the ml process
# is ready.
if scene_info["status"] != "GAME_ALIVE":
# Do some updating or resetting stuff
ball_served = False
# 3.2.1 Inform the game process that
# the ml process is ready for the next round
comm.ml_ready()
continue
# 3.3 Put the code here to handle the scene information
esti = model.predict(feature)
dire = direction(esti, plt)
# 3.4 Send the instruction for this frame to the game process
if not ball_served:
comm.send_to_game({"frame": scene_info["frame"], "command": "SERVE_TO_LEFT"})
ball_served = True
else:
if dire == 1:
comm.send_to_game({"frame": scene_info["frame"], "command": "MOVE_RIGHT"})
elif dire == 2:
comm.send_to_game({"frame": scene_info["frame"], "command": "MOVE_LEFT"})
else:
comm.send_to_game({"frame": scene_info["frame"], "command": "NONE"})