-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
146 lines (117 loc) · 3.33 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "env.h"
#include "qlearning.h"
#define ROWS 10
#define COLS 10
#define N_ACTIONS 4
#define PLAYERX 0
#define PLAYERY 0
#define EXITX 9
#define EXITY 9
void play(struct Env *env, struct QTable *qtable, int max_steps)
{
int obs = reset_env(env);
int terminated = 0;
int reward = 0;
int i = 0;
while (terminated == 0)
{
display_env(env);
putchar('\n');
int action = get_best_action(qtable, obs);
step(env, action, &obs, &terminated, &reward);
if (i++ > max_steps)
break;
}
display_env(env);
putchar('\n');
if (terminated == 1)
{
printf("YOU WON!\n");
}
else
{
printf("YOU LOST!\n");
}
}
int main(void)
{
srand(time(NULL));
//struct Env *env = create_env(ROWS, COLS, PLAYERX, PLAYERY, EXITX, EXITY);
struct Env *env = create_env_from_file("map.txt");
if (env == NULL)
{
fprintf(stderr, "unable to create env...\n");
return 1;
}
struct QTable *qa = create_qtable(env->rows * env->cols, N_ACTIONS);
if (qa == NULL)
{
fprintf(stderr, "unable to create qtable...\n");
destroy_env(env);
return 1;
}
struct QTable *qb = create_qtable(env->rows * env->cols, N_ACTIONS);
if (qb == NULL)
{
fprintf(stderr, "unable to create qtable...\n");
destroy_env(env);
destroy_qtable(qa);
return 1;
}
int oldObs = reset_env(env);
int newObs = oldObs;
int terminated = 0;
int reward = 0;
float alpha = 0.005;
float gamma = 0.9;
float epsilon = 0.95;
int epochs = 500;
printf(":::TRAINING\n");
for (int epoch = 0; epoch < epochs; ++epoch)
{
terminated = 0;
oldObs = reset_env(env);
if ((epoch > 0) && (epoch % 50 == 0))
{
epsilon = epsilon * 0.95;
printf("EPSILON => %f\n", epsilon);
}
if ((epoch > 0) && (epoch % 100 == 0))
{
printf("%5d / %5d\n", epoch, epochs);
}
while (terminated == 0)
{
int action;
if ((float)rand() / RAND_MAX > epsilon)
{
action = get_best_action(qa, oldObs);
}
else
{
action = (int)rand() % N_ACTIONS;
}
step(env, action, &newObs, &terminated, &reward);
int best_action_qa = get_best_action(qa, newObs);
float value1 = get_state_action_value(qa, oldObs, action) + alpha * (reward + gamma * get_state_action_value(qb, newObs, best_action_qa) - get_state_action_value(qa, oldObs, action));
int best_action_qb = get_best_action(qb, newObs);
float value2 = get_state_action_value(qb, oldObs, action) + alpha * (reward + gamma * get_state_action_value(qa, newObs, best_action_qb) - get_state_action_value(qb, oldObs, action));
set_state_action_value(qa, oldObs, action, value1);
set_state_action_value(qb, oldObs, action, value2);
oldObs = newObs;
}
}
putchar('\n');
printf(":::PLAYING\n");
play(env, qa, 50);
putchar('\n');
printf(":::QTABLE\n");
display_qtable(qa);
destroy_env(env);
destroy_qtable(qa);
destroy_qtable(qb);
return 0;
}