|
| 1 | +# ------------------------------------------------------------------------------------------------ |
| 2 | +# Copyright (c) 2018 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 | +import malmoenv |
| 21 | +import argparse |
| 22 | +from pathlib import Path |
| 23 | +import time |
| 24 | +import gym |
| 25 | +from gym.wrappers.monitoring.video_recorder import VideoRecorder |
| 26 | + |
| 27 | +import logging |
| 28 | +logging.basicConfig(level=logging.DEBUG) |
| 29 | + |
| 30 | +if __name__ == '__main__': |
| 31 | + |
| 32 | + parser = argparse.ArgumentParser(description='malmovnv test') |
| 33 | + parser.add_argument('--mission', type=str, default='missions/mobchase_single_agent.xml', help='the mission xml') |
| 34 | + parser.add_argument('--port', type=int, default=9000, help='the mission server port') |
| 35 | + parser.add_argument('--server', type=str, default='127.0.0.1', help='the mission server DNS or IP address') |
| 36 | + parser.add_argument('--port2', type=int, default=None, help="(Multi-agent) role N's mission port. Defaults to server port.") |
| 37 | + parser.add_argument('--server2', type=str, default=None, help="(Multi-agent) role N's server DNS or IP") |
| 38 | + parser.add_argument('--episodes', type=int, default=1, help='the number of resets to perform - default is 1') |
| 39 | + parser.add_argument('--episode', type=int, default=0, help='the start episode - default is 0') |
| 40 | + parser.add_argument('--role', type=int, default=0, help='the agent role - defaults to 0') |
| 41 | + parser.add_argument('--episodemaxsteps', type=int, default=0, help='max number of steps per episode') |
| 42 | + parser.add_argument('--resync', type=int, default=0, help='exit and re-sync every N resets' |
| 43 | + ' - default is 0 meaning never.') |
| 44 | + parser.add_argument('--experimentUniqueId', type=str, default='test1', help="the experiment's unique id.") |
| 45 | + parser.add_argument('--video_path', type=str, default="video.mp4", help="Optional video path.") |
| 46 | + args = parser.parse_args() |
| 47 | + if args.server2 is None: |
| 48 | + args.server2 = args.server |
| 49 | + |
| 50 | + xml = Path(args.mission).read_text() |
| 51 | + env = malmoenv.make() |
| 52 | + |
| 53 | + env.init(xml, args.port, |
| 54 | + server=args.server, |
| 55 | + server2=args.server2, port2=args.port2, |
| 56 | + role=args.role, |
| 57 | + exp_uid=args.experimentUniqueId, |
| 58 | + episode=args.episode, |
| 59 | + resync=args.resync, |
| 60 | + reshape=True) |
| 61 | + |
| 62 | + rec = VideoRecorder(env, args.video_path) |
| 63 | + |
| 64 | + for i in range(args.episodes): |
| 65 | + print("reset " + str(i)) |
| 66 | + obs = env.reset() |
| 67 | + rec.capture_frame() |
| 68 | + |
| 69 | + steps = 0 |
| 70 | + done = False |
| 71 | + while not done and (args.episodemaxsteps <= 0 or steps < args.episodemaxsteps): |
| 72 | + action = env.action_space.sample() |
| 73 | + rec.capture_frame() |
| 74 | + obs, reward, done, info = env.step(action) |
| 75 | + steps += 1 |
| 76 | + print("reward: " + str(reward)) |
| 77 | + # print("done: " + str(done)) |
| 78 | + print("obs: " + str(obs)) |
| 79 | + # print("info" + info) |
| 80 | + |
| 81 | + time.sleep(.05) |
| 82 | + |
| 83 | + rec.close() |
| 84 | + env.close() |
0 commit comments