-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.cpp
More file actions
137 lines (121 loc) · 3.51 KB
/
test.cpp
File metadata and controls
137 lines (121 loc) · 3.51 KB
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
#include <iostream>
#include <cstdlib>
#include <ctime>
const int MAZE_SIZE = 6;
const int ACTIONS = 4; // up, down, left, right
enum Actions { UP, DOWN, LEFT, RIGHT };
struct State {
int x, y;
};
class Maze {
public:
int maze[MAZE_SIZE][MAZE_SIZE] = {
{ 0, 1, 0, 0, 0, 1},
{ 0, 1, 0, 1, 0, 1},
{ 0, 0, 0, 1, 0, 1},
{ 0, 1, 0, 1, 0, 1},
{ 0, 1, 5, 0, 0, 1},
{ 1, 1, 1, 1, 1, 1}
};
State start = {0, 0};
State goal = {4, 2};
bool is_goal(State s) {
return s.x == goal.x && s.y == goal.y;
}
bool is_valid(State s) {
if (s.x < 0 || s.x >= MAZE_SIZE || s.y < 0 || s.y >= MAZE_SIZE) {
return false;
}
// return maze[s.x][s.y] == 0;
return maze[s.x][s.y] != 1;
}
};
class QLearning {
public:
double Q[MAZE_SIZE][MAZE_SIZE][ACTIONS];
double alpha = 0.1;
double gamma = 0.9;
double epsilon = 0.1;
QLearning() {
// Init QMatrix
for (int i = 0; i < MAZE_SIZE; ++i)
for (int j = 0; j < MAZE_SIZE; ++j)
for (int k = 0; k < ACTIONS; ++k)
Q[i][j][k] = 0.0;
}
Actions get_action(State s) {
// if ((double)rand() / RAND_MAX < epsilon) {
if (static_cast<double>(rand()) / RAND_MAX < epsilon) {
return (Actions)(rand() % ACTIONS);
} else {
double max_value = -1e9;
Actions best_action = UP;
for (int a = 0; a < ACTIONS; ++a) {
if (Q[s.x][s.y][a] > max_value) {
max_value = Q[s.x][s.y][a];
best_action = (Actions)a;
}
}
return best_action;
}
}
void update(State s, Actions a, State s_next, double reward) {
double max_q_next = -1e9;
for (int a_next = 0; a_next < ACTIONS; ++a_next) {
if (Q[s_next.x][s_next.y][a_next] > max_q_next) {
max_q_next = Q[s_next.x][s_next.y][a_next];
}
}
Q[s.x][s.y][a] += alpha * (reward + gamma * max_q_next - Q[s.x][s.y][a]);
}
};
State get_next_state(State s, Actions a) {
State s_next = s;
switch (a) {
case UP: s_next.x -= 1; break;
case DOWN: s_next.x += 1; break;
case LEFT: s_next.y -= 1; break;
case RIGHT: s_next.y += 1; break;
}
return s_next;
}
void train_agent(Maze& maze, QLearning& ql, int episodes) {
srand((unsigned)time(0));
for (int episode = 0; episode < episodes; ++episode) {
State s = maze.start;
while (!maze.is_goal(s)) {
Actions a = ql.get_action(s);
State s_next = get_next_state(s, a);
if (!maze.is_valid(s_next)) {
s_next = s; // if move not OK, stay in the same state
}
double reward = maze.is_goal(s_next) ? 100.0 : -1.0;
ql.update(s, a, s_next, reward);
s = s_next;
}
}
}
void test_agent(Maze& maze, QLearning& ql) {
State s = maze.start;
State temp;
while (!maze.is_goal(s)) {
Actions a = ql.get_action(s);
temp = get_next_state(s, a);
if (maze.is_valid(temp)) {
s = temp;
} else {
continue;
}
std::cout << "(" << s.x << ", " << s.y << ") -> ";
if (maze.is_goal(s)) {
std::cout << "Goal Reached!" << std::endl;
}
}
}
int main() {
Maze maze;
QLearning ql;
train_agent(maze, ql, 1000);
test_agent(maze, ql);
return 0;
}