/
hit_test.py
executable file
·270 lines (247 loc) · 12.4 KB
/
hit_test.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
# ------------------------------------------------------------------------------------------------
# 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.
# ------------------------------------------------------------------------------------------------
# Demo of reward for damaging mobs - create an arena filled with pigs and sheep,
# and reward the agent positively for attacking sheep, and negatively for attacking pigs.
# Using this reward signal to train the agent is left as an exercise for the reader...
# this demo just uses ObservationFromRay and ObservationFromNearbyEntities to determine
# when and where to attack.
import MalmoPython
import os
import random
import sys
import time
import json
import random
import errno
import math
from collections import namedtuple
EntityInfo = namedtuple('EntityInfo', 'x, y, z, yaw, pitch, name, colour, variation, quantity, life')
EntityInfo.__new__.__defaults__ = (0, 0, 0, 0, 0, "", "", "", 1, "")
# Task parameters:
ARENA_WIDTH = 20
ARENA_BREADTH = 20
def getCorner(index,top,left,expand=0,y=0):
''' Return part of the XML string that defines the requested corner'''
x = str(-(expand+ARENA_WIDTH/2)) if left else str(expand+ARENA_WIDTH/2)
z = str(-(expand+ARENA_BREADTH/2)) if top else str(expand+ARENA_BREADTH/2)
return 'x'+index+'="'+x+'" y'+index+'="' +str(y)+'" z'+index+'="'+z+'"'
def getSpawnEndTag(i):
return ' type="mob_spawner" variant="' + ["Sheep", "Pig"][i % 2] + '"/>'
def getMissionXML(summary):
''' Build an XML mission string.'''
# We put the spawners inside an animation object, to move them out of range of the player after a short period of time.
# Otherwise they will just keep spawning - as soon as the agent kills a sheep, it will be replaced.
# (Could use DrawEntity to create the pigs/sheep, rather than using spawners... but this way is much more fun.)
return '''<?xml version="1.0" encoding="UTF-8" ?>
<Mission xmlns="http://ProjectMalmo.microsoft.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<About>
<Summary>''' + summary + '''</Summary>
</About>
<ModSettings>
<MsPerTick>50</MsPerTick>
</ModSettings>
<ServerSection>
<ServerInitialConditions>
<Time>
<StartTime>1000</StartTime>
<AllowPassageOfTime>true</AllowPassageOfTime>
</Time>
<AllowSpawning>true</AllowSpawning>
<AllowedMobs>Pig Sheep</AllowedMobs>
</ServerInitialConditions>
<ServerHandlers>
<FlatWorldGenerator generatorString="3;7,202*1,5*3,2;3;,biome_1" />
<DrawingDecorator>
<DrawCuboid ''' + getCorner("1",True,True,expand=10,y=206) + " " + getCorner("2",False,False,y=246,expand=10) + ''' type="grass"/>
<DrawCuboid ''' + getCorner("1",True,True,y=207) + " " + getCorner("2",False,False,y=246) + ''' type="air"/>
</DrawingDecorator>
<AnimationDecorator ticksPerUpdate="10">
<Linear>
<CanvasBounds>
<min x="''' + str(-ARENA_BREADTH/2) + '''" y="205" z="''' + str(-ARENA_BREADTH/2) + '''"/>
<max x="''' + str(ARENA_WIDTH/2) + '''" y="217" z="''' + str(ARENA_WIDTH/2) + '''"/>
</CanvasBounds>
<InitialPos x="0" y="207" z="0"/>
<InitialVelocity x="0" y="0.025" z="0"/>
</Linear>
<DrawingDecorator>
<DrawLine ''' + getCorner("1",True,True,expand=-2) + " " + getCorner("2",True,False,expand=-2) + getSpawnEndTag(1) + '''
<DrawLine ''' + getCorner("1",True,True,expand=-2) + " " + getCorner("2",False,True,expand=-2) + getSpawnEndTag(1) + '''
<DrawLine ''' + getCorner("1",False,False,expand=-2) + " " + getCorner("2",True,False,expand=-2) + getSpawnEndTag(1) + '''
<DrawLine ''' + getCorner("1",False,False,expand=-2) + " " + getCorner("2",False,True,expand=-2) + getSpawnEndTag(1) + '''
<DrawLine ''' + getCorner("1",True,True,expand=-3) + " " + getCorner("2",True,False,expand=-3) + getSpawnEndTag(2) + '''
<DrawLine ''' + getCorner("1",True,True,expand=-3) + " " + getCorner("2",False,True,expand=-3) + getSpawnEndTag(2) + '''
<DrawLine ''' + getCorner("1",False,False,expand=-3) + " " + getCorner("2",True,False,expand=-3) + getSpawnEndTag(2) + '''
<DrawLine ''' + getCorner("1",False,False,expand=-3) + " " + getCorner("2",False,True,expand=-3) + getSpawnEndTag(2) + '''
</DrawingDecorator>
</AnimationDecorator>
<ServerQuitWhenAnyAgentFinishes />
<ServerQuitFromTimeUp timeLimitMs="120000"/>
</ServerHandlers>
</ServerSection>
<AgentSection mode="Survival">
<Name>The Hunter</Name>
<AgentStart>
<Placement x="0.5" y="207.0" z="0.5" pitch="20"/>
<Inventory>
<InventoryItem type="diamond_pickaxe" slot="0"/>
</Inventory>
</AgentStart>
<AgentHandlers>
<ContinuousMovementCommands turnSpeedDegs="420"/>
<ObservationFromRay/>
<RewardForDamagingEntity>
<Mob type="Sheep" reward="1"/>
<Mob type="Pig" reward="-1"/>
</RewardForDamagingEntity>
<ObservationFromNearbyEntities>
<Range name="entities" xrange="'''+str(ARENA_WIDTH)+'''" yrange="2" zrange="'''+str(ARENA_BREADTH)+'''" />
</ObservationFromNearbyEntities>
<ObservationFromFullStats/>
</AgentHandlers>
</AgentSection>
</Mission>'''
recordingsDirectory="HuntRecordings"
try:
os.makedirs(recordingsDirectory)
except OSError as exception:
if exception.errno != errno.EEXIST: # ignore error if already existed
raise
sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0) # flush print output immediately
validate = True
my_client_pool = MalmoPython.ClientPool()
my_client_pool.add(MalmoPython.ClientInfo("127.0.0.1", 10000))
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_reps = 1
else:
num_reps = 30000
for iRepeat in range(num_reps):
mission_xml = getMissionXML("Go hunting! #" + str(iRepeat))
my_mission = MalmoPython.MissionSpec(mission_xml,validate)
max_retries = 3
for retry in range(max_retries):
try:
# Set up a recording
my_mission_record = MalmoPython.MissionRecordSpec(recordingsDirectory + "//" + "Mission_" + str(iRepeat) + ".tgz")
my_mission_record.recordRewards()
# Attempt to start the mission:
agent_host.startMission( my_mission, my_client_pool, my_mission_record, 0, "hunterExperiment" )
break
except RuntimeError as e:
if retry == max_retries - 1:
print "Error starting mission",e
print "Is the game running?"
exit(1)
else:
time.sleep(2)
world_state = agent_host.getWorldState()
while not world_state.has_mission_begun:
time.sleep(0.1)
world_state = agent_host.getWorldState()
# main loop:
total_reward = 0
pig_population = 0
sheep_population = 0
self_x = 0
self_z = 0
current_yaw = 0
while world_state.is_mission_running:
world_state = agent_host.getWorldState()
if world_state.number_of_observations_since_last_state > 0:
msg = world_state.observations[-1].text
ob = json.loads(msg)
# Use the line-of-sight observation to determine when to hit and when not to hit:
if u'LineOfSight' in ob:
los = ob[u'LineOfSight']
type=los["type"]
if type == "Sheep":
agent_host.sendCommand("attack 1")
agent_host.sendCommand("attack 0")
# Get our position/orientation:
if u'Yaw' in ob:
current_yaw = ob[u'Yaw']
if u'XPos' in ob:
self_x = ob[u'XPos']
if u'ZPos' in ob:
self_z = ob[u'ZPos']
# Use the nearby-entities observation to decide which way to move, and to keep track
# of population sizes - allows us some measure of "progress".
if u'entities' in ob:
entities = [EntityInfo(**k) for k in ob["entities"]]
num_pigs = 0
num_sheep = 0
x_pull = 0
z_pull = 0
for e in entities:
if e.name == "Sheep":
num_sheep += 1
# Each sheep contributes to the direction we should head in...
dist = max(0.0001, (e.x - self_x) * (e.x - self_x) + (e.z - self_z) * (e.z - self_z))
# Prioritise going after wounded sheep. Max sheep health is 8, according to Minecraft wiki...
weight = 9.0 - e.life
x_pull += weight * (e.x - self_x) / dist
z_pull += weight * (e.z - self_z) / dist
elif e.name == "Pig":
num_pigs += 1
# Determine the direction we need to turn in order to head towards the "sheepiest" point:
yaw = -180 * math.atan2(x_pull, z_pull) / math.pi
difference = yaw - current_yaw;
while difference < -180:
difference += 360;
while difference > 180:
difference -= 360;
difference /= 180.0;
agent_host.sendCommand("turn " + str(difference))
move_speed = 1.0 if abs(difference) < 0.5 else 0 # move slower when turning faster - helps with "orbiting" problem
agent_host.sendCommand("move " + str(move_speed))
if num_sheep != sheep_population or num_pigs != pig_population:
# Print an update of our "progress":
sheep_population = num_sheep
pig_population = num_pigs
tot = sheep_population + pig_population
if tot:
print "PIGS:SHEEP",
r = 40.0 / tot
p = int(num_pigs * r)
print "P" * p, "|", "S" * (40 - p), "(", num_pigs, num_sheep, ")"
if world_state.number_of_rewards_since_last_state > 0:
# Keep track of our total reward:
total_reward += world_state.rewards[-1].getValue()
# mission has ended.
for error in world_state.errors:
print "Error:",error.text
if world_state.number_of_rewards_since_last_state > 0:
# A reward signal has come in - see what it is:
total_reward += world_state.rewards[-1].getValue()
print
print "=" * 41
print "Total score this round:", total_reward
print "=" * 41
print
time.sleep(1) # Give the mod a little time to prepare for the next mission.