Skip to content

Commit

Permalink
Import example learning agents
Browse files Browse the repository at this point in the history
  • Loading branch information
juztamau5 committed Mar 11, 2024
1 parent 8d9932d commit 198712e
Show file tree
Hide file tree
Showing 19 changed files with 1,575 additions and 2 deletions.
97 changes: 97 additions & 0 deletions .github/workflows/python.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
################################################################################
#
# Copyright (C) 2024 retro.ai
# This file is part of retro3 - https://github.com/retroai/retro3
#
# SPDX-License-Identifier: AGPL-3.0-or-later
# See the file LICENSE.txt for more information.
#
################################################################################

name: Python CI

on: [push, pull_request]

jobs:
build-and-deploy:
# The type of runner that the job will run on
runs-on: ${{ matrix.os }}

defaults:
run:
# Set the working directory to frontend folder
working-directory: src/learning

strategy:
fail-fast: false
matrix:
include:
- os: ubuntu-20.04
python-version: '3.10'
- os: ubuntu-22.04
python-version: '3.12'

steps:
- name: Checkout 🛎️
uses: actions/checkout@v4

- name: Cache OpenAI modules
id: cache-openai
uses: actions/cache@v4
with:
path: openai
key: cache-openai-${{ matrix.os }}-${{ matrix.python-version }}-${{ hashFiles('openai/**') }}

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}

- name: Install OpenAI dependencies
if: steps.cache-openai.outputs.cache-hit != 'true'
run: |
sudo apt-get update
sudo apt-get install -y \
build-essential \
cmake \
libgtest-dev
python3 -m pip install --upgrade pip setuptools setuptools_scm
- name: Build OpenAI modules
if: steps.cache-openai.outputs.cache-hit != 'true'
run: |
cd ../../openai
cmake .
make -j
- name: Install poetry
run: |
curl -sSL https://install.python-poetry.org | python3 -
- name: Configure Poetry
run: |
poetry config virtualenvs.create false
- name: Install Python dependencies
run: |
poetry install
- name: Check formatting with Black
run: |
poetry run black --check .
- name: Lint with Flake8
run: |
poetry run flake8 .
- name: Sort imports with isort
run: |
poetry run isort . --check-only
- name: Type check with mypy
run: |
poetry run mypy . --check-untyped-defs
- name: Run test cases
run: |
poetry run pytest
1 change: 1 addition & 0 deletions openai/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ if(NOT CMAKE_BUILD_TYPE)
endif()
option(BUILD_LUAJIT "Should static LuaJIT be used instead of system Lua" ON)
option(BUILD_MANYLINUX "Should use static libraries compatible with manylinux1" OFF)
option(BUILD_TESTING "Should build tests" OFF)

set(BUILD_PYTHON ON CACHE BOOL "Build Python module")
mark_as_advanced(BUILD_PYTHON)
Expand Down
2 changes: 1 addition & 1 deletion openai/retro/data/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from enum import Flag
except ImportError:
# Python < 3.6 doesn't support Flag, so we polyfill it ourself
class Flag(enum.Enum):
class Flag(enum.Enum): # type: ignore
def __and__(self, b):
value = self.value & b.value
try:
Expand Down
1 change: 0 additions & 1 deletion openai/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,5 @@ def run(self):
'retro.data.contrib': platform_globs,
},
setup_requires=['setuptools_scm'],
use_scm_version=use_scm_version,
**kwargs
)
10 changes: 10 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "retro3-meta",
"version": "1.0.0",
"scripts": {
"build": "npm run build:openai",
"build:openai": "cd openai && cmake . && make -j",
"clean": "npm run clean:openai",
"clean:openai": "cd openai && (git clean -xdf . || true)"
}
}
3 changes: 3 additions & 0 deletions src/learning/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.mypy_cache
.pytest_cache
__pycache__
40 changes: 40 additions & 0 deletions src/learning/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# retro.ai learning environment

## Dependencies

This project is managed by poetry. Install it with:

```bash
pip install poetry
```

Then, install the dependencies with:

```bash
poetry install
```

## Running the tests

To run the tests, use the following command:

```bash
poetry run ci
```

## Running the formatters

To run the formatters, use the following command:

```bash
poetry run format
```

## Running the agents

Learning agents are placed in the `agents` directory. To run an agent, invoke it directly:

```bash
cd agents
./random_agent.py
```
74 changes: 74 additions & 0 deletions src/learning/agents/brute_agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#!/usr/bin/env python3
################################################################################
#
# Copyright (C) 2023-2024 retro.ai
# This file is part of retro3 - https://github.com/retroai/retro3
#
# This file is derived from OpenAI's Gym Retro under the MIT license
# Copyright (C) 2017-2018 OpenAI (http://openai.com)
#
# SPDX-License-Identifier: AGPL-3.0-or-later AND MIT
# See the file LICENSE.txt for more information.
#
################################################################################


import os
import sys

# Get the absolute path of the current script's directory
current_dir: str = os.path.dirname(os.path.abspath(__file__))

# Get the parent directory of the current directory, which is the project root
project_root: str = os.path.dirname(current_dir)

# Add the project root to sys.path
sys.path.insert(0, project_root)


from typing import Optional

import gymnasium

import retroai.brute as brute_module
import retroai.enums
import retroai.retro_env


def main() -> None:
game: str = "Airstriker-Genesis"
state: retroai.enums.State = retroai.enums.State.DEFAULT
scenario: Optional[str] = None
max_episode_steps: int = 4500
timestep_limit: float = 1e8

retro_env: retroai.retro_env.RetroEnv = retroai.retro_env.retro_make(
game,
state,
use_restricted_actions=retroai.enums.Actions.DISCRETE,
scenario=scenario,
)

env: gymnasium.Wrapper = brute_module.Frameskip(retro_env)
env = brute_module.TimeLimit(env, max_episode_steps=max_episode_steps)

brute: brute_module.Brute = brute_module.Brute(
env, max_episode_steps=max_episode_steps
)
timesteps: int = 0
best_rew: float = float("-inf")
while True:
acts, rew = brute.run()
timesteps += len(acts)

if rew > best_rew:
print("new best reward {} => {}".format(best_rew, rew))
best_rew = rew

if timesteps > timestep_limit:
print("timestep limit exceeded")
break


if __name__ == "__main__":
main()
48 changes: 48 additions & 0 deletions src/learning/agents/random_agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env python3
################################################################################
#
# Copyright (C) 2023-2024 retro.ai
# This file is part of retro3 - https://github.com/retroai/retro3
#
# This file is derived from OpenAI's Gym Retro under the MIT license
# Copyright (C) 2017-2018 OpenAI (http://openai.com)
#
# SPDX-License-Identifier: AGPL-3.0-or-later AND MIT
# See the file LICENSE.txt for more information.
#
################################################################################


import os
import sys

# Get the absolute path of the current script's directory
current_dir = os.path.dirname(os.path.abspath(__file__))

# Get the parent directory of the current directory, which is the project root
project_root = os.path.dirname(current_dir)

# Add the project root to sys.path
sys.path.insert(0, project_root)


import retroai.retro_env


def main() -> None:
env: retroai.retro_env.RetroEnv = retroai.retro_env.retro_make(
game="Airstriker-Genesis"
)

env.reset()
while True:
obs, rew, done, info = env.step(env.action_space.sample())
print(info)
if done:
env.reset()

env.close()


if __name__ == "__main__":
main()
Loading

0 comments on commit 198712e

Please sign in to comment.