Skip to main content

PettingZoo environment for Polytopia-style multi-agent reinforcement learning

Project description

Polyterra Environment

A PettingZoo-compatible multi-agent reinforcement learning environment for The Battle of Polytopia.

Installation

pip install polyterra-env

That's it. The C# game engine is bundled — no .NET SDK or extra build steps required.

Note: Currently only supports macOS ARM (Apple Silicon). Linux/x64 support coming soon.

Quick Start

from polyterra_env import PolyterraEnv
import numpy as np

env = PolyterraEnv(num_players=2)
env.reset(seed=42)

# Game loop
while env.agents:
    agent = env.agent_selection
    obs = env.observe(agent)

    # Pick a random valid action
    mask = obs["valid_actions_mask"]
    valid_indices = np.where(mask == 1)[0]
    action = int(np.random.choice(valid_indices))

    env.step(action)

    if all(env.terminations.get(a, False) for a in env.possible_agents):
        break

env.close()

Observation Space

The observation is a Dict containing:

Key Description
turn Current game turn
currency Stars (in-game currency)
score Current score
tiles Full map state — terrain, improvements, units, visibility
units List of own units with type, health, position, status
cities List of own cities with level, population, production
opponents Partial info on other players (fog of war)
available_techs One-hot vector of researchable technologies
valid_actions_list Padded list of 512 action dicts
valid_actions_mask Binary mask over the 512 action slots

Action Space

Discrete(512) — the agent picks an index into valid_actions_list.

Each valid action is a dict with a type field:

  • end_turn — end the current turn
  • move — move a unit
  • attack — attack an enemy
  • build — build an improvement (farm, mine, etc.)
  • train — train a unit at a city
  • research — research a technology
  • capture — capture a village/city
  • harvest — harvest a resource
  • city_reward — choose a city level-up reward

Use valid_actions_mask for action masking during training.

Training

Works with standard RL libraries. Install training dependencies:

pip install polyterra-env[training]

With Stable-Baselines3 (MaskablePPO)

from polyterra_env import PolyterraEnv
from sb3_contrib import MaskablePPO
from sb3_contrib.common.wrappers import ActionMasker

# See training/train_ppo.py for a full example with reward shaping

With RLlib (Self-Play)

from polyterra_env import PolyterraEnv

# See training/train_rllib.py for a full multi-agent self-play example

Game Modes

  • Perfection — score-based, fixed number of turns (default 30)
  • Domination — last player standing

Development Setup

To modify the C# backend or Python environment:

git clone https://github.com/yourusername/polyterra-env.git
cd polyterra-env
./setup.sh   # Builds C# backend + installs Python deps

Requires .NET 8.0 SDK for development only. End users don't need it.

Running Tests

pytest tests/

Project Structure

polyterra-env/
├── src/polyterra_env/        # Python package
│   ├── env.py                # PettingZoo AEC environment
│   ├── game_data_mappings.py # Entity name/index mappings
│   ├── _backend.py           # C# binary discovery
│   └── backend/              # Bundled C# binary (gitignored)
├── csharp-backend/           # C# game engine source
├── polytopia-game-logic/     # Game logic (C# dependency)
├── training/                 # Training scripts & web UI
├── tests/                    # Test suite
└── scripts/build_backend.py  # Build script for C# backend

License

This project uses decompiled game logic from The Battle of Polytopia for educational and research purposes. All game assets and logic remain property of Midjiwan AB.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

polyterra_env-0.1.1.tar.gz (7.3 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

polyterra_env-0.1.1-py3-none-any.whl (6.4 MB view details)

Uploaded Python 3

File details

Details for the file polyterra_env-0.1.1.tar.gz.

File metadata

  • Download URL: polyterra_env-0.1.1.tar.gz
  • Upload date:
  • Size: 7.3 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.22

File hashes

Hashes for polyterra_env-0.1.1.tar.gz
Algorithm Hash digest
SHA256 46a141d3e0dc35ba74c36f952cc07089bdc537a188bc779d251db1d781476e8c
MD5 acd6852a9add46df3172a722a79ed513
BLAKE2b-256 04facf5fe276a0060ea18efd3ec1b64a17b0bce8f4c3637408f16d0b16133733

See more details on using hashes here.

File details

Details for the file polyterra_env-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for polyterra_env-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c7dc53015d29ede90710186a618a1ad9407fcbdf2d17a9b71b66452cb4520724
MD5 19f898c62720f73ea264a23455578e9b
BLAKE2b-256 5b6d14cacf0f4d49c4e4ee8709d7f2d2f8b0f25f74d394a5fe8f907fc904494f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page