openai / gym-http-api

API to access OpenAI Gym from other languages via HTTP
MIT License
293 stars 142 forks source link

TypeError: 0 is not JSON serializable #40

Closed markusdumke closed 7 years ago

markusdumke commented 7 years ago

I'm using the gym API in R running the example_agent.R file.

When I use the CartPole-v0 environment this works fine, but then I try the "FrozenLake-v0" I get an error: Error: API did not return json when I run

for (i in 1:episode_count) { ob <- env_reset(client, instance_id) for (i in 1:max_steps) { action <- env_action_space_sample(client, instance_id) results <- env_step(client, instance_id, action, render = TRUE) if (results[["done"]]) break } }

and in my WinPython Command Prompt (Python 3.6.0.1Qt5) I get the following error:

TypeError: 0 is not JSON serializable

Hope, someone can help :)

paulhendricks commented 7 years ago

The error arises from calling env_reset on the client when the "FrozenLake-v0" environment is specified.

The same error occurs when I run the Julia client for the "FrozenLake-v0" environment and the following Python code results in the same error. This makes me think that this is something with either the gym API or the gym package. I'll keep exploring.

    remote_base = 'http://127.0.0.1:5000'
    client = Client(remote_base)

    # Create environment
    # env_id = 'CartPole-v0'
    env_id = 'FrozenLake-v0'
    instance_id = client.env_create(env_id)

    # Check properties
    all_envs = client.env_list_all()
    action_info = client.env_action_space_info(instance_id)
    obs_info = client.env_observation_space_info(instance_id)

    # Run a single step
    client.env_monitor_start(instance_id, directory='tmp', force=True)
    init_obs = client.env_reset(instance_id)
paulhendricks commented 7 years ago

The issues arises because Gym returns an observation that is a Numpy type instead of a native Python type. When Flask attempts to jsonify a Numpy type, a TypeError arises. See these StackOverflow question and answers for more details on the problem and solution:

To solve this problem for the Gym API, I recommend adding observation = observation.item() to the Flask route for env_reset (original code found here: https://github.com/openai/gym-http-api/blob/master/gym_http_server.py#L213):

@app.route('/v1/envs/<instance_id>/reset/', methods=['POST'])
def env_reset(instance_id):
    """
    Reset the state of the environment and return an initial
    observation.

    Parameters:
        - instance_id: a short identifier (such as '3c657dbc')
        for the environment instance
    Returns:
        - observation: the initial observation of the space
    """
    observation = envs.reset(instance_id)
    if np.isscalar(observation):
        observation = observation.item()
    return jsonify(observation = observation)

@catherio Any thoughts? Again, I can make a pull request with the code change (and appropriate unit tests and documentation changes) if approved!

catherio commented 7 years ago

@paulhendricks I'd be very grateful if you could submit a pull request! I'm not using this codebase directly myself anymore, so am very appreciative of contributions from folks in the community who are invested in its development.

catherio commented 7 years ago

Assuming this is resolved via the pull request

markusdumke commented 7 years ago

Thank you so much. It works now 👍