diff --git a/enums/action_probabilities.py b/enums/action_probabilities.py index 53b330d..aa2de9a 100644 --- a/enums/action_probabilities.py +++ b/enums/action_probabilities.py @@ -8,8 +8,8 @@ PlayerAction.EAST : {PlayerAction.NORTH: 0, PlayerAction.SOUTH: 0, PlayerAction.EAST: 1, PlayerAction.WEST: 0, PlayerAction.NOP: 0}, PlayerAction.WEST : {PlayerAction.NORTH: 0, PlayerAction.SOUTH: 0, PlayerAction.EAST: 0, PlayerAction.WEST: 1, PlayerAction.NOP: 0}, PlayerAction.INTERACT : {PlayerAction.INTERACT: 1, PlayerAction.NOP: 0}, - PlayerAction.TOGGLE : {PlayerAction.TOGGLE: 1, PlayerAction.NOP: 0}, + PlayerAction.TOGGLE_CART : {PlayerAction.TOGGLE_CART: 1, PlayerAction.NOP: 0}, PlayerAction.CANCEL : {PlayerAction.CANCEL: 1, PlayerAction.NOP: 0}, PlayerAction.PICKUP : {PlayerAction.PICKUP: 1, PlayerAction.NOP: 0}, PlayerAction.RESET : {PlayerAction.RESET: 1}, -} \ No newline at end of file +} diff --git a/enums/player_action.py b/enums/player_action.py index ac25c4b..81f0a80 100644 --- a/enums/player_action.py +++ b/enums/player_action.py @@ -1,27 +1,17 @@ -from enum import Enum, IntEnum - +from enum import IntEnum class PlayerAction(IntEnum): - NOP = 0, - NORTH = 1, - SOUTH = 2, - EAST = 3, - WEST = 4, - INTERACT = 5, - TOGGLE = 6, - CANCEL = 7, - PICKUP = 8, - RESET = 9, + NOP = 0, + NORTH = 1, + SOUTH = 2, + EAST = 3, + WEST = 4, + INTERACT = 5, + TOGGLE_CART = 6, + CANCEL = 7, + PICKUP = 8, + RESET = 9 -PlayerActionTable = { - "NOP" : PlayerAction.NOP, - "NORTH" : PlayerAction.NORTH, - "SOUTH" : PlayerAction.SOUTH, - "EAST" : PlayerAction.EAST, - "WEST" : PlayerAction.WEST, - "INTERACT" : PlayerAction.INTERACT, - "TOGGLE" : PlayerAction.TOGGLE, - "CANCEL" : PlayerAction.CANCEL, - "PICKUP" : PlayerAction.PICKUP, - "RESET" : PlayerAction.RESET, -} + @classmethod + def get_names(cls): + return [member.name for member in cls] diff --git a/env.py b/env.py index 3544267..4e015a6 100755 --- a/env.py +++ b/env.py @@ -1,7 +1,7 @@ import time import random import gymnasium as gym -from enums.player_action import PlayerAction, PlayerActionTable +from enums.player_action import PlayerAction from game import Game MOVEMENT_ACTIONS = [PlayerAction.NORTH, PlayerAction.SOUTH, PlayerAction.EAST, PlayerAction.WEST] @@ -52,7 +52,7 @@ def __init__(self, num_players=1, player_speed=0.15, keyboard_input=False, rende for row in content.split("\n"): action_row = list(map(lambda column: column.strip(": "), row.split("\t"))) probability_pairs = map(lambda result: tuple(result.split(" ")), action_row[1:]) - self.action_probability[PlayerActionTable[action_row[0]]] = dict(map(lambda pair: (PlayerActionTable[pair[0]], float(pair[1])), probability_pairs)) + self.action_probability[PlayerAction[action_row[0]]] = dict(map(lambda pair: (PlayerAction[pair[0]], float(pair[1])), probability_pairs)) # else: # self.action_probability = Action_Probabilities @@ -73,7 +73,7 @@ def step(self, action): self.unwrapped.game.nop(i) elif player_action == PlayerAction.INTERACT: self.unwrapped.game.interact(i) - elif player_action == PlayerAction.TOGGLE: + elif player_action == PlayerAction.TOGGLE_CART: self.unwrapped.game.toggle_cart(i) self.unwrapped.game.toggle_basket(i) elif player_action == PlayerAction.CANCEL: @@ -144,7 +144,7 @@ def step(self, player_action): self.unwrapped.game.nop(i) elif player_action == PlayerAction.INTERACT: self.unwrapped.game.interact(i) - elif player_action == PlayerAction.TOGGLE: + elif player_action == PlayerAction.TOGGLE_CART: self.unwrapped.game.toggle_cart(i) self.unwrapped.game.toggle_basket(i) elif player_action == PlayerAction.CANCEL: diff --git a/socket_env.py b/socket_env.py index d4ea735..ad6ce2e 100755 --- a/socket_env.py +++ b/socket_env.py @@ -5,6 +5,7 @@ import socket import types +from enums.player_action import PlayerAction from env import SupermarketEnv, SinglePlayerSupermarketEnv from norms.norm import NormWrapper from norms.norms import * @@ -14,8 +15,6 @@ action_file = "actions.txt" agent_completion_file = "agent_completion.txt" -ACTION_COMMANDS = ['NOP', 'NORTH', 'SOUTH', 'EAST', 'WEST', 'INTERACT', 'TOGGLE_CART', 'CANCEL', 'SELECT','RESET'] - def serialize_data(data): if isinstance(data, set): return list(data) @@ -72,7 +71,7 @@ def handle_exploratory_events(self): player.interacting = True elif event.key == pygame.K_c: - self.env.step(self.single_player_action(PlayerAction.TOGGLE)) + self.env.step(self.single_player_action(PlayerAction.TOGGLE_CART)) # switch players (up to 9 players) else: @@ -383,8 +382,8 @@ def accept_wrapper(sock): if is_single_player(command): player, command, arg = get_player_and_command(command) e.append((key, mask, command)) - if command in ACTION_COMMANDS: - action_id = ACTION_COMMANDS.index(command) + if command in PlayerAction.get_names(): + action_id = PlayerAction[command].value curr_action[player] = (action_id, arg) should_perform_action = True action_taken[player].append(action_id) @@ -400,7 +399,7 @@ def accept_wrapper(sock): # for i in range(env.unwrapped.num_players): # f.write("actions taken by player " + str(i) + ": \n") # for action in action_taken[i]: - # f.write(ACTION_COMMANDS[action] + "\n") + # f.write(PlayerAction[action].name + "\n") f.close() else: info = {'result': False, 'step_cost': 0.0, 'message': 'Invalid Command'} diff --git a/stochastic_probability.txt b/stochastic_probability.txt index 42e1d84..e179f0b 100644 --- a/stochastic_probability.txt +++ b/stochastic_probability.txt @@ -4,7 +4,7 @@ SOUTH: NORTH 0 SOUTH 0.8 EAST 0.05 WEST 0.05 NOP 0.1 EAST: NORTH 0.05 SOUTH 0.05 EAST 0.8 WEST 0 NOP 0.1 WEST: NORTH 0.05 SOUTH 0.05 EAST 0 WEST 0.8 NOP 0.1 INTERACT: INTERACT 1 NOP 0 -TOGGLE: TOGGLE 0.9 NOP 0.1 +TOGGLE_CART: TOGGLE_CART 0.9 NOP 0.1 CANCEL: CANCEL 0.9 NOP 0.1 PICKUP: PICKUP 0.9 NOP 0.1 RESET: RESET 1 \ No newline at end of file