Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 27 additions & 7 deletions fle/env/gym_env/environment.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import time

import gymnasium as gym
import numpy as np
Expand Down Expand Up @@ -27,6 +28,26 @@

logger = logging.getLogger(__name__)


def _call_with_retry(fn, what: str, delays=(0, 2, 5)):
"""Call fn(), retrying transient failures with short backoff.

A single transient failure (engine busy, RCON hiccup, malformed
response) must not kill a multi-hour rollout. The underlying error is
included in the raised exception so real failures stay diagnosable.
"""
last_error = None
for attempt, delay in enumerate(delays, start=1):
if delay:
time.sleep(delay)
try:
return fn()
except Exception as e:
last_error = e
logger.warning(f"Error {what} (attempt {attempt}/{len(delays)}): {e}")
raise Exception(f"Error {what}: {last_error}") from last_error


# need to do this since gym doesn't work with numpy>=2.0 otherwise.
np.bool8 = np.dtype(np.bool)

Expand Down Expand Up @@ -304,12 +325,9 @@ def get_observation(
if self.enable_vision:
map_image = namespace._render().to_base64()

# Get entity observations
try:
entities = namespace.get_entities()
except Exception as e:
logger.warning(f"Error getting entities: {e}")
raise Exception("Error getting entities while getting observation") from e
entities = _call_with_retry(
namespace.get_entities, "getting entities while getting observation"
)

entity_obs = [e.__dict__ for e in entities]

Expand Down Expand Up @@ -463,7 +481,9 @@ def step(
)
terminated = task_success.success

production_score, automated_production_score = namespace.score()
production_score, automated_production_score = _call_with_retry(
namespace.score, "getting score after step"
)
if not automated_production_score:
automated_production_score = 0
# Calculate reward
Expand Down
Loading