Search code examples
pythonreinforcement-learningopenai-gym

Setting display width for OpenAI Gym (now Gymnasium)


I'm trying to print out some values in Gymnasium (previously OpenAI Gym), such as:

import gymnasium as gym
env = gym.make("LunarLander-v2", render_mode="human")
observation, info = env.reset()

print(f'env.observation_space: {env.observation_space}')
print(f'obs: {observation}')

The output looks like:

env.observation_space: Box([-90.        -90.         -5.         -5.         -3.1415927  -5.
  -0.         -0.       ], [90.        90.         5.         5.         3.1415927  5.
  1.         1.       ], (8,), float32)

obs: [-0.00316305  1.3999956  -0.3203935  -0.48554128  0.00367194  0.072574
  0.          0.        ]

Is there any way to set options such as set_option('display.max_columns', 500) or set_option('display.width', 1000) like Pandas?

- The tag should be Gymnasium but there's only openai-gym right now, so I'm using it.
- I'm not sure if StackOverflow is the right place to ask this question, but there are many questions like this and helpful answers. Please let me know if there's any advice.


EDIT) Summing up the comment and answers:

  1. use np.set_printoptions(linewidth=1000) since Box2D has a np.array representation. (np.set_printoptions has more options so please check them out)
  2. Use pprint: pp = pprint.PrettyPrinter(width=500, compact=True); pp.pprint(...)
  3. Use np.array2string if it's np.array. (observation is np.array so this works, but Gymnasium spaces such as Box itself are not numpy array so this doesn't work. Though its "representation" seems to be a numpy array so the solution 1 works.)

Solution

  • Box2D has a np.array representation. You can therefore use the np options for printing:

    import numpy as np
    import sys
    np.set_printoptions(threshold=sys.maxsize)
    

    This line states that there are as many columns printed as possible by the system. If you now apply this to your example, it should print all columns. Check out this thread or the documentation if you are stuck.