OverviewFilesCommitsIssuesDocsReleasesPeers

opencog-rocca commits / 45c9918

refactored the code to add abstract perception
45c9918d6a799beab6a8443c4c86874526b2a19f
by Hedra <tanksh24@gmail.com> · 2021-12-29T16:15:55.000Z

Changed files (2) +81 -48

examples/flatworld.py +16 -17
……@@ -11,7 +11,7 @@ from rocca.agents.utils import agent_log
1111from rocca.envs.malmo_demo.flatworld_env import *
1212from rocca.envs.wrappers import MalmoWrapper
1313from opencog.ure import ure_logger
14from rocca.envs.wrappers.utils import mk_action
14from rocca.envs.wrappers.utils import mk_evaluation
1515from functools import wraps
1616
1717
……@@ -24,28 +24,27 @@ class AbstractMalmoWrapper(MalmoWrapper):
2424 @wraps(step)
2525 def wrapper(ref, action, update_callback=None):
2626 name = action.out[0]
27 ws = step(ref, name, update_callback)
28
29 return MalmoWrapper.parse_world_state(ws)
27 return step(ref, name, update_callback)
3028
3129 return wrapper
3230
3331 @step_decorator.__func__
34 def step(self, action, update_callback=None):
32 def step(self, abstract_action, update_callback=None):
3533 try:
36 func = globals()[action.name]
37 func(self.agent_host)
34 func = globals()[abstract_action.name]
35 observations, reward, done = func(self.agent_host)
3836 time.sleep(0.2)
39 self.world_state = self.agent_host.getWorldState()
40
41 for error in self.world_state.errors:
42 print("Error: ", error.text)
43
44 if update_callback is not None:
45 update_callback(action, self.world_state)
37 obs_list = []
38 if observations:
39 for k in observations:
40 if isinstance(observations[k], list):
41 for i in observations[k]:
42 obs_list.append(mk_evaluation(i, k))
43 else:
44 obs_list.append(mk_evaluation(k, observations[k]))
4645 except RuntimeError as e:
4746 print("Error sending command:", e)
48 return self.world_state
47 return obs_list, mk_evaluation("Reward", reward), done
4948
5049if __name__ == "__main__":
5150 atomspace = AtomSpace()
……@@ -56,7 +55,7 @@ if __name__ == "__main__":
5655 # log.set_sync(True)
5756 agent_log.set_level("debug")
5857 # agent_log.set_sync(True)
59 ure_logger().set_level("debug")
58 ure_logger().set_level("info")
6059
6160 #
6261 wrapped_env = AbstractMalmoWrapper(missionXML=missionXML, validate=True)
……@@ -78,7 +77,7 @@ if __name__ == "__main__":
7877
7978 # Training/learning loop
8079 lt_iterations = 3 # Number of learning-training iterations
81 lt_period = 10 # Duration of a learning-training iteration
80 lt_period = 50 # Duration of a learning-training iteration
8281 for i in range(lt_iterations):
8382 cog_agent.reset_action_counter()
8483 par = cog_agent.accumulated_reward # Keep track of the reward before
rocca/envs/malmo_demo/flatworld_env.py +65 -31
……@@ -76,14 +76,17 @@ key_x, key_y, key_z = 7, 228, -10
7676door_x, door_y, door_z = -4, 228, -3
7777diamond_x, diamond_y, diamond_z = -18, 228, -3
7878start_x, start_y, start_z, width, length, height = -4, 226, -1,6,15,5
79holds_key = False
7980
8081# relocates the agent to the starting point
8182def relocate_agent(agent):
83 print("Agent relocated to original place")
8284 agent.sendCommand("tp {} {} {}".format(agent_x, agent_y, agent_z))
8385 agent.sendCommand("setYaw {}".format(agent_yaw))
86 time.sleep(0.2)
8487
8588# stop the agent
86def stop_condition(agent, blocktype, sec=5):
89def stop_condition(agent, blocktype, sec=10):
8790 print("Moving to the {}".format(blocktype))
8891 def stop(agent, blocktype):
8992 done = False
……@@ -96,27 +99,29 @@ def stop_condition(agent, blocktype, sec=5):
9699 print("reached at {}".format(blocktype))
97100 agent.sendCommand("move 0")
98101 done = True
99 try:
100 with timeout(sec):
102
103 with timeout(sec):
104 try:
101105 stop(agent, blocktype)
102106 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
108112
109113# Get realtime location coordinates
110114def get_curr_loc(agent):
115 loc = (agent_x, agent_y, agent_z)
111116 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']))
119122
123 return loc
124
120125# Turns the agent towards the location given
121126def turn_to(agent, x1, y1, z1, x2, y2, z2):
122127 def get_angle(x1, y1, z1, x2, y2, z2):
……@@ -133,18 +138,27 @@ def turn_to(agent, x1, y1, z1, x2, y2, z2):
133138 angle = yaw + angle if z1 < z2 else yaw - angle
134139 agent.sendCommand("setYaw {}".format(angle))
135140
141def is_mission_running(agent):
142 world_state = agent.peekWorldState()
143 return world_state.is_mission_running
144
136145# Move the agent towards the key
137146# reward=0
138147def go_to_the_key(agent):
148 global holds_key
139149 curr_x, curr_y, curr_z = get_curr_loc(agent)
140150 turn_to(agent,curr_x, curr_y, curr_z, key_x, key_y, key_z)
141151 time.sleep(0.2)
142152 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:
145155 agent.sendCommand("hotbar.2 1")
156 observation = {"agent_status":["agent_holds_the_key"]}
157 holds_key = True
146158 else:
147 relocate_agent(agent)
159 observation = []
160 reward = 0
161 return observation, reward, is_mission_running
148162
149163# Move the agent towards the house and enter the house.
150164# reward = 0
……@@ -153,10 +167,20 @@ def go_to_the_house(agent):
153167 turn_to(agent, curr_x, curr_y, curr_z, door_x, door_y, door_z)
154168 time.sleep(0.2)
155169 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
158180 else:
159 relocate_agent(agent)
181 observation = []
182 reward = 0
183 return observation, reward, is_mission_running
160184
161185# Move the agent towards the diamond and collect it.
162186# reward = 1
……@@ -165,10 +189,12 @@ def go_to_the_diamonds(agent):
165189 turn_to(agent,curr_x, curr_y, curr_z, diamond_x, diamond_y, diamond_z)
166190 time.sleep(0.2)
167191 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
172198
173199missionXML = '''
174200<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
……@@ -249,17 +275,25 @@ if __name__ == "__main__":
249275 rw, ob, done = malmoWrapper.restart()
250276 total_reward = 0
251277 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
254281 world_state = agent.getWorldState()
255282 while world_state.is_mission_running:
256283 world_state = agent.getWorldState()
257284 if world_state.number_of_rewards_since_last_state > 0:
258285 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
263296 time.sleep(2)
297 print("total reward = {}".format(total_reward))
264298 print()
265299 print("Mission ended")