| … | … | @@ -76,14 +76,17 @@ key_x, key_y, key_z = 7, 228, -10 |
| 76 | 76 | door_x, door_y, door_z = -4, 228, -3 |
| 77 | 77 | diamond_x, diamond_y, diamond_z = -18, 228, -3 |
| 78 | 78 | start_x, start_y, start_z, width, length, height = -4, 226, -1,6,15,5 |
| 79 | holds_key = False |
| 79 | 80 | |
| 80 | 81 | # relocates the agent to the starting point |
| 81 | 82 | def relocate_agent(agent): |
| 83 | print("Agent relocated to original place") |
| 82 | 84 | agent.sendCommand("tp {} {} {}".format(agent_x, agent_y, agent_z)) |
| 83 | 85 | agent.sendCommand("setYaw {}".format(agent_yaw)) |
| 86 | time.sleep(0.2) |
| 84 | 87 | |
| 85 | 88 | # stop the agent |
| 86 | | def stop_condition(agent, blocktype, sec=5): |
| 89 | def stop_condition(agent, blocktype, sec=10): |
| 87 | 90 | print("Moving to the {}".format(blocktype)) |
| 88 | 91 | def stop(agent, blocktype): |
| 89 | 92 | done = False |
| … | … | @@ -96,27 +99,29 @@ def stop_condition(agent, blocktype, sec=5): |
| 96 | 99 | print("reached at {}".format(blocktype)) |
| 97 | 100 | agent.sendCommand("move 0") |
| 98 | 101 | done = True |
| 99 | | try: |
| 100 | | with timeout(sec): |
| 102 | |
| 103 | with timeout(sec): |
| 104 | try: |
| 101 | 105 | stop(agent, blocktype) |
| 102 | 106 | return True |
| 103 | | except TimeoutError: |
| 104 | | print("Time out") |
| 105 | | agent.sendCommand("move 0") |
| 106 | | relocate_agent(agent) |
| 107 | | return False |
| 107 | except TimeoutError: |
| 108 | print("*********** Time out **************") |
| 109 | agent.sendCommand("move 0") |
| 110 | relocate_agent(agent) |
| 111 | return False |
| 108 | 112 | |
| 109 | 113 | # Get realtime location coordinates |
| 110 | 114 | def get_curr_loc(agent): |
| 115 | loc = (agent_x, agent_y, agent_z) |
| 111 | 116 | world_state = agent.peekWorldState() |
| 112 | | while world_state.is_mission_running: |
| 113 | | world_state = agent.peekWorldState() |
| 114 | | if world_state.number_of_observations_since_last_state > 0: |
| 115 | | ob = json.loads(world_state.observations[-1].text) |
| 116 | | return (int(ob[u'XPos']), int(ob[u'YPos']), int(ob[u'ZPos'])) |
| 117 | | else: |
| 118 | | return (agent_x, agent_y, agent_z) |
| 117 | # while world_state.is_mission_running: |
| 118 | # world_state = agent.peekWorldState() |
| 119 | if world_state.number_of_observations_since_last_state > 0: |
| 120 | ob = json.loads(world_state.observations[-1].text) |
| 121 | loc = (int(ob[u'XPos']), int(ob[u'YPos']), int(ob[u'ZPos'])) |
| 119 | 122 | |
| 123 | return loc |
| 124 | |
| 120 | 125 | # Turns the agent towards the location given |
| 121 | 126 | def turn_to(agent, x1, y1, z1, x2, y2, z2): |
| 122 | 127 | def get_angle(x1, y1, z1, x2, y2, z2): |
| … | … | @@ -133,18 +138,27 @@ def turn_to(agent, x1, y1, z1, x2, y2, z2): |
| 133 | 138 | angle = yaw + angle if z1 < z2 else yaw - angle |
| 134 | 139 | agent.sendCommand("setYaw {}".format(angle)) |
| 135 | 140 | |
| 141 | def is_mission_running(agent): |
| 142 | world_state = agent.peekWorldState() |
| 143 | return world_state.is_mission_running |
| 144 | |
| 136 | 145 | # Move the agent towards the key |
| 137 | 146 | # reward=0 |
| 138 | 147 | def go_to_the_key(agent): |
| 148 | global holds_key |
| 139 | 149 | curr_x, curr_y, curr_z = get_curr_loc(agent) |
| 140 | 150 | turn_to(agent,curr_x, curr_y, curr_z, key_x, key_y, key_z) |
| 141 | 151 | time.sleep(0.2) |
| 142 | 152 | agent.sendCommand("move 1") |
| 143 | | if stop_condition(agent, 'tripwire_hook'): |
| 144 | | # agent.sendCommand("tp {} {} {}".format(key_x, key_y, key_z)) |
| 153 | action_complete = stop_condition(agent, 'tripwire_hook') |
| 154 | if action_complete: |
| 145 | 155 | agent.sendCommand("hotbar.2 1") |
| 156 | observation = {"agent_status":["agent_holds_the_key"]} |
| 157 | holds_key = True |
| 146 | 158 | else: |
| 147 | | relocate_agent(agent) |
| 159 | observation = [] |
| 160 | reward = 0 |
| 161 | return observation, reward, is_mission_running |
| 148 | 162 | |
| 149 | 163 | # Move the agent towards the house and enter the house. |
| 150 | 164 | # reward = 0 |
| … | … | @@ -153,10 +167,20 @@ def go_to_the_house(agent): |
| 153 | 167 | turn_to(agent, curr_x, curr_y, curr_z, door_x, door_y, door_z) |
| 154 | 168 | time.sleep(0.2) |
| 155 | 169 | agent.sendCommand("move 1") |
| 156 | | if stop_condition(agent, 'dark_oak_door'): |
| 157 | | agent.sendCommand("tp {} {} {}".format(door_x, door_y, door_z)) |
| 170 | action_complete = stop_condition(agent, 'dark_oak_door') |
| 171 | if action_complete: |
| 172 | observation = {"agent_status":[]} |
| 173 | if holds_key: |
| 174 | agent.sendCommand("tp {} {} {}".format(door_x, door_y, door_z)) |
| 175 | observation["agent_status"].append("agent_inside_the_house") |
| 176 | observation["agent_status"].append("agent_holds_the_key") |
| 177 | else: |
| 178 | observation["agent_status"].append("agent_nextto_closed_door") |
| 179 | |
| 158 | 180 | else: |
| 159 | | relocate_agent(agent) |
| 181 | observation = [] |
| 182 | reward = 0 |
| 183 | return observation, reward, is_mission_running |
| 160 | 184 | |
| 161 | 185 | # Move the agent towards the diamond and collect it. |
| 162 | 186 | # reward = 1 |
| … | … | @@ -165,10 +189,12 @@ def go_to_the_diamonds(agent): |
| 165 | 189 | turn_to(agent,curr_x, curr_y, curr_z, diamond_x, diamond_y, diamond_z) |
| 166 | 190 | time.sleep(0.2) |
| 167 | 191 | agent.sendCommand("move 1") |
| 168 | | if stop_condition(agent, 'diamond_block'): |
| 169 | | agent.sendCommand("tp {} {} {}".format(diamond_x, diamond_y, diamond_z)) |
| 170 | | time.sleep(0.2) |
| 171 | | relocate_agent(agent) |
| 192 | action_complete = stop_condition(agent, 'diamond_block') |
| 193 | if action_complete: |
| 194 | reward = 1 |
| 195 | else: |
| 196 | reward = 0 |
| 197 | return [], reward, is_mission_running |
| 172 | 198 | |
| 173 | 199 | missionXML = ''' |
| 174 | 200 | <?xml version="1.0" encoding="UTF-8" standalone="no" ?> |
| … | … | @@ -249,17 +275,25 @@ if __name__ == "__main__": |
| 249 | 275 | rw, ob, done = malmoWrapper.restart() |
| 250 | 276 | total_reward = 0 |
| 251 | 277 | agent = malmoWrapper.agent_host |
| 252 | | go_to_the_key(agent) |
| 253 | | |
| 278 | _, rw, _= go_to_the_key(agent) |
| 279 | print(ob) |
| 280 | total_reward += rw |
| 254 | 281 | world_state = agent.getWorldState() |
| 255 | 282 | while world_state.is_mission_running: |
| 256 | 283 | world_state = agent.getWorldState() |
| 257 | 284 | if world_state.number_of_rewards_since_last_state > 0: |
| 258 | 285 | total_reward += world_state.rewards[-1].getValue() |
| 259 | | print("total reward = {}".format(total_reward)) |
| 260 | | go_to_the_key(agent) |
| 261 | | go_to_the_house(agent) |
| 262 | | go_to_the_diamonds(agent) |
| 286 | print("total reward minecraft = {}".format(total_reward)) |
| 287 | |
| 288 | ob, rw, _ = go_to_the_house(agent) |
| 289 | print(ob) |
| 290 | total_reward += rw |
| 291 | |
| 292 | ob, rw, _ = go_to_the_diamonds(agent) |
| 293 | print(ob) |
| 294 | total_reward += rw |
| 295 | |
| 263 | 296 | time.sleep(2) |
| 297 | print("total reward = {}".format(total_reward)) |
| 264 | 298 | print() |
| 265 | 299 | print("Mission ended") |