-
Notifications
You must be signed in to change notification settings - Fork 0
/
Agent.cpp
190 lines (148 loc) · 4.25 KB
/
Agent.cpp
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include "pch.h"
#include "Agent.h"
#include "movesEnum.h"
Agent::Agent(double alpha, double gamma, int initPos, int gridSize)
{
COORD start;
moves_order[0] = 1;
moves_order[1] = 2;
moves_order[2] = 3;
moves_order[3] = 4;
start.X = initPos % gridSize;
start.Y = initPos / gridSize;
this->alpha = alpha;
this->gamma = gamma;
position = start;
startPos = start;
this->gridSize = gridSize;
qTable = new double*[gridSize*gridSize];
prevFirstMoveBestqVal = 0;
prevSecondMoveBestqVal = 0;
prevLastStateqVal = 0;
prevPenultimateStateqVal = 0;
for (int i = 0; i < gridSize*gridSize; ++i)
{
qTable[i] = new double[4];
for (int j = 0; j < 4; ++j)
qTable[i][j] = 0;
}
}
Moves Agent::pickAction(int state, double epsilon)
{
double testVal = 0;
double maxVal=-1e9;
Moves action = Moves(0);
int intAction = 0;
testVal = static_cast <double> (rand()) / static_cast <double> (RAND_MAX);
if (testVal < epsilon)
action = Moves(1 + rand() % 4); //pick random action if value is less than epsilon
else //otherwise choose a move depending on the value of the qTable
{
for (int i = 0; i < 4; ++i)
{
if (qTable[state][moves_order[i]-1] > maxVal)
{
maxVal = qTable[state][moves_order[i]-1];
intAction = moves_order[i];
}
}
action = Moves(intAction);
std::next_permutation(moves_order, moves_order + 4); //consider the moves in different order every time
}
return action;
}
void Agent::updatePos(int newState)
{
position.X = newState % gridSize;
position.Y = newState / gridSize;
}
double Agent::getBestActionQVal(int state)
{
double maxVal = -1e9;
for (int i = 0; i < 4; ++i)
{
if (position.X == 0 && i == 2) //only look at the actions that will result in a position inside the grid
continue;
else if (position.Y == 0 && i == 0)
continue;
else if (position.X == gridSize - 1 && i == 3)
continue;
else if (position.Y == gridSize - 1 && i == 1)
continue;
if (qTable[state][i] > maxVal)
maxVal = qTable[state][i];
}
return maxVal;
}
void Agent::printQTable()
{
for (int i = 0; i < gridSize; ++i)
{
for (int j = 0; j < gridSize; ++j)
printf("%.2lf ", getBestActionQVal(i*gridSize + j));
printf("\n");
}
}
void Agent::updateQValueOutput(int state, COORD startingPoint)
{
COORD dest;
HANDLE hStdout;
COORD stateCoord;
stateCoord.X = state % gridSize;
stateCoord.Y = state / gridSize;
hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
dest.X = 6 * stateCoord.X + startingPoint.X; //account for the spaces between the characters in the grid
dest.Y = stateCoord.Y + startingPoint.Y;
SetConsoleCursorPosition(hStdout, dest); //move the cursor to the correct location
printf("%.2lf", getBestActionQVal(state));
}
bool Agent::checkForConvergence(int lastState, int penultimateState)
{
double currFirstMoveBestqVal,
currSecondMoveBestqVal;
double lastStateqVal,
penultimateStateqVal;
bool converged;
Moves action;
COORD pos;
pos = startPos;
currFirstMoveBestqVal = getBestActionQVal(startPos.X + gridSize * startPos.Y);
action = pickAction(startPos.X + gridSize * startPos.Y, 0.f);
switch (action) //use it to find the action that will lead to the 2nd state
{
case up:
if (pos.Y - 1 < 0)
break;
pos.Y -= 1;
break;
case down:
if (pos.Y + 1 > gridSize - 1)
break;
pos.Y += 1;
break;
case left:
if (pos.X - 1 < 0)
break;
pos.X -= 1;
break;
case right:
if (pos.X + 1 > gridSize - 1)
break;
pos.X += 1;
break;
default:
break;
}
currSecondMoveBestqVal = getBestActionQVal(pos.X + gridSize * pos.Y);
penultimateStateqVal = getBestActionQVal(penultimateState);
lastStateqVal = getBestActionQVal(lastState);
if (abs(currFirstMoveBestqVal - prevFirstMoveBestqVal) < 0.01 && abs(currSecondMoveBestqVal - prevSecondMoveBestqVal) < 0.01 && abs(lastStateqVal - prevLastStateqVal) < 0.01 && abs(penultimateStateqVal - prevPenultimateStateqVal) < 0.01)
converged = true;
else
converged = false;
prevFirstMoveBestqVal = currFirstMoveBestqVal;
prevSecondMoveBestqVal = currSecondMoveBestqVal;
prevLastStateqVal = lastStateqVal;
prevPenultimateStateqVal = penultimateStateqVal;
return converged;
}