Skip to content

Commit

Permalink
AgentHost::sendCommand no longer throws an exception, adds to worldst…
Browse files Browse the repository at this point in the history
…ate.errors instead.
  • Loading branch information
Tim Hutton committed Jun 14, 2016
1 parent ff1174b commit d09bcec
Show file tree
Hide file tree
Showing 12 changed files with 85 additions and 148 deletions.
120 changes: 58 additions & 62 deletions Malmo/samples/Python_examples/ALE_HAC.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def keyUp(event):
if event.keysym == 'Escape':
root.destroy()
if event.keysym == 'Right':
right = 0
right = 0
if event.keysym == 'Left':
left = 0
if event.keysym == 'Up':
Expand All @@ -54,48 +54,48 @@ def keyUp(event):
def keyDown(event):
global left, right, up, down, fire
if event.keysym == 'Right':
right = 1
left = 0 # left and right are mutually exclusive
right = 1
left = 0 # left and right are mutually exclusive
if event.keysym == 'Left':
left = 1
right = 0
right = 0
if event.keysym == 'Up':
up = 1
down = 0 # up and down are mutally exclusive
down = 0 # up and down are mutally exclusive
if event.keysym == 'Down':
down = 1
up = 0
up = 0
if event.keysym == 'space':
fire = 1



# ALE op-codes:
# PLAYER_A_NOOP = 0,
# PLAYER_A_FIRE = 1,
# PLAYER_A_UP = 2,
# PLAYER_A_RIGHT = 3,
# PLAYER_A_LEFT = 4,
# PLAYER_A_DOWN = 5,
# PLAYER_A_UPRIGHT = 6,
# PLAYER_A_UPLEFT = 7,
# PLAYER_A_DOWNRIGHT = 8,
# PLAYER_A_DOWNLEFT = 9,
# PLAYER_A_UPFIRE = 10,
# PLAYER_A_RIGHTFIRE = 11,
# PLAYER_A_LEFTFIRE = 12,
# PLAYER_A_DOWNFIRE = 13,
# PLAYER_A_UPRIGHTFIRE = 14,
# PLAYER_A_UPLEFTFIRE = 15,
# PLAYER_A_DOWNRIGHTFIRE = 16,
# PLAYER_A_DOWNLEFTFIRE = 17

allops=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17] # all allowed op-codes
leftops=[4,7,9,12,15,17] # op-codes with left pressed
rightops=[3,6,8,11,14,16] # op-codes with right pressed
upops=[2,6,7,10,14,15] # op-codes with up pressed
downops=[5,8,9,13,16,17] # op-codes with down pressed
fireops=[1,10,11,12,13,14,15,16,17] # op-codes with fire pressed
# PLAYER_A_NOOP = 0,
# PLAYER_A_FIRE = 1,
# PLAYER_A_UP = 2,
# PLAYER_A_RIGHT = 3,
# PLAYER_A_LEFT = 4,
# PLAYER_A_DOWN = 5,
# PLAYER_A_UPRIGHT = 6,
# PLAYER_A_UPLEFT = 7,
# PLAYER_A_DOWNRIGHT = 8,
# PLAYER_A_DOWNLEFT = 9,
# PLAYER_A_UPFIRE = 10,
# PLAYER_A_RIGHTFIRE = 11,
# PLAYER_A_LEFTFIRE = 12,
# PLAYER_A_DOWNFIRE = 13,
# PLAYER_A_UPRIGHTFIRE = 14,
# PLAYER_A_UPLEFTFIRE = 15,
# PLAYER_A_DOWNRIGHTFIRE = 16,
# PLAYER_A_DOWNLEFTFIRE = 17

allops=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17] # all allowed op-codes
leftops=[4,7,9,12,15,17] # op-codes with left pressed
rightops=[3,6,8,11,14,16] # op-codes with right pressed
upops=[2,6,7,10,14,15] # op-codes with up pressed
downops=[5,8,9,13,16,17] # op-codes with down pressed
fireops=[1,10,11,12,13,14,15,16,17] # op-codes with fire pressed

def startGame():
#Find filename for the recording:
Expand Down Expand Up @@ -134,7 +134,7 @@ def startGame():

gamestats = "Go " + str(gameNum+1) + " out of " + str(iterations) + "\n"
canvas.delete("all")
canvas.create_text(80, 105, text=gamestats + "Click to begin!\nEscape to end") # The window needs keyboard focus or no way to control game.
canvas.create_text(80, 105, text=gamestats + "Click to begin!\nEscape to end") # The window needs keyboard focus or no way to control game.


def sendCommand():
Expand All @@ -148,52 +148,48 @@ def sendCommand():
ops = ops - set(leftops)

if right:
ops = ops & set(rightops)
ops = ops & set(rightops)
else:
ops = ops - set(rightops)

if up:
ops = ops & set(upops)
ops = ops & set(upops)
else:
ops = ops - set(upops)

if down:
ops = ops & set(downops)
ops = ops & set(downops)
else:
ops = ops - set(downops)

if fire:
ops = ops & set(fireops)
ops = ops & set(fireops)
else:
ops = ops - set(fireops)

if len(ops) > 0:
try:
agent_host.sendCommand( str(list(ops)[0]) ) # If no keys pressed will send no-op
except RuntimeError as e:
#print "Failed to send command:",e
pass
# The ALE only updates in response to a command, so get the new world state now.
world_state = agent_host.getWorldState()
for reward in world_state.rewards:
if reward.value > 0:
print "Summed reward:",reward.value
for error in world_state.errors:
print "Error:",error.text
if world_state.number_of_video_frames_since_last_state > 0 and want_own_display:
# Turn the frame into an image to display on our canvas.
# On my system creating buff was too slow to be usable, whichever of these three apporaches I tried:
buff = str(bytearray(world_state.video_frames[-1].pixels))
# Or buff = pack('100800B', *(world_state.video_frames[-1].pixels))
# Or buff = array('B', world_state.video_frames[-1].pixels)
image = Image.frombytes('RGB', (320,420), buff)
photo = ImageTk.PhotoImage(image)
canvas.delete("all")
canvas.create_image(80,105, image=photo)
root.update()
agent_host.sendCommand( str(list(ops)[0]) ) # If no keys pressed will send no-op
# The ALE only updates in response to a command, so get the new world state now.
world_state = agent_host.getWorldState()
for reward in world_state.rewards:
if reward.value > 0:
print "Summed reward:",reward.value
for error in world_state.errors:
print "Error:",error.text
if world_state.number_of_video_frames_since_last_state > 0 and want_own_display:
# Turn the frame into an image to display on our canvas.
# On my system creating buff was too slow to be usable, whichever of these three apporaches I tried:
buff = str(bytearray(world_state.video_frames[-1].pixels))
# Or buff = pack('100800B', *(world_state.video_frames[-1].pixels))
# Or buff = array('B', world_state.video_frames[-1].pixels)
image = Image.frombytes('RGB', (320,420), buff)
photo = ImageTk.PhotoImage(image)
canvas.delete("all")
canvas.create_image(80,105, image=photo)
root.update()

if world_state.is_mission_running:
canvas.after(0, sendCommand) # Call sendCommand again as soon as possible within tkinter's event loop.
canvas.after(0, sendCommand) # Call sendCommand again as soon as possible within tkinter's event loop.
else:
gameNum = gameNum + 1
if gameNum < iterations:
Expand Down Expand Up @@ -237,7 +233,7 @@ def sendCommand():
raise

startGame() # Get things up and ready...
root.mainloop() # and enter the event loop
root.mainloop() # and enter the event loop
print "Mission has stopped."
os.system('xset r on') # set auto-repeat back
os.system('xset r on') # set auto-repeat back

8 changes: 2 additions & 6 deletions Malmo/samples/Python_examples/MazeRunner.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,8 @@ def GetMissionXML( current_seed ):
current_speed = 1-abs(current_yaw_delta)
print "Got observation: " + str(current_yaw_delta)

try:
agent_host.sendCommand( "move " + str(current_speed) )
agent_host.sendCommand( "turn " + str(current_yaw_delta) )
except RuntimeError as e:
print "Failed to send command:",e
pass
agent_host.sendCommand( "move " + str(current_speed) )
agent_host.sendCommand( "turn " + str(current_yaw_delta) )

print "Mission has stopped."
time.sleep(0.5) # Give mod a little time to get back to dormant state.
18 changes: 7 additions & 11 deletions Malmo/samples/Python_examples/MultiMaze.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,17 +155,13 @@ def GetMissionXML( current_seed, xorg, yorg, zorg ):
current_yaw_delta = ob.get(u'yawDelta', 0)
current_speed = 1-abs(current_yaw_delta)

try:
agent_host.sendCommand( "move " + str(current_speed) )
agent_host.sendCommand( "turn " + str(current_yaw_delta) )
if num_steps_since_last_chat >= chat_frequency:
agent_host.sendCommand( "chat " + "hello from agent " + str(role) )
num_steps_since_last_chat = 0
else:
num_steps_since_last_chat = num_steps_since_last_chat + 1
except RuntimeError as e:
print "Failed to send command:",e
pass
agent_host.sendCommand( "move " + str(current_speed) )
agent_host.sendCommand( "turn " + str(current_yaw_delta) )
if num_steps_since_last_chat >= chat_frequency:
agent_host.sendCommand( "chat " + "hello from agent " + str(role) )
num_steps_since_last_chat = 0
else:
num_steps_since_last_chat = num_steps_since_last_chat + 1

print "Mission has stopped."

Expand Down
9 changes: 1 addition & 8 deletions Malmo/samples/Python_examples/default_world_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,6 @@ def GetMissionXML():
</Mission>'''

def SendCommand(command):
try:
agent_host.sendCommand( command )
except RuntimeError as e:
print "Failed to send command:",e
pass

# Variety of strategies for dealing with loss of motion:
commandSequences=[
"jump 1; move 1; wait 1; jump 0; move 1; wait 2", # attempt to jump over obstacle
Expand Down Expand Up @@ -124,7 +117,7 @@ def SendCommand(command):
if verb == "wait": # "wait" isn't a Malmo command - it's just used here to pause execution of our "programme".
waitCycles = int(param.strip())
else:
SendCommand(command) # Send the command to Minecraft.
agent_host.sendCommand(command) # Send the command to Minecraft.

if currentSequence == "" and currentSpeed < 50 and waitCycles == 0: # Are we stuck?
currentSequence = random.choice(commandSequences) # Choose a random action (or insert your own logic here for choosing more sensibly...)
Expand Down
12 changes: 2 additions & 10 deletions Malmo/samples/Python_examples/depth_map_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,7 @@ def processFrame( frame ):
world_state = agent_host.getWorldState()
print

try:
agent_host.sendCommand( "move 1" )
except RuntimeError as e:
logger.error("Failed to send command: %s" % e)
pass
agent_host.sendCommand( "move 1" )

# main loop:
while world_state.is_mission_running:
Expand All @@ -211,11 +207,7 @@ def processFrame( frame ):
if world_state.is_mission_running:
processFrame(world_state.video_frames[0].pixels)

try:
agent_host.sendCommand( "turn " + str(current_yaw_delta_from_depth) )
except RuntimeError as e:
logger.error("Failed to send command: %s" % e)
pass
agent_host.sendCommand( "turn " + str(current_yaw_delta_from_depth) )

logger.info("Mission has stopped.")
time.sleep(1) # let the Mod recover
6 changes: 1 addition & 5 deletions Malmo/samples/Python_examples/manual_input_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,7 @@ def GetMissionXML( current_seed ):
# main loop:
while world_state.is_mission_running:
nb = raw_input('Enter command: ')
try:
agent_host.sendCommand(nb)
except RuntimeError as e:
print "Failed to send command:",e
pass
agent_host.sendCommand(nb)
world_state = agent_host.getWorldState()

print "Mission has stopped."
8 changes: 2 additions & 6 deletions Malmo/samples/Python_examples/patchwork_quilt.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,8 @@ def GetMissionXML( current_seed, xorg, yorg, zorg, iteration ):
current_yaw_delta = ob.get(u'yawDelta', 0)
current_speed = 1-abs(current_yaw_delta)

try:
agent_host.sendCommand( "move " + str(current_speed) )
agent_host.sendCommand( "turn " + str(current_yaw_delta) )
except RuntimeError as e:
print "Failed to send command:",e
pass
agent_host.sendCommand( "move " + str(current_speed) )
agent_host.sendCommand( "turn " + str(current_yaw_delta) )

print "Mission has stopped."
time.sleep(0.5) # Short pause to allow the Mod to get ready for the next mission.
12 changes: 2 additions & 10 deletions Malmo/samples/Python_examples/reward_for_items_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,10 @@ def GetItemDrawingXML():
return xml

def SetVelocity(vel):
try:
agent_host.sendCommand( "move " + str(vel) )
except RuntimeError as e:
print "Failed to send command:",e
pass
agent_host.sendCommand( "move " + str(vel) )

def SetTurn(turn):
try:
agent_host.sendCommand( "turn " + str(turn) )
except RuntimeError as e:
print "Failed to send command:",e
pass
agent_host.sendCommand( "turn " + str(turn) )

recordingsDirectory="EatingRecordings"
try:
Expand Down
12 changes: 2 additions & 10 deletions Malmo/samples/Python_examples/reward_for_mission_end_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,10 @@ def GetMineDrawingXML():
return xml

def SetVelocity(vel):
try:
agent_host.sendCommand( "move " + str(vel) )
except RuntimeError as e:
print "Failed to send command:",e
pass
agent_host.sendCommand( "move " + str(vel) )

def SetTurn(turn):
try:
agent_host.sendCommand( "turn " + str(turn) )
except RuntimeError as e:
print "Failed to send command:",e
pass
agent_host.sendCommand( "turn " + str(turn) )

sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0) # flush print output immediately

Expand Down
8 changes: 2 additions & 6 deletions Malmo/samples/Python_examples/run_mission.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,8 @@

# main loop:
while world_state.is_mission_running:
try:
agent_host.sendCommand( "move 1" )
agent_host.sendCommand( "turn " + str(random.random()*2-1) )
except RuntimeError as e:
#print "Failed to send command:",e
pass
agent_host.sendCommand( "move 1" )
agent_host.sendCommand( "turn " + str(random.random()*2-1) )
time.sleep(0.5)
world_state = agent_host.getWorldState()
print "video,observations,rewards received:",world_state.number_of_video_frames_since_last_state,world_state.number_of_observations_since_last_state,world_state.number_of_rewards_since_last_state
Expand Down
12 changes: 4 additions & 8 deletions Malmo/samples/Python_examples/tabular_q_learning.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,10 @@ def act(self, world_state, agent_host, current_r ):
a = l[y]
self.logger.info("Taking q action: %s" % self.actions[a])

# try to send the selected action, only update prev_s if this succeeds
try:
agent_host.sendCommand(self.actions[a])
self.prev_s = current_s
self.prev_a = a

except RuntimeError as e:
self.logger.error("Failed to send command: %s" % e)
# send the selected action
agent_host.sendCommand(self.actions[a])
self.prev_s = current_s
self.prev_a = a

return current_r

Expand Down
8 changes: 2 additions & 6 deletions Malmo/samples/Python_examples/to_string_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,8 @@

# main loop:
while world_state.is_mission_running:
try:
agent_host.sendCommand( "move 1" )
agent_host.sendCommand( "turn " + str(random.random()*2-1) )
except RuntimeError as e:
#print "Failed to send command:",e
pass
agent_host.sendCommand( "move 1" )
agent_host.sendCommand( "turn " + str(random.random()*2-1) )
time.sleep(0.5)
world_state = agent_host.getWorldState()
print world_state
Expand Down

0 comments on commit d09bcec

Please sign in to comment.