-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_winning_games_db.py
301 lines (244 loc) · 10.7 KB
/
generate_winning_games_db.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
from math import perm
import pickle
from re import A
from tabnanny import check
import os
from ray.rllib.models import ModelCatalog
from ray.rllib.agents.ppo.ppo import PPOTrainer
from ray.rllib.agents.ppo.ppo import DEFAULT_CONFIG as ppo_config
from ray.tune.registry import register_env
from ray.rllib.env.wrappers.pettingzoo_env import PettingZooEnv
from for_sale_env_r1.fs_first_round import env as fs_env
from ray.rllib.utils.torch_utils import FLOAT_MIN, FLOAT_MAX
from ray.rllib.utils.framework import try_import_torch
from ray.rllib.models.torch.torch_modelv2 import TorchModelV2
import random
import numpy as np
from collections import defaultdict, Counter
import tqdm
from itertools import permutations
torch, nn = try_import_torch()
rounds = 4
stack_number = int((3 * rounds) * 1.25)
observation_shape = (6 + rounds) * 3 + 4 + stack_number
class DQN(TorchModelV2, nn.Module):
def __init__(self, observational_space, action_spaces, num_outputs, *args, **kwargs):
TorchModelV2.__init__(self, observational_space, action_spaces, num_outputs, *args, **kwargs)
nn.Module.__init__(self)
self.model = nn.Sequential(
nn.Linear(observation_shape, 256),
nn.ReLU(),
nn.Linear(256, 256),
nn.ReLU(),
nn.Linear(256, 256),
nn.ReLU())
self.policy_fn = nn.Linear(256, num_outputs)
self.value_fn = nn.Linear(256, 1)
# Called with either one element to determine next action, or a batch
# during optimization. Returns tensor([[left0exp,right0exp]...]).
def forward(self, input_dict, state, seq_lens):
action_mask = input_dict["obs"]["action_mask"]
x = input_dict["obs"]["observation"]
model_out = self.model(x)
self._value_out = self.value_fn(model_out)
inf_mask = torch.clamp(torch.log(action_mask), FLOAT_MIN, FLOAT_MAX)
# print(self.policy_fn(model_out) + inf_mask)
return self.policy_fn(model_out) + inf_mask, state
def value_function(self):
return self._value_out.flatten()
# class DQN(TorchModelV2, nn.Module):
# def __init__(self, observational_space, action_spaces, num_outputs, *args, **kwargs):
# TorchModelV2.__init__(self, observational_space, action_spaces, num_outputs, *args, **kwargs)
# nn.Module.__init__(self)
# self.model = nn.Sequential(
# nn.Linear(42, 256),
# nn.ReLU(),
# nn.Linear(256, 256),
# nn.ReLU())
# self.policy_fn = nn.Linear(256, num_outputs)
# self.value_fn = nn.Linear(256, 1)
# # Called with either one element to determine next action, or a batch
# # during optimization. Returns tensor([[left0exp,right0exp]...]).
# def forward(self, input_dict, state, seq_lens):
# action_mask = input_dict["obs"]["action_mask"]
# x = input_dict["obs"]["observation"]
# model_out = self.model(x)
# self._value_out = self.value_fn(model_out)
# inf_mask = torch.clamp(torch.log(action_mask), FLOAT_MIN, FLOAT_MAX)
# # print(self.policy_fn(model_out) + inf_mask)
# return self.policy_fn(model_out) + inf_mask, state
# def value_function(self):
# return self._value_out.flatten()
def env_creator(args):
env = fs_env(rounds=4)
# env = ss.color_reduction_v0(env, mode='B')
# env = ss.dtype_v0(env, 'float32')
# env = ss.resize_v0(env, x_size=84, y_size=84)
# env = ss.frame_stack_v1(env, 3)
# env = ss.normalize_obs_v0(env, env_min=0, env_max=1)
return env
env_name = "fs_1r_4_rounds_game_PPO_v0"
register_env(env_name, lambda config: PettingZooEnv(env_creator(config)))
test_env = PettingZooEnv(env_creator({}))
obs_space = test_env.observation_space
act_space = test_env.action_space
ModelCatalog.register_custom_model("DQN", DQN)
def gen_policy(i):
config = {
"model": {
"custom_model": "DQN",
},
"gamma": 0.99,
}
return (None, obs_space, act_space, config)
policies = {"policy_0": gen_policy(0)}
policy_ids = list(policies.keys())
config = ppo_config.copy()
config["env"] = env_name
config["framework"] = "torch"
config["num_workers"] = 0
config["multiagent"] = {
"policies": policies,
"policy_mapping_fn": (
lambda agent_id: policy_ids[0]),
}
config_icm = ppo_config.copy()
config_icm["env"] = env_name
config_icm["framework"] = "torch"
config_icm["num_workers"] = 0
config_icm["multiagent"] = {
"policies": policies,
"policy_mapping_fn": (
lambda agent_id: policy_ids[0]),
}
config_icm["exploration_config"] = {
"type": "Curiosity", # <- Use the Curiosity module for exploring.
# Weight for intrinsic rewards before being added to extrinsic ones.
"eta": 1.0,
"lr": 0.001, # Learning rate of the curiosity (ICM) module.
"feature_dim": 288, # Dimensionality of the generated feature vectors.
# Setup of the feature net (used to encode observations into feature (latent) vectors).
"feature_net_config": {
"fcnet_hiddens": [],
"fcnet_activation": "relu",
},
"inverse_net_hiddens": [256], # Hidden layers of the "inverse" model.
"inverse_net_activation": "relu", # Activation of the "inverse" model.
"forward_net_hiddens": [256], # Hidden layers of the "forward" model.
"forward_net_activation": "relu", # Activation of the "forward" model.
# Weight for the "forward" loss (beta) over the "inverse" loss (1.0 - beta).
"beta": 0.2,
# Specify, which exploration sub-type to use (usually, the algo's "default"
# exploration, e.g. EpsilonGreedy for DQN, StochasticSampling for PG/SAC).
"sub_exploration": {
"type": "EpsilonGreedy",
}
}
# checkpoint_file = "/home/jaoi/ray_results/fs_1r_3_rounds_game_ICM_v1/PPO/PPO_fs_1r_3_rounds_game_ICM_v1_4fe38_00000_0_2022-04-22_13-14-01/checkpoint_012090/checkpoint-12090"
# checkpoint_file = "/home/jaoi/imported_models/PPO3r_v0/checkpoint_025000/checkpoint-25000"
checkpoint_file = "/home/jaoi/imported_models/PPO4r_v0/checkpoint_025000/checkpoint-25000"
checkpoint_file_1 = "/home/jaoi/imported_models/PPO4r_v1/checkpoint_025000/checkpoint-25000"
checkpoint_file_2 = "/home/jaoi/imported_models/ICM4r_v0/checkpoint_025000/checkpoint-25000"
if os.path.isfile(checkpoint_file):
print("its here")
else:
print("its not here")
# with open(checkpoint_file, "rb") as fp:
# d = pickle.load(fp)
# print(type(d))
# print(list(d.keys()))
# print(list(d["train_exec_impl"]["info"]
# ["learner"]["policy_0"]["model"].keys()))
# print(type(d["train_exec_impl"]["info"]["learner"]["policy_0"]["model"]))
def rand_policy(observation, agent):
action = random.choice(np.flatnonzero(observation['action_mask']))
return action
# player_models = {}
# player_models["ICMv0"] = PPOTrainer(config=config_icm)
# player_models["ICMv0"].restore(checkpoint_file_2)
# player_models["PPOv0"] = PPOTrainer(config=config)
# player_models["PPOv0"].restore(checkpoint_file)
# player_models["PPOv1"] = PPOTrainer(config=config)
# player_models["PPOv1"].restore(checkpoint_file_1)
# model_names = ["ICMv0", "PPOv0", "PPOv1"]
# players_perms = permutations(model_names)
def compare_agents(model_names, player_models, env, games):
model_perms = permutations(model_names)
env.reset()
agent_list = list(env.agents)
for perm in model_perms:
agent_to_model_dict = {agent_list[i]: perm[i] for i in range(len(agent_list))}
games_to_save = []
c = Counter()
for game in tqdm.tqdm(range(games)):
g = defaultdict(list)
env.reset()
winning_models = []
for agent in env.agent_iter():
observation, reward, done, info = env.last()
if done:
action = None
elif agent_to_model_dict[agent] == "ICMv0":
# action = rand_policy(observation, agent)
action, _, _ = player_models["PPOv0"].get_policy("policy_0").compute_single_action(observation)
else:
action, _, _ = player_models[agent_to_model_dict[agent]].get_policy("policy_0").compute_single_action(observation)
if action != None:
g[agent_to_model_dict[agent]].append((observation["observation"], action))
env.step(action)
if reward == 1:
winning_models.append(agent_to_model_dict[agent])
c[agent_to_model_dict[agent]] += 1
modelstat_to_save = random.choice(winning_models)
games_to_save.extend(g[modelstat_to_save])
print(agent_to_model_dict)
print(c)
if __name__ == "__main__":
# config = ppo_config.copy()
# config["env"] = env_name
# config["framework"] = "torch"
PPOagent = PPOTrainer(config=config_icm)
PPOagent.restore(checkpoint_file_2)
env = fs_env(rounds=4)
smart_agent = "player"
env.reset()
games = 100000
# compare_agents(model_names, player_models, env, games)
games_to_save = []
c = Counter()
for game in tqdm.tqdm(range(games)):
g = defaultdict(list)
env.reset()
winning_agents = []
for agent in env.agent_iter():
observation, reward, done, info = env.last()
if done:
action = None
# env.render()
# elif agent != smart_agent:
# action = rand_policy(observation, agent)
# action, _, _ = PPOagent.get_policy("policy_0").compute_single_action(observation)
# print(observation["observation"])
# env.render()
# action = int(input())
# while action not in observation["action_mask"]:
# print("you can't do {}".format(action))
# action = int(input())
else:
# action, _, _ = player_models["ICMv0"].get_policy("policy_0").compute_single_action(observation)
action, _, _ = PPOagent.get_policy("policy_0").compute_single_action(observation)
if action != None:
g[agent].append((observation["observation"], action))
env.step(action)
if reward == 1:
winning_agents.append(agent)
c[agent] += 1
agent_to_save = random.choice(winning_agents)
games_to_save.extend(g[agent_to_save])
print(c)
with open("/home/jaoi/master22/pet_for_sale/winning_games_db/4ICM_{}_games.pkl".format(games), "wb") as fp:
pickle.dump(np.array(games_to_save, dtype=object), fp)
# print(agent, action)
# print(agent.get_default_policy_class().get_weights())
# agent.compute_action(test_env.reset())
# print(agent.compute_single_action(test_env.reset()["player_0"]))