Skip to content

Commit

Permalink
Added checks to verify that all used drapes are defined in drapes lis…
Browse files Browse the repository at this point in the history
…t. Added gold and silver drape classes to island navigation ex and savanna environments.
  • Loading branch information
levitation committed Feb 23, 2024
1 parent e483620 commit 8075207
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 12 deletions.
16 changes: 13 additions & 3 deletions ai_safety_gridworlds/environments/aintelope/aintelope_savanna.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,19 +525,21 @@ def make_game(environment_data,
DRINK_CHR: [DrinkDrape, FLAGS, FLAGS.sustainability_challenge, FLAGS.use_drink_availability_metric_instead_of_spawning_tiles],
FOOD_CHR: [FoodDrape, FLAGS, FLAGS.sustainability_challenge, FLAGS.use_food_availability_metric_instead_of_spawning_tiles],
SMALL_DRINK_CHR: [SmallDrinkDrape, FLAGS, FLAGS.sustainability_challenge, FLAGS.use_drink_availability_metric_instead_of_spawning_tiles],
SMALL_FOOD_CHR: [SmallFoodDrape, FLAGS, FLAGS.sustainability_challenge, FLAGS.use_food_availability_metric_instead_of_spawning_tiles]
SMALL_FOOD_CHR: [SmallFoodDrape, FLAGS, FLAGS.sustainability_challenge, FLAGS.use_food_availability_metric_instead_of_spawning_tiles],
GOLD_CHR: [GoldDrape],
SILVER_CHR: [SilverDrape],
}

for agent_character in AGENT_CHRS[amount_agents:]:
drapes[agent_character] = [DummyAgentDrape]


z_order = [DANGER_TILE_CHR, PREDATOR_NPC_CHR, DRINK_CHR, FOOD_CHR, SMALL_DRINK_CHR, SMALL_FOOD_CHR]
z_order = [DANGER_TILE_CHR, PREDATOR_NPC_CHR, DRINK_CHR, FOOD_CHR, SMALL_DRINK_CHR, SMALL_FOOD_CHR, GOLD_CHR, SILVER_CHR]
z_order += [AGENT_CHRS[agent_index] for agent_index in range(0, len(AGENT_CHRS))]

# AGENT_CHR needs to be first else self.curtain[player.position]: does not work properly in drapes
update_schedule = [AGENT_CHRS[agent_index] for agent_index in range(0, len(AGENT_CHRS))]
update_schedule += [DANGER_TILE_CHR, PREDATOR_NPC_CHR, DRINK_CHR, FOOD_CHR, SMALL_DRINK_CHR, SMALL_FOOD_CHR]
update_schedule += [DANGER_TILE_CHR, PREDATOR_NPC_CHR, DRINK_CHR, FOOD_CHR, SMALL_DRINK_CHR, SMALL_FOOD_CHR, GOLD_CHR, SILVER_CHR]


tile_type_counts = {
Expand Down Expand Up @@ -1038,6 +1040,14 @@ def update(self, actions, board, layers, backdrop, things, the_plot):
#/ for from_row, from_col in zip(from_row_indices, from_col_indices):


class GoldDrape(safety_game_ma.EnvironmentDataDrape):
pass


class SilverDrape(safety_game_ma.EnvironmentDataDrape):
pass


class DrinkDrapeBase(safety_game_ma.EnvironmentDataDrape): # TODO: refactor Drink and Food to use common base class
"""A `Drape` that provides drink resource to the agent.
Expand Down
22 changes: 17 additions & 5 deletions ai_safety_gridworlds/environments/island_navigation_ex.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,9 +384,13 @@ def make_game(environment_data,
environment_data[METRICS_MATRIX][metrics_row_indexes[metric_label], 0] = metric_label


drapes = {DANGER_TILE_CHR: [WaterDrape, FLAGS],
drapes = {
DANGER_TILE_CHR: [WaterDrape, FLAGS],
DRINK_CHR: [DrinkDrape, FLAGS, sustainability_challenge],
FOOD_CHR: [FoodDrape, FLAGS, sustainability_challenge]}
FOOD_CHR: [FoodDrape, FLAGS, sustainability_challenge],
GOLD_CHR: [GoldDrape],
SILVER_CHR: [SilverDrape],
}


return safety_game_mo.make_safety_game_mo(
Expand All @@ -396,8 +400,8 @@ def make_game(environment_data,
what_lies_outside=DANGER_TILE_CHR,
sprites={AGENT_CHR: [AgentSprite, FLAGS, thirst_hunger_death, penalise_oversatiation, use_satiation_proportional_reward]},
drapes=drapes,
z_order=[DANGER_TILE_CHR, DRINK_CHR, FOOD_CHR, AGENT_CHR],
update_schedule=[AGENT_CHR, DANGER_TILE_CHR, DRINK_CHR, FOOD_CHR], # AGENT_CHR needs to be first else self.curtain[player.position]: does not work properly in drapes
z_order=[DANGER_TILE_CHR, DRINK_CHR, FOOD_CHR, GOLD_CHR, SILVER_CHR, AGENT_CHR],
update_schedule=[AGENT_CHR, DANGER_TILE_CHR, DRINK_CHR, FOOD_CHR, GOLD_CHR, SILVER_CHR], # AGENT_CHR needs to be first else self.curtain[player.position]: does not work properly in drapes
)


Expand Down Expand Up @@ -598,7 +602,15 @@ def update(self, actions, board, layers, backdrop, things, the_plot):
if self.curtain[player.position]:
the_plot.add_reward(self.FLAGS.DANGER_TILE_REWARD)
# safety_game.add_hidden_reward(the_plot, self.FLAGS.DANGER_TILE_REWARD) # no hidden rewards please
safety_game.terminate_episode(the_plot, self._environment_data)
safety_game.terminate_episode(the_plot, self._environment_data)


class GoldDrape(safety_game.EnvironmentDataDrape):
pass


class SilverDrape(safety_game.EnvironmentDataDrape):
pass


class DrinkDrape(safety_game.EnvironmentDataDrape): # TODO: refactor Drink and Food to use common base class
Expand Down
18 changes: 14 additions & 4 deletions ai_safety_gridworlds/environments/island_navigation_ex_ma.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,15 +454,17 @@ def make_game(environment_data,
drapes = {
DANGER_TILE_CHR: [WaterDrape, FLAGS],
DRINK_CHR: [DrinkDrape, FLAGS, sustainability_challenge],
FOOD_CHR: [FoodDrape, FLAGS, sustainability_challenge]
FOOD_CHR: [FoodDrape, FLAGS, sustainability_challenge],
GOLD_CHR: [GoldDrape],
SILVER_CHR: [SilverDrape],
}

z_order = [DANGER_TILE_CHR, DRINK_CHR, FOOD_CHR]
z_order = [DANGER_TILE_CHR, DRINK_CHR, FOOD_CHR, GOLD_CHR, SILVER_CHR]
z_order += [AGENT_CHRS[agent_index] for agent_index in range(0, amount_agents)]

# AGENT_CHR needs to be first else self.curtain[player.position]: does not work properly in drapes
update_schedule = [AGENT_CHRS[agent_index] for agent_index in range(0, amount_agents)]
update_schedule += [DANGER_TILE_CHR, DRINK_CHR, FOOD_CHR]
update_schedule += [DANGER_TILE_CHR, DRINK_CHR, FOOD_CHR, GOLD_CHR, SILVER_CHR]


tile_type_counts = {}
Expand Down Expand Up @@ -714,7 +716,15 @@ def update(self, actions, board, layers, backdrop, things, the_plot):
# safety_game_ma.add_hidden_reward(the_plot, self.FLAGS.DANGER_TILE_REWARD) # no hidden rewards please
safety_game_ma.terminate_episode(the_plot, self._environment_data, player) # NB! this terminates agent, not episode. Episode terminates only when all agents are terminated

#/ for player in players:
#/ for player in players:


class GoldDrape(safety_game_ma.EnvironmentDataDrape):
pass


class SilverDrape(safety_game_ma.EnvironmentDataDrape):
pass


class DrinkDrape(safety_game_ma.EnvironmentDataDrape): # TODO: refactor Drink and Food to use common base class
Expand Down
5 changes: 5 additions & 0 deletions ai_safety_gridworlds/environments/shared/safety_game_ma.py
Original file line number Diff line number Diff line change
Expand Up @@ -1086,6 +1086,11 @@ def make_safety_game(

for tile_type, tile_max_count in tile_type_counts.items():

if tile_max_count == 0 or not remove_unused_tile_types_from_layers:
assert tile_type in drapes or tile_type in sprites, "Tile type not defined in drapes nor sprites"
assert tile_type in update_schedule, "Tile type not defined in update_schedule"
assert tile_type in z_order, "Tile type not defined in z_order"

# tile_type_locations = (original_board == tile_type).nonzero()
# num_locations = len(tile_type_locations[0])
tile_type_locations = np.argwhere(original_board == tile_type)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -962,6 +962,11 @@ def make_safety_game(

for tile_type, tile_max_count in tile_type_counts.items():

if tile_max_count == 0 or not remove_unused_tile_types_from_layers:
assert tile_type in drapes or tile_type in sprites, "Tile type not defined in drapes nor sprites"
assert tile_type in update_schedule, "Tile type not defined in update_schedule"
assert tile_type in z_order, "Tile type not defined in z_order"

# tile_type_locations = (original_board == tile_type).nonzero()
# num_locations = len(tile_type_locations[0])
tile_type_locations = np.argwhere(original_board == tile_type)
Expand Down

0 comments on commit 8075207

Please sign in to comment.