|
| 1 | +import argparse |
| 2 | + |
| 3 | +import gym |
| 4 | + |
| 5 | +def build_arg_parser(): |
| 6 | + parser = argparse.ArgumentParser(description='Run an environment') |
| 7 | + parser.add_argument('--input-env', dest='input_env', required=True, |
| 8 | + choices=['cartpole', 'mountaincar', 'pendulum'], |
| 9 | + help='Specify the name of the environment') |
| 10 | + return parser |
| 11 | + |
| 12 | +if __name__=='__main__': |
| 13 | + args = build_arg_parser().parse_args() |
| 14 | + input_env = args.input_env |
| 15 | + |
| 16 | + name_map = {'cartpole': 'CartPole-v0', |
| 17 | + 'mountaincar': 'MountainCar-v0', |
| 18 | + 'pendulum': 'Pendulum-v0'} |
| 19 | + |
| 20 | + # Create the environment |
| 21 | + env = gym.make(name_map[input_env]) |
| 22 | + |
| 23 | + # Start iterating |
| 24 | + for _ in range(20): |
| 25 | + # Reset the environment |
| 26 | + observation = env.reset() |
| 27 | + |
| 28 | + # Iterate 100 times |
| 29 | + for i in range(100): |
| 30 | + # Render the environment |
| 31 | + env.render() |
| 32 | + |
| 33 | + # Print the current observation |
| 34 | + print(observation) |
| 35 | + |
| 36 | + # Take action |
| 37 | + action = env.action_space.sample() |
| 38 | + |
| 39 | + # Extract the observation, reward, status and |
| 40 | + # other info based on the action taken |
| 41 | + observation, reward, done, info = env.step(action) |
| 42 | + |
| 43 | + # Check if it's done |
| 44 | + if done: |
| 45 | + print('Episode finished after {} timesteps'.format(i+1)) |
| 46 | + break |
0 commit comments