多智能体环境设计:高级主题与实践应用
引言
欢迎来到我们多智能体环境系列的第三篇博客!在前两篇文章中,我们介绍了基础概念和接口设计。今天,我们将深入探讨多智能体环境的高级主题,包括复杂场景设计、智能体交互机制、环境动态性,以及一些实际应用案例。本文旨在帮助初学者更好地理解和应用多智能体环境技术。
1. 复杂场景设计
在实际应用中,多智能体环境通常比简单的网格世界更加复杂。让我们看看如何设计一些更加真实的场景。
1.1 异构智能体环境
在许多实际情况下,环境中的智能体可能具有不同的能力和特征。让我们设计一个包含不同类型机器人的仓库环境:
import numpy as np
from gymnasium import spaces
from pettingzoo import AECEnv
from pettingzoo.utils import agent_selector
class HeterogeneousWarehouseEnv(AECEnv):
def __init__(self, grid_size=20, n_carriers=3, n_sorters=2, n_shelves=30):
super().__init__()
self.grid_size = grid_size
self.grid = np.zeros((grid_size, grid_size), dtype=int)
self.possible_agents = ([f"carrier_{
i}" for i in range(n_carriers)] +
[f"sorter_{
i}" for i in range(n_sorters)])
self.agent_types = {
agent: agent.split('_')[0] for agent in self.possible_agents}
self.shelves = self._place_shelves(n_shelves)
self.sorting_stations = self._place_sorting_stations(n_sorters)
self.action_spaces = {
agent: spaces.Discrete(5) for agent in self.possible_agents} # 4个方向 + 停留
self.observation_spaces = {
agent: self._get_obs_space() for agent in self.possible_agents}
def _place_shelves(self, n_shelves):
shelves = []
for _ in range(n_shelves):
pos = self._get_random_empty_position()
self.grid[pos] = 2 # 2 表示货架
shelves.append(pos)
return shelves
def _place_sorting_stations(self, n_stations):
stations = []
for _ in range(n_stations):
pos = self._get_random_empty_position()
self.grid[pos] = 3 # 3 表示分拣站
stations.append(pos)
return stations
def _get_obs_space(self):
return spaces.Dict({
"position": spaces.Box(low=0, high=self.grid_size-1, shape=(2,), dtype=int),
"grid": spaces.Box(low=0, high=3, shape=(self.grid_size, self.grid_size), dtype=int),
"carrier_positions": spaces.Dict({
agent: spaces.Box(low=0, high=self.grid_size-1, shape=(2,), dtype=int)
for agent in self.possible_agents if 'carrier' in agent}),
"sorter_positions": spaces.Dict({
agent: spaces.Box(low=0, high=self.grid_size-1, shape=(2,), dtype=int)
for agent in self.possible_agents if 'sorter' in agent}),
"carrying_item": spaces.Discrete(2) # 0: 没有携带物品, 1: 携带物品
})
def reset(self, seed=None, options=None):
self.agents = self.possible_agents[:]
self.agent_positions = {
agent: self._get_random_empty_position() for agent in self.agents}
self.carrying_items = {
agent: 0 for agent in self.agents if 'carrier' in agent}
self.agent_selector = agent_selector(self.agents)
self.agent_selection = self.agent_selector.next()
observations = {
agent: self._get_obs(agent) for agent in self.agents}
return observations, {
}
def step(self, action):
agent = self.agent_selection
current_pos = self.agent_positions[agent]
new_pos = self._get_new_position(current_pos, action)
if self._is_valid_move(agent, new_pos):
self.agent_positions[agent] = new_pos
# 处理特定类型智能体的行为
if self.agent_types[agent]
标签:agents,self,环境,pos,agent,智能,spaces,grid,设计
From: https://blog.csdn.net/weixin_46246346/article/details/141784446