-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDp.h
174 lines (158 loc) · 4.15 KB
/
Dp.h
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
#ifndef DP_H
#define DP_H
#include <vector>
#include <map>
#include <string>
#include <cmath>
#include <iostream>
using namespace std;
class Grid {
public:
Grid(int sz) {
size = sz;
for (int i = 0; i < sz; i = i + 1) {
vector<float> temp;
for (int a = 0; a < sz; a = a + 1) {
temp.push_back(0.0);
}
G.push_back(temp);
}
}
void operator= (Grid cpy) { this->G = cpy.G; }
void show() {
for (int a = 0; a < size; a = a + 1) {
for (int b = 0; b < size; b = b + 1) {
cout << G[a][b] << " | ";
}
cout << endl;
}
}
vector<vector<float>> G;
int size = 4;
};
class DP
{
public:
enum Action { up, right, left, down };
DP() {};
~DP() {};
void ValueIter(float threshold) {
// This is a non-discount procedure (discount = 1)
while (true)
{
Grid NewGd = Grid(GridSize);
float Delta = 0;
for (int p = 0; p < GridSize; p = p + 1) {
for (int q = 0; q < GridSize; q = q + 1) {
if (T(p, q)) { continue; }
for (map<Action, float>::iterator iter = Prob.begin(); iter != Prob.end(); iter++) {
int a = p; int b = q;
Nxt(a, b, iter->first);
NewGd.G[p][q] = NewGd.G[p][q] + (iter->second)*(reward + Gd.G[a][b]);
}
Delta = (abs(NewGd.G[p][q] - Gd.G[p][q]) > Delta) ? abs(NewGd.G[p][q] - Gd.G[p][q]) : Delta;
}
}
Gd = NewGd;
if (Delta < threshold) { break; }
}
}
void Policy() {
P.resize(GridSize);
for (int a = 0; a < GridSize; a = a + 1) {
P[a].resize(GridSize);
}
for (int p = 0; p < GridSize; p++)
{
for (int q = 0; q < GridSize; q++)
{
if (T(p, q)) { P[p][q] = "Ter"; continue; }
vector<float> v = {};
for (map<Action, string>::iterator iter = Symbol.begin(); iter != Symbol.end(); iter++) {
int a = p; int b = q;
if (Nxt(a, b, iter->first)) {Comp(v, Gd.G[a][b], P[p][q], iter->second);}
}
}
}
}
void show() { Gd.show(); }
void showpolicy() {
for (int a = 0; a < GridSize; a = a + 1) {
for (int b = 0; b < GridSize; b = b + 1) {
cout << P[a][b] << " | ";
}
cout << '\n';
}
}
protected:
// Assume that if the next state out of boundary, return to the current state
bool Nxt(int& x, int& y, const Action& act) {
if (act == up) {
if (B(x - 1, y)) { x = x - 1; return true; }
else { return false; }
}
else if (act == down) {
if (B(x + 1, y)) { x = x + 1; return true; }
else { return false; }
}
else if (act == left) {
if (B(x, y - 1)) { y = y - 1; return true; }
else { return false; }
}
else if (act == right) {
if (B(x, y + 1)) { y = y + 1; return true; }
else { return false; }
}
return true;
}
// It defines the border and the block area
// If it is a block --> false
bool B(const int& x, const int& y) {
if (x<0 || x>GridSize - 1) { return false; }
if (y<0 || y>GridSize - 1) { return false; }
for (vector<vector<int>>::iterator iter = Block.begin(); iter != Block.end(); iter++) {
if (*iter == vector<int>{x, y}) {
return false;
}
}
return true;
}
// If it is a terminal -->true
bool T(const int& x, const int& y) {
for (vector<vector<int>>::iterator iter = Terminal.begin(); iter != Terminal.end(); iter++) {
if (*iter == vector<int>{x, y}) {
return true;
}
}
}
void Comp(vector<float>& v, float value, string& st, const string& output) {
if (v.empty()) { st = output; v.push_back(value); return; }
else {
if (value > v[0]) {
st = output; v[0] = value; return;
}
else if (value == v[0]) {
st = st + " , "+ output; return;
}
return;
}
}
private:
Grid Gd = Grid(5);
int GridSize = 5;
vector<vector<string>> P = { {} };
vector<vector<int>> Terminal = { {0,0},{4,4} };
vector<vector<int>> Block = { {} };
// The policy is to follow any direction Randomly (equal probability)
map<Action, float> Prob =
{ {up, 0.25}, {down, 0.25}, {left,0.25},{right,0.25} };
map<Action, string> Symbol =
{
{up, "up"}, {down, "down"},
{left, "left"}, {right, "right"}
};
// the reward is always -1 for each action
float reward = -1.0;
};
#endif // !DP_H
#pragma once