-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
154 lines (128 loc) · 3.45 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
146
147
148
149
150
151
152
import gym
import torch
import torch.nn as nn
from torch.autograd import Variable
import torchvision
import torch.backends.cudnn as cudnn
from utils import *
from agent import *
import os
import numpy as np
import matplotlib.pyplot as plt
import csv
LongTensor = torch.LongTensor
FloatTensor = torch.FloatTensor
env = gym.make('Tennis-v0')
#env = NoopResetEnv(env)
#env = MaxAndSkipEnv(env)
#print(env.action_space.n)
#print(env.observation_space.shape[0])
N_ACT = env.action_space.n
#N_OBS = env.observation_space.shape[0]
history_m = 3
lr = 0.00025
epsilon = 1.0
epsilon_bound = 0.01
gamma = 0.99
replace_iter = 10000
batch_size = 32
buffer_size = 85000
EPISODE = 20000
TEST = False
skip_num = 3
agent = DQN_agent(N_ACT, history_m, lr, epsilon, epsilon_bound, gamma, replace_iter, batch_size, buffer_size)
def play_game(EPISODE):
return_writer = csv.writer(open("./Return.csv", "w"))
summary_writer = csv.writer(open("./training_log.csv", "w"))
for episode in range(EPISODE):
done = False
R = 0
num_step = 0
pass_time = 0
if TEST:
obs = env.reset()
else:
obs = preprocess(np_to_pil(env.reset()))
obs = np.stack([obs]*3, axis = 0)
AGENT_SCORE = 0
OPPO_SCORE = 0
AGENT_SCORE_per = 0
OPPO_SCORE_per = 0
SWITCH_SIDE = 1
while not done:
#env.render()
if TEST:
action = agent.select_action(Variable(torch.Tensor([obs])).cuda())
else:
with torch.no_grad():
S = torch.Tensor(obs)
MaxQ, action = agent.select_action(Variable(S.view(1, 3, 84, 84)).cuda())
obs_buffer = []
reward_buffer = 0.0
org_reward_buffer = 0.0
for frame_num in range(skip_num):
obs_, reward, done, _ = env.step(action[0])
obs_ = preprocess(np_to_pil(obs_))
obs_buffer.append(obs_)
if reward == -1:
OPPO_SCORE += 1
OPPO_SCORE_per += 1
elif reward == 1:
AGENT_SCORE += 1
AGENT_SCORE_per += 1
reward_buffer += reward
if done:
for i in range(skip_num - frame_num - 1):
obs_buffer.append(obs_)
break
case = check_serve(action[0], pass_time)
serve_reward = 0
if case == -1:
pass_time = -1
elif case == 1:
serve_reward = 1
pass_time = -1
elif case == 2:
pass_time += 1
elif case == 3:
serve_reward = -1
pass_time = -1
if reward_buffer != 0:
pass_time = 0
if check_end(AGENT_SCORE_per, OPPO_SCORE_per):
SWITCH_SIDE += 1
AGENT_SCORE_per = 0
OPPO_SCORE_per = 0
org_reward_buffer += reward_buffer
reward_buffer += 0.5 * serve_reward * (0 if SWITCH_SIDE%2 == 0 else 1)
obs_ = np.stack(obs_buffer, axis = 0)
if TEST:
transition = [
FloatTensor([obs]),
LongTensor(action),
FloatTensor([reward]),
FloatTensor([obs_]),
done
]
else:
transition = [
obs.reshape(3, 84, 84),#obs.view(1, 4, 84, 84),#np_obs,
action,#LongTensor(action),
reward_buffer,#FloatTensor([reward_buffer]),#np.array([reward]),
obs_.reshape(3, 84, 84),#obs_.view(1, 4, 84, 84),#np_obs_,
done
]
agent.store_transition(transition)
loss, grad_norm = agent.train('DDQN')
obs = obs_
R += org_reward_buffer
num_step += 1
summary_writer.writerow([MaxQ[0], action[0], loss, grad_norm])
print('Episode: %3d,\tReturn: %f,\tStep: %f' %(episode, R, num_step))
print('OPPONENT: %f' %(OPPO_SCORE))
print('AGENT : %f' %(AGENT_SCORE))
if episode % 200 == 0:
agent.save_param(episode)
return_writer.writerow([R, num_step])
if __name__ == "__main__":
play_game(EPISODE)