Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Answer in c + + please in one cpp file, I ' d prefer a detailed step by step explanation if possible. Thanks ( I
Answer in c please in one cpp file, Id prefer a detailed step by step explanation if possible. Thanks
I tried to solve it by my own but Im stuck here so your solution should depend on my codes in the last of qustion
First, we need to create a D array with a size of x or x to represent the initial state. In this array, we define the empty spaces, boxes, rocks, and storage locations, but we do not specify the player's location. Then, we ask the user to input the player's location by entering the index of the location. There's no need to write code to verify if the user entered a correct index or not. For example, if the array size is x and the user enters the player's location as we don't need to check, but inform them that the location is incorrect.
After that, we need to call the Qlearning algorithm and create the Qlearning table.
You should ensure that every state has a unique id
Then, we just need to handle the output, by printing for the user all the steps the player needs to take to place all the boxes in the storage locations.
here is my codes
pseudocode the the Sokoban game using Qlearning Home work:
Initialize Qtable
Initialize Rtable
For each episode:
Initialize Environment
While not isGoalState and not isInDeadlock:
Select one possible action x
Execute action x observe reward, and next state NextState
maxQ maxQNextStateaction for action in possible actions
Qsx Rsxgamma maxQ
s NextState
End While
End For
and Qlearning:
#include
#include
#include
using namespace std;
const int STATECOUNT ;
const int ACTIONCOUNT ;
const int TARGETSTATE ;
const double ALPHA ; Learning rate if needed for adjustments
const double GAMMA ; Discount factor for future rewards
double QTable;
double RewardTable
;
int actionsList;
int actionsCount;
void PrintMatrixdouble matrix int rows, int columns;
void FindActionsdouble matrix int state, int actionsList;
double MaxQValuedouble matrix int state;
int PerformEpisodeint startState, double QTable double RewardTable;
int BestActionint state, double QTable;
void TrainModelint startState, int episodes;
int main
cout "Initial QTable:" endl;
PrintMatrixQTable STATECOUNT, STATECOUNT;
cout "Reward Table:" endl;
PrintMatrixRewardTable STATECOUNT, STATECOUNT;
cout "Enter the number of episodes to run: ;
int episodes;
cin episodes;
TrainModel episodes;
cout "Final QTable:" endl;
PrintMatrixQTable STATECOUNT, STATECOUNT;
return ;
void PrintMatrixdouble matrix int rows, int columns
for int i ; i rows; i
for int j ; j columns; j
cout matrixijt;
cout endl;
void FindActionsdouble matrix int state, int actionsList
actionsCount ;
for int i ; i ACTIONCOUNT; i
if matrixstatei
actionsListactionsCount i;
double MaxQValuedouble matrix int state
double maxQ ;
for int i ; i ACTIONCOUNT; i
if matrixstatei maxQ
maxQ matrixstatei;
return maxQ;
int PerformEpisodeint startState, double QTable double RewardTable
int currentState startState;
int stepCount ;
while true
FindActionsRewardTable currentState, actionsList;
if actionsCount
break;
int action actionsListrand actionsCount;
double maxQ MaxQValueQTable action;
QTablecurrentStateaction RewardTablecurrentStateaction GAMMA maxQ;
if action TARGETSTATE
currentState rand STATECOUNT;
break; else
currentState action;
stepCount;
return currentState;
int BestActionint state, double QTable
int best ;
double maxQ ;
for int i ; i ACTIONCOUNT; i
if QTablestatei maxQ
maxQ QTablestatei;
best i;
return best;
void TrainModelint startState, int episodes
srandstaticcasttimenullptr;
cout "Training started..." endl;
for int i ; i episodes; i
cout "Episode i : endl;
startState PerformEpisodestartState QTable, RewardTable;
PrintMatrixQTable STATECOUNT, STATECOUNT;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started