I have written a Q-learning program in C using a hash function and a Q-table. I now need to include a Q-value in addition to the state action and rewrd states that I have already got. Does anyone know how to do this? Any help would be much apreciated.
header file
Code:
.
struct qtnode{
int state;
int action;
int reward;
int result;
};
typedef struct qtnode** qtable;
qtable new_qtable(void);
void add(qtable qt,int s,int a,int r, int s2);
void printqtable(qtable qt);
qtnode * resultQuery(qtable qt,int cs,int a,int r);
cpp file
Code:
#include <stdio.h>
#include <stdlib.h>
#include "qtable.h"
const int QTABLE_SIZE =31;
const long MAX_STATEVAL=1000000;
qtable new_qtable(void){
qtable qt= (qtable)malloc(sizeof(qtnode*)*QTABLE_SIZE);
for( int i =0;i<QTABLE_SIZE;i++)
qt[i]= NULL;
return qt;
}
int hash(int state,int action)
{
long key = action*MAX_STATEVAL+state;
printf( "Key %ld\n",key);
int index= key%QTABLE_SIZE;
printf("state %d Action %d \n",state,action);
printf("Index %d\n",index);
return index;
}
void add(qtable qt,int s,int a,int r, int s2)
{
struct qtnode* n = (qtnode*)malloc(sizeof(qtnode));
int i=hash(s,a);
while (qt[i]!=NULL)
i=i++%QTABLE_SIZE;
qt[i] = n;
}
void printqtable(qtable qt)
{
for( int i =0;i<QTABLE_SIZE;i++)
{
if (qt[i]!= NULL)
{
printf ("State %d\n",qt[i]->state);
printf ("Action %d\n",qt[i]->action);
printf ("Reward %d\n",qt[i]->reward);
printf ("Result %d\n\n",qt[i]->result);
}
}
}
qtnode* resultQuery(qtable qt,int cs,int a,int r
)
{
int i=hash(cs,a);
while (!(qt[i]->action==a
&& qt[i]->state==cs)
&&!(qt[i]==NULL))
i= i++%QTABLE_SIZE;
return qt[i];
}
source file
Code:
#include "qtable.h"
#include <stdio.h>
int currentstate(int cs, qtable qt,int r){
for (int i=0;i<4;i++)
{int a =i*6;
qtnode * resultQuery(qtable qt,int cs,int a);
if (r !=NULL)
{printf("Action%dtried\n",a);
}
}
}
int main(){
int i;
int cs;
int as;
int rs;
int r;
qtable qt= new_qtable();
/*add(qt,0,0,-1,1000);
add(qt,1000,0,-1,2000);
add(qt,2000,6,-1,2001);
add(qt,2001,6,-1,2002);
add(qt,2002,12,-1,1002);
add(qt,1002,12,-1,2);
add(qt,2,6,50,3);
printqtable(qt);*/
printf("Please Enter A Current State");
scanf ("%d",&cs);
while(i!=0) {
printf("current state %d\n",cs);
currentstate (cs,qt,r);
printf("Please Enter An Action");
scanf ("%d",&as);
printf("Please Enter A Resulting State");
scanf ("%d",&rs);
printf("Please Enter A Reward");
scanf ("%d",&r);
printf("Do you want to enter another state value?");
scanf("%s",&i);
add(qt,cs,as,r,rs);
cs=rs;
} }