reinforcement learning c program

This is a discussion on reinforcement learning c program 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
    Posts
    5

    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

    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;
    }   }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,544
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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

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