
* first iteration * change to support openai api endpoints * Refactor APIFactory to use OpenAI-compatible endpoints - Unified all providers to use OpenAI client format - Eliminated provider-specific conditional branches - Simplified provider detection using dict ordering - Removed unused parameters and added missing return - 90% reduction in code complexity * Further simplify APIFactory - Remove redundant MODELS_WITH_IMAGE_SUPPORT array - Use provider config supports_images instead - Inline _prepare_messages logic - Extract _get_reasoning_length helper - Add missing default return - 20+ line reduction while maintaining functionality * removecomment * Inline reasoning length logic - Remove _get_reasoning_length helper method - Inline reasoning effort logic in o1/o3 handling - Keep code simpler and more direct * add provider sorting for openrouter to get fastest throughput * add nitro * add usage tracking * usage * undo changes that added logging * update config paths * remove offset * offset * Aug 20, 2025 at 20:25 * fix run_idx port offset * make sure there is keyerror if no port * fix
Factorio Gym Registry
This module provides a registry system for Factorio gym environments that allows you to create environments using the standard gym.make()
interface.
Overview
The registry system automatically discovers all task definitions in eval/tasks/task_definitions/
and registers them as gym environments. This means you can create any Factorio environment using the familiar gym.make()
pattern.
Features
- Automatic Discovery: Automatically discovers all task definitions in
eval/tasks/task_definitions/
- Gym Integration: All environments are registered with
gym
and can be created usinggym.make()
- Task Metadata: Provides access to task descriptions, configurations, and metadata
- Multi-agent Support: Supports both single-agent and multi-agent environments
- Command-line Tools: Built-in tools for exploring and testing environments
Quick Start
1. List Available Environments
from gym_env.registry import list_available_environments
# Get all available environment IDs
env_ids = list_available_environments()
print(f"Available environments: {env_ids}")
Or use the command-line tool:
python env/src/gym_env/example_usage.py --list
2. Create an Environment
import gym
# Create any available environment
env = gym.make("Factorio-iron_ore_throughput_16-v0")
3. Use the Environment
# Reset the environment
obs = env.reset(options={'game_state': None})
# Take an action
action = Action(
agent_idx=0, # Which agent takes the action
code='print("Hello Factorio!")', # Python code to execute
game_state=None, # game state to reset to before running code
)
# Execute the action
obs, reward, terminated, truncated, info = env.step(action)
# Clean up
env.close()
Available Environments
The registry automatically discovers all JSON task definition files and creates corresponding gym environments. Environment IDs follow the pattern:
Factorio-{task_key}-v0
Example Environment IDs
Factorio-iron_ore_throughput_16-v0
- Iron ore production taskFactorio-iron_plate_throughput_16-v0
- Iron plate production taskFactorio-crude_oil_throughput_16-v0
- Crude oil production taskFactorio-open_play-v0
- Open-ended factory buildingFactorio-automation_science_pack_throughput_16-v0
- Science pack production
Command-Line Tools
Environment Explorer
The example_usage.py
script provides both interactive examples and command-line tools:
# Run interactive examples
python env/src/gym_env/example_usage.py
# List all environments
python env/src/gym_env/example_usage.py --list
# Show detailed information
python env/src/gym_env/example_usage.py --detail
# Search for specific environments
python env/src/gym_env/example_usage.py --search iron
# Output in gym.make() format
python env/src/gym_env/example_usage.py --gym-format
API Reference
Registry Functions
list_available_environments() -> List[str]
Returns a list of all registered environment IDs.
get_environment_info(env_id: str) -> Optional[Dict[str, Any]]
Returns detailed information about a specific environment.
register_all_environments() -> None
Manually trigger environment discovery and registration.
Environment Creation
gym.make(env_id: str, **kwargs) -> FactorioGymEnv
Creates a Factorio gym environment. The environment will:
- Load the task configuration from the JSON file
- Create a Factorio instance
- Set up the task environment
- Return a ready-to-use gym environment
Environment Interface
All environments follow the standard gym interface:
Action Space
{
'agent_idx': Discrete(instance.num_agents), # Index of the agent taking the action
'game_state': Text(max_length=1000000), # The game state to reset to before running code (GameState.to_raw() str)
'code': Text(max_length=10000) # The Python code to execute
}
Observation Space
The observation space includes:
raw_text
: Output from the last actionentities
: List of entities on the mapinventory
: Current inventory stateresearch
: Research progress and technologiesgame_info
: Game state (tick, time, speed)score
: Current scoreflows
: Production statisticstask_verification
: Task completion statusmessages
: Inter-agent messagesserialized_functions
: Available functions
Methods
reset(options: Dict[str, Any], seed: Optional[int] = None) -> Dict[str, Any]
step(action: Dict[str, Any]) -> Tuple[Dict[str, Any], float, bool, bool, Dict[str, Any]]
close() -> None
Task Definitions
Task definitions are JSON files located in eval/tasks/task_definitions/
. Each file defines:
{
"task_type": "throughput",
"config": {
"goal_description": "Create an automatic iron ore factory...",
"throughput_entity": "iron-ore",
"quota": 16,
"trajectory_length": 128,
"task_key": "iron_ore_throughput_16"
}
}
Adding New Tasks
To add a new task:
- Create a JSON file in
eval/tasks/task_definitions/
- Define the task configuration following the existing format
- The registry will automatically discover and register the new environment
Complete Example
Here's a complete example that demonstrates the full workflow:
import gym
from gym_env.registry import list_available_environments, get_environment_info
from gym_env.action import Action
# 1. List available environments
env_ids = list_available_environments()
print(f"Found {len(env_ids)} environments")
# 2. Get information about a specific environment
info = get_environment_info("Factorio-iron_ore_throughput_16-v0")
print(f"Description: {info['description']}")
# 3. Create the environment
env = gym.make("Factorio-iron_ore_throughput_16-v0")
# 4. Use the environment
obs = env.reset(options={'game_state': None})
print(f"Initial observation keys: {list(obs.keys())}")
# 5. Take actions
current_state = None
for step in range(5):
action = Action(
agent_idx=0,
game_state=current_state,
code=f'print("Step {step}: Hello Factorio!")'
)
obs, reward, terminated, truncated, info = env.step(action)
done = terminated or truncated
current_state = info['output_game_state']
print(f"Step {step}: Reward={reward}, Done={done}")
if done:
break
# 6. Clean up
env.close()
Requirements
- Factorio containers must be running and accessible
- All dependencies from the main project must be installed
- The task definitions directory must be accessible
Error Handling
The registry includes error handling for:
- Missing task definition files
- Invalid JSON configurations
- Missing Factorio containers
- Environment creation failures
If an environment fails to load, a warning will be printed but the registry will continue to load other environments.
Troubleshooting
Environment Creation Fails
If gym.make()
fails with connection errors:
- Ensure Factorio containers are running
- Check that the cluster setup is working
- Verify network connectivity
No Environments Found
If no environments are listed:
- Check that the task definitions directory exists
- Verify JSON files are valid
- Check file permissions
Import Errors
If you get import errors:
- Ensure you're running from the correct directory
- Check that all dependencies are installed
- Verify the Python path includes the project root
Advanced Usage
Custom Environment Registration
You can also register custom environments programmatically:
from gym_env.registry import _registry
_registry.register_environment(
env_id="Factorio-CustomTask-v0",
task_key="custom_task",
task_config_path="/path/to/custom_task.json",
description="My custom task",
num_agents=2
)
Multi-Agent Environments
The registry supports multi-agent environments. When creating a multi-agent environment, specify the number of agents:
# Create a multi-agent environment
env = gym.make("Factorio-MultiAgentTask-v0")
# Actions for different agents
action1 = Action(agent_idx=0, code='print("Agent 0 action")', game_state=None)
action2 = Action(agent_idx=1, code='print("Agent 1 action")', game_state=None)
Testing
Run the test suite to verify the registry is working correctly:
python env/tests/gym_env/test_registry.py
This registry system provides a clean, standardized interface for working with Factorio gym environments, making it easy to experiment with different tasks and integrate with existing gym-based frameworks.