Skip to main content

Open AI Gym to play 1v1 Catan against a random bot

Project description

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 gym

env = gym.make("catanatron_gym:catanatron-v0")
observation = env.reset()
for _ in range(1000):
  action = random.choice(env.get_valid_actions()) # your agent here (this takes random actions)

  observation, reward, done, info = env.step(action)
  if done:
      observation = 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 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-v0")
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-v0",
    config={
        "map_type": "MINI",
        "vps_to_win": 6,
        "enemies": [WeightedRandomPlayer(Color.RED), WeightedRandomPlayer(Color.ORANGE)],
        "reward_function": my_reward_function,
        "representation": "mixed",
    },
)

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

catanatron_gym-3.2.1.tar.gz (14.3 kB view details)

Uploaded Source

Built Distribution

catanatron_gym-3.2.1-py3-none-any.whl (14.6 kB view details)

Uploaded Python 3

File details

Details for the file catanatron_gym-3.2.1.tar.gz.

File metadata

  • Download URL: catanatron_gym-3.2.1.tar.gz
  • Upload date:
  • Size: 14.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.6

File hashes

Hashes for catanatron_gym-3.2.1.tar.gz
Algorithm Hash digest
SHA256 60a376a2d29489a5c7f7f9ba8e5251700264f0305b57eafb7215ace85c931da7
MD5 eb2188c0c4e59ea75100ad8fc04a6471
BLAKE2b-256 d723cea986ed6baf9ff0b76b57b43c82528b1e383522721cef1dabad9d0fae45

See more details on using hashes here.

File details

Details for the file catanatron_gym-3.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for catanatron_gym-3.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 67e0a51be18418393e600c0a9f388b9da702751eeb5737f0a6cc8c87bf89c2a0
MD5 19a67dcc152f2fcf71860fb40ce65b72
BLAKE2b-256 9d9532c77dab766387c9f7e5968670bcc9a4a8ab3855e0e3e57e7c3e3e1007cf

See more details on using hashes here.

Supported by

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