-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCQLearningController.cpp
More file actions
90 lines (82 loc) · 2.94 KB
/
Copy pathCQLearningController.cpp
File metadata and controls
90 lines (82 loc) · 2.94 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
/**
(
( )\ )
( )\ (()/( ( ) ( ( ( (
)((_) /(_)) ))\( /( )( ( )\ ( )\))(
((_)_ (_)) /((_)(_)|()\ )\ |(_) )\ )((_))\
/ _ \ | | (_))((_)_ ((_)_(_/((_)_(_/( (()(_)
| (_) | | |__/ -_) _` | '_| ' \)) | ' \)) _` |
\__\_\ |____\___\__,_|_| |_||_||_|_||_|\__, |
|___/
Refer to Watkins, Christopher JCH, and Peter Dayan. "Q-learning." Machine learning 8. 3-4 (1992): 279-292
for a detailed discussion on Q Learning
*/
#include "CQLearningController.h"
CQLearningController::CQLearningController(HWND hwndMain):
CDiscController(hwndMain),
_grid_size_x(CParams::WindowWidth / CParams::iGridCellDim + 1),
_grid_size_y(CParams::WindowHeight / CParams::iGridCellDim + 1)
{
}
/**
The update method should allocate a Q table for each sweeper (this can
be allocated in one shot - use an offset to store the tables one after the other)
You can also use a boost multiarray if you wish
*/
void CQLearningController::InitializeLearningAlgorithm(void)
{
//TODO
}
/**
The immediate reward function. This computes a reward upon achieving the goal state of
collecting all the mines on the field. It may also penalize movement to encourage exploring all directions and
of course for hitting supermines/rocks!
*/
double CQLearningController::R(uint x,uint y, uint sweeper_no){
//TODO: roll your own here!
return 0;
}
/**
The update method. Main loop body of our Q Learning implementation
See: Watkins, Christopher JCH, and Peter Dayan. "Q-learning." Machine learning 8. 3-4 (1992): 279-292
*/
bool CQLearningController::Update(void)
{
//m_vecSweepers is the array of minesweepers
//everything you need will be m_[something] ;)
uint cDead = std::count_if(m_vecSweepers.begin(),
m_vecSweepers.end(),
[](CDiscMinesweeper * s)->bool{
return s->isDead();
});
if (cDead == CParams::iNumSweepers){
printf("All dead ... skipping to next iteration\n");
m_iTicks = CParams::iNumTicks;
}
for (uint sw = 0; sw < CParams::iNumSweepers; ++sw){
if (m_vecSweepers[sw]->isDead()) continue;
/**
Q-learning algorithm according to:
Watkins, Christopher JCH, and Peter Dayan. "Q-learning." Machine learning 8. 3-4 (1992): 279-292
*/
//1:::Observe the current state:
//TODO
//2:::Select action with highest historic return:
//TODO
//now call the parents update, so all the sweepers fulfill their chosen action
}
CDiscController::Update(); //call the parent's class update. Do not delete this.
for (uint sw = 0; sw < CParams::iNumSweepers; ++sw){
if (m_vecSweepers[sw]->isDead()) continue;
//TODO:compute your indexes.. it may also be necessary to keep track of the previous state
//3:::Observe new state:
//TODO
//4:::Update _Q_s_a accordingly:
//TODO
}
return true;
}
CQLearningController::~CQLearningController(void)
{
//TODO: dealloc stuff here if you need to
}