/* This code was written by Abhinav Garg, abhinav@cs.umass.edu, August, 1998 */ # include # include # include # define n 10 # define h 0.5 # define p 6 # define alpha 0.01 # define beta 0.01 # define epsilon 10 # define iterations 2000000 # define max(a,b) (((a)>(b)) ? (a) : (b)); double q[n+1][4][2] = {0.0}; /* 0 - reject, 1 - accept */ /* To generate a priority request */ int req_priority(void) { int req = 0; /* req = rand() % 4; return req; */ /* CHECK OUT LATER */ req = rand() %100; if (req<40) return 3; else if (req >= 40 && req <60) return 2; else if (req >=60 && req <80) return 1; else if (req >=80 && req <100) return 0; } /* Find out whether server is to be freed or not */ int free_server(void) { int free = 0; free = (rand() % 100); if ( free < p ) return 1; /* free the server */ return 0; /* server is still busy */ } int choose_action(int r, int c) /* r,c are the state's subscripts and q is the array */ { int number; int row, column ; if (r==0) return 0; number = (rand() % 100) ; if (number > epsilon) { if ( q[r][c][0] > q[r][c][1] ) return 0; /* {printf("\n1"); fflush(stdout); return 0;} */ else if ( q[r][c][0] < q[r][c][1] ) return 1; /* {printf("\n2"); ffluqsh(stdout); return 1;} */ else if ( q[r][c][0] == q[r][c][1] ) return (rand() % 2 ); /* {printf("\n3");fflush(stdout); return (rand() % 2 );} */ } else return (rand() % 2 ); /* {printf("\n4");fflush(stdout); return (rand() % 2 );} */ } int states_optimal_action(int r ,int c) { if (r == 0) return 0; if ( q[r][c][0] > q[r][c][1] ) return 0; else return 1; } int request_reward(int a) { if (a==0) return 1; else if (a==1) return 2; else if (a==2) return 4; else if (a==3) return 8; } main (int argc, char ** argv) { double rho = 0.0; int reward; int i, j, k; int server_free = n ; /* Current no. of free servers */ int server_state[n]; int action , step ; int cur_state[2], next_state[2]; double next_optimal_q_value, best_q; /* Making all servers initially free */ for (i = 0; i< n; i++) server_state[i] = 1; /* Initialization */ srand(123456); cur_state[0] = n; cur_state[1] = req_priority(); server_free = n; for(step = 0; step <= iterations ; step++) { /* Choose an action */ action = choose_action(cur_state[0], cur_state[1]); /* Find reward */ reward = request_reward(cur_state[1]); if (action==0) reward = 0; /* Whether to free a busy server or not */ for(i = 0; i=n+1) { printf("error!\n"); getchar(); } for (i=0; i