简体   繁体   中英

Random agent on multi-agent gym environments

I'm not able to select random actions for multi-agent gym environments.

def make_env(scenario_name, benchmark=False):

    from multiagent.environment import MultiAgentEnv
    import multiagent.scenarios as scenarios

    # load scenario from script
    scenario = scenarios.load(scenario_name + ".py").Scenario()
    # create world
    world = scenario.make_world()
    # create multiagent environment
    if benchmark:        
        env = MultiAgentEnv(world, scenario.reset_world, scenario.reward, scenario.observation, scenario.benchmark_data)
    else:
        env = MultiAgentEnv(world, scenario.reset_world, scenario.reward, scenario.observation)
    return env

env = make_env('simple_tag')
env.reset()
for i in range(100):
    env.render()
    actions = [action_space.sample() for action_space in env.action_space]
    env.step(actions)

The above code throws this error:

Traceback (most recent call last):
  File "hello.py", line 22, in <module>
    env.step(actions)
  File "c:\multiagent-particle-envs\multiagent\environment.py", line 88, in step
    self._set_action(action_n[i], agent, self.action_space[i])
  File "c:\multiagent-particle-envs\multiagent\environment.py", line 174, in _set_action
    agent.action.u[0] += action[0][1] - action[0][2]
TypeError: 'int' object is not subscriptable

I can't find a fix since there's not enough talk about these multi agent environments.

Answering my own question, let's consider the simple_tag environment.

env.action_space for this environment gives:

[Discrete(5), Discrete(5), Discrete(5), Discrete(5)] (4 agents)

This is what I found misleading. I thought the actions would have to be a list of 4 elements, something like: [0, 3, 4, 1] but what it expects is a one-hot vector (of 5 elements) for all 4 agents. So, the correct way to encode actions is:

[array([1., 0., 0., 0., 0.]), array([0., 0., 1., 0., 0.]), array([0., 0., 0., 0., 1.]), array([0., 0., 0., 1., 0.])]

(depending on the environment)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM