tensorforce / tensorforce

Tensorforce: a TensorFlow library for applied reinforcement learning
Apache License 2.0
3.3k stars 531 forks source link

How to render the environment? #677

Closed CristianMonea closed 4 years ago

CristianMonea commented 4 years ago

How can you render the environment using the Tensorforce library? I've tried calling environment.render, but it says that the function does not exist. This is my code:

from tensorforce.agents import Agent
from tensorforce.environments import Environment
from tensorforce.execution import Runner

# Create an OpenAI-Gym environment
environment = Environment.create(environment='gym', level='MountainCarContinuous-v0')

agent = Agent.create(agent='random', environment=environment)   

# runner = Runner(agent=agent, environment=environment) # Initialize the runner
# runner.run(num_episodes=NUM_EPISODES) # Start the runner
# runner.close()

# Train
for ep in range(NUM_EPISODES): # Number of episodes

    print('********Episode ' + str(ep) + '********')

    # Initialize episode
    states = environment.reset()
    done = False
    step = 0

    while not done: # Episode timestep
        actions = agent.act(states=states)
        states, done, reward = environment.execute(actions=actions)
        agent.observe(terminal=done, reward=reward)
        environment.render() # Gives error

environment.close()
agent.close()
AlexKuhnle commented 4 years ago

Hi, I haven't been checking render for a long time, I have to admit... Will have a look.

AlexKuhnle commented 4 years ago

Quick look: There is actually no render() function for Tensorforce environments. However, there is the visualize argument which should call it as part of execute(). Could you try whether that works?

CristianMonea commented 4 years ago

I've tried it, but I get the following error:

Traceback (most recent call last):

  File "c:\users\user\reinforcement learning\rl.py", line 188, in <module>
    states, done, reward = environment.execute(actions=actions, visualize=True)

TypeError: execute() got an unexpected keyword argument 'visualize'
AlexKuhnle commented 4 years ago

Sorry, should have been clearer: It's the environment which takes the visualize argument, so environment = Environment.create(environment='gym', level='MountainCarContinuous-v0', visualize=True).

CristianMonea commented 4 years ago

It works. Thank you!

AlexKuhnle commented 4 years ago

Great, and good to know -- as I've said, hadn't used that myself for a while. Closing then.