Q-Learning C program help

This is a discussion on Q-Learning C program help within the C++ Programming forums, part of the General Programming Boards category; I have written a Q-learning program in C using a hash function and a Q-table. I now need to include ...

  1. #1
    Registered User
    Join Date
    Apr 2006

    Q-Learning C program help

    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?");
    }   }
    Last edited by Temden; 05-10-2006 at 08:09 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    I have written a Q-learning program in C using a hash function and a Q-table.
    What's 'Q' (queue?), and why not post in the C programming forum if your program is a C program?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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, 11:17 PM
  2. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 11:58 PM
  3. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 08:51 AM
  4. Replies: 5
    Last Post: 06-15-2003, 12:20 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21