-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add class/bot registering functionality
* Add class/bot registering functionality. * Refactor .md files. * Run black linter.
- Loading branch information
Showing
11 changed files
with
318 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# Get started | ||
|
||
## Play | ||
If the user just wants to play with the available Bots and games, the procedure is simple. After launching PygameSpiel, the main menu appears showing two dropdown menus which include the available games and Bots. Simply select the game and BOT and click play. | ||
|
||
## Register a new bot | ||
To dynamically add new algorithms, Pygame_spiel uses the Bot class from pyspiel as interface (pyspiel.Bot). This class is present in the OpenSpiel library, and it's currently used as template for other available algorithms in OpenSpiel (e.g., [MCTSBot](https://github.com/google-deepmind/open_spiel/blob/master/open_spiel/python/algorithms/mcts.py), [RandomUniform](https://github.com/google-deepmind/open_spiel/blob/master/open_spiel/python/bots/uniform_random.py), [Human](https://github.com/google-deepmind/open_spiel/blob/master/open_spiel/python/bots/human.py), ...). This class offers a useful format to create a generic interface between new algorithms and Pygame_spiel. The user can create a new class, which includes the logic of their new algorithm. | ||
|
||
### Create a Bot class | ||
In the snippet below we show an example of a simple Bot which always select the first legal action: | ||
|
||
```python | ||
import pyspiel | ||
|
||
class Dummy(pyspiel.Bot): | ||
|
||
def __init__(self, game, player_id): | ||
pyspiel.Bot.__init__(self) | ||
self._player_id = player_id | ||
|
||
def step(self, state): | ||
legal_actions = state.legal_actions(self._player_id) | ||
if not legal_actions: | ||
return [], pyspiel.INVALID_ACTION | ||
action = legal_actions[0] # Always choose 1st action | ||
return action | ||
``` | ||
This class definition can be saved in a .py file in any folder. | ||
**NOTE:** There are two requirements when creating a new Bot: | ||
1) The class needs to be inherited from pyspiel.Bot | ||
2) The class needs to have the two mandatory arguments **game** and **player_id**. While these might not be used by the Bot, Pygame_spiel expects these parameters. | ||
|
||
### Register the class | ||
After launching Pygame_spiel, the user can provide the path the the Python file containing the Bot definition, which will be loaded and visible in the list of Bots (called "Opponent" in the main menu). In the *Module* dropdown menu navigate through the filesystem and select the Python file | ||
|
||
![alt text](images/module_dropdown_1.png) | ||
|
||
After selecting the file, the file name will be visualized below the dropdown menu as in the next image. Also, if the Python file contains classes inherited from pyspiel.Bot, the class names will be printed next to the file name. This information can be used to know if the Bots have been correctly registered or not. If no names are printed after the text "new Bots:", the classes have not been correctly registered. | ||
|
||
![alt text](images/module_dropdown_2.png) | ||
|
||
If the new classes are correctly registered, the class names will appear in the *Opponent* dropdown menu, after the Bots which are already available in Pygame_spiel by default (see the new class "Dummy" in the image below). | ||
|
||
![alt text](images/module_dropdown_3.png) | ||
|
||
After this, just click on *Play*!. |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Installation | ||
|
||
## Just play | ||
If the user just wants to play with the algorithms and games available by default, without changing or adding extra functionalities to Pygame_spiel or OpenSpiel, they can just install the PyPI package and run the game. To install Pygame_spiel run: | ||
```bash | ||
pip install pygame_spiel[spiel] | ||
``` | ||
Adding the parameter **[spiel]** within square brackets forces pip to install OpenSpiel as a dependency. OpenSpiel is not installed by default, because the main use case in PyGame_spiel is to support users who are working on a local clone of OpenSpiel, and by specifying it as dependency, the PyPI installation of OpenSpiel would override the local one. | ||
|
||
NOTE: if you're using a zsh terminal, specify pygame_spiel[spiel] within single quotes: | ||
```bash | ||
pip install 'pygame_spiel[spiel]' | ||
``` | ||
|
||
To launch Pygame_spiel run: | ||
```bash | ||
pygame_spiel | ||
``` | ||
|
||
## With the local version of OpenSpiel | ||
If you are working with a OpenSpiel source code locally, you can install PyGame_spiel as an extra library for local development. There are two ways to install PyGame_spiel: 1) clone the repo and install it, 2) via pip install. | ||
|
||
### Install via pip | ||
If the user only needs to experiment with new algorithms, and no additional games or changes in the GUI are required, the best way to install Pygame_spiel is via pip install. Unlike with the installation in the previous section, when working with an existing version of OpenSpiel installed locally, run the following: | ||
```bash | ||
pip install 'pygame_spiel' | ||
``` | ||
Here we don't specify the parameter **[spiel]**, since we don't want to override the local version of OpenSpiel with the library from pip. Here we assume that OpenSpiel is already installed locally via pip (as explained in secion 4 in [OpenSpiel Installation tutorial](https://github.com/google-deepmind/open_spiel/blob/master/docs/install.md)). | ||
|
||
### Clone the repo | ||
If you want to customize PyGame_spiel while also working on a local version of OpenSpiel, the simplest way is to clone the repository and install it. | ||
1) Clone pygame_spiel | ||
2) run pip install . | ||
3) run pygame_spiel from terminal as usual to launch it | ||
|
||
By cloning the repo the user full access to the code, and the possibility to modify any part of the code (including the graphical UI and elements). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import pyspiel | ||
|
||
|
||
class Dummy(pyspiel.Bot): | ||
def __init__(self, game, player_id): | ||
pyspiel.Bot.__init__(self) | ||
self._player_id = player_id | ||
|
||
def step(self, state): | ||
legal_actions = state.legal_actions(self._player_id) | ||
if not legal_actions: | ||
return [], pyspiel.INVALID_ACTION | ||
action = legal_actions[0] # Always choose 1st action | ||
return action |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.