-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
62 lines (51 loc) · 2.16 KB
/
test.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
#-*- coding:utf-8
import sys
import os
os.environ['CUDA_VISIBLE_DEVICES'] = ""
stdi, stdo, stde = sys.stdin, sys.stdout, sys.stderr
reload(sys)
sys.setdefaultencoding('utf-8')
sys.stdin, sys.stdout, sys.stderr = stdi, stdo, stde
import GameEnv
import random
import numpy as np
import DQN
gameEnv = GameEnv.GlodenFlower([20000,20000])
RLModel = DQN.DQN(embedding_size=50,sequence_length=20,learning_rate=0.01,batch_size=1000)
gameEnv.debug = False
memory = []
for episode in range(500000):
# 初始化环境
gameEnv.reset()
playerI = gameEnv.getStartTurn()
observation_this = [[],gameEnv.playerCards["A"],gameEnv.personStatus["A"]]
if playerI == "B":
availble_actions = gameEnv.chooseAvailbleAction(playerI)
action,_ = RLModel.choose_action(np.array([observation_this]),availble_actions)
#print (playerI, action, gameEnv.deskMoney, gameEnv.nowPrice)
observation_next, reward, done = gameEnv.step(action, "B")
playerI = "A"
if done:
continue
while True:
# DQN 根据观测值选择行为
availble_actions = gameEnv.chooseAvailbleAction(playerI)
action,_ = RLModel.choose_action(np.array([observation_this]),availble_actions)
# 环境根据行为给出下一个 state, reward, 是否终止
#print (playerI, action, gameEnv.deskMoney, gameEnv.nowPrice,gameEnv.personStatus[playerI])
observation_next, reward, done = gameEnv.stepA(action,RLModel)
# DQN 存储记忆
# RL.store_transition(observation, action, reward)
RLModel.store_transition(observation_this, action, reward,done,observation_next,gameEnv.playerCards["B"],gameEnv.personStatus["A"],gameEnv.nowPrice)
# 控制学习起始时间和频率 (先累积一些记忆再开始学习)
if (episode > 100000):
if episode % 10000 == 0 :print "episode",episode
if episode % 10 == 0:
RLModel.train(RLModel.experience_replay())
# 将下一个 state_ 变为 下次循环的 state
observation_this = observation_next
# 如果终止, 就跳出循环
if done:
break
# end of game
print('game over')