-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathproject.py
More file actions
135 lines (119 loc) · 4.77 KB
/
Copy pathproject.py
File metadata and controls
135 lines (119 loc) · 4.77 KB
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
#
# _ooOoo_
# o8888888o
# 88" . "88
# (| -_- |)
# O\ = /O
# ____/`---'\____
# .' \\| |// `.
# / \\||| : |||// \
# / _||||| -:- |||||- \
# | | \\\ - /// | |
# | \_| ''\---/'' | |
# \ .-\__ `-` ___/-. /
# ___`. .' /--.--\ `. . __
# ."" '< `.___\_<|>_/___.' >'"".
# | | : `- \`.;`\ _ /`;.`/ - ` : | |
# \ \ `-. \_ __\ /__ _/ .-` / /
# ======`-.____`-.___\_____/___.-`____.-'======
# `=---='
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# 佛祖保佑 永无BUG
#
try:
import MalmoPython
except:
from malmo import MalmoPython
import os
import sys
import time
import json
from agent import Agent
import random
import utils
if __name__ == "__main__":
# read mission from
with open('mission.xml','r') as iFile:
missionXML = iFile.read()
iFile.close()
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)
iRepeat = 120
agent = Agent()
# repeat mission for
for i in range(iRepeat):
agent.pastActions = []
my_mission = MalmoPython.MissionSpec(missionXML, True)
my_mission_record = MalmoPython.MissionRecordSpec()
# Start mission
max_retries = 3
my_client_pool = MalmoPython.ClientPool()
my_client_pool.add(MalmoPython.ClientInfo("127.0.0.1", 10000))
for retry in range(max_retries):
try:
agent_host.startMission(my_mission, my_client_pool, my_mission_record, 0, "Monster Killer")
break
except RuntimeError as e:
if retry == max_retries - 1:
print("Error starting mission:", e)
exit(1)
else:
time.sleep(2)
# Wait until mission starts:
print("Waiting for the mission to start")
world_state = agent_host.getWorldState()
while not world_state.has_mission_begun:
time.sleep(0.1)
world_state = agent_host.getWorldState()
for error in world_state.errors:
print("Error:", error.text)
print()
print("Mission started: ",i, " with n = 1"," and with eps = ",agent.epsilon)
# Mission running
# let agent hunger s.t. no life recovery
agent_host.sendCommand("chat " + "/effect @p minecraft:hunger 5 201")
#agent_host.sendCommand("chat " + "/replaceitem entity @p slot.armor.chest minecraft:diamond_chestplate 1 0")
#agent_host.sendCommand("chat " + "/replaceitem entity @p slot.armor.head minecraft:diamond_helmet 1 0")
#agent_host.sendCommand("chat " + "/replaceitem entity @p slot.armor.legs minecraft:diamond_leggings 1 0")
#agent_host.sendCommand("chat " + "/replaceitem entity @p slot.armor.feet minecraft:diamond_boots 1 0")
time.sleep(2)
observations = agent.getObservations(world_state)
# generate enemy after hunger
#agent_host.sendCommand("chat " + "/summon minecraft:witch ~ ~0 ~15")
#agent_host.sendCommand("chat " + "/summon villager_golem ~2 ~0 ~10")
agent_host.sendCommand("chat " + "/summon zombie ~2 ~0 ~5")
time.sleep(0.5)
agent.run(agent_host)
# Mission ended, analyze Q-table
print("Mission ended")
# delete state where enemy died
for t in agent.q_table:
if t[0] < 0:
del agent.q_table[t]
break
for state in sorted(agent.q_table.keys()):
lowest = -10000
theaction = 0
for action in agent.q_table[state]:
if agent.q_table[state][action] > lowest:
lowest = agent.q_table[state][action]
theaction = action
print("On state: ",state," best action is ",theaction," with q_value of ", lowest)
# teleport to end current mission
tp_command = "tp 0 255 0"
agent_host.sendCommand(tp_command)
time.sleep(1) # sleep for 1s, otherwise it will not restart
#if agent.Heart == 0:
#if i+1 % 10 == 0:
# agent.epsilon-=0.01
#else:
# reset agent parameters
agent.resetAgent()