diff --git a/Malmo/samples/Python_examples/ALE_HAC.py b/Malmo/samples/Python_examples/ALE_HAC.py index 22afef5e6..2a3625275 100755 --- a/Malmo/samples/Python_examples/ALE_HAC.py +++ b/Malmo/samples/Python_examples/ALE_HAC.py @@ -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': @@ -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: @@ -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(): @@ -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: @@ -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 diff --git a/Malmo/samples/Python_examples/MazeRunner.py b/Malmo/samples/Python_examples/MazeRunner.py index 79cd73b39..d8dc180cf 100755 --- a/Malmo/samples/Python_examples/MazeRunner.py +++ b/Malmo/samples/Python_examples/MazeRunner.py @@ -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. diff --git a/Malmo/samples/Python_examples/MultiMaze.py b/Malmo/samples/Python_examples/MultiMaze.py index 903d7b481..404d40e04 100755 --- a/Malmo/samples/Python_examples/MultiMaze.py +++ b/Malmo/samples/Python_examples/MultiMaze.py @@ -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." diff --git a/Malmo/samples/Python_examples/default_world_test.py b/Malmo/samples/Python_examples/default_world_test.py index 01878dcf3..bb7080982 100755 --- a/Malmo/samples/Python_examples/default_world_test.py +++ b/Malmo/samples/Python_examples/default_world_test.py @@ -45,13 +45,6 @@ def GetMissionXML(): ''' -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 @@ -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...) diff --git a/Malmo/samples/Python_examples/depth_map_runner.py b/Malmo/samples/Python_examples/depth_map_runner.py index b04ac6b3f..7eb9bec67 100755 --- a/Malmo/samples/Python_examples/depth_map_runner.py +++ b/Malmo/samples/Python_examples/depth_map_runner.py @@ -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: @@ -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 diff --git a/Malmo/samples/Python_examples/manual_input_test.py b/Malmo/samples/Python_examples/manual_input_test.py index e2b7d3a4a..88db04a70 100755 --- a/Malmo/samples/Python_examples/manual_input_test.py +++ b/Malmo/samples/Python_examples/manual_input_test.py @@ -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." diff --git a/Malmo/samples/Python_examples/patchwork_quilt.py b/Malmo/samples/Python_examples/patchwork_quilt.py index a93eb4f43..cb05f3dbd 100755 --- a/Malmo/samples/Python_examples/patchwork_quilt.py +++ b/Malmo/samples/Python_examples/patchwork_quilt.py @@ -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. \ No newline at end of file diff --git a/Malmo/samples/Python_examples/reward_for_items_test.py b/Malmo/samples/Python_examples/reward_for_items_test.py index 1350bba58..17f074eac 100755 --- a/Malmo/samples/Python_examples/reward_for_items_test.py +++ b/Malmo/samples/Python_examples/reward_for_items_test.py @@ -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: diff --git a/Malmo/samples/Python_examples/reward_for_mission_end_test.py b/Malmo/samples/Python_examples/reward_for_mission_end_test.py index aa56c58d4..7cc303b03 100755 --- a/Malmo/samples/Python_examples/reward_for_mission_end_test.py +++ b/Malmo/samples/Python_examples/reward_for_mission_end_test.py @@ -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 diff --git a/Malmo/samples/Python_examples/run_mission.py b/Malmo/samples/Python_examples/run_mission.py index ed82c455d..6e9f65cd9 100755 --- a/Malmo/samples/Python_examples/run_mission.py +++ b/Malmo/samples/Python_examples/run_mission.py @@ -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 diff --git a/Malmo/samples/Python_examples/tabular_q_learning.py b/Malmo/samples/Python_examples/tabular_q_learning.py index 9b3925fa6..3e4bf5ada 100755 --- a/Malmo/samples/Python_examples/tabular_q_learning.py +++ b/Malmo/samples/Python_examples/tabular_q_learning.py @@ -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 diff --git a/Malmo/samples/Python_examples/to_string_test.py b/Malmo/samples/Python_examples/to_string_test.py index 124cc4139..6cdc14b55 100755 --- a/Malmo/samples/Python_examples/to_string_test.py +++ b/Malmo/samples/Python_examples/to_string_test.py @@ -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