From a75e361dbdd0ae0e149563b7fe065367bad15a51 Mon Sep 17 00:00:00 2001 From: Boyko Vodenicharski Date: Thu, 29 Jun 2023 19:37:28 +0100 Subject: [PATCH 1/2] Change docker image to run the simulation on start --- README.md | 6 +++--- Dockerfile => docker/Dockerfile | 8 ++++++-- docker/entrypoint.sh | 9 +++++++++ 3 files changed, 18 insertions(+), 5 deletions(-) rename Dockerfile => docker/Dockerfile (75%) create mode 100644 docker/entrypoint.sh diff --git a/README.md b/README.md index ba923d7..9669a4b 100644 --- a/README.md +++ b/README.md @@ -79,10 +79,10 @@ 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 p2:latest` -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`. ## Components used diff --git a/Dockerfile b/docker/Dockerfile similarity index 75% rename from Dockerfile rename to docker/Dockerfile index 637dcfd..f42bcaa 100644 --- a/Dockerfile +++ b/docker/Dockerfile @@ -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 @@ -19,5 +19,9 @@ 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 && + +ENTRYPOINT $P2_PATH/pokemon-showdown/entrypoint.sh diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh new file mode 100644 index 0000000..beceb4a --- /dev/null +++ b/docker/entrypoint.sh @@ -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 From 2cfb3de9b6400a21d8d778efdb77c7094bd8a71e Mon Sep 17 00:00:00 2001 From: Boyko Vodenicharski Date: Sat, 1 Jul 2023 16:47:27 +0100 Subject: [PATCH 2/2] Change p2lab to run with config file Example config.yaml added to the root of the repo. Dockerfile now contains a new environment variable which tells the container where it should look for the config file. Additional dependency on pyyaml to parse the config file. __main__.py now parses the arguments in from the config file rather than from inputs. README.md updated with the command that runs the container with a custom config file. By default the docker image will be build with the config.yaml file from the repository. This can be overridden by mounting a config from the local filesystem using the -v flag as shown in the README.md example. --- README.md | 5 +++-- config.yaml | 11 +++++++++++ docker/Dockerfile | 2 ++ pyproject.toml | 1 + src/p2lab/__main__.py | 14 +++++++++++++- 5 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 config.yaml diff --git a/README.md b/README.md index 9669a4b..ba6cbe4 100644 --- a/README.md +++ b/README.md @@ -80,9 +80,10 @@ options: ### Docker: Alternatively, using docker: `docker build -t p2:latest docker/` -`docker run -it p2:latest` +`docker run -it -v .yaml:/home/p2lab-pokemon/config.yaml p2` -The docker image is built to start a server and run `p2lab`. +The docker image is built to start a server and run `p2lab` with the config +mounted at the root of the repo. ## Components used diff --git a/config.yaml b/config.yaml new file mode 100644 index 0000000..02c2129 --- /dev/null +++ b/config.yaml @@ -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 diff --git a/docker/Dockerfile b/docker/Dockerfile index f42bcaa..364e688 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -24,4 +24,6 @@ RUN chmod +x $P2_PATH/pokemon-showdown/entrypoint.sh WORKDIR $P2_PATH/pokemon-showdown +ENV CONFIG_PATH=$P2_PATH/config.yaml + ENTRYPOINT $P2_PATH/pokemon-showdown/entrypoint.sh diff --git a/pyproject.toml b/pyproject.toml index 89a8ab8..465d0bb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -33,6 +33,7 @@ dependencies = [ "numpy==1.24.3", "networkx", "tqdm", + "pyyaml", "pytest" ] diff --git a/src/p2lab/__main__.py b/src/p2lab/__main__.py index 585ad4b..c901688 100644 --- a/src/p2lab/__main__.py +++ b/src/p2lab/__main__.py @@ -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 ( @@ -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( @@ -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"])