Giter Club home page Giter Club logo

rlexample's Introduction

Some basic examples for reinforcement learning

Installing Anaconda and Gymnasium

  • Download and install Anaconda here
  • Create conda env for managing dependencies and activate the conda env
conda create -n conda_env
conda activate conda_env
  • Install gymnasium (Dependencies installed by pip will also go to the conda env)
pip install gymnasium[all]
pip install gymnasium[accept-rom-license]

# Try the next line if box2d-py fails to install.
conda install swig
  • Install ai2thor if you want to run navigation_agent.py
pip install ai2thor==2.4.10
  • Install torch with either conda or pip
conda install pytorch torchvision torchaudio pytorch-cuda=12.1 -c pytorch -c nvidia
pip install torch torchvision torchaudio
  • Install other dependencies
pip install numpy pandas matplotlib

Examples

  • Play with the environment and visualize the agent behaviour
import gymnasium as gym
render = True # switch if visualize the agent
if render:
    env = gym.make('CartPole-v0', render_mode='human')
else:
    env = gym.make('CartPole-v0')
env.reset(seed=0)
for _ in range(1000):
    env.step(env.action_space.sample()) # take a random action
env.close()
  • Random play with CartPole-v0
import gymnasium as gym
env = gym.make('CartPole-v0')
for i_episode in range(20):
    observation = env.reset()
    for t in range(100):
        print(observation)
        action = env.action_space.sample()
        observation, reward, terminated, truncated, info = env.step(action)
        done = np.logical_or(terminated, truncated)
env.close()
  • Example code for random playing (Pong-ram-v0,Acrobot-v1,Breakout-v0)
python my_random_agent.py Pong-ram-v0
  • Very naive learnable agent playing CartPole-v0 or Acrobot-v1
python my_learning_agent.py CartPole-v0

  • Playing Pong on CPU (with a great blog). One pretrained model is pong_model_bolei.p(after training 20,000 episodes), which you can load in by replacing save_file in the script.
python pg-pong.py

  • Random navigation agent in AI2THOR
python navigation_agent.py

rlexample's People

Contributors

pengzhenghao avatar szcf-weiya avatar waldlecai avatar zhoubolei avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rlexample's Issues

The small problem in value_iteration code

In line 79, sum([p*(r + prev_v[s_]) lack the gamma (the gamma=1.0 is not affected the result). The right code is sum([p*(r + gamma*prev_v[s_]) in line 70.
Thanks.

BEETLE Algorithm

Is there an implementation of BEETLE algorithm from paper "An Analytic Solution to Discrete bayesian RL"? Thanks!

cliffwalk.py running issue

Below code in def _draw_grid gives an error (run by python 3.6)

  • ValueError: Missing category information for StrCategoryConverter; this might be caused by unintendedly mixing categorical and numeric data
  • ConversionError: Failed to convert value(s) to axis units: '0'
self.q_texts = [self.ax.text( '0',*self._id_to_position(i)[::-1],
                                     fontsize=11, verticalalignment='center', 
                                     horizontalalignment='center') for i in range(12 * 4)]     

switch position and '0' could work. Could you please check and correct it?

self.q_texts = [self.ax.text(*self._id_to_position(i)[::-1], '0',
                                     fontsize=11, verticalalignment='center', 
                                     horizontalalignment='center') for i in range(12 * 4)]     

Thanks!

Question about the policy evaluation function and policy extraction of the MDP code

Hi, thanks for your course! It helps me a lot.

I have some question about the code in frozenlake_policy_iteration.py. Why is the expression of the value fuction in compute_policy_v (line 52) same as the state-action function in compute_policy_v (line37) ?

And why is the expression of the value function v[s] = sum([p * (r + gamma * prev_v[s_]) for p, s_, r, _ in env.env.P[s][policy_a]]) different from the formula(17) in the slide? It seems that the expression in the code ignore the transition probability P(s'|s,a)?

Thanks! Look forward your reply~

Problems with code in pgb-pong-pytorch and pg-pong-pytorch

Hi, I assumed there are some errors with the above two algorithms codes. Basically, they are similar.

In both of them, Professor used "args.batch_size" to update model params every batch_size episodes, this corresponds to what was presented in professor's lecture slide 5. But in the defined function: finish_episode(), G is calculated for every single episode, I guess you might forget to separate rewards in each episode since you also commented in the ac-pong codes and flatten rewards and values you defined for calculation.

If the model is updated every batch_size time, then policy.rewards should append a [] for every episode separately. Hope my understanding is correct.

policy iteration doesn't work for deterministic frozen_lake env

I am trying to use the code as an example. Well, it is a little bit strange, when I changed the frozen lake env to the deterministic version, e.g. env = gym.make("FrozenLake=v0, is_slippery=False), I found the policy iteration algorithm can't work correctly. I checked the code, it seems nothing wrong. One of the reason might be the insufficient exploration of the agent, however, the env is simple enough and the default iteration numbers are set to 200000. But the problem still can't be solved.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.