Skip to main content

MultiAgent gym environment for reinforcement learning

Project description

Installation

pip install tankwar-env

or

git clone https://github.com/khoda81/tankwar.git
cd tankwar
pip install -e

or

pip install git+https://github.com/khoda81/tankwar.git

Keyboard

  • Environment:
    • Esc : set env.done to True (end episode on next step)
    • F : toggle limited frame rate

  • HumanAgent:
    • W A S D : movement
    • Q E : rotate turret
    • Space : toggle shooting
    • Left Mouse Button : start shooting when pressed and stop shooting when released

  • Script:
    • G : toggle window update (disabling window update will blur window and increase performance)

Basic Usage

import pygame

from tankwar.agents import HumanAgent, RandomAgent
from tankwar.envs import TankWarEnv


def main():
    random_agents = 2

    # create environment
    env = TankWarEnv(random_agents + 1, shape=(200, 200))

    # create agents
    agents = [HumanAgent(env)] + [RandomAgent(env) for _ in range(random_agents)]

    # initialize a window with the height of 200
    # width is calculated based on env.shape
    # limit frame rate to 60 if a human is playing
    env.init_window(600, True)
    done = False

    # reset environment
    observations = env.reset()
    display_to_human = True

    while True:
        if display_to_human:
            env.render("human")  # render to screen
        else:
            # if render is not called window events are not processed either
            # so, manually processing window events
            env.process_window_events()

        actions = [
            agent.act((None, None), 0, done)
            for agent in agents
        ]

        observations, rewards, done, info = env.step(actions, print_fps=True)

        # if g is pressed screen is toggled
        # disabling screen will make rendering faster
        # because copying frame to window takes time
        for event in env.events:
            if event.type == pygame.KEYDOWN and event.key == pygame.K_g:
                display_to_human = not display_to_human

                if not display_to_human:
                    env.blur_window()

        if done:
            break


if __name__ == "__main__":
    main()

Main Usage

import time

import pygame
import torch
from torch.nn.functional import interpolate
from tqdm import tqdm

from tankwar.agents import HumanAgent, RandomAgent
from tankwar.envs import TankWarEnv

MAX_EPISODE_STEPS = 5000


def main():
    random_agents = 2
    human_agent = False  # whether or not a human is currently playing
    display_to_human = human_agent

    # create environment
    env = TankWarEnv(random_agents + human_agent, shape=(200, 200))
    w, h = env.shape

    # create agents
    agents = (
            [HumanAgent(env)] * human_agent +
            [RandomAgent(env) for _ in range(random_agents)])

    # initialize a window with the height of 200
    # width is calculated based on env.shape
    # limit frame rate to 60 if a human is playing
    env.init_window(h, human_agent)
    padded_frame = torch.zeros(3, w + 200, h + 200)

    observations = env.reset()
    done = False

    rewards = [0] * env.n

    s = time.time()
    for _ in tqdm(range(MAX_EPISODE_STEPS), unit="step"):
        # render to screen
        if display_to_human:
            env.render("human")

        # convert frame to pytorch tensor with shape (3, height, width)
        # frames are cached and will be rendered once per step
        frame = env.render("rgb_array")
        frame_torch = (torch.from_numpy(frame) / 255).permute(2, 0, 1)

        # each frame will be down sampled to (w, h)
        if env.window_scale != 1:
            frame_torch = interpolate(frame_torch.reshape(1, *frame_torch.shape), (h, w))[0]
        # with 100 pixels padding on each side:
        padded_frame[:, 100:-100, 100:-100] = frame_torch

        # image that each agent can see
        images = []
        for i, tank in enumerate(env.tanks):
            x, y = tank.body.position.int_tuple
            images.append(padded_frame[:, y:y + 200, x:x + 200])

        actions = [
            agent.act((observation, image), reward, done)
            for agent, observation, image, reward
            in zip(agents, observations, images, rewards)
        ]

        observations, rewards, done, info = env.step(actions)

        # if g is pressed screen is toggled
        # disabling screen will make rendering faster
        # because copying frame to window takes time
        for event in env.events:
            if event.type == pygame.KEYDOWN and event.key == pygame.K_g:
                display_to_human = not display_to_human

                if not display_to_human:
                    env.blur_window()

        if done:
            break

    print(f"Episode took {time.time() - s:.2f} seconds.")


if __name__ == "__main__":
    main()

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

tankwar-env-1.0.1.tar.gz (9.9 kB view details)

Uploaded Source

Built Distribution

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

tankwar_env-1.0.1-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file tankwar-env-1.0.1.tar.gz.

File metadata

  • Download URL: tankwar-env-1.0.1.tar.gz
  • Upload date:
  • Size: 9.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for tankwar-env-1.0.1.tar.gz
Algorithm Hash digest
SHA256 dd59c0debb551e122e2d54ed2080480ad2fedc257df78732af9ea5e5bf4f8bd8
MD5 a665f9c117f11bd3bf2c4a7200deacf3
BLAKE2b-256 f7dde23933573a4b3c6bca3c316981b282d7b62c57e0ee9074a23b279bb5364e

See more details on using hashes here.

File details

Details for the file tankwar_env-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: tankwar_env-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 9.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for tankwar_env-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4a4a3f8799de4a652428fc19ad99a065db7af540206647688afb183c421c9166
MD5 ff8bbf45b463b7165d3a19c691a616f3
BLAKE2b-256 ea85c4ba6f45182b0891cd5decd4e950f9236d950f6cd683b7928e972e85454c

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