OverviewFilesCommitsIssuesDocsReleasesPeers

opencog-rocca commits / baa21ba

Renamed files
baa21ba11c066c03a691d802632ca7c22b8a652b
by Hedra <tanksh24@gmail.com> · 2022-06-06T22:56:57.000Z

Changed files (2) +855 -0

examples/collect_diamonds.py +192 -0
……@@ -0,0 +1,192 @@
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
8from opencog.ure import ure_logger
9
10
11from rocca.agents import OpencogAgent
12from rocca.agents.utils import TRUE_TV, agent_log
13from rocca.envs.malmo_demo.collect_diamonds_env import *
14from rocca.envs.wrappers import MalmoWrapper
15from rocca.envs.wrappers.utils import mk_evaluation
16from rocca.agents.utils import *
17from functools import wraps
18import os
19import requests
20
21
22class AbstractMalmoWrapper(MalmoWrapper):
23 def __init__(self, missionXML, validate):
24 MalmoWrapper.__init__(self, missionXML, validate)
25
26 @staticmethod
27 def step_decorator(step):
28 @wraps(step)
29 def wrapper(ref, action, update_callback=None):
30 name = action.out[0]
31 return step(ref, name, update_callback)
32
33 return wrapper
34
35 @step_decorator.__func__
36 def step(self, abstract_action, update_callback=None):
37 try:
38 func = globals()[abstract_action.name]
39 observations, reward, done = func(self.agent_host)
40 time.sleep(0.2)
41 obs_list = []
42 if observations:
43 for k in observations:
44 if isinstance(observations[k], list):
45 obs_list.append(mk_evaluation(k, *observations[k]))
46 else:
47 obs_list.append(mk_evaluation(k, observations[k]))
48 except RuntimeError as e:
49 print("Error sending command:", e)
50 return obs_list, mk_evaluation("Reward", reward), done
51
52
53class FlatworldAgent(OpencogAgent):
54 def __init__(self, env, atomspace):
55 set_default_atomspace(atomspace)
56
57 # Create Action Space. The set of allowed actions an agent can take.
58 # TODO take care of action parameters.
59 action_space = {
60 ExecutionLink(SchemaNode("go_to_key")),
61 ExecutionLink(SchemaNode("go_to_house")),
62 ExecutionLink(SchemaNode("go_to_diamonds")),
63 }
64 # Create Goal
65 pgoal = EvaluationLink(PredicateNode("Reward"), NumberNode("1"))
66 ngoal = EvaluationLink(PredicateNode("Reward"), NumberNode("0"))
67
68 # Call super ctor
69 OpencogAgent.__init__(self, env, atomspace, action_space, pgoal, ngoal)
70
71 # Overwrite some OpencogAgent parameters
72 self.polyaction_mining = False
73 self.monoaction_general_succeedent_mining = True
74 self.temporal_deduction = True
75 self.cogscm_minimum_strength = 0.75
76 self.cogscm_maximum_shannon_entropy = 1
77 self.cogscm_maximum_differential_entropy = 0
78 self.cogscm_maximum_variables = 0
79 self.miner_maximum_iterations = 100000
80 self.miner_maximum_variables = 9
81 self.miner_minimum_support = 3
82 self.conditional_conjunction_introduction = False
83 self.expiry = 3
84 self.visualize_cogscm = False
85 self.mixture_model.complexity_penalty = 0.6
86 self.mixture_model.delta = 1.0e-30
87 self.mixture_model.compressiveness = 0.1
88 self.mixture_model.weight_influence = 1.0
89
90 # Todo: restart the environment to get an initial reward
91 self.initial_reward = EvaluationLink(PredicateNode("Reward"), NumberNode("0"))
92
93
94if __name__ == "__main__":
95 atomspace = AtomSpace()
96 set_default_atomspace(atomspace)
97
98 # Init loggers
99 log.set_level("info")
100 # log.set_sync(True)
101 agent_log.set_level("fine")
102 # agent_log.set_sync(True)
103 ure_logger().set_level("debug")
104 miner_log = MinerLogger(atomspace)
105 miner_log.set_level("debug")
106
107 #
108 wrapped_env = AbstractMalmoWrapper(missionXML=missionXML, validate=True)
109
110 cog_agent = FlatworldAgent(wrapped_env, atomspace)
111
112 cogscm_atomspace_file = "cogscms_as.scm"
113 saved_cogscm = os.path.isfile(cogscm_atomspace_file)
114 if cog_agent.visualize_cogscm:
115 # The address of the scheme REST endpoint
116 PORT, IP_ADDRESS = "5000", "127.0.0.1"
117 try:
118 status_code = requests.get(
119 "http://{}:{}/api/v1.1/atoms".format(IP_ADDRESS, PORT)
120 )
121 except:
122 raise RuntimeError(
123 "REST service is not started. Please run start_rest_service.py file first."
124 )
125
126 # Log all parameters of cag, useful for debugging
127 cog_agent.log_parameters(level="debug")
128 reward_t0 = cog_agent.record(cog_agent.initial_reward, 0, TRUE_TV)
129 agent_log.info("Initial reward: {}".format(reward_t0))
130
131 # Training/learning loop
132 lt_iterations = 2 # Number of learning-training iterations
133 lt_period = 50 # Duration of a learning-training iteration
134 for i in range(lt_iterations):
135 cog_agent.reset_action_counter()
136 par = cog_agent.accumulated_reward # Keep track of the reward before
137 if i > 0:
138 print(
139 """
140 **************************************
141 * Applying PLN and Pattern Miner ... *
142 **************************************
143
144 """
145 )
146 if saved_cogscm:
147 # load cogscm_atomspace
148 cog_agent.load_cogscms_atomspace(cogscm_atomspace_file)
149 else:
150 # Discover patterns to make more informed decisions
151 agent_log.info("Start learning ({}/{})".format(i + 1, lt_iterations))
152 cog_agent.learn()
153
154 # Visualize discovered cogscms
155 # set self.visualize_cogscm=True to enable visualization of the cogscms.
156 # Note that, RESTAPI needs be running to be able to post to the scheme endpoint.
157 # check start_rest_service.py file.
158 if cog_agent.visualize_cogscm:
159 post_to_restapi_scheme_endpoint(
160 None, cogscm_as=cog_agent.cogscms_atomspace
161 )
162
163 # Run agent to accumulate percepta
164 agent_log.info("Start training ({}/{})".format(i + 1, lt_iterations))
165 for j in range(lt_period):
166 cog_agent.control_cycle()
167 time.sleep(0.01)
168 log.info("step_count = {}".format(cog_agent.cycle_count))
169 nar = cog_agent.accumulated_reward - par
170 agent_log.info(
171 "Accumulated reward during {}th iteration = {}".format(i + 1, nar)
172 )
173 agent_log.info(
174 "Action counter during {}th iteration:\n{}".format(
175 i + 1, cog_agent.action_counter
176 )
177 )
178 # Log all agent's atomspaces at the end (at fine level)
179 agent_log_atomspace(cog_agent.atomspace, "fine", "cog_agent.atomspace")
180 agent_log_atomspace(
181 cog_agent.percepta_atomspace, "fine", "cog_agent.percepta_atomspace"
182 )
183 agent_log_atomspace(
184 cog_agent.cogscms_atomspace, "fine", "cog_agent.cogscms_atomspace"
185 )
186 agent_log_atomspace(
187 cog_agent.working_atomspace, "fine", "cog_agent.working_atomspace"
188 )
189
190 # Save cogscm_atomspace
191 if not saved_cogscm:
192 cog_agent.save_cogscms_atomspace("cogscms_as.scm")
rocca/envs/malmo_demo/collect_diamonds_env.py +663 -0
……@@ -0,0 +1,663 @@
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 "tripwire_hook": "key",
260 "dark_oak_door": "house",
261 "diamond_block": "diamonds",
262 "planks": "planks",
263 "diamond_ore": "collect diamonds",
264}
265
266# stop the agent
267def stop_condition(agent, blocktype, sec=10):
268 print("\nMoving to {} \n".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, "tripwire_hook")
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 agent.sendCommand("move 1")
413 agent.sendCommand("tp {} {} {}".format(door_x, door_y, door_z))
414 time.sleep(0.2)
415 agent.sendCommand("move 0")
416 observation["inside"] = ["self", "house"]
417 agent.sendCommand("hotbar.1 1")
418 hold_key = False
419 else:
420 observation["outside"] = ["self", "house"]
421 observation["nextto"] = ["self", "closed_door"]
422 reward = 0
423 print("Observation: {}".format(get_txt(observation)))
424 return observation, reward, is_mission_running
425
426
427def place_diamonds(agent):
428 global place_count
429 curr_x, curr_y, curr_z, curr_yaw = get_curr_loc(agent)
430 turn_to(
431 agent,
432 curr_x,
433 curr_y,
434 curr_z,
435 (exit_x + 2) + (2 * place_count),
436 exit_y,
437 fence_max,
438 )
439 agent.sendCommand("move 1")
440 time.sleep(2 - (place_count * 0.01))
441 placed = False
442 while not placed:
443 world_state = agent.getWorldState()
444 if world_state.number_of_observations_since_last_state > 0:
445 msg = world_state.observations[-1].text
446 ob = json.loads(msg)
447 # Use the line-of-sight observation to determine where to place:
448 if u"LineOfSight" in ob:
449 agent.sendCommand("turn {}".format(random.choice([0.05, -0.05])))
450 los = ob[u"LineOfSight"]
451 x = int(math.floor(los["x"]))
452 z = int(math.floor(los["z"]))
453 if los["inRange"]:
454 agent.sendCommand("move 0")
455 time.sleep(2)
456 agent.sendCommand("use")
457 agent.sendCommand("turn 0")
458 agent.sendCommand("hotbar.5 1")
459 time.sleep(2)
460 place_count += 1
461 placed = True
462
463
464def exit(agent):
465 curr_x, curr_y, curr_z, curr_yaw = get_curr_loc(agent)
466 turn_to(agent, curr_x, curr_y, curr_z, exit_x, exit_y, exit_z)
467 agent.sendCommand("move 1")
468 agent.sendCommand("use 1")
469 time.sleep(0.2)
470 while is_mission_running(agent):
471 curr_x, curr_y, curr_z, _ = get_curr_loc(agent)
472 if not inside_house(curr_x, curr_z):
473 agent.sendCommand("move 0")
474 agent.sendCommand("tp {} {} {}".format(exit_x + 0.5, curr_y, exit_z + 1))
475 time.sleep(0.1)
476 curr_x, curr_y, curr_z, _ = get_curr_loc(agent)
477 turn_to(agent, curr_x, curr_y, curr_z, exit_x, exit_y, exit_z)
478 agent.sendCommand("use 1")
479 time.sleep(0.05)
480 agent.sendCommand("use 0")
481 break
482 agent.sendCommand("hotbar.3 1")
483 place_diamonds(agent)
484
485
486# Move the agent towards the diamond and collect it.
487# reward = 1
488def go_to_diamonds(agent):
489 global total_reward
490 adjust_pitch(agent)
491 # agent.sendCommand("chat Collect diamonds")
492 observation = {}
493 curr_x, curr_y, curr_z, curr_yaw = get_curr_loc(agent)
494 turn_to(
495 agent,
496 curr_x,
497 curr_y,
498 curr_z,
499 diamond_x,
500 diamond_y,
501 diamond_z,
502 curr_yaw=curr_yaw,
503 )
504 time.sleep(0.2)
505 agent.sendCommand("move 1")
506 action_complete = stop_condition(agent, "diamond_ore")
507 if action_complete:
508 agent.sendCommand("attack 1")
509 time.sleep(0.2)
510 agent.sendCommand("attack 0")
511 time.sleep(0.2)
512 reward = 1
513 total_reward += 1
514 exit(agent)
515 relocate_agent(agent)
516 time.sleep(0.3)
517 observation["outside"] = ["self", "house"]
518 else:
519 reward = 0
520 if not inside_house(curr_x, curr_z):
521 observation["outside"] = ["self", "house"]
522 if hold_key:
523 observation["hold"] = ["self", "key"]
524 if reward > 0:
525 agent.sendCommand("chat Reward = {}".format(reward))
526 agent.sendCommand("chat: Total Reward of {} diamonds!".format(total_reward))
527 print("Reward = {}".format(reward))
528 print("Total Reward of {} diamonds!".format(total_reward))
529
530 else:
531 print("Observation: {}".format(get_txt(observation)))
532
533 return observation, reward, is_mission_running
534
535
536missionXML = (
537 """
538<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
539<Mission xmlns="http://ProjectMalmo.microsoft.com"
540 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
541
542 <About>
543 <Summary>Collect diamonds!</Summary>
544 </About>
545
546 <ServerSection>
547 <ServerInitialConditions>
548 <Time>
549 <StartTime>1000</StartTime>
550 <AllowPassageOfTime>false</AllowPassageOfTime>
551 </Time>
552 <Weather>clear</Weather>
553 <AllowSpawning>true</AllowSpawning>
554 </ServerInitialConditions>
555 <ServerHandlers>
556 <FlatWorldGenerator generatorString="3;7,220*1,5*3,2;1;village,decoration"/>
557 <DrawingDecorator>
558 """
559 + build_house(start_x, start_y, start_z, width, length, height, "brick_block")
560 + drawBlock(diamond_x + 5, diamond_y, diamond_z - 1, "diamond_block")
561 + drawBlock(
562 door_x, door_y - 1, door_z, "dark_oak_door", variant="lower", face="WEST"
563 )
564 + drawBlock(door_x, door_y, door_z, "dark_oak_door", variant="upper", face="WEST")
565 + drawBlock(exit_x, exit_y, exit_z, "dark_oak_door", variant="upper", face="NORTH")
566 + drawBlock(
567 exit_x, exit_y - 1, exit_z, "dark_oak_door", variant="lower", face="NORTH"
568 )
569 + drawBlock(key_x, key_y - 1, key_z, "stone")
570 + drawBlock(key_x, key_y, key_z, "tripwire_hook")
571 + draw_fence(fence_max, start_y + 1, fence_max)
572 + """
573
574 </DrawingDecorator>
575 <ServerQuitWhenAnyAgentFinishes/>
576 </ServerHandlers>
577 </ServerSection>
578
579 <AgentSection mode="Survival">
580 <Name>Cog Agent</Name>
581 <AgentStart>
582 """
583 + '<Placement x="{}" y="{}" z="{}" yaw="{}"/>'.format(
584 agent_x, agent_y, agent_z, agent_yaw
585 )
586 + """
587 <Inventory>
588 <InventoryItem slot="0" type="diamond_pickaxe"/>
589 <InventoryItem slot="1" type="tripwire_hook"/>
590 <InventoryBlock slot="2" type="diamond_block" quantity="63"/>
591 </Inventory>
592 </AgentStart>
593 <AgentHandlers>
594 <ObservationFromFullStats/>
595 <ObservationFromGrid>
596 <Grid name="BlocksInFront">
597 <min x="-1" y="1" z="-1"/>
598 <max x="1" y="1" z="1"/>
599 </Grid>
600 </ObservationFromGrid>
601 <ObservationFromRay/>
602 <ObservationFromRecentCommands/>
603 <ContinuousMovementCommands turnSpeedDegs="80"/>
604 <AbsoluteMovementCommands />
605 <DiscreteMovementCommands />
606 <InventoryCommands/>
607 <ChatCommands/>
608 <RewardForTouchingBlockType>
609 <Block type="diamond_block" reward="1.0"/>
610 </RewardForTouchingBlockType>
611 </AgentHandlers>
612 </AgentSection>
613
614</Mission>
615
616"""
617)
618
619if __name__ == "__main__":
620 """
621 The following is some random heuristics to demo how you
622 would use the MalmoWrapper and the environment defined
623 with the xml above.
624 """
625
626 a = AtomSpace()
627 set_default_atomspace(a)
628
629 malmoWrapper = MalmoWrapper(missionXML=missionXML, validate=True)
630
631 rw, ob, done = malmoWrapper.restart()
632 total_reward = 0
633 agent = malmoWrapper.agent_host
634 world_state = agent.getWorldState()
635 # relocate_agent(agent)
636 adjust_pitch(agent)
637 while world_state.is_mission_running:
638 # curr_x, curr_y, curr_z = get_curr_loc(agent)
639 # print(curr_x, curr_y, curr_z)
640 # nb = input("Enter command: ")
641
642 # nx, ny, nz = nb.split(",")
643 # turn_to(agent, curr_x, curr_y, curr_z, int(nx), int(ny), int(nz))
644
645 # agent.sendCommand(nb)
646
647 world_state = agent.getWorldState()
648 if world_state.number_of_rewards_since_last_state > 0:
649 total_reward += world_state.rewards[-1].getValue()
650 print("total reward minecraft = {}".format(total_reward))
651 ob, rw, _ = go_to_key(agent)
652 print("Observation: {}, reward: {}".format(ob, rw))
653
654 ob, rw, _ = go_to_house(agent)
655 print("Observation: {}, reward: {}".format(ob, rw))
656
657 ob, rw, _ = go_to_diamonds(agent)
658 print("Observation: {}, reward: {}".format(ob, rw))
659
660 time.sleep(2)
661 print("total reward = {}".format(total_reward))
662 print()
663 print("Mission ended")