GithubHelp home page GithubHelp logo

a-r-r-o-w / pacman-rl Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 364 KB

Reinforcement learning environment for Pacman in C++

License: MIT License

CMake 1.02% C++ 22.73% Shell 0.77% Jupyter Notebook 75.49%
cpp cpp20 gym-environment machine-learning pybind11 python3 reinforcement-learning

pacman-rl's Introduction

PacmanRL

PacmanRL is a reinforcement learning environment for Pacman with an API similar to OpenAI Gym.

Note: This is a WIP. The code is held together with duct tape and will probably break or not compile if you try to use it ๐Ÿซ 

Installation

Linux

You'll need to have installed CMake before you can build PacmanRL.

git clone --recurse-submodules --depth 1 https://github.com/a-r-r-o-w/pacman-rl.git
cd pacman-rl
./build.sh -t RELEASE

# Tested on PopOS 22.04 LTS (NVIDIA)
# CMake 3.26.0
# GCC 11.3.0

Yeah, no. That's probably not going to work since you require a few apt install's before you can build PacmanRL. It should be easy to figure out based on the error messages though, right...? I wrote the initial version of this over a year ago and cannot recall all the packages required. Will update this soon.

Windows

Nope. To be updated soon.

Usage

Python
import random
import time

# I haven't figured out how to write a setup.py yet to properly install this
import build.pacman_rl as pacman_rl

# No comments ๐Ÿ˜” I'm sorry, I'll improve this soon...
config = pacman_rl.Config()
config.rows = 21
config.cols = 19
config.max_episode_steps = 200
config.pacman_lives = 3
config.map = [
    "###################",
    "#........#........#",
    "#@##.###.#.###.##@#",
    "#.................#",
    "#.##.#.#####.#.##.#",
    "#....#...#...#....#",
    "####.###.#.###.####",
    "####.#...0...#.####",
    "####.#.##G##.#.####",
    "#......#123#......#",
    "####.#.#####.#.####",
    "####.#...P...#.####",
    "####.#.#####.#.####",
    "#........#........#",
    "#.##.###.#.###.##.#",
    "#@.#...........#.@#",
    "##.#.#.#####.#.#.##",
    "#....#...#...#....#",
    "#.######.#.######.#",
    "#.................#",
    "###################",
]

random.seed(42)

# Don't name the below variables the same.
# There's this weird bug that causes the game to crash if you do.
# A C++ destructor is called twice apparently, due to the difference in how
# C++ and Python handle memory management.
# As I mentioned, this is held together with duct tape.
pacman_env = pacman_rl.make(config, pacman_rl.RenderMode.HUMAN)
env = pacman_rl.RecordVideoEnvironment(pacman_env)
state = env.reset()

moves = [
    pacman_rl.MovementDirection.right,
    pacman_rl.MovementDirection.left,
    pacman_rl.MovementDirection.up,
    pacman_rl.MovementDirection.down,
]

while not state.completed:
    old_pacman_location = state.pacman_location
    move = random.choice(moves)
    state = env.step(move)
    env.render()
    time.sleep(0.1)

env.close()
C++
#include <iostream>
#include <thread>
#include <vector>

// TODO: Make a single header file to include all of these
#include "types.hpp"
#include "environment.hpp"
#include "wrappers/record_video_env.hpp"

int main() {
  const i32 sleep_ms = 50;

  // Sorry, will work on making this better
  const Config config = {
    .rows = 21,
    .cols = 19,
    .max_episode_steps = 200,
    .map = {
      "###################",
      "#........#........#",
      "#@##.###.#.###.##@#",
      "#.................#",
      "#.##.#.#####.#.##.#",
      "#....#...#...#....#",
      "####.###.#.###.####",
      "####.#...0...#.####",
      "####.#.##G##.#.####",
      "#......#123#......#",
      "####.#.#####.#.####",
      "####.#...P...#.####",
      "####.#.#####.#.####",
      "#........#........#",
      "#.##.###.#.###.##.#",
      "#@.#...........#.@#",
      "##.#.#.#####.#.#.##",
      "#....#...#...#....#",
      "#.######.#.######.#",
      "#.................#",
      "###################",
    },
    .pacman_lives = 3,
  };

  PacmanEnvironment p(config, RenderMode::human);
  RecordVideoEnvironment e(p);

  std::vector <MovementDirection> moves = {
    MovementDirection::left, MovementDirection::up, MovementDirection::right, MovementDirection::down
  };

  State state = e.reset();

  while (not state.completed) {
    state = e.step(moves[rand() % 4]);
    e.render();
    std::this_thread::sleep_for(std::chrono::milliseconds(sleep_ms));
  }

  e.close();

  return 0;
}

Results

After training for not too many steps (my GPU was dying because of how unoptimized this is), here's an interesting run that demonstrates duct tape code in action, buggy implementation of the environment, and a Pacman that's not very good at playing Pacman.

output.mp4

License

MIT

pacman-rl's People

Contributors

a-r-r-o-w avatar

Stargazers

 avatar

Watchers

 avatar

pacman-rl's Issues

Create setup.py and improve install instructions

Currently, there is no direct way of installing PacmanRL easily. You need to clone the repo, download all the tools necessary for building, and the final exported bindings lib.so file for Python is not directly importable (i.e. needs to be imported from the build/ directory or wherever you decide to move it). A setup.py file that handles the installation/building by running python3 setup.py [develop|build|install] will be very useful here.

Updates to the README with proper install instructions for different OS will also be really helpful.

Probably helpful resource: https://gist.github.com/hovren/5b62175731433c741d07ee6f482e2936

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.