Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
print_every=10, record=False, render=False, eps=None):
env.reset()
generate_map(env, map_size, food_handle, handles)
step_ct = 0
total_reward = 0
done = False
pos_reward_ct = set()
n = len(handles)
obs = [None for _ in range(n)]
ids = [None for _ in range(n)]
acts = [None for _ in range(n)]
nums = [env.get_num(handle) for handle in handles]
sample_buffer = magent.utility.EpisodesBuffer(capacity=5000)
center_x, center_y = map_size // 2, map_size // 2
print("===== sample =====")
print("eps %s number %s" % (eps, nums))
start_time = time.time()
new_rule_ct = 0
last_base_reward = {}
while not done:
# take actions for every model
for i in range(n):
obs[i] = env.get_observation(handles[i])
ids[i] = env.get_agent_id(handles[i])
# rule modification signal
print_every=10, record=False, render=False, eps=None):
env.reset()
generate_map(env, map_size, food_handle, handles, rnd)
step_ct = 0
total_reward = 0
done = False
pos_reward_ct = set()
n = len(handles)
obs = [None for _ in range(n)]
ids = [None for _ in range(n)]
acts = [None for _ in range(n)]
nums = [env.get_num(handle) for handle in handles]
sample_buffer = magent.utility.EpisodesBuffer(capacity=5000)
center_x, center_y = map_size // 2, map_size // 2
print("===== sample =====")
print("eps %s number %s" % (eps, nums))
start_time = time.time()
new_rule_ct = 0
last_base_reward = {}
while not done:
# take actions for every model
for i in range(n):
obs[i] = env.get_observation(handles[i])
ids[i] = env.get_agent_id(handles[i])
# rule modification signal
def generate_map(env, map_size, goal_handle, handles, rnd):
# random message
font = FontProvider('data/font_8x8/basic.txt')
n_msg = random.randint(1, 4)
messages = []
for i in range(n_msg):
length = random.randint(2, 9)
tmp = []
for j in range(length):
tmp.append(random.randint(0x20, 0x7E))
messages.append(tmp)
center_x, center_y = map_size // 2, map_size // 2
# create maze: left pos, width, height
radius = 90
pos_list = create_maze([center_x - radius, center_y - radius], radius + 1, 15, 2, font_area=[radius * 2 - 28, radius * 2 - 28])
# pos_list = create_naive_maze([center_x - radius, center_y - radius], radius + 1, 15, 2, font_area=[radius * 2 - 28, radius * 2 - 28])
env.add_walls(method="custom", pos=pos_list)
def generate_map(env, map_size, goal_handle, handles):
# random message
font = FontProvider('data/font_8x8/basic.txt')
n_msg = random.randint(1, 4)
messages = []
for i in range(n_msg):
length = random.randint(2, 9)
tmp = []
for j in range(length):
tmp.append(random.randint(0x20, 0x7E))
messages.append(tmp)
center_x, center_y = map_size // 2, map_size // 2
# create maze: left pos, width, height
radius = 90
pos_list = create_maze([center_x - radius, center_y - radius], radius + 1, 15, 2, font_area=[radius * 2 - 28, radius * 2 - 28])
env.add_walls(method="custom", pos=pos_list)
'''
return cfg
def measure_time(msg, func, *args, **kwargs):
start_time = time.time()
ret = func(*args, **kwargs)
print("%-11s %.3f" % (msg, time.time() - start_time))
return ret
if __name__ == "__main__":
n_step = 10
# init the game "forest" (or "battle" here)
env = gw.GridWorld(load_forest())
env.reset()
# add two groups of animals
deer_handle, tiger_handle = env.get_handles()
env.add_agents(deer_handle, method="random", n=1000000)
env.add_agents(tiger_handle, method="random", n=1000000)
# init two models
model1 = RandomActor(env, deer_handle)
model2 = RandomActor(env, tiger_handle)
total_reward = 0
print(env.get_view_space(deer_handle))
print(env.get_view_space(tiger_handle))
cfg.set({"map_width": 10000, "map_height": 10000})
cfg.set({"minimap_mode": 0})
cfg.set({"turn_mode": 0})
cfg.set({"embedding_size": 8})
deer = cfg.register_agent_type(
name="deer",
attr={'width': 1, 'length': 1, 'hp': 10, 'speed': 1,
'view_range': gw.CircleRange(7), 'attack_range': gw.CircleRange(0),
'damage': 0, 'step_recover': 0, 'kill_supply': 8})
tiger = cfg.register_agent_type(
name="tiger",
attr={'width': 1, 'length': 1, 'hp': 10, 'speed': 1,
'view_range': gw.CircleRange(6), 'attack_range': gw.CircleRange(2),
'damage': 10, 'step_recover': -0.01, 'kill_supply': 0})
deer_g = cfg.add_group(agent_type=deer)
tiger_g = cfg.add_group(agent_type=tiger)
'''
a = gw.AgentSymbol(group=deer_g, index='any')
b = gw.AgentSymbol(group=tiger_g, index='any')
a_kill = gw.Event(a, 'kill', b)
b_kill = gw.Event(b, 'kill', a)
cfg.add_reward_rule(on=a_kill, receiver=[a, b], value=[1, -1])
cfg.add_reward_rule(on=b_kill, receiver=[a, b], value=[-1, 1])
'''
def load_forest():
cfg = gw.Config()
cfg.set({"map_width": 10000, "map_height": 10000})
cfg.set({"minimap_mode": 0})
cfg.set({"turn_mode": 0})
cfg.set({"embedding_size": 8})
deer = cfg.register_agent_type(
name="deer",
attr={'width': 1, 'length': 1, 'hp': 10, 'speed': 1,
'view_range': gw.CircleRange(7), 'attack_range': gw.CircleRange(0),
'damage': 0, 'step_recover': 0, 'kill_supply': 8})
tiger = cfg.register_agent_type(
name="tiger",
attr={'width': 1, 'length': 1, 'hp': 10, 'speed': 1,
'view_range': gw.CircleRange(6), 'attack_range': gw.CircleRange(2),
def load_forest():
cfg = gw.Config()
cfg.set({"map_width": 10000, "map_height": 10000})
cfg.set({"minimap_mode": 0})
cfg.set({"turn_mode": 0})
cfg.set({"embedding_size": 8})
deer = cfg.register_agent_type(
name="deer",
attr={'width': 1, 'length': 1, 'hp': 10, 'speed': 1,
'view_range': gw.CircleRange(7), 'attack_range': gw.CircleRange(0),
'damage': 0, 'step_recover': 0, 'kill_supply': 8})
tiger = cfg.register_agent_type(
name="tiger",
attr={'width': 1, 'length': 1, 'hp': 10, 'speed': 1,
'view_range': gw.CircleRange(6), 'attack_range': gw.CircleRange(2),
'damage': 10, 'step_recover': -0.01, 'kill_supply': 0})
deer_g = cfg.add_group(agent_type=deer)
tiger_g = cfg.add_group(agent_type=tiger)
'''
a = gw.AgentSymbol(group=deer_g, index='any')
b = gw.AgentSymbol(group=tiger_g, index='any')
a_kill = gw.Event(a, 'kill', b)
if __name__ == "__main__":
n_step = 10
# init the game "forest" (or "battle" here)
env = gw.GridWorld(load_forest())
env.reset()
# add two groups of animals
deer_handle, tiger_handle = env.get_handles()
env.add_agents(deer_handle, method="random", n=1000000)
env.add_agents(tiger_handle, method="random", n=1000000)
# init two models
model1 = RandomActor(env, deer_handle)
model2 = RandomActor(env, tiger_handle)
total_reward = 0
print(env.get_view_space(deer_handle))
print(env.get_view_space(tiger_handle))
for i in range(n_step):
print("===== step %d =====" % i)
start_time = time.time()
obs_1 = measure_time("get obs 1", env.get_observation, deer_handle)
acts_1 = measure_time("infer act 1", model1.infer_action, obs_1)
measure_time("set act 1", env.set_action, deer_handle, acts_1)
obs_2 = measure_time("get obs 2", env.get_observation, tiger_handle)
if __name__ == "__main__":
n_step = 10
# init the game "forest" (or "battle" here)
env = gw.GridWorld(load_forest())
env.reset()
# add two groups of animals
deer_handle, tiger_handle = env.get_handles()
env.add_agents(deer_handle, method="random", n=1000000)
env.add_agents(tiger_handle, method="random", n=1000000)
# init two models
model1 = RandomActor(env, deer_handle)
model2 = RandomActor(env, tiger_handle)
total_reward = 0
print(env.get_view_space(deer_handle))
print(env.get_view_space(tiger_handle))
for i in range(n_step):
print("===== step %d =====" % i)
start_time = time.time()
obs_1 = measure_time("get obs 1", env.get_observation, deer_handle)
acts_1 = measure_time("infer act 1", model1.infer_action, obs_1)
measure_time("set act 1", env.set_action, deer_handle, acts_1)
obs_2 = measure_time("get obs 2", env.get_observation, tiger_handle)
acts_2 = measure_time("infer act 2", model2.infer_action, obs_2)