|
| 1 | +# ------------------------------------------------------------------------------------------------ |
| 2 | +# Copyright (c) 2016 Microsoft Corporation |
| 3 | +# |
| 4 | +# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and |
| 5 | +# associated documentation files (the "Software"), to deal in the Software without restriction, |
| 6 | +# including without limitation the rights to use, copy, modify, merge, publish, distribute, |
| 7 | +# sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
| 8 | +# furnished to do so, subject to the following conditions: |
| 9 | +# |
| 10 | +# The above copyright notice and this permission notice shall be included in all copies or |
| 11 | +# substantial portions of the Software. |
| 12 | +# |
| 13 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT |
| 14 | +# NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 15 | +# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 16 | +# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 17 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 18 | +# ------------------------------------------------------------------------------------------------ |
| 19 | + |
| 20 | +# Work in progress: this will eventually be a test of Malmo's support for all block types. |
| 21 | + |
| 22 | +import MalmoPython |
| 23 | +import os |
| 24 | +import sys |
| 25 | +import time |
| 26 | +import json |
| 27 | +import copy |
| 28 | +import errno |
| 29 | +import xml.etree.ElementTree as ET |
| 30 | + |
| 31 | +# Parse schema to collect all block types. |
| 32 | +# First find the schema file: |
| 33 | +schema_dir = None |
| 34 | +try: |
| 35 | + schema_dir = os.environ['MALMO_XSD_PATH'] |
| 36 | +except KeyError: |
| 37 | + print "MALMO_XSD_PATH not set? Check environment." |
| 38 | + exit(1) |
| 39 | +types_xsd = schema_dir + os.sep + "Types.xsd" |
| 40 | + |
| 41 | +# Now try to parse it: |
| 42 | +types_tree = None |
| 43 | +try: |
| 44 | + types_tree = ET.parse(types_xsd) |
| 45 | +except (ET.ParseError, IOError): |
| 46 | + print "Could not find or parse Types.xsd - check Malmo installation." |
| 47 | + exit(1) |
| 48 | + |
| 49 | +# Find the BlockType element: |
| 50 | +root = types_tree.getroot() |
| 51 | +block_types_element = root.find("*[@name='BlockType']") |
| 52 | +if block_types_element == None: |
| 53 | + print "Could not find block types in Types.xsd - file corruption?" |
| 54 | + exit(1) |
| 55 | + |
| 56 | +# Find the enum inside the BlockType element: |
| 57 | +block_types_enum = block_types_element.find("*[@base]") |
| 58 | +if block_types_enum == None: |
| 59 | + print "Unexpected schema format. Did the format get changed without this test getting updated?" |
| 60 | + exit(1) |
| 61 | + |
| 62 | +# Now make a list of block types: |
| 63 | +block_types = [block.get("value") for block in block_types_enum] |
| 64 | + |
| 65 | +def getMissionXML(block_types): |
| 66 | + forceReset = '"true"' |
| 67 | + structureXML = "<DrawingDecorator>" |
| 68 | + for i, b in enumerate(block_types): |
| 69 | + structureXML += '<DrawBlock x="{}" y="{}" z="{}" type="{}" />'.format(i % 10, 3, i / 10, b) |
| 70 | + structureXML += "</DrawingDecorator>" |
| 71 | + startpos=(-2, 10, -2) |
| 72 | + |
| 73 | + return '''<?xml version="1.0" encoding="UTF-8" ?> |
| 74 | + <Mission xmlns="http://ProjectMalmo.microsoft.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> |
| 75 | +
|
| 76 | + <About> |
| 77 | + <Summary>Block Test</Summary> |
| 78 | + </About> |
| 79 | +
|
| 80 | + <ServerSection> |
| 81 | + <ServerHandlers> |
| 82 | + <FlatWorldGenerator generatorString="3;7,2*3,2;12;" forceReset=''' + forceReset + '''/>''' + structureXML + ''' |
| 83 | + <ServerQuitWhenAnyAgentFinishes /> |
| 84 | + </ServerHandlers> |
| 85 | + </ServerSection> |
| 86 | +
|
| 87 | + <AgentSection mode="Survival"> |
| 88 | + <Name>Blocky</Name> |
| 89 | + <AgentStart> |
| 90 | + <Placement x="''' + str(startpos[0] + 0.5) + '''" y="''' + str(startpos[1] + 1) + '''" z="''' + str(startpos[2] + 0.5) + '''" pitch="90"/> |
| 91 | + </AgentStart> |
| 92 | + <AgentHandlers> |
| 93 | + <MissionQuitCommands /> |
| 94 | + <ObservationFromFullStats/> |
| 95 | + <ObservationFromGrid> |
| 96 | + <Grid name="all_the_blocks" absoluteCoords="true"> |
| 97 | + <min x="0" y="3" z="0"/> |
| 98 | + <max x="9" y="3" z="30"/> |
| 99 | + </Grid> |
| 100 | + </ObservationFromGrid> |
| 101 | + <VideoProducer viewpoint="1"> |
| 102 | + <Width>860</Width> |
| 103 | + <Height>480</Height> |
| 104 | + </VideoProducer> |
| 105 | + </AgentHandlers> |
| 106 | + </AgentSection> |
| 107 | + </Mission>''' |
| 108 | + |
| 109 | +sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0) # flush print output immediately |
| 110 | + |
| 111 | +agent_host = MalmoPython.AgentHost() |
| 112 | +agent_host.addOptionalStringArgument( "recordingDir,r", "Path to location for saving mission recordings", "" ) |
| 113 | +try: |
| 114 | + agent_host.parse( sys.argv ) |
| 115 | +except RuntimeError as e: |
| 116 | + print 'ERROR:',e |
| 117 | + print agent_host.getUsage() |
| 118 | + exit(1) |
| 119 | +if agent_host.receivedArgument("help"): |
| 120 | + print agent_host.getUsage() |
| 121 | + exit(0) |
| 122 | + |
| 123 | +num_iterations = 30000 |
| 124 | +if agent_host.receivedArgument("test"): |
| 125 | + num_iterations = 10 |
| 126 | + |
| 127 | +recording = False |
| 128 | +my_mission_record = MalmoPython.MissionRecordSpec() |
| 129 | +recordingsDirectory = agent_host.getStringArgument("recordingDir") |
| 130 | +if len(recordingsDirectory) > 0: |
| 131 | + recording = True |
| 132 | + try: |
| 133 | + os.makedirs(recordingsDirectory) |
| 134 | + except OSError as exception: |
| 135 | + if exception.errno != errno.EEXIST: # ignore error if already existed |
| 136 | + raise |
| 137 | + my_mission_record.recordRewards() |
| 138 | + my_mission_record.recordObservations() |
| 139 | + my_mission_record.recordCommands() |
| 140 | + my_mission_record.recordMP4(24,2000000) |
| 141 | + |
| 142 | +for i in xrange(num_iterations): |
| 143 | + missionXML = getMissionXML(block_types) |
| 144 | + if recording: |
| 145 | + my_mission_record.setDestination(recordingsDirectory + "//" + "Mission_" + str(i+1) + ".tgz") |
| 146 | + my_mission = MalmoPython.MissionSpec(missionXML, True) |
| 147 | + |
| 148 | + max_retries = 3 |
| 149 | + for retry in range(max_retries): |
| 150 | + try: |
| 151 | + agent_host.startMission( my_mission, my_mission_record ) |
| 152 | + break |
| 153 | + except RuntimeError as e: |
| 154 | + if retry == max_retries - 1: |
| 155 | + print "Error starting mission:",e |
| 156 | + exit(1) |
| 157 | + else: |
| 158 | + time.sleep(2) |
| 159 | + |
| 160 | + world_state = agent_host.getWorldState() |
| 161 | + while not world_state.has_mission_begun: |
| 162 | + sys.stdout.write(".") |
| 163 | + time.sleep(0.1) |
| 164 | + world_state = agent_host.getWorldState() |
| 165 | + print |
| 166 | + |
| 167 | + # main loop: |
| 168 | + while world_state.is_mission_running: |
| 169 | + if world_state.number_of_observations_since_last_state > 0: |
| 170 | + msg = world_state.observations[-1].text |
| 171 | + ob = json.loads(msg) |
| 172 | + if "all_the_blocks" in ob: |
| 173 | + blocks = ob["all_the_blocks"] |
| 174 | + missing_blocks = [b for b in block_types if not b in blocks] |
| 175 | + if len(missing_blocks) > 0: |
| 176 | + print "MISSING:" |
| 177 | + for b in missing_blocks: |
| 178 | + print b, |
| 179 | + print |
| 180 | + world_state = agent_host.getWorldState() |
0 commit comments