def add_generated_mdp_params_orders(mdp_params): """ adds generated parameters (i.e. generated orders) to mdp_params, returns onchanged copy of mdp_params when there is no "generate_all_orders" and "generate_bonus_orders" keys inside mdp_params """ mdp_params = copy.deepcopy(mdp_params) if mdp_params.get("generate_all_orders"): all_orders_kwargs = copy.deepcopy( mdp_params["generate_all_orders"]) if all_orders_kwargs.get("recipes"): all_orders_kwargs["recipes"] = [ Recipe.from_dict(r) for r in all_orders_kwargs["recipes"] ] all_recipes = Recipe.generate_random_recipes(**all_orders_kwargs) mdp_params["start_all_orders"] = [r.to_dict() for r in all_recipes] else: Recipe.configure({}) all_recipes = Recipe.ALL_RECIPES if mdp_params.get("generate_bonus_orders"): bonus_orders_kwargs = copy.deepcopy( mdp_params["generate_bonus_orders"]) if not bonus_orders_kwargs.get("recipes"): bonus_orders_kwargs["recipes"] = all_recipes bonus_recipes = Recipe.generate_random_recipes( **bonus_orders_kwargs) mdp_params["start_bonus_orders"] = [ r.to_dict() for r in bonus_recipes ] return mdp_params
def test_attributes(self): self.assertListEqual(self.s1.ingredients, []) self.assertListEqual(self.s2.ingredients, [Recipe.ONION, Recipe.ONION, Recipe.TOMATO]) self.assertListEqual(self.s3.ingredients, [Recipe.ONION]) self.assertListEqual(self.s4.ingredients, [Recipe.TOMATO, Recipe.TOMATO]) try: self.s1.recipe self.fail("Expected ValueError to be raised") except ValueError as e: pass except Exception as e: self.fail( "Expected ValueError to be raised, {} raised instead".format( e)) try: self.s2.recipe self.fail("Expected ValueError to be raised") except ValueError as e: pass except Exception as e: self.fail( "Expected ValueError to be raised, {} raised instead".format( e)) self.assertEqual(self.s3.recipe, Recipe([Recipe.ONION])) self.assertEqual(self.s4.recipe, Recipe([Recipe.TOMATO, Recipe.TOMATO]))
def setUp(self): Recipe.configure({}) trajectory_path = os.path.join(TESTING_DATA_DIR, "test_visualizations", "trajectory.json") events_path = os.path.join(TESTING_DATA_DIR, "test_visualizations", "expected_extracted_events.json") self.trajectory1 = AgentEvaluator.load_traj_from_json(trajectory_path) self.extracted_events1 = load_from_json(events_path)
def test_all_recipes(self): for recipe in self.recipes: self.assertTrue(recipe in Recipe.ALL_RECIPES) self.assertEqual(len(Recipe.ALL_RECIPES), self._expected_num_recipes(len(Recipe.ALL_INGREDIENTS), Recipe.MAX_NUM_INGREDIENTS)) Recipe.configure({ "max_num_ingredients" : 4 }) self.assertEqual(len(Recipe.ALL_RECIPES), self._expected_num_recipes(len(Recipe.ALL_INGREDIENTS), 4))
def _configure_recipe_if_needed(config={}): """ method that configures recipe to avoid ValueError when recipe is created before configuring class recipe is created on reading trajectory because recipe is part of the order that is part of the state that is part of the treajectory """ if not Recipe._configured: print("Configuring recipe from AgentEvaluator class with config:", config) Recipe.configure(config)
def test_recipes_generation(self): self.assertRaises(AssertionError, Recipe.generate_random_recipes, max_size=Recipe.MAX_NUM_INGREDIENTS + 1) self.assertRaises(AssertionError, Recipe.generate_random_recipes, min_size=0) self.assertRaises(AssertionError, Recipe.generate_random_recipes, min_size=3, max_size=2) self.assertRaises(AssertionError, Recipe.generate_random_recipes, ingredients=["onion", "tomato", "fake_ingredient"]) self.assertRaises(AssertionError, Recipe.generate_random_recipes, n=99999) self.assertEqual(len(Recipe.generate_random_recipes(n=3)), 3) self.assertEqual( len(Recipe.generate_random_recipes(n=99, unique=False)), 99) two_sized_recipes = [ Recipe(["onion", "onion"]), Recipe(["onion", "tomato"]), Recipe(["tomato", "tomato"]) ] for _ in range(100): self.assertCountEqual( two_sized_recipes, Recipe.generate_random_recipes(n=3, min_size=2, max_size=2, ingredients=["onion", "tomato"])) only_onions_recipes = [ Recipe(["onion", "onion"]), Recipe(["onion", "onion", "onion"]) ] for _ in range(100): self.assertCountEqual( only_onions_recipes, Recipe.generate_random_recipes(n=2, min_size=2, max_size=3, ingredients=["onion"])) self.assertCountEqual( only_onions_recipes, set([ Recipe.generate_random_recipes(n=1, recipes=only_onions_recipes)[0] for _ in range(100) ])) # false positives rate for this test is 1/10^99
def setUp(self): Recipe.configure({}) self.s1 = SoupState.get_soup((0, 0), num_onions=0, num_tomatoes=0) self.s2 = SoupState.get_soup((0, 1), num_onions=2, num_tomatoes=1) self.s3 = SoupState.get_soup((1, 1), num_onions=1, num_tomatoes=0, cooking_tick=1) self.s4 = SoupState.get_soup((1, 0), num_onions=0, num_tomatoes=2, finished=True)
def test_random_layout_generated_recipes(self): only_onions_recipes = [Recipe(["onion", "onion"]), Recipe(["onion", "onion", "onion"])] only_onions_dict_recipes = [r.to_dict() for r in only_onions_recipes] # checking if recipes are generated from mdp_params mdp_gen_params = {"generate_all_orders": {"n":2, "ingredients": ["onion"], "min_size":2, "max_size":3}, "prop_feats": 0.9, "prop_empty": 0.1, "inner_shape": (6, 5), "display": False} mdp_fn = LayoutGenerator.mdp_gen_fn_from_dict(mdp_gen_params, outer_shape=(6, 5)) env = OvercookedEnv(mdp_fn, **DEFAULT_ENV_PARAMS) for _ in range(10): env.reset() self.assertCountEqual(env.mdp.start_all_orders, only_onions_dict_recipes) self.assertEqual(len(env.mdp.start_bonus_orders), 0) # checking if bonus_orders is subset of all_orders even if not specified mdp_gen_params = {"generate_all_orders": {"n":2, "ingredients": ["onion"], "min_size":2, "max_size":3}, "generate_bonus_orders": {"n":1, "min_size":2, "max_size":3}, "prop_feats": 0.9, "prop_empty": 0.1, "inner_shape": (6, 5), "display": False} mdp_fn = LayoutGenerator.mdp_gen_fn_from_dict(mdp_gen_params, outer_shape=(6,5)) env = OvercookedEnv(mdp_fn, **DEFAULT_ENV_PARAMS) for _ in range(10): env.reset() self.assertCountEqual(env.mdp.start_all_orders, only_onions_dict_recipes) self.assertEqual(len(env.mdp.start_bonus_orders), 1) self.assertTrue(env.mdp.start_bonus_orders[0] in only_onions_dict_recipes) # checking if after reset there are new recipes generated mdp_gen_params = {"generate_all_orders": {"n":3, "min_size":2, "max_size":3}, "prop_feats": 0.9, "prop_empty": 0.1, "inner_shape": (6, 5), "display": False, "feature_types": [POT, DISH_DISPENSER, SERVING_LOC, ONION_DISPENSER, TOMATO_DISPENSER] } mdp_fn = LayoutGenerator.mdp_gen_fn_from_dict(mdp_gen_params, outer_shape=(6,5)) env = OvercookedEnv(mdp_fn, **DEFAULT_ENV_PARAMS) generated_recipes_strings = set() for _ in range(20): env.reset() generated_recipes_strings |= {json.dumps(o, sort_keys=True) for o in env.mdp.start_all_orders} self.assertTrue(len(generated_recipes_strings) > 3)
def setUp(self): Recipe.configure({}) self.r1 = Recipe([Recipe.ONION, Recipe.ONION, Recipe.ONION]) self.r2 = Recipe([Recipe.ONION, Recipe.ONION, Recipe.ONION]) self.r3 = Recipe([Recipe.ONION, Recipe.TOMATO]) self.r4 = Recipe([Recipe.ONION, Recipe.TOMATO]) self.r5 = Recipe([Recipe.TOMATO, Recipe.ONION]) self.r6 = Recipe([Recipe.ONION, Recipe.ONION]) self.recipes = [self.r1, self.r2, self.r3, self.r4, self.r5, self.r6]
def setUp(self): Recipe.configure({}) self.r1 = Recipe([Recipe.ONION, Recipe.ONION, Recipe.ONION]) self.r2 = Recipe([Recipe.ONION, Recipe.ONION, Recipe.ONION]) self.r3 = Recipe([Recipe.ONION, Recipe.TOMATO]) self.r4 = Recipe([Recipe.ONION, Recipe.TOMATO]) self.r5 = Recipe([Recipe.TOMATO, Recipe.ONION]) self.r6 = Recipe([Recipe.ONION, Recipe.ONION]) self.recipes = [self.r1, self.r2, self.r3, self.r4, self.r5, self.r6] self.pickle_temp_dir = os.path.join(TESTING_DATA_DIR, 'recipes') if not os.path.exists(self.pickle_temp_dir): os.makedirs(self.pickle_temp_dir)
def tearDown(self): Recipe.configure({}) if os.path.exists(self.pickle_temp_dir): shutil.rmtree(self.pickle_temp_dir)
def setUp(self): Recipe.configure({})
# All other imports must come after patch to ensure eventlet compatibility import pickle, queue, atexit, json, logging, copy from threading import Lock from utils import ThreadSafeSet, ThreadSafeDict from flask import Flask, render_template, jsonify, request from flask_socketio import SocketIO, join_room, leave_room, emit from game import OvercookedGame, OvercookedTutorial, Game, OvercookedPsiturk import game from overcooked_ai_py.utils import load_from_json, cumulative_rewards_from_rew_list from overcooked_ai_py.visualization.extract_events import extract_events from overcooked_ai_py.visualization.visualization_utils import DEFAULT_EVENT_CHART_SETTINGS from overcooked_ai_py.agents.benchmarking import AgentEvaluator from overcooked_ai_py.mdp.overcooked_mdp import Recipe if not Recipe._configured: Recipe.configure({}) ### Thoughts -- where I'll log potential issues/ideas as they come up # Should make game driver code more error robust -- if overcooked randomlly errors we should catch it and report it to user # Right now, if one user 'join's before other user's 'join' finishes, they won't end up in same game # Could use a monitor on a conditional to block all global ops during calls to _ensure_consistent_state for debugging # Could cap number of sinlge- and multi-player games separately since the latter has much higher RAM and CPU usage ########### # Globals # ########### # Read in global config CONF_PATH = os.getenv('CONF_PATH', 'config.json') with open(CONF_PATH, 'r') as f: CONFIG = json.load(f)
def tearDown(self): Recipe.configure({})