- Cartpole is a classic game used for learning and introducing reinforcement learning.
- Its simple (just 4 inputs to the neural network) and easy to understand nature makes it the ideal project for beginners to reinforcement learning and neural networks in general.
- I have used
pytorchas my primary library for the neural network. - The model
cart-stable.ptfor trained for about 1000 generations, after which, it can run basically infinitely, without losing. - For the actual game, I have used a modified version of the
gymnasiumlibrary.
- For my purposes, I modified the
cartpole.pyfile in thegymnasiummodule for better viewing for the layman. - It now has, in
humanrendering mode, a score counter, which just displays a modified version of the number of steps in an episode. - I also heavily modified the scoring system. Using the default module, it took many more generations for the NN to become stable, compared to my mod. In my modified version, I made it so that the NN gets a negative reward for every episode for every unit that it strays farther from the center, in the X plane.
- This strict rewarding system made the NN play cartpole in a far more stable way.
- As you can see in the images above, I have added a red indicator of the move of the NN and also the score.
-
This should work properly with all Linux distros.
-
First of all, clone this repo with
git clone https://github.com/dharmik2319/cartpole.git- Then install all the required dependencies with
pip install -r requirements.txt- Now, for the modified
cartpoleenvironment, you can either replace your actual, original module files, or create avenv. To replace the module in a local install, use
cp -R gymnasium/ ~/.local/lib/pythonX.XX/site-packages/gymnasiumReplace the X.XX with your python version (like 3.11 or 2.7).
- To run the trained model with a GUI, just do
python actor_critic.py- To train your own model, just comment out the lines containing
time.sleep(1/24)env = gym.make("CartPole-v1", render_mode="human")model.load_state_dict(torch.load("./cart-stable.pt"))- And uncomment these:
torch.save(model.state_dict(), "./cart-stable.pt")env = gym.make("CartPole-v1")- (Optional) And modify 50000 to any other number, (the steps till which you want an episode to last)
for t in range(1, 50000):WARNING: A higher number means more resource usage, so be careful and realistic when modifying it
- And lastly, you can also modify other parameters like the learning rate and the optimizer, and the number of layers and neurons.
-
Due to lack of support from the
gymnasiumlibrary, you cannot run any environment with visuals (GUI) on Windows systems. -
But, you can follow the steps for GNU/Linux and train the NN and also see its score in the CLI.
-
Due to my limited
pygameknowledge, and the way I implemented the scoring system, you cannot play a playable version of cartpole anymore, because it requires you to usergb-arrayas rendering mode. -
The model can probably be much better, but due to time and computational constraints, I have not made it better.
-
You are welcome to create a pull request to submit a more trained model, or to submit better, more documented code.
-
This NN runs on a single core (and a single thread, I think). Making it multi-core would make performance skyrocket.
-
The template code was taken from
pytorch's examples. -
The
gymnasiumlibrary which contains many environments useful for RL beginners.

