Skip to content

Commit

Permalink
Add optional sync_mode to CraftiumEnv and MarlCraftiumEnv
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikel committed Nov 16, 2024
1 parent 1bacac1 commit b5325d8
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
3 changes: 3 additions & 0 deletions craftium/craftium_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class CraftiumEnv(Env):
:param rgb_observations: Whether to use RGB images or gray scale images as observations. Note that RGB images are slower to send from MT to python via TCP. By default RGB images are used.
:param gray_scale_keepdim: If `True`, a singleton dimension will be added, i.e. observations are of the shape WxHx1. Otherwise, they are of shape WxH.
:param seed: Random seed. Affects minetest's map generation and Lua's RNG (in mods).
:param sync_mode: If set to true, minetest's internal client and server steps are synchronized. This is useful for training models slower than realtime.
"""
metadata = {"render_modes": ["human", "rgb_array"], "render_fps": 30}

Expand All @@ -63,6 +64,7 @@ def __init__(
rgb_observations: bool = True,
gray_scale_keepdim: bool = False,
seed: Optional[int] = None,
sync_mode: bool = False,
):
super(CraftiumEnv, self).__init__()

Expand Down Expand Up @@ -119,6 +121,7 @@ def __init__(
mt_port=mt_port,
frameskip=frameskip,
rgb_frames=rgb_observations,
sync_mode=sync_mode,
)

self.last_observation = None # used in render if "rgb_array"
Expand Down
13 changes: 10 additions & 3 deletions craftium/minetest.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def __init__(
mt_port: Optional[int] = None,
frameskip: int = 1,
rgb_frames: bool = True,
sync_mode: bool = False,
):
self.pipe_proc = pipe_proc

Expand Down Expand Up @@ -75,7 +76,7 @@ def __init__(
port=port,
remote_port=port,

multi_agent=False,
sync_env_mode=sync_mode,

# Adapt HUD size to display size, based on (1024, 600) default
# hud_scaling=self.display_size[0] / 1024,
Expand Down Expand Up @@ -231,6 +232,7 @@ def __init__(
minetest_conf: dict[str, Any] = dict(),
pipe_proc: bool = True,
mt_server_port: Optional[int] = None,
sync_mode: bool = True,
):
self.pipe_proc = pipe_proc

Expand Down Expand Up @@ -264,11 +266,13 @@ def __init__(
port=self.server_port,
remote_port=self.server_port,

multi_agent=True,
sync_env_mode=sync_mode,

# Adapt HUD size to display size, based on (1024, 600) default
# hud_scaling=self.display_size[0] / 1024,

dedicated_server_step = 1/60,

# Attempt to improve performance. Impact unclear.
server_map_save_interval=1000000,
profiler_print_interval=0,
Expand Down Expand Up @@ -423,6 +427,7 @@ def __init__(
pipe_proc: bool = True,
frameskip: int = 1,
rgb_frames: bool = True,
sync_mode: bool = True,
):
self.pipe_proc = pipe_proc

Expand Down Expand Up @@ -460,11 +465,13 @@ def __init__(
port=mt_server_port,
remote_port=mt_server_port,

multi_agent=True,
sync_env_mode=sync_mode,

# Adapt HUD size to display size, based on (1024, 600) default
# hud_scaling=self.display_size[0] / 1024,

dedicated_server_step = 1/60,

# Attempt to improve performance. Impact unclear.
server_map_save_interval=1000000,
profiler_print_interval=0,
Expand Down
3 changes: 3 additions & 0 deletions craftium/multiagent_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def __init__(
rgb_observations: bool = True,
gray_scale_keepdim: bool = False,
seed: Optional[int] = None,
sync_mode: bool = True,
):
self.num_agents = num_agents
self.obs_width = obs_width
Expand Down Expand Up @@ -88,6 +89,7 @@ def __init__(
minetest_conf=mt_server_conf,
pipe_proc=pipe_proc,
mt_server_port=mt_server_port,
sync_mode=sync_mode,
)

# create a MT (client) instance for each agent
Expand All @@ -108,6 +110,7 @@ def __init__(
pipe_proc=pipe_proc,
frameskip=frameskip,
rgb_frames=rgb_observations,
sync_mode=sync_mode,
)
self.mt_clients.append(client)

Expand Down
11 changes: 7 additions & 4 deletions src/client/craftium.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ inline int syncClientInit() {
}

inline void syncServerStep() {
if (!g_settings->getBool("sync_env_mode")) {
return;
}

// printf("<= SRV start\n");

if (srv_sem_A == nullptr)
Expand All @@ -129,6 +133,9 @@ inline void syncServerStep() {
}

inline void syncClientStep() {
if (!g_settings->getBool("sync_env_mode")) {
return;
}
// printf("> CLI start\n");

if (cli_sem_A == nullptr)
Expand All @@ -144,10 +151,6 @@ inline void syncClientStep() {
exit(EXIT_FAILURE);
}

// if (g_settings->getBool("multi_agent")) {
// return;
// }

// printf("< CLI end\n");
}

Expand Down

0 comments on commit b5325d8

Please sign in to comment.