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

Import and refactor aiAdrian's dead lock avoidance policy #2

Draft
wants to merge 21 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 20 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
6 changes: 5 additions & 1 deletion .github/workflows/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ jobs:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Convert to github_repo_name and tag string to lowercase
id: convert2lowercase
run: INPUT=${{ github.repository }}:${{ github.event.inputs.tag }}; echo "REPO_TAG_LOWERCASE=${INPUT,,}">>${GITHUB_OUTPUT}
- run: echo ${{steps.convert2lowercase.outputs.REPO_TAG_LOWERCASE}}
- name: Build and Push Container Image
uses: docker/build-push-action@v6
with:
Expand All @@ -33,4 +37,4 @@ jobs:
platforms: linux/amd64,linux/arm64/v8
push: true
tags: |
ghcr.io/${{ github.repository }}:${{ github.event.inputs.tag }}
ghcr.io/${{ steps.convert2lowercase.outputs.REPO_TAG_LOWERCASE }}
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ RUN conda --version && \
python -c 'from flatland.evaluators.client import FlatlandRemoteClient'

COPY run.sh ./
COPY random_agent.py ./
COPY src/ ./src
COPY run_solution.py ./

ENTRYPOINT bash run.sh
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ More precisely, Flatland 3 Benchmarks follow Flatland 3 Challenge's
[Round 2 Environment Configurations](https://flatland-association.github.io/flatland-book/challenges/flatland3/envconfig.html#round-2), having the same
environment configuration but generated with different seeds.

This starterkit features a random agent [random_agent.py](random_agent.py)
This starterkit features a shortest path deadlock avoidance agent [run_solution.py](run_solution.py)

## TL;DR; aka. First Submission

Expand Down Expand Up @@ -41,6 +41,7 @@ There is a `demo` showcase illustrating this setup:

```shell
cd demo
cocker compose build
docker compose up
```

Expand Down
55 changes: 0 additions & 55 deletions random_agent.py

This file was deleted.

2 changes: 1 addition & 1 deletion run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ source /home/conda/.bashrc
source activate base
conda activate flatland-rl
python -m pip list
python random_agent.py
python run_solution.py
echo "\\ end submission_template/run.sh"
93 changes: 93 additions & 0 deletions run_solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
from flatland.envs.rail_env import RailEnv
from flatland.evaluators.client import FlatlandRemoteClient

from src.observation.dummy_observation import FlatlandDummyObservation
from src.policy.deadlock_avoidance_policy import DeadLockAvoidancePolicy
from src.policy.random_policy import RandomPolicy
from src.utils.progress_bar import ProgressBar

remote_client = FlatlandRemoteClient()

# ------------------- user code -------------------------
my_observation_builder = FlatlandDummyObservation()
flatlandSolver = DeadLockAvoidancePolicy()

use_random_policy = False
if use_random_policy:
flatlandSolver = RandomPolicy()
# ---------------------------------------------------------

episode = 0

while True:
print("/ start {}".format(flatlandSolver.get_name()), flush=True)
episode += 1
print("[INFO] EPISODE_START : {}".format(episode))
# NO WAY TO CHECK service/self.evaluation_done in client

observations, info = remote_client.env_create(obs_builder_object=my_observation_builder)
if isinstance(observations, bool):
if not observations:
"""
The remote env returns False as the first obs
when it is done evaluating all the individual episodes
"""
print("[INFO] DONE ALL, BREAKING")
break

# ------------------- user code -------------------------
# init the policy
env: RailEnv = remote_client.env
flatlandSolver.reset(env)
flatlandSolver.start_episode(False)
# ---------------------------------------------------------

pbar = ProgressBar()
total_reward = 0
nbr_done = 0
while True:
try:
# ------------------- user code -------------------------
# call the policy to act
flatlandSolver.start_step(False)
# ---------------------------------------------------------

# ------------------- user code -------------------------
actions = {}
eps = 0
for handle in env.get_agent_handles():
# choose action for agent (handle)
action = flatlandSolver.act(handle, observations[handle])
actions.update({handle: action})
# ---------------------------------------------------------

observations, all_rewards, done, info = remote_client.env_step(actions)
total_reward += sum(list(all_rewards.values()))
if env._elapsed_steps < env._max_episode_steps:
nbr_done = sum(list(done.values())[:-1])

# ------------------- user code -------------------------
flatlandSolver.end_step(False)
# ---------------------------------------------------------

except:
print("[ERR] DONE BUT step() CALLED")

if (True): # debug
if done['__all__']:
pbar.console_print(nbr_done, env.get_num_agents(), 'Nbr of done agents: {}'.format(len(done) - 1), '')

# break
if done['__all__']:
print("[INFO] TOTAL_REW: ", total_reward)
print("[INFO] EPISODE_DONE : ", episode)
break

# ------------------- user code -------------------------
# do clean up
flatlandSolver.end_episode(False)
# ---------------------------------------------------------

print("Evaluation Complete...")
print(remote_client.submit())
print("\\ end random_agent", flush=True)
15 changes: 15 additions & 0 deletions src/observation/dummy_observation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from typing import Optional, List

import numpy as np
from flatland.core.env_observation_builder import DummyObservationBuilder


class FlatlandDummyObservation(DummyObservationBuilder):
def __init__(self):
pass

def get_many(self, handles: Optional[List[int]] = None) -> bool:
obs = np.ones((len(handles), 1))
for handle in handles:
obs[handle][0] = handle
return obs
Loading