Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change docker image to run the simulation on start #61

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,11 @@ options:

### Docker:

Alternatively, using docker: `docker build -t p2:latest .`
`docker run -it p2:latest` `docker exec -it your_container_id /bin/bash`
Alternatively, using docker: `docker build -t p2:latest docker/`
`docker run -it -v <path-to-config>.yaml:/home/p2lab-pokemon/config.yaml p2`

Run docker build with `--no-cache` to rebuild with newer versions of the repos.
The docker image is built to start a server and run `p2lab` with the config
mounted at the root of the repo.

## Components used

Expand Down
11 changes: 11 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
generations: 10 # Number of generations to iterate over
team_size: 3 # Number of pokemon per team (max 6)
teams: 30 # Number of teams i.e., individuals per generation
seed: # Random seed to use
unique: True # Determines if a team can have duplicate pokemon species
crossover: # Determines which crossover function to use
p1: "Player 1" # Name of the first player
p2: "Player 2" # Name of the second player
battles_per_match: 3 # Number of battles per match
write_every: 10 # Write every N generations
write_path: "./results" # Path to write to
10 changes: 8 additions & 2 deletions Dockerfile → docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from python

RUN apt-get update
RUN apt-get upgrade
RUN apt-get upgrade -y
RUN apt-get install nodejs npm -y

ENV ROOT_DIR=/home
Expand All @@ -19,5 +19,11 @@ RUN pip install -e $P2_PATH/poke_env

RUN npm install -g $P2_PATH/pokemon-showdown

COPY ./entrypoint.sh $P2_PATH/pokemon-showdown/entrypoint.sh
RUN chmod +x $P2_PATH/pokemon-showdown/entrypoint.sh

WORKDIR $P2_PATH/pokemon-showdown
ENTRYPOINT node pokemon-showdown start --no-security &&

ENV CONFIG_PATH=$P2_PATH/config.yaml

ENTRYPOINT $P2_PATH/pokemon-showdown/entrypoint.sh
9 changes: 9 additions & 0 deletions docker/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

node pokemon-showdown start --no-security &

while ! curl -I -s 0.0.0.0:8000; do
sleep 1
done

p2lab
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ dependencies = [
"numpy==1.24.3",
"networkx",
"tqdm",
"pyyaml",
"pytest"
]

Expand Down
14 changes: 13 additions & 1 deletion src/p2lab/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import argparse
import asyncio
import os
from pathlib import Path

import numpy as np
import yaml

from p2lab.genetic.genetic import genetic_algorithm
from p2lab.genetic.operations import (
Expand Down Expand Up @@ -90,6 +92,16 @@ async def main_loop(
print(f"Fitness: {fitness}")


def load_config_file(config_filepath):
"""
Helper function that reads a yaml file and returns its contents as a dict.
Args:
:param config_filepath: str, a path pointing to the yaml config.
"""
with Path.open(config_filepath) as yaml_config:
return yaml.load(yaml_config, Loader=yaml.Loader)


def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument(
Expand Down Expand Up @@ -153,7 +165,7 @@ def parse_args():


def main():
args = parse_args()
args = load_config_file(os.environ["CONFIG_PATH"])
if args["seed"] is not None:
np.random.seed(args["seed"])

Expand Down