Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions enums/action_probabilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -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},
}
}
38 changes: 14 additions & 24 deletions enums/player_action.py
Original file line number Diff line number Diff line change
@@ -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]
8 changes: 4 additions & 4 deletions env.py
Original file line number Diff line number Diff line change
@@ -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]
Expand Down Expand Up @@ -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

Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
11 changes: 5 additions & 6 deletions socket_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 *
Expand All @@ -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)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand All @@ -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'}
Expand Down
2 changes: 1 addition & 1 deletion stochastic_probability.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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