This repository contains implementations of two classic reinforcement learning problems using dynamic programming algorithms. Both problems demonstrate fundamental concepts in Markov Decision Processes (MDPs) and optimal policy computation.
- Problems Implemented
- Installation
- Usage
- File Structure
- Algorithms
- Results
- Performance Optimizations
- Dependencies
- Contributing
A classic dynamic programming problem involving optimal car allocation between two rental locations.
Problem Description:
- State Space: 21×21 grid representing number of cars at two locations (0-20 cars each)
- Action Space: Move -5 to +5 cars between locations
- Objective: Maximize expected rental revenue while minimizing car movement costs
- Real-world Constraints:
- Free employee shuttle from Location 1 to Location 2 (first car moved is free)
- Parking costs for locations with >10 cars ($4/night per excess car)
- Poisson-distributed rental requests and returns
Files:
JacksCarRental.py- Original implementationJacksCarRental_Optimized.py- Highly optimized version (~120x faster)
A finite MDP demonstrating value iteration with a simple gambling scenario.
Problem Description:
- State Space: Capital amounts from $0 to $100
- Action Space: Stake amounts [1, min(capital, 100-capital)]
- Objective: Reach $100 with optimal betting strategy
- Game Rules: Biased coin flip (55% probability of heads)
- Termination: Game ends at $0 (lose) or $100 (win)
File:
GamblerProblem.py- Complete implementation with visualization
- Python 3.7+
- NumPy
- SciPy
- Matplotlib
- tqdm (for progress bars)
# Clone the repository
git clone <repository-url>
cd ReinforcementLearning
# Install required packages
pip install numpy scipy matplotlib tqdm
# Run the problems
python GamblerProblem.py
python JacksCarRental_Optimized.py # Recommended optimized version# Run optimized version (recommended)
python JacksCarRental_Optimized.py
# Run original version (slower, for educational purposes)
python JacksCarRental.pyOutput:
- Convergence progress with iteration details
- Final policy matrix showing optimal car movements
- Value function statistics
- Comprehensive visualizations saved as
jacks_car_rental_results.png - Execution time and performance metrics
python GamblerProblem.pyOutput:
- Value function for each capital state
- Optimal policy (stake amounts)
- Interactive plots showing value function and policy curves
ReinforcementLearning/
├── README.md # This documentation
├── GamblerProblem.py # Gambler's problem implementation
├── JacksCarRental.py # Original car rental implementation
├── JacksCarRental_Optimized.py # Optimized car rental (recommended)
├── jacks_car_rental_results.png # Generated visualization
├── carrental.log # Original implementation logs
├── carrental_optimized.log # Optimized implementation logs
└── .git/ # Git repository files
- Policy Evaluation: Compute value function for current policy
- Policy Improvement: Update policy based on computed values
- Convergence Check: Repeat until policy stabilizes
- Value Update: Compute optimal value for each state
- Policy Extraction: Derive optimal policy from value function
- Convergence: Continue until value function stabilizes
- Convergence: Typically converges in 5-15 iterations
- Optimal Strategy: Balances rental revenue with movement costs
- Key Insights:
- Free shuttle service significantly impacts optimal policy
- Parking costs create non-linear decision boundaries
- Higher demand locations require proactive car positioning
- Optimal Policy: Conservative betting strategy emerges
- Value Function: Shows probability of reaching $100 from each state
- Key Insights:
- Biased coin (55% heads) creates favorable gambling conditions
- Optimal stakes often involve "all-in" strategies near goal
- Risk management balances potential gains with loss probability
The optimized Jack's Car Rental implementation includes:
- Pre-computed Poisson Probabilities: Eliminates repeated calculations
- Vectorized Operations: NumPy broadcasting replaces nested loops
- Truncated Distributions: Focuses computation on high-probability events
- State-Action Caching: Reduces redundant computations
- Early Convergence: Stops when improvement threshold is met
Performance Improvement: ~120x speedup over original implementation
numpy>=1.19.0 # Numerical computations
scipy>=1.5.0 # Statistical distributions
matplotlib>=3.3.0 # Plotting and visualization
tqdm>=4.50.0 # Progress barsContributions are welcome! Areas for improvement:
- Additional RL Problems: Implement more classic RL scenarios
- Algorithm Variants: Add different solution methods (Q-learning, etc.)
- Visualization Enhancements: Interactive plots or animations
- Performance Optimizations: Further speedup techniques
- Documentation: Additional examples and tutorials
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
This repository serves as an excellent resource for:
- Students: Learning fundamental RL concepts
- Researchers: Baseline implementations for comparison
- Practitioners: Production-ready optimized algorithms
- Educators: Teaching dynamic programming in RL
- Sutton, R. S., & Barto, A. G. (2018). Reinforcement Learning: An Introduction
- Bellman, R. (1957). Dynamic Programming
- Classic RL textbook examples and problem formulations
Note: The optimized implementation (JacksCarRental_Optimized.py) is recommended for practical use due to its significant performance improvements while maintaining mathematical accuracy.