Skip to content
Snippets Groups Projects
Commit da7d4369 authored by Christoph Kowalski's avatar Christoph Kowalski
Browse files

Adapted LayoutConverter to not be integrated in gym_env.py but to be called...

Adapted LayoutConverter to not be integrated in gym_env.py but to be called seperately with a command line argument. Additionally, all layouts are now stored in the config/layouts folder and no layout files with a different format are stored within our project.
parent a35949c8
No related branches found
No related tags found
2 merge requests!110V1.2.0 changes,!103Integrated overcooked-ai into cooperative cuisine.
Pipeline #58975 passed
Showing
with 99 additions and 90 deletions
##U##
NA_AN
#___#
#P#$#
\ No newline at end of file
#########
N_#$#N#_$
#_A_U_A_#
#___U___#
###P#P###
\ No newline at end of file
###U#
#__AU
P_#_#
NA__#
#N$##
###U#
N_#AU
N_#_#
PA#_#
###$#
###UU###
#A_____#
P_####_$
#_____A#
###NN###
\ No newline at end of file
......@@ -11,7 +11,7 @@ game:
undo_dispenser_pickup: true
validate_recipes: false
layout_name: rl_small.layout
layout_name: configs/layouts/rl/rl_small.layout
layout_chars:
_: Free
......
......@@ -11,7 +11,7 @@ game:
undo_dispenser_pickup: true
validate_recipes: false
layout_name: rl_small.layout
layout_name: configs/layouts/rl/rl_small.layout
layout_chars:
_: Free
......
......@@ -11,7 +11,7 @@ game:
undo_dispenser_pickup: true
validate_recipes: false
layout_name: rl_small.layout
layout_name: configs/layouts/rl/rl_small.layout
layout_chars:
......
......@@ -11,7 +11,7 @@ game:
undo_dispenser_pickup: true
validate_recipes: false
layout_name: centre_pots.layout
layout_name: configs/layouts/rl/rl_small.layout
layout_chars:
......
from pathlib import Path
import argparse
from pathlib import Path, PurePath
from cooperative_cuisine import ROOT_DIR
def convert_overcookd_ai_layouts(file_root, filename):
def convert_overcookd_ai_layouts():
parser = argparse.ArgumentParser()
parser.add_argument(
"--file", dest="inputfile", help="Input file path", required=True
)
args = parser.parse_args()
filepath = PurePath(args.inputfile)
print(filepath)
convertion_dict = {
" ": "_",
"X": "#",
......@@ -11,20 +20,20 @@ def convert_overcookd_ai_layouts(file_root, filename):
"D": "P",
"S": "$",
"1": "A",
"2": "_"
"2": "A"
}
loadpath = Path(file_root) / "reinforcement_learning" / "layouts" / "overcooked_ai_layouts" / filename
savepath = Path(file_root) / "reinforcement_learning" / "layouts" / filename
with open(loadpath, "r") as f:
savepath = Path(ROOT_DIR) / "configs" / "layouts" / "overcooked-ai" / filepath.name
with open(args.inputfile, "r") as f:
layoutfile = f.read()
f.close()
layout = eval(layoutfile)
lines = layout["grid"].split("\n")
additional_info = []
for key in layout:
if key != "grid":
additional_info.append(
'; {}: {}'.format(key, str(layout[key]).replace("'", "").replace("None", "null")))
layout = eval(layoutfile)
lines = layout["grid"].split("\n")
additional_info = []
for key in layout:
if key != "grid":
additional_info.append(
'; {}: {}'.format(key, str(layout[key]).replace("'", "").replace("None", "null")))
with open(savepath, "w+") as f:
for line in lines:
......@@ -39,4 +48,4 @@ def convert_overcookd_ai_layouts(file_root, filename):
if __name__ == "__main__":
convert_overcookd_ai_layouts(Path().resolve().parents[1], "cramped_corridor.layout")
convert_overcookd_ai_layouts()
......@@ -12,7 +12,6 @@ import yaml
from gymnasium import spaces, Env
from hydra.utils import instantiate
from omegaconf import OmegaConf
from layouts.convert_overcooked_ai_layouts import convert_overcookd_ai_layouts
from cooperative_cuisine import ROOT_DIR
from cooperative_cuisine.action import ActionType, InterActionData, Action
......@@ -35,13 +34,18 @@ class SimpleActionSpace(Enum):
def get_env_action(player_id, simple_action, duration):
"""
Creates a concrete action.
:param player_id:id of the player
:param simple_action: an action in the form of a SimpleActionSpace
:param duration: for how long an action should be conducted
:return: a concrete action
Args:
player_id: id of the player
simple_action: an action in the form of a SimpleActionSpace
duration: for how long an action should be conducted
Returns: a concrete action
"""
match simple_action:
case SimpleActionSpace.Up:
return Action(
......@@ -93,9 +97,11 @@ with open(ROOT_DIR / "pygame_2d_vis" / "visualization.yaml", "r") as file:
def shuffle_counters(env):
"""
Shuffles the counters of an environment
:param env: the environment object
Args:
env: the environment object
"""
sample_counter = []
other_counters = []
......@@ -135,8 +141,10 @@ class EnvGymWrapper(Env):
def __init__(self, config):
"""
:param config: gets the rl and environment configuration from hydra
Initializes all necessary variables
Args:
config:gets the rl and environment configuration from hydra
"""
super().__init__()
self.gridsize = 40
......@@ -151,10 +159,7 @@ class EnvGymWrapper(Env):
self.config_env = config_env
self.config_item_info = config_item_info
layout_file = config_env["layout_name"]
layout_path: Path = ROOT_DIR / "reinforcement_learning" / "layouts" / layout_file
if not Path(layout_path).is_file():
convert_overcookd_ai_layouts(ROOT_DIR, layout_file)
layout_path: Path = ROOT_DIR / layout_file
with open(layout_path, "r") as file:
self.layout = file.read()
self.env: Environment = Environment(
......
##U##
NA_AN
#___#
#P#$#
\ No newline at end of file
{
"grid": """XXXOSSX
X 1 X
X P P X
X 2 X
XDDOXXX""",
"start_bonus_orders": [],
"start_all_orders" : [
{ "ingredients" : ["onion", "onion", "onion"]}
],
"rew_shaping_params": None
}
{
"grid": """XTPXXXXSX
D 2X
XXXX XXXX
D 1X
XOPXXXXSX""",
"start_bonus_orders": [
{ "ingredients" : ["tomato", "onion"]},
{ "ingredients" : ["tomato"]},
{ "ingredients" : ["tomato", "tomato"]}
],
"start_all_orders" : [
{ "ingredients" : ["tomato"]},
{ "ingredients" : ["onion", "onion", "onion"]},
{ "ingredients" : ["tomato", "tomato", "tomato"]},
{ "ingredients" : ["tomato", "onion"] },
{ "ingredients" : ["tomato", "tomato"]}
],
"onion_value" : 21,
"tomato_value" : 13,
"onion_time" : 15,
"tomato_time" : 7
}
\ No newline at end of file
##X#
T__L
U__P
#C$#
......@@ -11,6 +11,7 @@ class BaseStateConverter(StateToObservationConverter):
"""
Converts an environment state to an Encoding where each counter/item has its unique value
"""
def __init__(self):
self.onehot = False
self.grid_height: int | None = None
......@@ -65,6 +66,15 @@ class BaseStateConverter(StateToObservationConverter):
def convert_state_to_observation(self, env) -> np.ndarray:
"""
Convert the environment into an onehot encoding
Args:
env: The environment object used
Returns: An encoding for the environment state that is not onehot
"""
grid_base_array = np.zeros(
(
self.grid_width,
......
......@@ -11,6 +11,7 @@ class BaseStateConverterOnehot(StateToObservationConverter):
"""
Converts an environment state to an Onehot Encoding
"""
def __init__(self):
"""
Constructor setting basic variables as attributes.
......@@ -62,16 +63,25 @@ class BaseStateConverterOnehot(StateToObservationConverter):
def setup(self, env):
"""
Set the grid width and height according to the present environment
Args: Environment used
Args:
env: The environment object used
"""
self.grid_width, self.grid_height = int(env.kitchen_width), int(
env.kitchen_height)
def convert_state_to_observation(self, env) -> np.ndarray:
"""
Converts the environment into an onehot encoding
Args: Environment
Convert the environment into an onehot encoding
Args:
env: The environment object used
Returns: An onehot encoding for the environment state
"""
grid_base_array = np.zeros(
(
self.grid_width,
......
import time
from pathlib import Path
import cv2
from stable_baselines3 import DQN, A2C, PPO
......@@ -15,14 +16,13 @@ def main(cfg: DictConfig):
loads the trained model and enables the user to see an example with the according rewards.
"""
additional_config = OmegaConf.to_container(cfg.additional_configs, resolve=True)
model_save_path = additional_config["log_path"] + "/" + additional_config["checkpoint_path"] + "/" + \
additional_config["project_name"] + "_" + OmegaConf.to_container(cfg.model, resolve=True)[
"model_name"]
model_save_path = Path(additional_config["log_path"]) / Path(additional_config["checkpoint_path"]) / Path(
additional_config["project_name"] + "_" + OmegaConf.to_container(cfg.model, resolve=True)["model_name"])
model_class = call(cfg.model.model_type_inference)
model = model_class(model_save_path)
env = EnvGymWrapper(cfg)
#check_env(env)
# check_env(env)
obs, info = env.reset()
print(obs)
while True:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment