-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQLearning.py
355 lines (314 loc) · 12.3 KB
/
QLearning.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# ------------------------------------------------------------------------------------------------
# Copyright (c) 2016 Microsoft Corporation
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
# associated documentation files (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge, publish, distribute,
# sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or
# substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
# NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# ------------------------------------------------------------------------------------------------
# Tutorial sample #7: The Maze Decorator
try:
from malmo import MalmoPython
except:
import MalmoPython
import os
import sys
import time
import json
import numpy as np
import random
# from priority_dict import priorityDictionary as PQ
# sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0) # flush print output immediately
def GetMissionXML(seed, gp, size=10):
return '''<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<Mission xmlns="http://ProjectMalmo.microsoft.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<About>
<Summary>Hello world!</Summary>
</About>
<ServerSection>
<ServerInitialConditions>
<Time>
<StartTime>1000</StartTime>
<AllowPassageOfTime>false</AllowPassageOfTime>
</Time>
<Weather>clear</Weather>
</ServerInitialConditions>
<ServerHandlers>
<FlatWorldGenerator generatorString="3;7,44*49,73,35:1,159:4,95:13,35:13,159:11,95:10,159:14,159:6,35:6,95:6;12;"/>
<DrawingDecorator>
<DrawSphere x="-27" y="70" z="0" radius="30" type="air"/>
</DrawingDecorator>
<MazeDecorator>
<Seed>''' + str(seed) + '''</Seed>
<SizeAndPosition width="''' + str(size) + '''" length="''' + str(size) + '''" height="10" xOrigin="-32" yOrigin="69" zOrigin="-5"/>
<StartBlock type="emerald_block" fixedToEdge="true"/>
<EndBlock type="redstone_block" fixedToEdge="true"/>
<PathBlock type="diamond_block"/>
<FloorBlock type="air"/>
<GapBlock type="air"/>
<GapProbability>''' + str(gp) + '''</GapProbability>
<AllowDiagonalMovement>false</AllowDiagonalMovement>
</MazeDecorator>
<ServerQuitFromTimeUp timeLimitMs="30000"/>
<ServerQuitWhenAnyAgentFinishes/>
</ServerHandlers>
</ServerSection>
<AgentSection mode="Survival">
<Name>CS175AwesomeMazeBot</Name>
<AgentStart>
<Placement x="0.5" y="56.0" z="0.5" yaw="0"/>
</AgentStart>
<AgentHandlers>
<DiscreteMovementCommands/>
<AgentQuitFromTouchingBlockType>
<Block type="redstone_block"/>
</AgentQuitFromTouchingBlockType>
<ObservationFromGrid>
<Grid name="floorAll">
<min x="-10" y="-1" z="-10"/>
<max x="10" y="-1" z="10"/>
</Grid>
</ObservationFromGrid>
</AgentHandlers>
</AgentSection>
</Mission>'''
def load_grid(world_state):
"""
Used the agent observation API to get a 21 X 21 grid box around the agent (the agent is in the middle).
Args
world_state: <object> current agent world state
Returns
grid: <list> the world grid blocks represented as a list of blocks (see Tutorial.pdf)
"""
while world_state.is_mission_running:
# sys.stdout.write(".")
time.sleep(0.1)
world_state = agent_host.getWorldState()
if len(world_state.errors) > 0:
raise AssertionError('Could not load grid.')
if world_state.number_of_observations_since_last_state > 0:
msg = world_state.observations[-1].text
observations = json.loads(msg)
grid = observations.get(u'floorAll', 0)
break
return grid
#找到起点、终点、air_block、diamond_block
def find_start_end(grid):
"""
Finds the source and destination block indexes from the list.
Args
grid: <list> the world grid blocks represented as a list of blocks (see Tutorial.pdf)
Returns
start: <int> source block index in the list
end: <int> destination block index in the list
"""
#------------------------------------
#
# Fill and submit this code
#
# return (None, None)
#-------------------------------------
counter = 0
eb_index = None
rb_index = None
air_block=[]
diamond_block=[]
state=[]
for i in grid:
if i =='diamond_block':
diamond_block.append(counter)
if i =='air':
air_block.append(counter)
if i == 'emerald_block':
eb_index = counter
if i == 'redstone_block':
rb_index = counter
state.append(counter)
counter+=1
return (eb_index, rb_index,air_block,diamond_block)
# -------------------------------------
def extract_action_list_from_path(path_list):
"""
Converts a block idx path to action list.
Args
path_list: <list> list of block idx from source block to dest block.
Returns
action_list: <list> list of string discrete action commands (e.g. ['movesouth 1', 'movewest 1', ...]
"""
action_trans = {-21: 'movenorth 1', 21: 'movesouth 1', -1: 'movewest 1', 1: 'moveeast 1'}
alist = []
for i in range(len(path_list) - 1):
curr_block, next_block = path_list[i:(i + 2)]
alist.append(action_trans[next_block - curr_block])
return alist
# 状态转移函数,需要返回本次动作是否到达终点,本次动作的reward,本次动作后的下一个state
def Reward_state_action(s, a):
# 向上移动
if a == 0:
# Fill and submit this code
new_s = s - 21
if new_s == end:
return ((True, 1, new_s))
if new_s in states:
return((False, -1, new_s))
return((False, -1, s))
# 向下移动
elif a == 1:
# Fill and submit this code
new_s = s + 21
if new_s == end:
return ((True, 1, new_s))
if new_s in states:
return((False, -1, new_s))
return((False, -1, s))
# 向左移动
elif a == 2:
# Fill and submit this code
new_s = s - 1
if new_s == end:
return ((True, 1, new_s))
if new_s in states:
return((False, -1, new_s))
return((False, -1, s))
# 向右移动
else:
new_s = s + 1
if new_s == end:
return ((True, 1, new_s))
if new_s in states:
return((False, -1, new_s))
return((False, -1, s))
# Fill and submit this code
def epsilon_greedy(qtem, s, epsilon):
p = np.random.random()
if p < epsilon:
action = np.random.choice(actions)
else :
action = qtem[states.index(s)]
return action
def QLearning(num, epsilon, gamma):
qfunc = np.random.normal(size=(len(states),len(actions)))
qtem = np.argmax(qfunc, axis=1)
for k in range(num):
if k % 25 == 0:
epsilon = max(epsilon * 0.99, 0.01)
k_start = start
k_tag = False
if k_start == end:
k_tag = True
while not k_tag:
action = epsilon_greedy(qtem, k_start, epsilon)
k_tag, reward, new_state = Reward_state_action(k_start, action)
qfunc[states.index(k_start)][action] += 0.01 * (reward + gamma * max(qfunc[states.index(new_state)]) - qfunc[states.index(k_start)][action])
k_start = new_state
return qfunc
def get_shortest_path(qfunc):
s_path = []
s_path.append(start)
cur = start
while cur != end:
action = np.argmax(qfunc[states.index(cur)])
tc = Reward_state_action(cur, action)
cur = tc[2]
s_path.append(cur)
return s_path
agent_host = MalmoPython.AgentHost()
try:
agent_host.parse(sys.argv)
except RuntimeError as e:
print('ERROR:', e)
print(agent_host.getUsage())
exit(1)
if agent_host.receivedArgument("help"):
print(agent_host.getUsage())
exit(0)
if agent_host.receivedArgument("test"):
num_repeats = 1
else:
num_repeats = 5
for i in range(num_repeats):
size = int(5)
print("Size of maze:", size)
my_mission = MalmoPython.MissionSpec(GetMissionXML("0", 0.4 + float(i / 20.0), size), True)
my_mission_record = MalmoPython.MissionRecordSpec()
my_mission.requestVideo(800, 500)
my_mission.setViewpoint(1)
# Attempt to start a mission:
max_retries = 3
my_clients = MalmoPython.ClientPool()
my_clients.add(MalmoPython.ClientInfo('127.0.0.1', 10000)) # add Minecraft machines here as available
for retry in range(max_retries):
try:
agent_host.startMission(my_mission, my_clients, my_mission_record, 0, "%s-%d" % ('Moshe', i))
break
except RuntimeError as e:
if retry == max_retries - 1:
print("Error starting mission", (i + 1), ":", e)
exit(1)
else:
time.sleep(2)
# Loop until mission starts:
print("Waiting for the mission", (i + 1), "to start ", )
world_state = agent_host.getWorldState()
while not world_state.has_mission_begun:
# sys.stdout.write(".")
time.sleep(0.1)
world_state = agent_host.getWorldState()
for error in world_state.errors:
print("Error:", error.text)
print()
print("Mission", (i + 1), "running.")
grid = load_grid(world_state)
air_block = []
diamond_block = []
start,end,air_block,diamond_block=find_start_end(grid)
states = [] # 为简化计算可以仅获取迷宫中agent可站立的states
actions = np.arange(4) # 定义actions
num = 500 #定义采样次数
epsilon = 0.95 #定义epsilon
gamma = 1 #定义gamma
s=['emerald_block','diamond_block','redstone_block']
counter=0
for j in grid:
if j in s:
states.append(counter)
counter +=1
q = QLearning(num, epsilon, gamma)
path = get_shortest_path(q)
print(path)
action_list = extract_action_list_from_path(path)
print("Output (start,end)", (i + 1), ":", (start, end))
print("Output (path length)", (i + 1), ":", len(path))
print("Output (actions)", (i + 1), ":", action_list)
# Loop until mission ends:
action_index = 0
while world_state.is_mission_running:
# sys.stdout.write(".")
time.sleep(0.1)
# Sending the next commend from the action list -- found using the Dijkstra algo.
if action_index >= len(action_list):
print("Error:", "out of actions, but mission has not ended!")
time.sleep(2)
else:
agent_host.sendCommand(action_list[action_index])
action_index += 1
if len(action_list) == action_index:
# Need to wait few seconds to let the world state realise I'm in end block.
# Another option could be just to add no move actions -- I thought sleep is more elegant.
time.sleep(2)
world_state = agent_host.getWorldState()
for error in world_state.errors:
print("Error:", error.text)
print()
print("Mission", (i + 1), "ended")
# Mission has ended.