Package: reinforcement
ReinforcementThe reinforcement package aims to provide simple implementations for basic reinforcement learning algorithms, using Test Driven Development and other principles of Software Engineering in an attempt to minimize defects and improve reproducibility.InstallationThe library can be installed using pip:pipinstallreinforcementExample ImplementationThis section demonstrates how to implement a REINFORCE agent and benchmark it on the 'CartPole' gym environment.You can find the full implementation inexamples/reinforce.py. Theexample folderalso contains some additional utility classes and functions that are used in the implementation.defrun_reinforce(config):reporter,env,rewards=Reporter(config),gym.make('CartPole-v0'),[]withtf1.Session()assession:agent=_make_agent(config,session,env)forepisodeinrange(1,config.episodes+1):reward=_run_episode(env,episode,agent,reporter)rewards.append(reward)ifreporter.should_log(episode):logger.info(reporter.report(episode,rewards))env.close()This is the main function setting up the boiler plate code. It creates the tensorflow session, logs the progress, and creats the agent. TheReporterclass is just a helper to make logging at a certain frequency more convenientdef_make_agent(config,session,env):p=ParameterizedPolicy(session,env.observation_space.shape[0],env.action_space.n,NoLog(),config.lr_policy)b=ValueBaseline(session,env.observation_space.shape[0],NoLog(),config.lr_baseline)alg=Reinforce(p,config.gamma,b,config.num_trajectories)returnBatchAgent(alg)The factory function_make_agentcreates the REINFORCE agent object. It uses a parameterized policy and baseline to learn and estimate proper actions. In this case, both parameterizations are straightforward artificial neural networks with no hidden layer. Both have the same input layer, but the output layer of the policy is a softmax function, whereas the baseline outputs a single linear value. TheBatchAgenttype records trajectories (states, actions, rewards) which are then used to optimize the policy and the baseline. TheNoLogclass is a Null-Object implementing the TensorBoardFileWriterinterface.def_run_episode(env,episode,agent,report):obs=env.reset()done,reward=False,0whilenotdone:ifreport.should_render(episode):env.render()obs,r,done,_=env.step(agent.next_action(obs))agent.signal(r)reward+=ragent.train()returnrewardThis function performs a run through a single episode of the environment. Observations of the environment are passed to the agent'snext_actioninterface function. The resulting estimated actions are passed again to the environment, leading to the next observation and a reward signal. The agent is then trained at the end of the episode because we want to train it on whole trajectories. It also contains a call toenv.render()to visualize some runs.Running an ExampleRunning the REINFORCE agent example with default settings:pythonexample/reinforce.pyAfter a few 1000 episodes it should get very close to the highest achievable reward:...
INFO:__main__:Episode 2800: reward=200.0; mean reward of last 100 episodes: 199.71
INFO:__main__:Episode 2900: reward=200.0; mean reward of last 100 episodes: 199.36
INFO:__main__:Episode 3000: reward=200.0; mean reward of last 100 episodes: 198.09