Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# set logger
magent.utility.init_logger(args.name)
# init the game
env = magent.GridWorld("battle", map_size=args.map_size)
env.set_render_dir("build/render")
# two groups of agents
handles = env.get_handles()
# sample eval observation set
if args.eval:
print("sample eval set...")
env.reset()
generate_map(env, args.map_size, handles)
eval_obs = magent.utility.sample_observation(env, handles, n_obs=2048, step=500)
else:
eval_obs = [None, None]
# init models
names = [args.name + "-a", "battle"]
batch_size = 512
unroll_step = 16
train_freq = 5
models = []
# load opponent
if args.opponent >= 0:
from magent.builtin.tf_model import DeepQNetwork
models.append(magent.ProcessingModel(env, handles[1], names[1], 20000, 0, DeepQNetwork))
models[0].load("data/battle_model", args.opponent)
magent.utility.init_logger(args.name)
# init the game
env = magent.GridWorld(load_config(args.map_size))
env.set_render_dir("build/render")
# two groups of agents
handles = env.get_handles()
# sample eval observation set
eval_obs = [None for _ in range(len(handles))]
if args.eval:
print("sample eval set...")
env.reset()
generate_map(env, args.map_size, handles)
eval_obs = magent.utility.sample_observation(env, handles, 2048, 500)
# load models
batch_size = 256
unroll_step = 8
target_update = 1000
train_freq = 5
if args.alg == 'dqn':
from magent.builtin.tf_model import DeepQNetwork
RLModel = DeepQNetwork
base_args = {'batch_size': batch_size,
'memory_size': 2 ** 20,
'target_update': target_update, 'train_freq': train_freq}
elif args.alg == 'drqn':
from magent.builtin.tf_model import DeepRecurrentQNetwork
RLModel = DeepRecurrentQNetwork
log.getLogger('').addHandler(console)
# init the game
env = magent.GridWorld("battle", map_size=args.map_size)
env.set_render_dir("build/render")
# two groups of agents
handles = env.get_handles()
# sample eval observation set
eval_obs = None
if args.eval:
print("sample eval set...")
env.reset()
generate_map(env, args.map_size, handles)
eval_obs = magent.utility.sample_observation(env, handles, 2048, 500)[0]
# init models
batch_size = 512
unroll_step = 8
target_update = 1200
train_freq = 5
models = []
if args.alg == 'dqn':
from magent.builtin.tf_model import DeepQNetwork
models.append(DeepQNetwork(env, handles[0], args.name,
batch_size=batch_size,
learning_rate=3e-4,
memory_size=2 ** 21, target_update=target_update,
train_freq=train_freq, eval_obs=eval_obs))
elif args.alg == 'drqn':
# init env
env = magent.GridWorld(load_config(map_size=args.map_size))
env.set_render_dir("build/render")
handles = env.get_handles()
food_handle = handles[0]
player_handles = handles[1:]
# sample eval observation set
eval_obs = None
if args.eval:
print("sample eval set...")
env.reset()
generate_map(env, args.map_size, food_handle, player_handles)
eval_obs = magent.utility.sample_observation(env, player_handles, 0, 2048, 500)
# load models
models = [
RLModel(env, player_handles[0], args.name,
batch_size=512, memory_size=2 ** 20, target_update=1000,
train_freq=4, eval_obs=eval_obs)
]
# load saved model
save_dir = "save_model"
if args.load_from is not None:
start_from = args.load_from
print("load models...")
for model in models:
model.load(save_dir, start_from)
else:
# init the game
env = magent.GridWorld("battle", map_size=args.map_size)
env.set_render_dir("build/render")
# two groups of agents
handles = env.get_handles()
# sample eval observation set
eval_obs = [None, None]
if args.eval:
print("sample eval set...")
env.reset()
generate_map(env, args.map_size, handles)
for i in range(len(handles)):
eval_obs[i] = magent.utility.sample_observation(env, handles, 2048, 500)
# load models
batch_size = 256
unroll_step = 8
target_update = 1200
train_freq = 5
if args.alg == 'dqn':
RLModel = DeepQNetwork
base_args = {'batch_size': batch_size,
'memory_size': 2 ** 21, 'learning_rate': 1e-4,
'target_update': target_update, 'train_freq': train_freq}
elif args.alg == 'drqn':
RLModel = DeepRecurrentQNetwork
base_args = {'batch_size': batch_size / unroll_step, 'unroll_step': unroll_step,
'memory_size': 8 * 625, 'learning_rate': 1e-4,
# init the game
env = magent.GridWorld("battle", map_size=args.map_size)
env.set_render_dir("build/render")
# two groups of agents
handles = env.get_handles()
# sample eval observation set
eval_obs = [None, None]
if args.eval:
print("sample eval set...")
env.reset()
generate_map(env, args.map_size, handles)
for i in range(len(handles)):
eval_obs[i] = magent.utility.sample_observation(env, handles, 2048, 500)
# load models
batch_size = 256
unroll_step = 8
target_update = 1200
train_freq = 5
if args.alg == 'dqn':
from magent.builtin.tf_model import DeepQNetwork
RLModel = DeepQNetwork
base_args = {'batch_size': batch_size,
'memory_size': 2 ** 20, 'learning_rate': 1e-4,
'target_update': target_update, 'train_freq': train_freq}
elif args.alg == 'drqn':
from magent.builtin.tf_model import DeepRecurrentQNetwork
RLModel = DeepRecurrentQNetwork
# init env
env = magent.GridWorld(load_config(map_size=args.map_size))
env.set_render_dir("build/render")
handles = env.get_handles()
food_handle = handles[0]
player_handles = handles[1:]
# sample eval observation set
eval_obs = None
if args.eval:
print("sample eval set...")
env.reset()
generate_map(env, args.map_size, food_handle, player_handles)
eval_obs = magent.utility.sample_observation(env, player_handles, 0, 2048, 500)
# load models
models = [
RLModel(env, player_handles[0], args.name,
batch_size=512, memory_size=2 ** 20, target_update=1000,
train_freq=4, eval_obs=eval_obs)
]
# load saved model
save_dir = "save_model"
if args.load_from is not None:
start_from = args.load_from
print("load models...")
for model in models:
model.load(save_dir, start_from)
else: