forked from NeuralMMO/environment
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathForgePCG.py
134 lines (105 loc) · 3.14 KB
/
ForgePCG.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
''' way way deprecated
trains outer loop of rl agents to modify the map
'''
#My favorite debugging macro
from pdb import set_trace as T
from fire import Fire
import sys
import numpy as np
import torch
import ray
from ray import rllib
from forge.ethyr.torch import utils
import projekt
from projekt import realm
from pcgrl import rlutils
import pcg
from pcg import PCG
import pcgrl
from pcgrl.game.io.action import static
#Instantiate a new environment
def createPCGEnv(pcg_config):
#return projekt.Realm(config)
return PCG(pcg_config)
def createEnv(config):
return projekt.Realm(config)
#Map agentID to policyID -- requires config global
def mapPCGPolicy(agentID):
return 'policy_pcg_{}'.format(agentID % pcg_config.NPOLICIES)
#Generate RLlib policies
def createPolicies(config):
obs = projekt.realm.observationSpace(config)
atns = projekt.realm.actionSpace(config)
policies = {}
for i in range(config.NPOLICIES):
params = {
"agent_id": i,
"obs_space_dict": obs,
"act_space_dict": atns}
key = mapPolicy(i)
policies[key] = (None, obs, atns, params)
return policies
def createPCGPolicies(pcg_config):
obs = static.observationSpace(pcg_config)
atns = static.actionSpace(pcg_config)
policies = {}
for i in range(pcg_config.NPOLICIES):
params = {
"agent_id": i,
"obs_space_dict": obs,
"act_space_dict": atns}
key = mapPCGPolicy(i)
policies[key] = (None, obs, atns, params)
return policies
if __name__ == '__main__':
#Setup ray
torch.set_num_threads(1)
ray.init()
#Built config with CLI overrides
pcg_config = pcgrl.Config()
pcg_config.STIM = 4
if len(sys.argv) > 1:
sys.argv.insert(1, 'override')
Fire(pcg_config)
#RLlib registry
rllib.models.ModelCatalog.register_custom_model(
'test_pcg_model', pcgrl.PCGPolicy)
ray.tune.registry.register_env("custom_pcg", createPCGEnv)
#Create policies
pcg_policies = createPCGPolicies(pcg_config)
print('top level config', vars(pcg_config))
#Instantiate monolithic RLlib Trainer object.
trainer = rlutils.SanePPOPCGTrainer(
env="custom_pcg", path='experiment_pcg', config={
'num_workers': 1,
'num_gpus': 1,
'num_envs_per_worker': 1,
'train_batch_size': 4000,
'rollout_fragment_length': 100,
'sgd_minibatch_size': 128,
'num_sgd_iter': 1,
'use_pytorch': True,
'horizon': np.inf,
'soft_horizon': False,
'no_done_at_end': False,
'env_config': {
'config': pcg_config
},
'multiagent': {
"policies": pcg_policies,
"policy_mapping_fn": mapPCGPolicy
},
'model': {
'custom_model': 'test_pcg_model',
'custom_options': {'config': pcg_config}
},
})
# trainer.defaultModel().cuda()
#Print model size
utils.modelSize(trainer.model('policy_pcg_0'))
trainer.restore(pcg_config.MODEL)
if pcg_config.RENDER:
env = createPCGEnv({'config': pcg_config})
pcgrl.Evaluator(trainer, env, pcg_config).run()
else:
trainer.train()