mirror of
https://github.com/FoundationAgents/MetaGPT.git
synced 2026-04-30 19:36:24 +02:00
update stanford_town_ext_env
This commit is contained in:
parent
332b400052
commit
095ce5caf4
5 changed files with 76 additions and 14 deletions
|
|
@ -1,3 +1,15 @@
|
|||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
# @Desc :
|
||||
|
||||
from metagpt.environment.android_env.android_env import AndroidEnv
|
||||
from metagpt.environment.gym_env.gym_env import GymEnv
|
||||
from metagpt.environment.mincraft_env.mincraft_env import MincraftExtEnv
|
||||
from metagpt.environment.werewolf_env.werewolf_env import WerewolfEnv
|
||||
|
||||
from metagpt.environment.stanford_town_env.stanford_town_env import StanfordTownEnv
|
||||
|
||||
# from metagpt.environment.software_env.software_env import SoftwareEnv
|
||||
|
||||
|
||||
__all__ = ["AndroidEnv", "GymEnv", "MincraftExtEnv", "WerewolfEnv", "StanfordTownEnv"]
|
||||
|
|
|
|||
|
|
@ -2,7 +2,9 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# @Desc : MG StanfordTown Env
|
||||
|
||||
from metagpt.env.stanford_town_env.stanford_town_ext_env import StanfordTownExtEnv
|
||||
from metagpt.environment.stanford_town_env.stanford_town_ext_env import (
|
||||
StanfordTownExtEnv,
|
||||
)
|
||||
|
||||
|
||||
class StanfordTownEnv(StanfordTownExtEnv):
|
||||
|
|
|
|||
|
|
@ -2,39 +2,44 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# @Desc : The StanfordTown external environment to interate with the web interface
|
||||
|
||||
import math
|
||||
from pathlib import Path
|
||||
from typing import Optional, Tuple
|
||||
|
||||
from pydantic import Field, model_validator
|
||||
from pydantic import ConfigDict, Field, model_validator
|
||||
|
||||
from metagpt.env.base_env import ExtEnv, mark_as_readable, mark_as_writeable
|
||||
from metagpt.utils.common import read_json_file
|
||||
from metagpt.environment.base_env import ExtEnv, mark_as_readable, mark_as_writeable
|
||||
from metagpt.utils.common import read_csv_to_list, read_json_file
|
||||
|
||||
|
||||
class StanfordTownExtEnv(ExtEnv):
|
||||
model_config = ConfigDict(arbitrary_types_allowed=True)
|
||||
|
||||
maze_asset_path: Optional[Path] = Field(default=None, description="the path to store maze assets")
|
||||
maze_width: int = Field(default=140)
|
||||
meze_height: int = Field(default=100)
|
||||
maze_width: int = Field(default=140, description="maze map width")
|
||||
maze_height: int = Field(default=100, description="maze map height")
|
||||
sq_tile_size: int = Field(default=32, description="the pixel height/width of a tile")
|
||||
special_constraint: str = Field(
|
||||
default="", description="a string description of any relevant special constraints " "the world might have"
|
||||
)
|
||||
titles: list[list[dict]] = Field(default=[])
|
||||
address_tiles: dict[set] = Field(default={})
|
||||
tiles: list[list[dict]] = Field(default=[])
|
||||
address_tiles: dict[str, set] = Field(default=dict())
|
||||
collision_maze: list[list] = Field(default=[])
|
||||
|
||||
@model_validator(mode="before")
|
||||
@classmethod
|
||||
def _init_maze(cls, values: dict):
|
||||
maze_asset_path = values.get("maze_asset_path")
|
||||
def _init_maze(cls, values):
|
||||
maze_asset_path = values["maze_asset_path"]
|
||||
assert maze_asset_path
|
||||
maze_asset_path = Path(maze_asset_path)
|
||||
|
||||
maze_matrix_path = maze_asset_path.joinpath("matrix")
|
||||
meta_info = read_json_file(maze_matrix_path.joinpath("maze_meta_info.json"))
|
||||
|
||||
values["maze_width"] = int(meta_info["maze_width"])
|
||||
values["meze_height"] = int(meta_info["meze_height"])
|
||||
maze_width = int(meta_info["maze_width"])
|
||||
maze_height = int(meta_info["maze_height"])
|
||||
values["maze_width"] = maze_width
|
||||
values["maze_height"] = maze_height
|
||||
values["sq_tile_size"] = int(meta_info["sq_tile_size"])
|
||||
values["special_constraint"] = meta_info["special_constraint"]
|
||||
|
||||
|
|
@ -101,8 +106,8 @@ class StanfordTownExtEnv(ExtEnv):
|
|||
arena_maze = []
|
||||
game_object_maze = []
|
||||
spawning_location_maze = []
|
||||
for i in range(0, len(collision_maze_raw), meta_info["maze_width"]):
|
||||
tw = meta_info["maze_width"]
|
||||
for i in range(0, len(collision_maze_raw), maze_width):
|
||||
tw = maze_width
|
||||
collision_maze += [collision_maze_raw[i : i + tw]]
|
||||
sector_maze += [sector_maze_raw[i : i + tw]]
|
||||
arena_maze += [arena_maze_raw[i : i + tw]]
|
||||
|
|
|
|||
3
tests/metagpt/environment/stanford_town_env/__init__.py
Normal file
3
tests/metagpt/environment/stanford_town_env/__init__.py
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
# @Desc :
|
||||
|
|
@ -0,0 +1,40 @@
|
|||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
# @Desc : the unittest of StanfordTownExtEnv
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
from metagpt.environment.stanford_town_env.stanford_town_ext_env import (
|
||||
StanfordTownExtEnv,
|
||||
)
|
||||
|
||||
maze_asset_path = (
|
||||
Path(__file__).absolute().parent.joinpath("..", "..", "..", "data", "environment", "stanford_town", "the_ville")
|
||||
)
|
||||
|
||||
|
||||
def test_stanford_town_ext_env():
|
||||
ext_env = StanfordTownExtEnv(maze_asset_path=maze_asset_path)
|
||||
|
||||
tile_coord = ext_env.turn_coordinate_to_tile((64, 64))
|
||||
assert tile_coord == (2, 2)
|
||||
|
||||
tile = (58, 9)
|
||||
assert len(ext_env.get_collision_maze()) == 100
|
||||
assert len(ext_env.get_address_tiles()) == 306
|
||||
assert ext_env.access_tile(tile=tile)["world"] == "the Ville"
|
||||
assert ext_env.get_tile_path(tile=tile, level="world") == "the Ville"
|
||||
assert len(ext_env.get_nearby_tiles(tile=tile, vision_r=5)) == 121
|
||||
|
||||
event = ("double studio:double studio:bedroom 2:bed", None, None, None)
|
||||
ext_env.add_tiles_event(tile[1], tile[0], event=event)
|
||||
ext_env.add_event_from_tile(event, tile)
|
||||
assert len(ext_env.tiles[tile[1]][tile[0]]["events"]) == 1
|
||||
|
||||
ext_env.turn_event_from_tile_idle(event, tile)
|
||||
|
||||
ext_env.remove_event_from_tile(event, tile)
|
||||
assert len(ext_env.tiles[tile[1]][tile[0]]["events"]) == 0
|
||||
|
||||
ext_env.remove_subject_events_from_tile(subject=event[0], tile=tile)
|
||||
assert len(ext_env.tiles[tile[1]][tile[0]]["events"]) == 0
|
||||
Loading…
Add table
Add a link
Reference in a new issue