Thread: Q-Learning C program help

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    5

    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

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

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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, 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