Skip to content

Using OpenAI's gym environment, I create a basic genetic reinforcement learning model that solves the CartPole-v0 problem with ease.

License

Notifications You must be signed in to change notification settings

bjhammack/cartpole-genetic-ai

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CartPole-v0 Genetic RL

Using OpenAI's gym environment and PyTorch, I create a basic genetic algorithm reinforcement learning model to solve CartPole-v0. Currently, a model with two linear layers, a ReLU activation function, and a softmax function solves with near perfect success rate in 200 generations.

final_model_results

The Environment

OpenAI's gym environment provides an ideal way to test machine learning models on simple environments without having to build them or an API beforehand. For the CartPole-v0 problem, the premise is simple, a pole is connected to a block that can move left or right on a 2D plane, the goal is to balance the pole for 195 steps. If the pole falls 15 degrees in either direction, you lose.

Genetic Algorithm Reinforcement Learning

To solve this problem, I employed a genetic RL model. This model creates X models for Y generations, taking the top Z models of each generation to use as the basis for the next generation. This is a very basic implementation of the genetic algorithm, a much more robust algorithm would include cross-pollination of successful models, single-point crossovers, and a more robust mutation algorithm. Fortunately, the CartPole-v0 problem is simple enough to not need any of these advanced editions and can be solved quite easily with the a proof-of-concept genetic algorithm.

Results

The algorithm was able to succeed over 50% of the time after roughly 75 generations of 500 population each and near perfect success after 200 generations. In src/saved_models is the most successful model of a 500x500 algorithm; intense overkill, but effective at showing how near-perfect a genetic algorithm can be on such a simple problem. You can view the model's last run before upload on the .mp4 file in src/video/.

About

Using OpenAI's gym environment, I create a basic genetic reinforcement learning model that solves the CartPole-v0 problem with ease.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages