-
Notifications
You must be signed in to change notification settings - Fork 1
/
running_ai.py
264 lines (231 loc) · 9.02 KB
/
running_ai.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
from __future__ import print_function
# ------------------------------------------------------------------------------------------------
# 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.
# ------------------------------------------------------------------------------------------------
# Modification of tutorial 2 to generate xml string for piano generation
# and teleporting the agent onto the appropriate tiles (tp not ready yet)
from builtins import range
import MalmoPython
import os
import sys
import time
import math
from timeit import default_timer as timer
from sklearn.ensemble import RandomForestClassifier
from music21 import *
from music_generator import MusicGenerator, get_music_data
NOTE_TO_POS_MAP = {
'F#3': 0.5,
'G3': 1.5,
'G#3': 2.5,
'A3': 3.5,
'A#3': 4.5,
'B3': 5.5,
'C4': 6.5,
'C#4': 7.5,
'D4': 8.5,
'D#4': 9.5,
'E4': 10.5,
'E#4': 11.5, # same as F4!!
'F4': 11.5,
'F#4': 12.5,
'G4': 13.5,
'G#4': 14.5,
'A4': 15.5,
'B4': 16.5,
'C5': 17.5,
'C#5': 18.5,
'D5': 19.5,
'D#5': 20.5,
'E5': 21.5,
'F5': 22.5,
'F#5': 23.5
}
def teleport(agent_host, teleport_x, teleport_z):
'''
Teleports the agent to a specific coordinate location after a delay
**Parameters**
--------------
*agent_host*: malmo agent that will be acted on
*teleport_x*: desired x coordinate to teleport to
*teleport_z*: desired z coordinate to teleport to
**Notes**
---------
teleport_y is not needed because we will assume a desired
teleport height of 57, which is ground level
To teleport to a specific note, from the list:
notes = ['F_sharp_3','G3','G_sharp_3','A3','A_sharp_3','B3','C4','C_sharp_4','D4','D_sharp_4','E4','F4','F_sharp_4', \
'G4','G_sharp_4','A4','A_sharp_4','B4','C5','C_sharp_5','D5','D_sharp_5','E5','F5','F_sharp_5']
To teleport to a note at index i, call
teleport(agent_host, 14, i)
where i is the index of the note + 0.5
'''
print("\ntrying a teleport")
tp_command = "tp " + str(teleport_x) + " 57 " + str(teleport_z)
agent_host.sendCommand(tp_command)
good_frame = False
start = timer()
while not good_frame:
world_state = agent_host.getWorldState()
if not world_state.is_mission_running:
print("Mission ended prematurely - error.")
exit(1)
if not good_frame and world_state.number_of_video_frames_since_last_state > 0:
frame_x = world_state.video_frames[-1].xPos
frame_z = world_state.video_frames[-1].zPos
# print("Current frame_x: {}".format(frame_x))
# print("Current frame_z: {}".format(frame_z))
if math.fabs(frame_x - teleport_x) < 0.001 and math.fabs(frame_z - teleport_z) < 0.001:
good_frame = True
end_frame = timer()
def genString(predicted):
'''
Generates an appropriate xml string for creating the piano in minecraft
**Parameters**
--------------
predicted : list of str, corresponding to the pitches of the notes in the music
**Notes**
---------
Given the possible pitches for a Minecraft note block, places a tuned
note block and pressure plate to allow for easy playing of that note
'''
result = '''<?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>12000</StartTime>
<AllowPassageOfTime>false</AllowPassageOfTime>
</Time>
<Weather>clear</Weather>
</ServerInitialConditions>
<ServerHandlers>
<FlatWorldGenerator generatorString="3;3*7,52*3,2;1;village" forceReset="true"/>
'''
result += '''<DrawingDecorator>
'''
index = 0
for index, note in enumerate(predicted):
result += '''<DrawBlock
type="noteblock"
variant="{cur_note}"
x="15"
y="56"
z="{z_loc}" />
<DrawBlock
type="wooden_pressure_plate"
x="14"
y="56"
z="{z_loc}" />
'''.format(cur_note= "F4" if note=="E#4" else note.replace("#", "_sharp_"),
z_loc=index)
#adding redstone block at the end to make the agent stop
result += '''<DrawBlock
type="redstone_block"
x="14"
y="55"
z="{}" />
'''.format(index+1)
result += '''</DrawingDecorator>
'''
result += '''<ServerQuitFromTimeUp timeLimitMs="300000"/>
<ServerQuitWhenAnyAgentFinishes/>
</ServerHandlers>
</ServerSection>
<AgentSection mode="Survival">
<Name>ThePianoMan</Name>
<AgentStart>
<Placement x="14.5" y="56.0" z="{agent_loc}" yaw="0"/>
</AgentStart>
<AgentHandlers>
<AgentQuitFromTouchingBlockType>
<Block type="redstone_block"/>
</AgentQuitFromTouchingBlockType>
<ObservationFromFullStats/>
<ContinuousMovementCommands turnSpeedDegs="180"/>
<AbsoluteMovementCommands/>
</AgentHandlers>
</AgentSection>
</Mission>'''.format(agent_loc=-2)
return result
# Create default Malmo objects:
def main():
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)
## Training on the data to generate notes
# Loop until mission ends:
# teleporting to all of the notes, test run, to be changed
training_music, test_music = get_music_data()
music_gen = MusicGenerator(training_music, test_music)
predicted = music_gen.generate_music()
print(predicted)
missionXML=genString(predicted)
my_mission = MalmoPython.MissionSpec(missionXML, True)
my_mission_record = MalmoPython.MissionRecordSpec()
my_mission_video = my_mission.requestVideo(800,500)
# Attempt to start a mission:
max_retries = 3
for retry in range(max_retries):
try:
agent_host.startMission( my_mission, my_mission_record )
break
except RuntimeError as e:
if retry == max_retries - 1:
print("Error starting mission:",e)
exit(1)
else:
time.sleep(2)
# Loop until mission starts:
print("Waiting for the mission to start ", end=' ')
world_state = agent_host.getWorldState()
while not world_state.has_mission_begun:
print(".", end="")
time.sleep(0.11)
world_state = agent_host.getWorldState()
for error in world_state.errors:
print("Error:",error.text)
print()
print("Mission running ", end=' ')
agent_host.sendCommand("move 1")
while world_state.is_mission_running:
print(".", end="")
time.sleep(1)
world_state = agent_host.getWorldState()
for error in world_state.errors:
print("Error:",error.text)
print()
print("Mission ended")
# Mission has ended.
if __name__ == '__main__':
if sys.version_info[0] == 2:
sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0) # flush print output immediately
else:
import functools
print = functools.partial(print, flush=True)
main()