Skip to main content

Catanatron Gym

For reinforcement learning purposes, we provide an Open AI Gym environment. To use:

pip install catanatron_gym

Make your training loop, ensuring to respect env.get_valid_actions().

import random
import gymnasium as gym

env = gym.make("catanatron_gym:catanatron-v1")
observation, info = env.reset()
for _ in range(1000):
  # your agent here (this takes random actions)
  action = random.choice(env.unwrapped.get_valid_actions())
  observation, reward, terminated, truncated, info = env.step(action)
  done = terminated or truncated
  if done:
      observation, info = env.reset()
env.close()

For action documentation see here.

For observation documentation see here.

You can access env.game.state and build your own "observation" (features) vector as well.

Stable-Baselines3 Example

Catanatron works well with SB3, and better with the Maskable models of the SB3 Contrib repo. Here a small example of how it may work.

import gymnasium as gym
import numpy as np
from sb3_contrib.common.maskable.policies import MaskableActorCriticPolicy
from sb3_contrib.common.wrappers import ActionMasker
from sb3_contrib.ppo_mask import MaskablePPO

def mask_fn(env) -> np.ndarray:
    valid_actions = env.get_valid_actions()
    mask = np.zeros(env.action_space.n, dtype=np.float32)
    mask[valid_actions] = 1

    return np.array([bool(i) for i in mask])


# Init Environment and Model
env = gym.make("catanatron_gym:catanatron-v1")
env = ActionMasker(env, mask_fn)  # Wrap to enable masking
model = MaskablePPO(MaskableActorCriticPolicy, env, verbose=1)

# Train
model.learn(total_timesteps=1_000_000)

Configuration

You can also configure what map to use, how many vps to win, among other variables in the environment, with the config keyword argument. See source for details.

from catanatron import Color
from catanatron.players.weighted_random import WeightedRandomPlayer


def my_reward_function(game, p0_color):
    winning_color = game.winning_color()
    if p0_color == winning_color:
        return 100
    elif winning_color is None:
        return 0
    else:
        return -100

# 3-player catan on a "Mini" map (7 tiles) until 6 points.
env = gym.make(
    "catanatron_gym:catanatron-v1",
    config={
        "map_type": "MINI",
        "vps_to_win": 6,
        "enemies": [WeightedRandomPlayer(Color.RED), WeightedRandomPlayer(Color.ORANGE)],
        "reward_function": my_reward_function,
        "representation": "mixed",
    },
)

Metadata

Release files for catanatron-gym 4.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for catanatron-gym 4.0.0
File Size Uploaded Provenance
catanatron_gym-4.0.0.tar.gz 14.4 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for catanatron-gym 4.0.0
File Interpreter ABI Platform
catanatron_gym-4.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 29.1 kB

Release files / catanatron_gym-4.0.0.tar.gz

Download URL catanatron_gym-4.0.0.tar.gz
Size 14.4 kB
Tags Source
SHA-256 checksum
How to use checksums
e2d2afa7a061bacd2d01300929f9a1fb53d0598463c8b63b6023ec429d81a719
BLAKE2b-256 checksum
How to use checksums
c6f1b72ab3a8a996180238a86ab6bea198c4c38eb52cd9711ce0b28727956455
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.11.3

Release files / catanatron_gym-4.0.0-py3-none-any.whl

Download URL catanatron_gym-4.0.0-py3-none-any.whl
Size 14.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1fba36b0778d3e44317f2594b678f01338ef9c18462ea3e2ad351a8ef981f157
BLAKE2b-256 checksum
How to use checksums
83b9e425bb49a92ef9db8776d5c782ffa1219f1400378874cb965ec3c02f1148
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.11.3

Security for catanatron-gym 4.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page