diff --git a/constant.h b/constant.h index bf73899..5452100 100644 --- a/constant.h +++ b/constant.h @@ -6,11 +6,12 @@ #define LAMBDA 0.9 // discount factor #define STATE_NUM 19683 -#define ACTION_NUM 9 -#define EPISODE_NUM 100000 +#define ACTION_NUM 7 +#define EPISODE_NUM 1000000 #define FIRST true #define ROW_NUM 6 #define COL_NUM 7 -#define BIGNUM_LEN 22 \ No newline at end of file +#define BIGNUM_LEN 22 +#define TABLE_SIZE 1000000000 diff --git a/hash-table.c b/hash-table.c index 6ba4f70..1045882 100644 --- a/hash-table.c +++ b/hash-table.c @@ -3,14 +3,8 @@ #include #include #include - -#define TABLE_SIZE 10 - -struct Node { - char key[48]; - int value; - struct Node *next; -}; +#include "hash-table.h" +#include "constant.h" long long hash_function(char *key) { long long hash = 0; @@ -20,115 +14,69 @@ long long hash_function(char *key) { return hash ; } -void insert(struct Node **table, char *key, int value) { +void insert(struct Node **table, char *key) { long long hash = hash_function(key); - printf("Hash: %lli\n", hash); struct Node *node = malloc(sizeof(struct Node)); struct Node *temp, *past; strcpy(node->key, key); - node->value = value; + // init + for (short i=0; ivalue[i] = 0.0; + } node->next = NULL; if (table[hash] == NULL){ table[hash] = node; - printf("Create.\n"); } else { - printf("Add.\n"); temp = table[hash]; past = NULL; while(temp != NULL){ - assert(temp->key != key); - printf("%s -> ", temp->key); + assert(strcmp(temp->key, key)!=0); past = temp; temp = temp->next; } - printf("\n"); past->next = node; } } -void long_to_str(long long num, char *s, int length) { - int temp; - for (int i=length-1; i>=0; i--) { - temp = num % 10; - num /= 10; - s[i] = (char)(temp + 48); - } -} - -int search(struct Node **table, char *key) { +void search(struct Node **table, char *key, bool *find, float *ans) { long long hash = hash_function(key); struct Node *temp, *past; + *find = false; - if (table[hash] == NULL){ - return -1; - } else { + if (table[hash] != NULL){ temp = table[hash]; past = NULL; while(temp != NULL){ - // printf("%s - %s\n", temp->key, key); if (strcmp(temp->key, key) == 0){ - return temp->value; + *find = true; + for (short i=0; ivalue[i]; + } + break; } past = temp; temp = temp->next; } - return -1; } } -void update(struct Node **table, char *key, int value) { +void update(struct Node **table, char *key, short action, float value) { long long hash = hash_function(key); struct Node *temp, *past; + assert(table[hash]!=NULL); temp = table[hash]; past = NULL; while(temp != NULL){ if (strcmp(temp->key, key) == 0){ - temp->value = value; + temp->value[action] = value; break; } past = temp; temp = temp->next; } -} - -int main(){ - struct Node ** table; // pointer to pointer - int size; - srand(time(NULL)); - - table = malloc(TABLE_SIZE * sizeof(struct Node*)); - for (int i=0; i "); - scanf("%lli", &a); - printf("HERE\n"); - long_to_str(a, s, 20); - printf("HERE\n"); - - update(table, s, 100); - ans = search(table, s); - printf("%d\n\n", ans); - } - // long long a = hash_function("9999999999999"); - // printf("%lli\n", a); } \ No newline at end of file diff --git a/hash-table.h b/hash-table.h new file mode 100644 index 0000000..e47e916 --- /dev/null +++ b/hash-table.h @@ -0,0 +1,13 @@ +#include "constant.h" +#include + +struct Node { + char key[BIGNUM_LEN+1]; + float value[ACTION_NUM]; + struct Node *next; +}; + +long long hash_function(char *key); +void insert(struct Node **table, char *key); +void search(struct Node **table, char *key, bool *find, float *ans); +void update(struct Node **table, char *key, short action, float value); \ No newline at end of file diff --git a/main.c b/main.c index 202c16c..9fea68c 100644 --- a/main.c +++ b/main.c @@ -7,13 +7,21 @@ #include "q-learning.h" int main(){ - short board[9]= {0}; // tic tac toe's chessboard - float table[STATE_NUM][ACTION_NUM]; // q-learning table + short board[ROW_NUM][COL_NUM]= {0}; + short winner; + struct Node ** map; // pointer to pointer, hash table + bool find; + float state[ACTION_NUM]; - srand(time(NULL)); - init_table(&table[0][0]); + srand(time(NULL)); - run(&table[0][0], board, false, 10000, false); - run(&table[0][0], board, true, EPISODE_NUM, false); - run(&table[0][0], board, false, 10000, false); + // init hash table + map = malloc(TABLE_SIZE * sizeof(struct Node*)); + for (int i=0; i EPSILON -> random a action @@ -83,17 +97,15 @@ short bot_choose_action(float *table, short *board, int state){ Opponent random choose a action to do. Args: - - short *table (array's address): state table for Q-Learning - short *board (array's address): chessboards' status - - int state (integer, state hash): hash for board's status Results: - short choice (integer): random, -1 means no available action to choose */ -short opponent_random_action(float *table, short *board, int state){ +short opponent_random_action(short *board){ // get available actions for choosing - short available_actions[9]; + short available_actions[ACTION_NUM]; short available_action_length; get_available_actions(board, available_actions, &available_action_length); @@ -109,22 +121,24 @@ short opponent_random_action(float *table, short *board, int state){ return choice; } -/* - Inilialize the Q-Table +// Use Hash Table, so we needn't initilize Q-Table +// +// /* +// Inilialize the Q-Table - Args: - - float *table (two-dim array's start address) +// Args: +// - float *table (two-dim array's start address) - Results: - - None. -*/ -void init_table(float *table){ - for (int i=0; i