OverviewFilesCommitsIssuesDocsReleasesPeers

opencog-rocca commits / 76f75c3

Removed renamed files from history
76f75c34fa557fb7770375a674e644170a4c6d6c
by Hedra <tanksh24@gmail.com> · 2022-06-06T23:02:08.000Z

Changed files (2) +0 -809

examples/flatworld.py +0 -147
……@@ -1,147 +0,0 @@
1import time
2
3# OpenCog
4from opencog.logger import log
5from opencog.pln import *
6from opencog.type_constructors import *
7from opencog.utilities import set_default_atomspace
8
9from rocca.agents import OpencogAgent
10from rocca.agents.utils import TRUE_TV, agent_log
11from rocca.envs.malmo_demo.flatworld_env import *
12from rocca.envs.wrappers import MalmoWrapper
13from opencog.ure import ure_logger
14from rocca.envs.wrappers.utils import mk_evaluation
15from rocca.agents.utils import *
16from functools import wraps
17
18
19class AbstractMalmoWrapper(MalmoWrapper):
20 def __init__(self, missionXML, validate):
21 MalmoWrapper.__init__(self, missionXML, validate)
22
23 @staticmethod
24 def step_decorator(step):
25 @wraps(step)
26 def wrapper(ref, action, update_callback=None):
27 name = action.out[0]
28 return step(ref, name, update_callback)
29
30 return wrapper
31
32 @step_decorator.__func__
33 def step(self, abstract_action, update_callback=None):
34 try:
35 func = globals()[abstract_action.name]
36 observations, reward, done = func(self.agent_host)
37 time.sleep(0.2)
38 obs_list = []
39 if observations:
40 for k in observations:
41 if isinstance(observations[k], list):
42 obs_list.append(mk_evaluation(k, *observations[k]))
43 else:
44 obs_list.append(mk_evaluation(k, observations[k]))
45 except RuntimeError as e:
46 print("Error sending command:", e)
47 return obs_list, mk_evaluation("Reward", reward), done
48
49
50class FlatworldAgent(OpencogAgent):
51 def __init__(self, env, atomspace):
52 set_default_atomspace(atomspace)
53
54 # Create Action Space. The set of allowed actions an agent can take.
55 # TODO take care of action parameters.
56 action_space = {
57 ExecutionLink(SchemaNode("go_to_key")),
58 ExecutionLink(SchemaNode("go_to_house")),
59 ExecutionLink(SchemaNode("go_to_diamonds")),
60 }
61 # Create Goal
62 pgoal = EvaluationLink(PredicateNode("Reward"), NumberNode("1"))
63 ngoal = EvaluationLink(PredicateNode("Reward"), NumberNode("0"))
64
65 # Call super ctor
66 OpencogAgent.__init__(self, env, atomspace, action_space, pgoal, ngoal)
67
68 # Overwrite some OpencogAgent parameters
69 self.polyaction_mining = False
70 self.monoaction_general_succeedent_mining = True
71 self.temporal_deduction = True
72 self.cogscm_minimum_strength = 0.75
73 self.cogscm_maximum_shannon_entropy = 1
74 self.cogscm_maximum_differential_entropy = 0
75 self.cogscm_maximum_variables = 0
76 self.miner_maximum_iterations = 100000
77 self.miner_maximum_variables = 9
78 self.miner_minimum_support = 3
79 self.conditional_conjunction_introduction = False
80 self.expiry = 3
81 self.mixture_model.complexity_penalty = 0.6
82 self.mixture_model.delta = 1.0e-30
83 self.mixture_model.compressiveness = 0.1
84 self.mixture_model.weight_influence = 1.0
85
86 # Todo: restart the environment to get an initial reward
87 self.initial_reward = EvaluationLink(PredicateNode("Reward"), NumberNode("0"))
88
89
90if __name__ == "__main__":
91 atomspace = AtomSpace()
92 set_default_atomspace(atomspace)
93
94 # Init loggers
95 log.set_level("info")
96 # log.set_sync(True)
97 agent_log.set_level("fine")
98 # agent_log.set_sync(True)
99 ure_logger().set_level("debug")
100 miner_log = MinerLogger(atomspace)
101 miner_log.set_level("debug")
102
103 #
104 wrapped_env = AbstractMalmoWrapper(missionXML=missionXML, validate=True)
105
106 cog_agent = FlatworldAgent(wrapped_env, atomspace)
107
108 # Log all parameters of cag, useful for debugging
109 cog_agent.log_parameters(level="debug")
110 reward_t0 = cog_agent.record(cog_agent.initial_reward, 0, TRUE_TV)
111 agent_log.info("Initial reward: {}".format(reward_t0))
112
113 # Training/learning loop
114 lt_iterations = 2 # Number of learning-training iterations
115 lt_period = 50 # Duration of a learning-training iteration
116 for i in range(lt_iterations):
117 cog_agent.reset_action_counter()
118 par = cog_agent.accumulated_reward # Keep track of the reward before
119 # Discover patterns to make more informed decisions
120 agent_log.info("Start learning ({}/{})".format(i + 1, lt_iterations))
121 cog_agent.learn()
122 # Run agent to accumulate percepta
123 agent_log.info("Start training ({}/{})".format(i + 1, lt_iterations))
124 for j in range(lt_period):
125 cog_agent.control_cycle()
126 time.sleep(0.01)
127 log.info("step_count = {}".format(cog_agent.cycle_count))
128 nar = cog_agent.accumulated_reward - par
129 agent_log.info(
130 "Accumulated reward during {}th iteration = {}".format(i + 1, nar)
131 )
132 agent_log.info(
133 "Action counter during {}th iteration:\n{}".format(
134 i + 1, cog_agent.action_counter
135 )
136 )
137 # Log all agent's atomspaces at the end (at fine level)
138 agent_log_atomspace(cog_agent.atomspace, "fine", "cog_agent.atomspace")
139 agent_log_atomspace(
140 cog_agent.percepta_atomspace, "fine", "cog_agent.percepta_atomspace"
141 )
142 agent_log_atomspace(
143 cog_agent.cogscms_atomspace, "fine", "cog_agent.cogscms_atomspace"
144 )
145 agent_log_atomspace(
146 cog_agent.working_atomspace, "fine", "cog_agent.working_atomspace"
147 )
rocca/envs/malmo_demo/flatworld_env.py +0 -662
……@@ -1,662 +0,0 @@
1import os
2import time
3
4from rocca.envs.wrappers.malmo_wrapper import MalmoWrapper
5from rocca.envs.wrappers.utils import *
6from builtins import range
7import json
8import math
9import signal
10from contextlib import contextmanager
11import random
12
13
14@contextmanager
15def timeout(time):
16 # Register a function to raise a TimeoutError on the signal.
17 signal.signal(signal.SIGALRM, raise_timeout)
18 # Schedule the signal to be sent after ``time``.
19 signal.alarm(time)
20 try:
21 yield
22 except TimeoutError:
23 pass
24 finally:
25 # Unregister the signal so it won't be triggered
26 # if the timeout is not reached.
27 signal.signal(signal.SIGALRM, signal.SIG_IGN)
28
29
30def raise_timeout(signum, frame):
31 raise TimeoutError
32
33
34def drawBlock(x, y, z, type, variant=False, face=False):
35 if variant:
36 return """<DrawBlock x='{0}' y='{1}' z='{2}' type='{3}' variant='{4}' face='{5}'/>""".format(
37 x, y, z, type, variant, face
38 )
39 else:
40 return """<DrawBlock x='{0}' y='{1}' z='{2}' type='{3}' />""".format(
41 x, y, z, type, variant
42 )
43
44
45def GenCuboid(x1, y1, z1, x2, y2, z2, blocktype):
46 return (
47 '<DrawCuboid x1="'
48 + str(x1)
49 + '" y1="'
50 + str(y1)
51 + '" z1="'
52 + str(z1)
53 + '" x2="'
54 + str(x2)
55 + '" y2="'
56 + str(y2)
57 + '" z2="'
58 + str(z2)
59 + '" type="'
60 + blocktype
61 + '"/>'
62 )
63
64
65def drawItem(x, y, z, item_type):
66 return (
67 '<DrawItem x="'
68 + str(x)
69 + '" y="'
70 + str(y)
71 + '" z="'
72 + str(z)
73 + '" type="'
74 + item_type
75 + '"/>'
76 )
77
78
79def drawLine(x1, y1, z1, x2, y2, z2, blocktype, face):
80 return (
81 '<DrawLine x1="'
82 + str(x1)
83 + '" y1="'
84 + str(y1)
85 + '" z1="'
86 + str(z1)
87 + '" x2="'
88 + str(x2)
89 + '" y2="'
90 + str(y2)
91 + '" z2="'
92 + str(z2)
93 + '" type="'
94 + blocktype
95 + '" face="'
96 + face
97 + '"/>'
98 )
99
100
101def draw_fence(x, y, z):
102 fence = ""
103 xy_coord = [(-x, z), (x, z), (x, -z), (-x, -z)]
104 for i in range(4):
105 fence += drawLine(
106 x1=xy_coord[i][0],
107 y1=y,
108 z1=xy_coord[i][1],
109 x2=xy_coord[i + 1][0] if i < 3 else xy_coord[0][0],
110 y2=y,
111 z2=xy_coord[i + 1][1] if i < 3 else xy_coord[0][1],
112 blocktype="fence",
113 face="WEST",
114 )
115 return fence
116
117
118def build_house(x, y, z, width, length, height, blocktype):
119 x_front = x
120 x_back = x + length if x >= 0 else x - length
121 z_left = z + width if z >= 0 else z - width
122 z_right = z
123
124 genstring = ""
125 y_cur = y
126 for i in range(height + 1):
127 z_cur = z
128 x_cur = x
129 for j in range(width + 1):
130 genstring = genstring + drawBlock(x_front, y_cur, z_cur, "glass") + "\n"
131 genstring = genstring + drawBlock(x_back, y_cur, z_cur, blocktype) + "\n"
132 if j > 0 and j < width:
133 genstring = (
134 genstring
135 + drawBlock(x_back + 1, y_cur, z_cur, "diamond_ore")
136 + "\n"
137 )
138 z_cur = z_cur + 1 if z_cur >= 0 else z_cur - 1
139
140 for k in range(length):
141 if k % 2 != 0:
142 genstring = genstring + drawBlock(x_cur, y_cur, z_left, "glass") + "\n"
143 genstring = genstring + drawBlock(x_cur, y_cur, z_right, "glass") + "\n"
144 else:
145 genstring = (
146 genstring + drawBlock(x_cur, y_cur, z_left, blocktype) + "\n"
147 )
148 genstring = (
149 genstring + drawBlock(x_cur, y_cur, z_right, blocktype) + "\n"
150 )
151 x_cur = x_cur + 1 if x_cur >= 0 else x_cur - 1
152
153 y_cur = y_cur + 1 if y_cur >= 0 else y_cur - 1
154
155 # floor
156 genstring = genstring + GenCuboid(
157 -15,
158 y,
159 15,
160 15,
161 y,
162 -15,
163 "obsidian",
164 )
165 # ceiling
166 genstring = genstring + GenCuboid(
167 x_back + 1 if x_back >= 0 else x_back - 1,
168 y_cur,
169 z_left + 1 if z_left >= 0 else z_left - 1,
170 x - 1 if x >= 0 else x + 1,
171 y_cur,
172 z - 1 if x >= 0 else z + 1,
173 blocktype,
174 )
175 genstring = genstring + GenCuboid(
176 x_back,
177 y_cur + 1,
178 z_left,
179 x,
180 y_cur + 1,
181 z,
182 blocktype,
183 )
184 genstring = genstring + GenCuboid(
185 x_back - 1 if x_back >= 0 else x_back + 1,
186 y_cur + 2,
187 z_left - 1 if z_left >= 0 else z_left + 1,
188 x + 1 if x >= 0 else x - 1,
189 y_cur + 2,
190 z + 1 if x >= 0 else z - 1,
191 blocktype,
192 )
193 genstring = genstring + GenCuboid(
194 x_back,
195 y_cur,
196 z_left,
197 x,
198 y_cur,
199 z,
200 "air",
201 )
202 genstring = genstring + GenCuboid(
203 x_back - 1 if x_back >= 0 else x_back + 1,
204 y_cur + 1,
205 z_left - 1 if z_left >= 0 else z_left + 1,
206 x + 1 if x >= 0 else x - 1,
207 y_cur + 1,
208 z + 1 if x >= 0 else z - 1,
209 "air",
210 )
211 genstring = genstring + GenCuboid(
212 x_back - 2 if x_back >= 0 else x_back + 2,
213 y_cur + 2,
214 z_left - 2 if z_left >= 0 else z_left + 2,
215 x + 2 if x >= 0 else x - 2,
216 y_cur + 2,
217 z + 2 if x >= 0 else z - 2,
218 "glass",
219 )
220
221 return genstring
222
223
224# Initialize the location coordinates
225agent_x, agent_y, agent_z, agent_yaw = 13, 230, -2, 90
226key_x, key_y, key_z = 7, 228, -10
227door_x, door_y, door_z = -4, 228, -3
228exit_x, exit_y, exit_z = -9, 228, -1
229diamond_x, diamond_y, diamond_z = -18, 228, -3
230start_x, start_y, start_z, width, length, height = -4, 226, -1, 6, 9, 4
231hold_key = False
232fence_max = 15
233
234place_count = 0
235pitch_adjusted = False
236total_reward = 0
237
238
239def adjust_pitch(agent):
240 global pitch_adjusted
241 if not pitch_adjusted:
242 agent.sendCommand("pitch 0.5")
243 time.sleep(0.5)
244 agent.sendCommand("pitch 0")
245 pitch_adjusted = True
246
247
248# relocates the agent to the starting point
249def relocate_agent(agent, msg=""):
250 print(msg)
251 agent.sendCommand("tp {} {} {}".format(agent_x, agent_y, agent_z))
252 if not hold_key:
253 agent.sendCommand("hotbar.4 1")
254 agent.sendCommand("setYaw {}".format(agent_yaw))
255 time.sleep(0.2)
256
257
258blocks = {
259 "gold_block": "key",
260 "dark_oak_door": "house",
261 "diamond_block": "diamonds",
262 "planks": "planks",
263 "diamond_ore": "diamonds",
264}
265
266# stop the agent
267def stop_condition(agent, blocktype, sec=10):
268 print("Moving to the {}".format(blocks[blocktype]))
269
270 def stop(agent, blocktype):
271 done = False
272 world_state = agent.peekWorldState()
273 while not done:
274 world_state = agent.peekWorldState()
275 if world_state.number_of_observations_since_last_state > 0:
276 ob = json.loads(world_state.observations[-1].text)
277 if blocktype in ob["BlocksInFront"]:
278 agent.sendCommand("move 0")
279 done = True
280
281 with timeout(sec):
282 try:
283 stop(agent, blocktype)
284 return True
285 except TimeoutError:
286 agent.sendCommand("move 0")
287 relocate_agent(agent, msg="*** RELOCATE: Unable to reach the target ***")
288 return False
289
290
291# Get realtime location coordinates
292def get_curr_loc(agent):
293 loc = (agent_x, agent_y, agent_z)
294 world_state = agent.peekWorldState()
295 while world_state.is_mission_running:
296 world_state = agent.peekWorldState()
297 if world_state.number_of_observations_since_last_state > 0:
298 ob = json.loads(world_state.observations[-1].text)
299 yaw = int(ob["Yaw"])
300 loc = (int(ob[u"XPos"]), int(ob[u"YPos"]), int(ob[u"ZPos"]), yaw)
301 break
302 return loc
303
304
305# Turns the agent towards the location given
306def turn_to(agent, x1, y1, z1, x2, y2, z2, curr_yaw=False):
307 def get_angle(x1, y1, z1, x2, y2, z2):
308 x_dist = abs(x1 - x2)
309 z_dist = abs(z1 - z2)
310 h = math.sqrt(x_dist ** 2 + z_dist ** 2)
311 return math.degrees(z_dist / h) if h != 0 else 0
312
313 angle = get_angle(x1, y1, z1, x2, y2, z2)
314 yaw = 90 if x1 > x2 else -90
315 if yaw > 0:
316 angle = yaw - angle if z1 < z2 else yaw + angle
317 else:
318 angle = yaw + angle if z1 < z2 else yaw - angle
319 if curr_yaw:
320 if curr_yaw < angle:
321 agent.sendCommand("turn 0.5")
322 else:
323 agent.sendCommand("turn -0.5")
324 while is_mission_running:
325 world_state = agent.peekWorldState()
326 if world_state.number_of_observations_since_last_state > 0:
327 ob = json.loads(world_state.observations[-1].text)
328 yaw = int(ob["Yaw"])
329 if abs(yaw - angle) < 5:
330 agent.sendCommand("turn 0")
331 agent.sendCommand("setYaw {}".format(angle))
332 break
333 else:
334 agent.sendCommand("setYaw {}".format(angle))
335
336
337def is_mission_running(agent):
338 world_state = agent.peekWorldState()
339 return world_state.is_mission_running
340
341
342def inside_house(x, z):
343 return (
344 True
345 if (x in range(start_x, start_x - length, -1))
346 and (z in range(start_z, start_z - width, -1))
347 else False
348 )
349
350
351def get_txt(obs):
352 if obs:
353 return ",".join(
354 ["{}({},{})".format(ob, obs[ob][0], obs[ob][1]) for ob in obs.keys()]
355 )
356 else:
357 return ""
358
359
360# Move the agent towards the key
361# reward=0
362def go_to_key(agent):
363 adjust_pitch(agent)
364 global hold_key
365 reward = 0
366 observation = {}
367 curr_x, curr_y, curr_z, curr_yaw = get_curr_loc(agent)
368 if inside_house(curr_x, curr_z):
369 observation["inside"] = ["self", "house"]
370 print("Observation => {}".format(get_txt(observation)))
371 return observation, reward, is_mission_running
372 else:
373 observation["outside"] = ["self", "house"]
374 if hold_key:
375 observation["hold"] = ["self", "key"]
376 print("Observation => {}".format(get_txt(observation)))
377 return observation, reward, is_mission_running
378 turn_to(agent, curr_x, curr_y, curr_z, key_x, key_y, key_z, curr_yaw=curr_yaw)
379 time.sleep(0.2)
380 agent.sendCommand("move 1")
381 action_complete = stop_condition(agent, "gold_block")
382 if action_complete:
383 observation["hold"] = ["self", "key"]
384 agent.sendCommand("hotbar.2 1")
385 hold_key = True
386 else:
387 observation = []
388 print("Observation => {}".format(get_txt(observation)))
389 return observation, reward, is_mission_running
390
391
392# Move the agent towards the house and enter the house.
393# reward = 0
394def go_to_house(agent):
395 adjust_pitch(agent)
396 global hold_key
397 reward = 0
398 observation = {}
399 curr_x, curr_y, curr_z, curr_yaw = get_curr_loc(agent)
400 if inside_house(curr_x, curr_z):
401 observation["inside"] = ["self", "house"]
402 print("Observation => {}".format(get_txt(observation)))
403 return observation, reward, is_mission_running
404 turn_to(
405 agent, curr_x, curr_y, curr_z, door_x + 2, door_y, door_z, curr_yaw=curr_yaw
406 )
407 time.sleep(0.2)
408 agent.sendCommand("move 1")
409 action_complete = stop_condition(agent, "dark_oak_door")
410 if action_complete:
411 if hold_key:
412 time.sleep(0.2)
413 agent.sendCommand("tp {} {} {}".format(door_x, door_y, door_z))
414 time.sleep(0.1)
415 observation["inside"] = ["self", "house"]
416 agent.sendCommand("hotbar.1 1")
417 hold_key = False
418 else:
419 observation["outside"] = ["self", "house"]
420 observation["nextto"] = ["self", "closed_door"]
421 reward = 0
422 print("Observation => {}".format(get_txt(observation)))
423 return observation, reward, is_mission_running
424
425
426def place_diamonds(agent):
427 global place_count
428 curr_x, curr_y, curr_z, curr_yaw = get_curr_loc(agent)
429 turn_to(
430 agent,
431 curr_x,
432 curr_y,
433 curr_z,
434 (exit_x + 2) + (2 * place_count),
435 exit_y,
436 fence_max,
437 )
438 agent.sendCommand("move 1")
439 time.sleep(2 - (place_count * 0.01))
440 placed = False
441 while not placed:
442 world_state = agent.getWorldState()
443 if world_state.number_of_observations_since_last_state > 0:
444 msg = world_state.observations[-1].text
445 ob = json.loads(msg)
446 # Use the line-of-sight observation to determine where to place:
447 if u"LineOfSight" in ob:
448 agent.sendCommand("turn {}".format(random.choice([0.05, -0.05])))
449 los = ob[u"LineOfSight"]
450 x = int(math.floor(los["x"]))
451 z = int(math.floor(los["z"]))
452 if los["inRange"]:
453 agent.sendCommand("move 0")
454 time.sleep(2)
455 agent.sendCommand("use")
456 agent.sendCommand("turn 0")
457 agent.sendCommand("hotbar.5 1")
458 time.sleep(2)
459 place_count += 1
460 placed = True
461
462
463def exit(agent):
464 curr_x, curr_y, curr_z, curr_yaw = get_curr_loc(agent)
465 turn_to(agent, curr_x, curr_y, curr_z, exit_x, exit_y, exit_z)
466 agent.sendCommand("move 1")
467 agent.sendCommand("use 1")
468 time.sleep(0.2)
469 while is_mission_running(agent):
470 curr_x, curr_y, curr_z, _ = get_curr_loc(agent)
471 if not inside_house(curr_x, curr_z):
472 agent.sendCommand("move 0")
473 agent.sendCommand("tp {} {} {}".format(exit_x + 0.5, curr_y, exit_z + 1))
474 time.sleep(0.1)
475 curr_x, curr_y, curr_z, _ = get_curr_loc(agent)
476 turn_to(agent, curr_x, curr_y, curr_z, exit_x, exit_y, exit_z)
477 agent.sendCommand("use 1")
478 time.sleep(0.05)
479 agent.sendCommand("use 0")
480 break
481 agent.sendCommand("hotbar.3 1")
482 place_diamonds(agent)
483
484
485# Move the agent towards the diamond and collect it.
486# reward = 1
487def go_to_diamonds(agent):
488 global total_reward
489 adjust_pitch(agent)
490 # agent.sendCommand("chat Collect diamonds")
491 observation = {}
492 curr_x, curr_y, curr_z, curr_yaw = get_curr_loc(agent)
493 turn_to(
494 agent,
495 curr_x,
496 curr_y,
497 curr_z,
498 diamond_x,
499 diamond_y,
500 diamond_z,
501 curr_yaw=curr_yaw,
502 )
503 time.sleep(0.2)
504 agent.sendCommand("move 1")
505 action_complete = stop_condition(agent, "diamond_ore")
506 if action_complete:
507 agent.sendCommand("attack 1")
508 time.sleep(0.2)
509 agent.sendCommand("attack 0")
510 time.sleep(0.2)
511 reward = 1
512 total_reward += 1
513 exit(agent)
514 relocate_agent(agent)
515 time.sleep(0.3)
516 observation["outside"] = ["self", "house"]
517 else:
518 reward = 0
519 if not inside_house(curr_x, curr_z):
520 observation["outside"] = ["self", "house"]
521 if hold_key:
522 observation["hold"] = ["self", "key"]
523 if reward > 0:
524 agent.sendCommand("chat Reward = {}".format(reward))
525 agent.sendCommand("chat => Total Reward of {} diamonds!".format(total_reward))
526 print("Reward = {}".format(reward))
527 print("Total Reward of {} diamonds!".format(total_reward))
528
529 else:
530 print("Observation => {}".format(get_txt(observation)))
531
532 return observation, reward, is_mission_running
533
534
535missionXML = (
536 """
537<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
538<Mission xmlns="http://ProjectMalmo.microsoft.com"
539 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
540
541 <About>
542 <Summary>Collect diamonds!</Summary>
543 </About>
544
545 <ServerSection>
546 <ServerInitialConditions>
547 <Time>
548 <StartTime>1000</StartTime>
549 <AllowPassageOfTime>false</AllowPassageOfTime>
550 </Time>
551 <Weather>clear</Weather>
552 <AllowSpawning>true</AllowSpawning>
553 </ServerInitialConditions>
554 <ServerHandlers>
555 <FlatWorldGenerator generatorString="3;7,220*1,5*3,2;1;village,decoration"/>
556 <DrawingDecorator>
557 """
558 + build_house(start_x, start_y, start_z, width, length, height, "brick_block")
559 + drawBlock(diamond_x + 5, diamond_y, diamond_z - 1, "diamond_block")
560 + drawBlock(
561 door_x, door_y - 1, door_z, "dark_oak_door", variant="lower", face="WEST"
562 )
563 + drawBlock(door_x, door_y, door_z, "dark_oak_door", variant="upper", face="WEST")
564 + drawBlock(exit_x, exit_y, exit_z, "dark_oak_door", variant="upper", face="NORTH")
565 + drawBlock(
566 exit_x, exit_y - 1, exit_z, "dark_oak_door", variant="lower", face="NORTH"
567 )
568 + drawBlock(key_x, key_y - 1, key_z, "stone")
569 + drawBlock(key_x, key_y, key_z, "gold_block")
570 + draw_fence(fence_max, start_y + 1, fence_max)
571 + """
572
573 </DrawingDecorator>
574 <ServerQuitWhenAnyAgentFinishes/>
575 </ServerHandlers>
576 </ServerSection>
577
578 <AgentSection mode="Survival">
579 <Name>Cog Agent</Name>
580 <AgentStart>
581 """
582 + '<Placement x="{}" y="{}" z="{}" yaw="{}"/>'.format(
583 agent_x, agent_y, agent_z, agent_yaw
584 )
585 + """
586 <Inventory>
587 <InventoryItem slot="0" type="diamond_pickaxe"/>
588 <InventoryItem slot="1" type="tripwire_hook"/>
589 <InventoryBlock slot="2" type="diamond_block" quantity="63"/>
590 </Inventory>
591 </AgentStart>
592 <AgentHandlers>
593 <ObservationFromFullStats/>
594 <ObservationFromGrid>
595 <Grid name="BlocksInFront">
596 <min x="-1" y="1" z="-1"/>
597 <max x="1" y="1" z="1"/>
598 </Grid>
599 </ObservationFromGrid>
600 <ObservationFromRay/>
601 <ObservationFromRecentCommands/>
602 <ContinuousMovementCommands turnSpeedDegs="80"/>
603 <AbsoluteMovementCommands />
604 <DiscreteMovementCommands />
605 <InventoryCommands/>
606 <ChatCommands/>
607 <RewardForTouchingBlockType>
608 <Block type="diamond_block" reward="1.0"/>
609 </RewardForTouchingBlockType>
610 </AgentHandlers>
611 </AgentSection>
612
613</Mission>
614
615"""
616)
617
618if __name__ == "__main__":
619 """
620 The following is some random heuristics to demo how you
621 would use the MalmoWrapper and the environment defined
622 with the xml above.
623 """
624
625 a = AtomSpace()
626 set_default_atomspace(a)
627
628 malmoWrapper = MalmoWrapper(missionXML=missionXML, validate=True)
629
630 rw, ob, done = malmoWrapper.restart()
631 total_reward = 0
632 agent = malmoWrapper.agent_host
633 world_state = agent.getWorldState()
634 # relocate_agent(agent)
635 adjust_pitch(agent)
636 while world_state.is_mission_running:
637 # curr_x, curr_y, curr_z = get_curr_loc(agent)
638 # print(curr_x, curr_y, curr_z)
639 # nb = input("Enter command: ")
640
641 # nx, ny, nz = nb.split(",")
642 # turn_to(agent, curr_x, curr_y, curr_z, int(nx), int(ny), int(nz))
643
644 # agent.sendCommand(nb)
645
646 world_state = agent.getWorldState()
647 if world_state.number_of_rewards_since_last_state > 0:
648 total_reward += world_state.rewards[-1].getValue()
649 print("total reward minecraft = {}".format(total_reward))
650 ob, rw, _ = go_to_key(agent)
651 print("Observation: {}, reward: {}".format(ob, rw))
652
653 ob, rw, _ = go_to_house(agent)
654 print("Observation: {}, reward: {}".format(ob, rw))
655
656 ob, rw, _ = go_to_diamonds(agent)
657 print("Observation: {}, reward: {}".format(ob, rw))
658
659 time.sleep(2)
660 print("total reward = {}".format(total_reward))
661 print()
662 print("Mission ended")