-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplay.py
141 lines (117 loc) · 4.57 KB
/
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
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
# play warhammer!
import pickle
import os
import sys
from gym_mod.envs.warhamEnv import *
import warnings
warnings.filterwarnings("ignore")
from model.DQN import *
from model.utils import *
from gym_mod.engine.GUIinteract import *
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
if sys.argv[1] == "None":
savePath = "models/"
folders = os.listdir(savePath)
envs = []
modelpth = []
for i in folders:
if os.path.isdir(savePath+i):
fs = os.listdir(savePath+i)
for j in fs:
if j[-len(".pickle"):] == ".pickle":
envs.append(savePath+i+"/"+j)
elif j[-len(".pth"):] == ".pth":
modelpth.append(savePath+i+"/"+j)
envs.sort(key=lambda x: os.path.getmtime(x))
modelpth.sort()
checkpoint = torch.load(modelpth[-1])
#print("Playing with environment saved here: ", envs[-1])
with open(envs[-1], 'rb') as f:
env, model, enemy = pickle.load(f)
else:
#print("Playing with model saved here: ", sys.argv[1])
with open(sys.argv[1], 'rb') as f:
env, model, enemy = pickle.load(f)
f = str(sys.argv[1])
modelpth = f[:-len("pickle")]+"pth"
checkpoint = torch.load(modelpth)
playInGUI = False
if sys.argv[2] == "True":
playInGUI = True
deployType = ["Search and Destroy", "Hammer and Anvil", "Dawn of War"]
deployChang = np.random.choice(deployType)
if playInGUI == False:
print("Deployment Type: ", deployChang)
else:
sendToGUI("Deployment Type: {}".format(deployChang))
for m in model:
m.deployUnit(deployChang, "model")
i = 0
for e in enemy:
sendToGUI("Deploying Unit {} or {}".format(i, e.showUnitData()["Name"]))
e.deployUnit(deployChang, "player", GUI=playInGUI, choose = True)
i += 1
state, info = env.reset(m=model, e=enemy)
n_actions = [5,2,len(info["player health"]), len(info["player health"]), 5, len(info["model health"])]
for i in range(len(model)):
n_actions.append(12)
n_observations = len(state)
policy_net = DQN(n_observations, n_actions).to(device)
target_net = DQN(n_observations, n_actions).to(device)
optimizer = torch.optim.Adam(policy_net.parameters())
policy_net.load_state_dict(checkpoint['policy_net'])
target_net.load_state_dict(checkpoint['target_net'])
optimizer.load_state_dict(checkpoint['optimizer'])
policy_net.eval()
target_net.eval()
isdone = False
i = 0
if playInGUI == True:
env.reset(m=model, e=enemy, playType = playInGUI, Type="big", trunc=True)
else:
env.reset(m=model, e=enemy, playType = playInGUI, Type="big", trunc=False)
reward = 0
if playInGUI == False:
print("\nInstructions:\n")
print("Observe board at board.txt or click the 'Show Board' button")
print("The popup from the button automatically updates, so you won't need to keep pressing it")
print("The player (you) controls units starting with 1 (i.e. 11, 12, etc)")
print("The model controls units starting with 2 (i.e. 21, 22, etc)\n")
else:
sendToGUI("\nInstructions:\nObserve board at board.txt or click the 'Show Board' button\nThe popup from the button automatically updates, so you won't need to keep pressing it\nThe player (you) controls units starting with 1 (i.e. 11, 12, etc)\nThe model controls units starting with 2 (i.e. 21, 22, etc)\n")
while isdone == False:
done, info = env.player()
state = torch.tensor(state, dtype=torch.float32, device=device).unsqueeze(0)
action = select_action(env, state, i, policy_net, len(model))
action_dict = convertToDict(action)
if done != True:
next_observation, reward, done, _, info = env.step(action_dict)
reward = torch.tensor([reward], device=device)
unit_health = info["model health"]
enemy_health = info["player health"]
inAttack = info["in attack"]
board = env.render()
message = "Iteration {} ended with reward {}, Player health {}, Model health {}".format(i, reward, enemy_health, unit_health)
if playInGUI == False:
print(message)
else:
sendToGUI(message)
next_state = torch.tensor(next_observation, dtype=torch.float32, device=device).unsqueeze(0)
state = next_state
if done == True:
if reward > 0:
if playInGUI == False:
print("model won!")
else:
sendToGUI("model won!")
else:
if playInGUI == False:
print("you won!")
else:
sendToGUI("you won!")
isdone = True
i+=1