Commit 1183406e authored by Siddhartha Laghuvarapu's avatar Siddhartha Laghuvarapu
Browse files

Meta agent for round

parent f432cfc9
from evaluator.base_agent import NeuralMMOAgent
from evaluator.base_agent import NeuralMMOMultiAgent
import projekt
from neuralmmo.Forge import loadModel
class NeuralBaselineAgent(NeuralMMOAgent):
class NeuralBaselineAgent():
def __init__(self):
self.agent_type = 'neural'
self.config = projekt.config.CompetitionRound1()
......@@ -20,3 +20,20 @@ class NeuralBaselineAgent(NeuralMMOAgent):
actions,self.state,_ = self.trainer.compute_actions(obs,state={},policy_id='policy_0')
return actions[0]
class NeuralBaselineMultiAgent(NeuralMMOMultiAgent):
def __init__(self):
self.config = projekt.config.CompetitionRound2()
max_agents = 8
self.agent_types = ['neural' for i in range(max_agents)]
self.agents = [loadModel(self.config) for i in range(max_agents)]
# self.trainer = loadModel(self.config)
def register_reset(self, observations, agent_idx):
agent_observations = observations[agent_idx]
actions,self.state,_ = self.agents[agent_idx].compute_actions({0:agent_observations},state={},policy_id='policy_0')
return actions[0]
def compute_action(self, observations, agent_idx):
agent_observations = observations[agent_idx]
actions,self.state,_ = self.trainer.compute_actions({0:agent_observations},state={},policy_id='policy_0')
return actions[0]
from evaluator.base_agent import NeuralMMOAgent
from evaluator.base_agent import NeuralMMOMultiAgent
from neural_mmo.forge.trinity.scripted import baselines
import projekt
class BaselineForageAgent(NeuralMMOAgent):
class BaselineForageAgent():
def __init__(self):
self.agent_type = 'scripted'
self.agent = getattr(baselines, 'Forage')(projekt.config.SmallMaps(),0)
......@@ -15,7 +15,7 @@ class BaselineForageAgent(NeuralMMOAgent):
action = self.agent(observations)
return action
class BaselineCombatAgent(NeuralMMOAgent):
class BaselineCombatAgent():
def __init__(self):
self.agent_type = 'scripted'
self.agent = getattr(baselines, 'Combat')(projekt.config.SmallMaps(),0)
......@@ -28,7 +28,7 @@ class BaselineCombatAgent(NeuralMMOAgent):
action = self.agent(observations)
return action
class BaselineRandomAgent(NeuralMMOAgent):
class BaselineRandomAgent():
def __init__(self):
self.agent_type = 'scripted'
self.agent = getattr(baselines, 'Random')(projekt.config.SmallMaps(),0)
......@@ -40,3 +40,29 @@ class BaselineRandomAgent(NeuralMMOAgent):
def compute_action(self, observations, info=None):
action = self.agent(observations)
return action
class ScriptedBaselineMultiAgent(NeuralMMOMultiAgent):
def __init__(self):
self.config = projekt.config.SmallMaps()
max_agents = 8
self.combat_agent = getattr(baselines, 'Combat')(self.config,0)
self.random_agent = getattr(baselines, 'Random')(self.config,0)
self.forage_agent = getattr(baselines, 'Forage')(self.config,0)
self.agent_types = ['scripted' for i in range(max_agents)]
self.agents = []
self.agents += [self.combat_agent for i in range(3)]
self.agents += [self.random_agent for i in range(3)]
self.agents += [self.forage_agent for i in range(2)]
def register_reset(self, observations, agent_idx):
agent_observations = observations[agent_idx]
action = self.agents[agent_idx](agent_observations)
return action
def compute_action(self, observations, agent_idx):
agent_observations = observations[agent_idx]
action = self.agents[agent_idx](agent_observations)
return action
from agents import BaselineCombatAgent, BaselineForageAgent, RandomNeuralMMOAgent, BaselineRandomAgent, NeuralBaselineAgent
from agents import NeuralBaselineMultiAgent, ScriptedBaselineMultiAgent
class LocalEvaluationConfig:
player_agents = [NeuralBaselineAgent]
player_agents = [ScriptedBaselineMultiAgent]
opponents = []
opponents += [BaselineForageAgent] * 50
opponents += [BaselineCombatAgent] * 45
opponents += [RandomNeuralMMOAgent] * 6
opponents += [BaselineRandomAgent] * 26
opponents += [ScriptedBaselineMultiAgent] * 7
# opponents += [BaselineCombatAgent] * 45
# opponents += [RandomNeuralMMOAgent] * 6
# opponents += [BaselineRandomAgent] * 26
class NeuralMMOAgent():
class NeuralMMOMultiAgent():
def __init__(self):
self.agent_type = None
self.agent_types = None
def register_reset(self, observations):
def register_reset(self, observations, agent_id):
raise NotImplementedError
def compute_action(self, observations, info):
def compute_action(self, observations, agent_id):
raise NotImplementedError
......@@ -69,14 +69,14 @@ def assign_agents(player_agent, opponent_agents):
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)
player_index = random.randint(0, 7)
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()
config = projekt.config.CompetitionRound2()
config.RENDER = True
env = ProxyEnv(config)
n_steps = 0
......@@ -85,17 +85,34 @@ def run_episode(player_index, agents, N_TIME_STEPS):
obs = env.reset()
entids = list(obs.keys())
agent_entid_map = dict(zip(range(len(agents)), entids))
individual_agents = []
for agent_idx in range(len(agents)):
for player_idx in range(8):
individual_agents.append(
{
"agent":agents[agent_idx],
"player_idx":player_idx,
"agent_type":agents[agent_idx].agent_type[player_idx]
}
)
agent_entid_map = dict(zip(range(len(individual_agents)), entids))
entid_agent_map = {x[1]: x[0] for x in agent_entid_map.items()}
for idx, agent in enumerate(agents):
if agent.agent_type == "neural":
for idx, agent in enumerate(individual_agents):
if agent[idx]["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])
agent = individual_agents[entid_agent_map[entid]["agent"]]
player_idx = individual_agents[entid_agent_map[entid]["player_idx"]]
actions[entid] = agent.register_reset(obs[entid],player_idx)
alive_agents = list(obs.keys())
pbar = tqdm(total = N_TIME_STEPS)
while len(obs.keys()) > 0 and n_steps < N_TIME_STEPS:
pbar.update(n=1)
for entid in actions:
......@@ -119,7 +136,13 @@ def run_episode(player_index, agents, N_TIME_STEPS):
for entid in alive_agents:
if entid in dead_agents:
continue
actions[entid] = agents[entid_agent_map[entid]].compute_action(obs[entid])
agent = individual_agents[entid_agent_map[entid]["agent"]]
player_idx = individual_agents[entid_agent_map[entid]["player_idx"]]
actions[entid] = agent.compute_action(obs[entid],player_idx)
# actions[entid] = agents[entid_agent_map[entid]].compute_action(obs[entid])
n_steps += 1
for entid in sorted(list(obs.keys())):
......
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