This repository contains a C++ implementation of the Q-Learning algorithm applied to a simple grid world problem. The grid world consists of states represented by rows and actions represented by columns.
The goal is to reach a specific state denoted by F
from any staring point.
Here is the representation of the environment:
Here is the Reward Table:
The q_learning.cpp file contains the main implementation of the Q-Learning algorithm. Here's an overview of the key components:
- printArray: Function to print a 2D array.
- initializeRewardArray: Function to initialize the reward matrix based on the problem description.
- getRandomPossibleAction: Function to select a random possible action from a given state.
- QLearningAlgorithm: Function implementing the Q-Learning algorithm. It updates the Q-table based on the rewards and learns the optimal policy.
- main: The entry point of the program. It initializes the reward matrix, sets up the Q-table, runs the Q-Learning algorithm, and prints the final Q-table.
You can modify the parameters such as the number of episodes (episode) and the learning rate (y) in the main function to experiment with different settings.
// The # of trials for computer to learn, usually the # is big
int episode = 1000;
// Constant for learning rate
double y = 0.8;
Here is the Q matrix after 1000 episodes: