Thread: reinforcement learning c program

  1. #1
    Registered User
    Join Date
    Apr 2006

    reinforcement learning c program

    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

     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

    #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)
    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)
       i= i++%QTABLE_SIZE;
       	return qt[i];

    source file

    #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)
    int main(){
    int i;
    int cs;
    int as;
    int rs;
    int r;
    qtable qt= new_qtable();
       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?");
    }   }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    You need to start calling your programs with the .c extension if you're intent on learning C.
    You're doing certain things (like casting malloc) because you're compiling with a C++ compiler.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 10:17 PM
  2. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  3. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 07:51 AM
  4. Replies: 5
    Last Post: 06-15-2003, 11:20 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM