Commit 1baf173a authored by Siddhartha Laghuvarapu's avatar Siddhartha Laghuvarapu
Browse files

Add action spaces in env and for random agents

parent 3e7e26c3
from utils.base_agent import NeuralMMOAgent
from utils.env_spaces import get_action_spaces
from flexdict import FlexDict
from collections import defaultdict
class RandomNeuralMMOAgent(NeuralMMOAgent):
def __init__(self):
pass
self.action_space = get_action_spaces()
def register_reset(self, observations):
action = self.agent.compute_actions(observations)
......@@ -14,4 +18,9 @@ class RandomNeuralMMOAgent(NeuralMMOAgent):
return action
def get_action(self, observations):
return {}
action = FlexDict(defaultdict(FlexDict))
for atn in sorted(self.action_space):
for arg in sorted(atn.edges):
action[atn][arg] = self.action_space[atn][arg].sample()
return action
\ No newline at end of file
......@@ -6,22 +6,40 @@ from forge.blade.systems import ai
from forge.trinity.env import Env
import projekt
from forge.blade.io.action.static import Action
from flexdict import FlexDict
from collections import defaultdict
def get_action_spaces(config):
action_space = FlexDict(defaultdict(FlexDict))
for atn in sorted(Action.edges):
for arg in sorted(atn.edges):
n = arg.N(config)
action_space[atn][arg] = gym.spaces.Discrete(n)
return action_space
class NeuralMMOEval(gym.Env):
def __init__(self):
config = projekt.config.SmallMaps()
self.action_space = get_action_spaces(config)
self.env = Env(config)
self.agents_in_play = {}
self.available_agents = []
self.alive_agents = []
def reset(self):
self.agents_in_play = {}
self.available_agents = []
self.dead_agents = []
self.observations = self.env.reset()
self.alive_agents = list(self.observations.keys())
self.player_idx = self.alive_agents[0]
self.agents_in_play[self.alive_agents[0]] = self.player_agent
self.available_agents = self.eval_agents
self.available_agents = self.eval_agents[:]
self.assign_agents()
self.actions = self.get_agent_actions()
return self.observations
def get_available_agent(self):
......@@ -37,18 +55,20 @@ class NeuralMMOEval(gym.Env):
def get_agent_actions(self):
actions = {}
print(self.observations)
print(self.alive_agents)
for agent in self.observations:
print(agent)
if agent in self.dead_agents:
continue
actions[agent] = self.agents_in_play[agent].compute_action(
self.observations[agent]
)
return actions
def step(self):
self.actions = self.get_agent_actions()
self.observations, dones, rewards, _ = self.env.step(self.actions)
for agent in dones:
if dones[agent] is not False:
self.dead_agents.append(agent)
self.actions = self.get_agent_actions()
self.alive_agents = list(self.observations.keys())
self.assign_agents()
return self.parse_observations(self.observations, dones, rewards, _)
......@@ -66,7 +86,7 @@ class NeuralMMOEval(gym.Env):
parse_obs = {}
parse_dones = {}
parse_rewards = {}
parse_obs["player_agent"] = obs[self.player_idx]
parse_dones["player_agent"] = dones[self.player_idx]
parse_rewards["player_agent"] = rewards[self.player_idx]
parse_obs["player"] = obs[self.player_idx]
parse_dones["player"] = dones[self.player_idx]
parse_rewards["player"] = rewards[self.player_idx]
return parse_obs, parse_dones, parse_rewards, _
......@@ -19,8 +19,8 @@ def main():
done = False
while done == False:
obs, dones, rewards, _ = env.step()
total_rewards += rewards["player"]
done = dones["player"]
total_rewards += rewards["player"]
done = dones["player"]
print("The total reward is ", total_rewards)
......
from forge.blade.io.action.static import Action
from flexdict import FlexDict
from collections import defaultdict
import gym
import projekt
def get_action_spaces():
config = projekt.config.SmallMaps()
atns = FlexDict(defaultdict(FlexDict))
for atn in sorted(Action.edges):
for arg in sorted(atn.edges):
n = arg.N(config)
atns[atn][arg] = gym.spaces.Discrete(n)
return atns
import sys
import yaml
import importlib
def get_agent(agent_dict):
sys.path.append("agents/")
module = importlib.import_module(agent_dict["file"])
agent = getattr(module, agent_dict["agent_class"])()
return agent
def load_agents(agents_config):
with open(agents_config, "r") as stream:
data = yaml.safe_load(stream)
player_agent = get_agent(data["player_agent"])
opponent_agents = []
for agent in data["opponent_agents"]:
for num in range(data["opponent_agents"][agent]["num_agents"]):
opponent_agents.append(get_agent(data["opponent_agents"][agent]))
return (player_agent, opponent_agents)
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