OverviewFilesCommitsIssuesDocsReleasesPeers

opencog-rocca commits / 6d9a1f5

Example usage with opencog agent
6d9a1f5ab6ebba76e8e218f570bbf099c675b251
by Hedra <tanksh24@gmail.com> · 2021-11-22T14:40:33.000Z

Changed files (2) +180 -78

examples/flatworld.py +107 -0
……@@ -0,0 +1,107 @@
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 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_action
15from functools import wraps
16
17
18class AbstractMalmoWrapper(MalmoWrapper):
19 def __init__(self, missionXML, validate):
20 MalmoWrapper.__init__(self, missionXML, validate)
21
22 @staticmethod
23 def step_decorator(step):
24 @wraps(step)
25 def wrapper(ref, action, update_callback=None):
26 # name, value = action.out[0], action.out[1]
27 # ws = step(ref, name.name + " " + value.name, update_callback)
28 name = action.out[0]
29 ws = step(ref, name, update_callback)
30
31 return MalmoWrapper.parse_world_state(ws)
32
33 return wrapper
34
35 @step_decorator.__func__
36 def step(self, action, update_callback=None):
37 try:
38 func = globals()[action.name]
39 func(self.agent_host)
40 time.sleep(0.2)
41 self.world_state = self.agent_host.getWorldState()
42
43 for error in self.world_state.errors:
44 print("Error: ", error.text)
45
46 if update_callback is not None:
47 update_callback(action, self.world_state)
48 except RuntimeError as e:
49 print("Error sending command:", e)
50 return self.world_state
51
52
53
54if __name__ == "__main__":
55 atomspace = AtomSpace()
56 set_default_atomspace(atomspace)
57
58 # Init loggers
59 log.set_level("info")
60 # log.set_sync(True)
61 agent_log.set_level("debug")
62 # agent_log.set_sync(True)
63 ure_logger().set_level("debug")
64
65 #
66 wrapped_env = AbstractMalmoWrapper(missionXML=missionXML, validate=True)
67
68 # Create Goal
69 pgoal = EvaluationLink(PredicateNode("Reward"), NumberNode("1"))
70 ngoal = EvaluationLink(PredicateNode("Reward"), NumberNode("0"))
71
72 # Create Action Space. The set of allowed actions an agent can take.
73 # TODO take care of action parameters.
74 action_space = {
75 ExecutionLink(SchemaNode("go_to_the_key")),
76 ExecutionLink(SchemaNode("get_the_key")),
77 ExecutionLink(SchemaNode("go_to_the_house")),
78 ExecutionLink(SchemaNode("open_the_door")),
79 ExecutionLink(SchemaNode("go_to_the_diamonds")),
80 ExecutionLink(SchemaNode("collect_diamonds"))
81 }
82
83 cog_agent = OpencogAgent(wrapped_env, atomspace, action_space, pgoal, ngoal)
84
85
86 # Training/learning loop
87 lt_iterations = 3 # Number of learning-training iterations
88 lt_period = 200 # Duration of a learning-training iteration
89 for i in range(lt_iterations):
90 cog_agent.reset_action_counter()
91 par = cog_agent.accumulated_reward # Keep track of the reward before
92 # Discover patterns to make more informed decisions
93 agent_log.info("Start learning ({}/{})".format(i + 1, lt_iterations))
94 cog_agent.learn()
95 # Run agent to accumulate percepta
96 agent_log.info("Start training ({}/{})".format(i + 1, lt_iterations))
97 for j in range(lt_period):
98 cog_agent.control_cycle()
99 time.sleep(0.01)
100 log.info("step_count = {}".format(cog_agent.cycle_count))
101 nar = cog_agent.accumulated_reward - par
102 agent_log.info(
103 "Accumulated reward during {}th iteration = {}".format(i + 1, nar)
104 )
105 agent_log.info(
106 "Action counter during {}th iteration:\n{}".format(i + 1, cog_agent.action_counter)
107 )
rocca/envs/malmo_demo/flatworld_env.py +73 -78
……@@ -1,24 +1,13 @@
11import os
22import time
33
4from fastcore.basics import gen
5
64from rocca.envs.wrappers.malmo_wrapper import MalmoWrapper
75from rocca.envs.wrappers.utils import *
86from builtins import range
9from past.utils import old_div
10import os
11import sys
12import time
137import json
148import random
159import math
1610
17if sys.version_info[0] == 2:
18 sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0) # flush print output immediately
19else:
20 import functools
21 print = functools.partial(print, flush=True)
2211
2312def drawBlock(x, y, z, type):
2413 return """<DrawBlock x='{0}' y='{1}' z='{2}' type='{3}'/>""".format(x, y, z, type)
……@@ -26,6 +15,9 @@ def drawBlock(x, y, z, type):
2615def GenCuboid(x1, y1, z1, x2, y2, z2, blocktype):
2716 return '<DrawCuboid x1="' + str(x1) + '" y1="' + str(y1) + '" z1="' + str(z1) + '" x2="' + str(x2) + '" y2="' + str(y2) + '" z2="' + str(z2) + '" type="' + blocktype + '"/>'
2817
18def drawLine(x1, y1, z1, x2, y2, z2, blocktype, face):
19 return '<DrawLine x1="' + str(x1) + '" y1="' + str(y1) + '" z1="' + str(z1) + '" x2="' + str(x2) + '" y2="' + str(y2) + '" z2="' + str(z2) + '" type="' + blocktype + '" face="' + face +'"/>'
20
2921def build_house(x, y, z, width, length, height, blocktype):
3022 x_front = x
3123 x_back = x + length if x >= 0 else x-length
……@@ -56,8 +48,10 @@ def build_house(x, y, z, width, length, height, blocktype):
5648
5749 return genstring
5850
59
60agent_x, agent_y, agent_z, agent_yaw = 9, 230, -2, 90
51agent_x, agent_y, agent_z, agent_yaw = 19, 230, -2, 90
52key_x, key_y, key_z = 7, 228, -10
53door_x, door_y, door_z = -4, 228, -3
54diamond_x, diamond_y, diamond_z = -8, 228, -3
6155
6256missionXML = '''
6357<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
……@@ -81,14 +75,10 @@ missionXML = '''
8175 <FlatWorldGenerator generatorString="3;7,220*1,5*3,2;1;biome_1"/>
8276 <DrawingDecorator>
8377 '''+build_house(-4, 226, -1,6,6,5,"planks")+'''
84 <DrawBlock x="-8" y="228" z="-3" type="diamond_block"/>
85
86
87 <DrawLine x1="-4" y1="227" z1="-3" x2="-4" y2="228" z2="-3" type="dark_oak_door" face="EAST"/>
88
89 <DrawBlock x="-4" y="227" z="-15" type="command_block"/>
90 <DrawBlock x="-4" y="228" z="-15" type="tripwire_hook"/>
91
78 '''+drawBlock(diamond_x, diamond_y, diamond_z, "diamond_block")+'''
79 '''+drawLine(door_x, door_y-1, door_z, door_x, door_y, door_z, "dark_oak_door", "EAST")+'''
80 '''+drawBlock(key_x, key_y-1, key_z, "command_block")+'''
81 '''+drawBlock(key_x, key_y, key_z, "tripwire_hook")+'''
9282
9383 </DrawingDecorator>
9484 <ServerQuitFromTimeUp description="" timeLimitMs="1000000"/>
……@@ -128,60 +118,85 @@ missionXML = '''
128118</Mission>
129119
130120'''
131
132# Moves the agent to the position given
133def turn_to(agent,x1, y1, z1, yaw, x2, y2, z2):
134 x_dist = abs(x1) + abs(x2)
135 z_dist = abs(z1) + abs(z2)
121def get_angle(x1, y1, z1, x2, y2, z2):
122 x_dist = abs(x1 - x2)
123 z_dist = abs(z1 - z2)
136124 h = math.sqrt(x_dist**2 + z_dist**2)
137 angle = yaw + math.degrees(z_dist/h)
138 agent.step(mk_action("setYaw",angle))
125 return math.degrees(z_dist/h) if h != 0 else 0
126
127# Turn the agent towards the location given
128def turn_to(agent, x1, y1, z1, yaw, x2, y2, z2):
129 angle = get_angle(x1, y1, z1, x2, y2, z2)
130 yaw = 90 if x1 > x2 else -90
131 if yaw > 0:
132 angle = yaw - angle if z1 < z2 else yaw + angle
133 else:
134 angle = yaw + angle if z1 < z2 else yaw - angle
135 agent.sendCommand("setYaw {}".format(angle))
139136
140137# Agent goes to the key
141138# reward=0
142def go_to_the_key(agent, x, y, z):
143 turn_to(agent,agent_x, agent_y, agent_z, agent_yaw, x, y, z)
144 agent.step(mk_action("move", 1))
145
139def go_to_the_key(agent):
140 global agent_x, agent_y, agent_z
141 turn_to(agent,agent_x, agent_y, agent_z, agent_yaw, key_x, key_y, key_z)
142 time.sleep(0.2)
143 agent.sendCommand("move 1")
144 stop_condition(agent, 'tripwire_hook')
145 agent_x, agent_y, agent_z = key_x, key_y, key_z
146146
147147# Agent stops and grab the key
148148# observations: agent holds the key and reward=0
149149def get_the_key(agent):
150 global agent_z
151 agent_z = -15
152 agent.step(mk_action("move", 0))
153 agent.step(mk_action("hotbar.2", 1))
150 agent.sendCommand("move 0")
151 agent.sendCommand("hotbar.2 1")
154152
155153# Agent goes to the house. The agent should have the key
156154# observations:
157155# - Agent is next to closed door
158156# - Agent holds the key
159157# - reward = 0
160def go_to_the_house(agent, x, y, z):
161 agent.step(mk_action("tpx", agent_x))
158def go_to_the_house(agent):
159 global agent_x, agent_y, agent_z
160 turn_to(agent,agent_x, agent_y, agent_z, agent_yaw, door_x, door_y, door_z)
162161 time.sleep(0.2)
163 turn_to(agent, x, y, z)
164 agent.step(mk_action("move", 1))
162 agent.sendCommand("move 1")
163 stop_condition(agent, 'dark_oak_door')
164 agent_x, agent_y, agent_z = door_x, door_y, door_z
165165
166166# Agent opens the house.
167167# observations:
168168# - Agent next to opened door
169169# - reward = 0
170170def open_the_door(agent):
171 agent.step(mk_action("move", 0))
172 agent.step(mk_action("attack", 1))
171 agent.sendCommand("move 0")
172 agent.sendCommand("attack 1")
173173
174174# Agent goes to the diamond inside the house.
175175# reward = 0
176def go_to_the_diamond():
177 return True
176def go_to_the_diamonds(agent):
177 global agent_x, agent_y, agent_z
178 turn_to(agent,agent_x, agent_y, agent_z, agent_yaw, diamond_x, diamond_y, diamond_z)
179 time.sleep(0.2)
180 agent.sendCommand("move 1")
181 stop_condition(agent, "diamond_block")
182 agent_x, agent_y, agent_z = diamond_x, diamond_y, diamond_z
183
184def stop_condition(agent, block_type):
185 done = False
186 while not done:
187 world_state = agent.getWorldState()
188 if world_state.number_of_observations_since_last_state > 0:
189 ob = world_state.observations[-1].text
190 if block_type in ob:
191 agent.sendCommand("move 0")
192 done = True
178193
179194# Agent collects the diamond inside the house
180195# reward = 1
181def collect_diamond(agent):
182 agent.step(mk_action("hotbar.1", 1))
183 rw, ob, _ = agent.step(mk_action("attack", 1))
184 return rw, ob
196def collect_diamonds(agent):
197 agent.sendCommand("hotbar.1 1")
198 agent.sendCommand("attack 1")
199
185200
186201if __name__ == "__main__":
187202 """
……@@ -205,42 +220,22 @@ if __name__ == "__main__":
205220 malmoWrapper.step(mk_action("hotbar.1", 0))
206221 malmoWrapper.step(mk_action("hotbar.2", 0))
207222
208 go_to_the_key(malmoWrapper, -4, 228, -15)
223 agent = malmoWrapper.agent_host
224
225 go_to_the_key(agent)
209226
210 while not done :
227 while not done:
211228 print(".", end="")
212229 time.sleep(0.5)
213 agent = malmoWrapper.agent_host
214230 world_state = malmoWrapper.agent_host.getWorldState()
215231
216232 if world_state.number_of_observations_since_last_state > 0:
217233 ob = world_state.observations[-1].text
218
219 print(ob)
220 if "tripwire_hook" in ob:
221 malmoWrapper.step(mk_action("move", 0))
222 malmoWrapper.step(mk_action("hotbar.2", 1))
223 malmoWrapper.step(mk_action("tpx", agent_x))
224
225 malmoWrapper.step(mk_action("setYaw", 45))
226 malmoWrapper.step(mk_action("move", 1))
227
228 if "dark_oak_door" in ob:
229 malmoWrapper.step(mk_action("move", 0))
230 malmoWrapper.step(mk_action("setYaw", 90))
231 malmoWrapper.step(mk_action("attack", 1))
232 malmoWrapper.step(mk_action("move", 1))
233
234 if "diamond_block" in ob:
235 malmoWrapper.step(mk_action("move", 0))
236 collect_diamond(malmoWrapper)
237
238 if world_state.number_of_rewards_since_last_state > 0:
239 malmoWrapper.step(mk_action("attack", 0))
240 reward = world_state.rewards[-1].getValue()
241 print("Reward: " + str(reward))
242 total_reward += reward
243 malmoWrapper.step(mk_action("tp", "{} {} {}".format(agent_x, agent_y, agent_z)))
244
234 go_to_the_house(agent)
235 get_the_key(agent)
236 open_the_door(agent)
237 collect_diamonds(agent)
238 malmoWrapper.step(mk_action("tp", "{} {} {}".format(19, 230, -2)))
239 done = True
245240 print()
246241 print("Mission ended")