OverviewFilesCommitsIssuesDocsReleasesPeers

opencog-rocca commits / 948bcc2

Updates to the observation and refactored code
948bcc2836ef31e9b4821a8ede2d8a76f8a1364f
by Hedra <tanksh24@gmail.com> · 2022-01-14T21:05:07.000Z

Changed files (2) +35 -24

examples/flatworld.py +3 -4
……@@ -38,8 +38,7 @@ class AbstractMalmoWrapper(MalmoWrapper):
3838 if observations:
3939 for k in observations:
4040 if isinstance(observations[k], list):
41 for i in observations[k]:
42 obs_list.append(mk_evaluation(i, k))
41 obs_list.append(mk_evaluation(k, *observations[k]))
4342 else:
4443 obs_list.append(mk_evaluation(k, observations[k]))
4544 except RuntimeError as e:
……@@ -69,8 +68,8 @@ class FlatworldAgent(OpencogAgent):
6968 self.polyaction_mining = False
7069 self.monoaction_general_succeedent_mining = True
7170 self.temporal_deduction = True
72 self.true_cogscm = True
73 self.empty_vardecl_cogscm = True
71 self.cogscm_min_strength = 0.99
72 self.cogscm_max_variables = 0
7473 # Todo: restart the environment to get an initial reward
7574 self.initial_reward = EvaluationLink(PredicateNode("Reward"), NumberNode("0"))
7675
rocca/envs/malmo_demo/flatworld_env.py +32 -20
……@@ -89,12 +89,13 @@ def build_house(x, y, z, width, length, height, blocktype):
8989 x_cur = x
9090 for j in range(width):
9191 genstring = genstring + drawBlock(x_front, y_cur, z_cur, "glass") + "\n"
92 genstring = (
93 genstring + drawBlock(x_back, y_cur, z_cur, "diamond_block") + "\n"
94 )
95 genstring = (
96 genstring + drawBlock(x_back - 1, y_cur, z_cur, blocktype) + "\n"
97 )
92 genstring = genstring + drawBlock(x_back, y_cur, z_cur, blocktype) + "\n"
93 if j > 0 and j < width - 1:
94 genstring = (
95 genstring
96 + drawBlock(x_back + 1, y_cur, z_cur, "diamond_block")
97 + "\n"
98 )
9899 z_cur = z_cur + 1 if z_cur >= 0 else z_cur - 1
99100
100101 for k in range(length):
……@@ -164,7 +165,6 @@ def stop_condition(agent, blocktype, sec=10):
164165 if world_state.number_of_observations_since_last_state > 0:
165166 ob = json.loads(world_state.observations[-1].text)
166167 if blocktype in ob["BlocksInFront"]:
167 print("reached at {}".format(blocktype))
168168 agent.sendCommand("move 0")
169169 done = True
170170
……@@ -218,6 +218,10 @@ def is_mission_running(agent):
218218# reward=0
219219def go_to_the_key(agent):
220220 global holds_key
221 reward = 0
222 observation = {"holds": ["self", "key"]}
223 if holds_key:
224 return observation, reward, is_mission_running
221225 curr_x, curr_y, curr_z = get_curr_loc(agent)
222226 turn_to(agent, curr_x, curr_y, curr_z, key_x, key_y, key_z)
223227 time.sleep(0.2)
……@@ -225,30 +229,36 @@ def go_to_the_key(agent):
225229 action_complete = stop_condition(agent, "tripwire_hook")
226230 if action_complete:
227231 agent.sendCommand("hotbar.2 1")
228 observation = {"observation": ["holds_key"]}
229232 holds_key = True
230233 else:
231234 observation = []
232 reward = 0
233235 return observation, reward, is_mission_running
234236
235237
236238# Move the agent towards the house and enter the house.
237239# reward = 0
238240def go_to_the_house(agent):
241 global holds_key
242 reward = 0
243 observation = {}
239244 curr_x, curr_y, curr_z = get_curr_loc(agent)
245 if curr_x == door_x and curr_z == door_z:
246 observation["inside"] = ["self", "house"]
247 return observation, reward, is_mission_running
248
240249 turn_to(agent, curr_x, curr_y, curr_z, door_x, door_y, door_z)
241250 time.sleep(0.2)
242251 agent.sendCommand("move 1")
243252 action_complete = stop_condition(agent, "dark_oak_door")
244253 if action_complete:
245 observation = {"observation": []}
246254 if holds_key:
255 observation["holds"] = ["self", "key"]
247256 agent.sendCommand("tp {} {} {}".format(door_x, door_y, door_z))
248 observation["observation"].append("inside_the_house")
249 observation["observation"].append("holds_key")
257 observation["inside"] = ["self", "house"]
258 agent.sendCommand("hotbar.1 1")
259 holds_key = False
250260 else:
251 observation["observation"].append("nextto_closed_door")
261 observation["nextto"] = ["self", "closed_door"]
252262
253263 else:
254264 observation = []
……@@ -266,6 +276,7 @@ def go_to_the_diamonds(agent):
266276 action_complete = stop_condition(agent, "diamond_block")
267277 if action_complete:
268278 reward = 1
279 relocate_agent(agent)
269280 else:
270281 reward = 0
271282 return [], reward, is_mission_running
……@@ -285,7 +296,7 @@ missionXML = (
285296 <ServerInitialConditions>
286297 <Time>
287298 <StartTime>1000</StartTime>
288 <AllowPassageOfTime>true</AllowPassageOfTime>
299 <AllowPassageOfTime>false</AllowPassageOfTime>
289300 </Time>
290301 <Weather>clear</Weather>
291302 <AllowSpawning>true</AllowSpawning>
……@@ -309,7 +320,7 @@ missionXML = (
309320 + """
310321
311322 </DrawingDecorator>
312 <ServerQuitFromTimeUp description="" timeLimitMs="1000000"/>
323 <ServerQuitFromTimeUp description="" timeLimitMs="10000000"/>
313324 <ServerQuitWhenAnyAgentFinishes/>
314325 </ServerHandlers>
315326 </ServerSection>
……@@ -365,22 +376,23 @@ if __name__ == "__main__":
365376 rw, ob, done = malmoWrapper.restart()
366377 total_reward = 0
367378 agent = malmoWrapper.agent_host
368 _, rw, _ = go_to_the_key(agent)
369 print(ob)
370 total_reward += rw
371379 world_state = agent.getWorldState()
380 relocate_agent(agent)
372381 while world_state.is_mission_running:
373382 world_state = agent.getWorldState()
374383 if world_state.number_of_rewards_since_last_state > 0:
375384 total_reward += world_state.rewards[-1].getValue()
376385 print("total reward minecraft = {}".format(total_reward))
386 ob, rw, _ = go_to_the_key(agent)
387 print("Observation: {}, reward: {}".format(ob, rw))
388 total_reward += rw
377389
378390 ob, rw, _ = go_to_the_house(agent)
379 print(ob)
391 print("Observation: {}, reward: {}".format(ob, rw))
380392 total_reward += rw
381393
382394 ob, rw, _ = go_to_the_diamonds(agent)
383 print(ob)
395 print("Observation: {}, reward: {}".format(ob, rw))
384396 total_reward += rw
385397
386398 time.sleep(2)