Commit 9b812f28 authored by Siddhartha Laghuvarapu's avatar Siddhartha Laghuvarapu
Browse files

Changes for submission-test-rllib-v3

parent 2be9cd06
../../neuralmmo_master/neural-mmo
\ No newline at end of file
# Define agents that will be used for evaluation. # Define agents that will be used for evaluation.
# Agents need to implement abstract class NeuralMMOAgent. # Agents need to implement abstract class NeuralMMOAgent.
# Agents will be located in agents/ # Agents will be located in agents/
# Max number of opponent agents is 127 # Number of opponent agents is exactly 127
player_agent: player_agent:
file: neural_baseline_agent file: scripted_baseline_agent
agent_class: NeuralBaselineAgent agent_class: BaselineCombatAgent
agent_type: neural agent_type: scripted
opponent_agents: opponent_agents:
agent_1: agent_1:
......
neural-mmo/docs/source/resource/ ../../neuralmmo_master/neural-mmo/resource
\ No newline at end of file \ No newline at end of file
from tqdm import trange import sys
sys.path.append('neural-mmo/')
import gym from forge.ethyr.torch import utils
import gym_neuralmmo from forge.trinity.env import Env
from forge.blade.io.action import static as Action
import projekt
from utils.helpers import load_agents from utils.helpers import load_agents
import random
import copy
def assign_agents(player_agent,opponent_agents):
player_index = 0
if len(opponent_agents) != 127:
raise Exception("Number of opponent agents should add up to exactly 127")
random.shuffle(opponent_agents)
player_index = random.randint(0,127)
agents = copy.deepcopy(opponent_agents)
agents.insert(player_index,player_agent)
return agents,player_index
def main():
env = gym.make("neuralmmo-v0") def run_episode(player_index, agents, N_TIME_STEPS):
config = projekt.config.CompetitionRound1()
env = Env(config)
n_steps = 0
neural_agents = set()
dead_agents = []
obs = env.reset()
entids = list(obs.keys())
agent_entid_map = dict(zip(range(len(agents)), entids))
entid_agent_map = {x[1]:x[0] for x in agent_entid_map.items()}
for idx,agent in enumerate(agents):
if agent.type == 'neural':
neural_agents.add(agent_entid_map[idx])
actions = {}
for entid in entids:
actions[entid] = agents[entid_agent_map[entid]].register_reset(obs[entid])
alive_agents = list(obs.keys())
while len(obs.keys()) > 0 and n_steps < N_TIME_STEPS:
for entid in actions:
if entid not in neural_agents:
realm = env.realm
if Action.Attack in actions[entid]:
targID = actions[entid][Action.Attack][Action.Target]
actions[entid][Action.Attack][Action.Target] = realm.entity(targID)
obs,dones,rewards,_ = env.step(actions,omitDead=True,preprocess=neural_agents)
for entid in alive_agents:
if entid not in list(obs.keys()):
dead_agents.append(entid)
alive_agents = list(obs.keys())
actions = {}
for entid in alive_agents:
if entid in dead_agents:
continue
actions[entid] = agents[entid_agent_map[entid]].compute_action(obs[entid])
n_steps += 1
for entid in obs:
if entid not in dead_agents:
dead_agents.append(entid)
logs = env.terminal()
player_entid = entid_agent_map[player_index]
player_log = {}
player_log["Achievement"] = logs['Stats']['Achievement'][player_entid]
player_log["Equipment"] = logs['Stats']['Equipment'][player_entid]
player_log["Equipment"] = logs['Stats']['Exploration'][player_entid]
player_log["PlayerKills"] = logs['Stats']['PlayerKills'][player_entid]
player_log["Foraging"] = logs['Stats']['Foraging'][player_entid]
return player_log
def print_statistics(player_statistics,episode):
print("======= Episode {} ========".format(episode+1))
print("Achievement ",player_statistics['Achievement'])
print("Equipment ",player_statistics['Equipment'])
print("Equipment ",player_statistics['Equipment'])
print("PlayerKills ",player_statistics['PlayerKills'])
print("Foraging ",player_statistics['Foraging'])
print("=========================")
if __name__== "__main__":
player_agent, opponent_agents = load_agents("players.yaml") player_agent, opponent_agents = load_agents("players.yaml")
env.set_player_agent(player_agent) N_EPISODES = 10
env.set_eval_agents(opponent_agents) N_TIME_STEPS = 1024
n_episodes = 100 for episode in range(N_EPISODES):
total_rewards = 0 agents,player_index = assign_agents(player_agent,opponent_agents)
for _ in trange(n_episodes): statistics = run_episode(player_index,agents,N_TIME_STEPS)
obs = env.reset() print_statistics(statistics,episode)
action = player_agent.register_reset(obs)
done = False
while done == False:
obs, dones, rewards, _ = env.step(action)
action = player_agent.compute_action(obs["player"])
total_rewards += rewards["player"]
done = dones["player"]
print("The total reward is ", total_rewards)
if __name__ == "__main__":
main()
import sys
sys.path.append('neural-mmo/')
from forge.ethyr.torch import utils
from forge.trinity.env import Env
import projekt
from utils.helpers import load_agents
import random
import copy
def assign_agents(player_agent,opponent_agents):
player_index = 0
if len(opponent_agents) != 127:
raise Exception("Number of opponent agents should add up to exactly 127")
random.shuffle(opponent_agents)
player_index = random.randint(0,127)
agents = copy.deepcopy(opponent_agents)
agents.insert(player_index,player_agent)
return agents,player_index
def run_episode(player_index, agents, N_TIME_STEPS):
config = projekt.config.CompetitionRound1()
env = Env(config)
n_steps = 0
neural_agents = set()
dead_agents = []
obs = env.reset()
entids = list(obs.keys())
agent_entid_map = dict(zip(range(len(agents)), entids))
entid_agent_map = {x[1]:x[0] for x in agent_entid_map.items()}
for idx,agent in enumerate(agents):
if agent.type == 'neural':
neural_agents.add(agent_entid_map[idx])
actions = {}
for entid in entids:
actions[entid] = agents[entid_agent_map[entid]].register_reset(obs[entid])
while len(obs.keys()) > 0 and n_steps < N_TIME_STEPS:
obs,dones,rewards,_ = env.step(actions,omitDead=False)
alive_agents = list(obs.keys())
for entid in dones:
if dones[entid]:
dead_agents.append(entid)
actions = {}
for entid in alive_agents:
if entid not in entid_agent_map:
continue
actions[entid] = agents[entid_agent_map[entid]].compute_action(obs[entid])
n_steps += 1
for entid in obs:
if entid not in dead_agents:
dead_agents.append(entid)
logs = env.terminal()
player_entid = entid_agent_map[player_index]
player_log = {}
if player_entid in dead_agents:
player_log["Achievement"] = logs['Stats']['Achievement'][player_entid]
player_log["Equipment"] = logs['Stats']['Equipment'][player_entid]
player_log["Exploration"] = logs['Stats']['Exploration'][player_entid]
player_log["PlayerKills"] = logs['Stats']['PlayerKills'][player_entid]
player_log["Foraging"] = logs['Stats']['Foraging'][player_entid]
return player_log
def print_statistics(player_statistics,episode):
print(player_statistics, episode)
if __name__== "__main__":
player_agent, opponent_agents = load_agents("players.yaml")
N_EPISODES = 10
N_TIME_STEPS = 10
for episode in range(N_EPISODES):
agents,player_index = assign_agents(player_agent,opponent_agents)
statistics = run_episode(player_index,agents,N_TIME_STEPS)
print_statistics(statistics,episode)
...@@ -7,6 +7,7 @@ def get_agent(agent_dict): ...@@ -7,6 +7,7 @@ def get_agent(agent_dict):
sys.path.append("agents/") sys.path.append("agents/")
module = importlib.import_module(agent_dict["file"]) module = importlib.import_module(agent_dict["file"])
agent = getattr(module, agent_dict["agent_class"])() agent = getattr(module, agent_dict["agent_class"])()
agent.type = agent_dict["agent_type"]
return agent return agent
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment