-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
216 lines (201 loc) · 4.75 KB
/
main.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include <QCoreApplication>
#include <iostream>
using namespace std;
struct node{
double up;
double right;
double down;
double left;
double value;
};
char M_map[5][5]={
{' ','#',' ','#',' '},
{' ',' ',' ',' ','#'},
{' ',' ','#',' ',' '},
{' ',' ',' ',' ',' '},
{' ',' ',' ','#','G'}};
node stateAction[5][5];
int myX = 0;
int myY = 0;
double alpha = 0.5;
string random_move();
string best_move();
void update_value();
void initStateActionTable();
void showMap();
void doAct(string action);
double getStateReward(int x , int y);
bool resetGame();
int main()
{
initStateActionTable();
cout << "here"<<endl;
for(int e = 0 ; e < 1000 ; e++){
while(!resetGame()){
int r = rand()%100;
if(r >= alpha*100){
string action = best_move();
//cout << action<<endl;
doAct(action);
update_value();
}
else{
string action = random_move();
//cout << action<<endl;
doAct(action);
update_value();
}
}
myX = 0;
myY = 0;
}
//cout << stateAction[0][0].up<<endl;
//cout << stateAction[0][0].right<<endl;
//cout << stateAction[0][0].down<<endl;
//cout << stateAction[0][0].left<<endl;
while(!resetGame()){
string action = best_move();
doAct(action);
cout << action << endl;
update_value();
//showMap();
cout << endl;
}
return 0;
}
void showMap(){
for(int i = 0 ; i < 5 ; i++){
for(int j = 0 ; j < 5 ; j++){
if(i == myX && j == myY){
cout << "@";
}
else{
cout<<M_map[i][j];
}
}
cout << endl;
}
}
string random_move(){
vector<string> ways;
ways.clear();
ways.push_back("UP");
ways.push_back("RIGHT");
ways.push_back("DOWN");
ways.push_back("LEFT");
int index = rand()%ways.size();
return ways.at(index);
}
string best_move(){
double maxValue = stateAction[myX][myY].value;
vector<string> ways;
ways.clear();
if(stateAction[myX][myY].up == maxValue){
ways.push_back("UP");
}
if(stateAction[myX][myY].right == maxValue){
ways.push_back("RIGHT");
}
if(stateAction[myX][myY].down == maxValue){
ways.push_back("DOWN");
}
if(stateAction[myX][myY].left == maxValue){
ways.push_back("LEFT");
}
int index = rand()%ways.size();
return ways.at(index);
}
void update_value(){
for(int i = 0 ; i < 5 ; i++){
for(int j = 0 ; j < 5 ; j++){
stateAction[i][j].value = max(stateAction[i][j].up , max(stateAction[i][j].right , max(stateAction[i][j].down , stateAction[i][j].left)));
}
}
}
void initStateActionTable(){
for(int i = 0 ; i < 5 ; i++){
for(int j = 0 ; j < 5 ; j++){
node tmp;
tmp.up = 0.0;
tmp.right=0.0;
tmp.down=0.0;
tmp.left=0.0;
tmp.value=0.0;
stateAction[i][j]=tmp;
}
}
}
void doAct(string action){
if(action == "UP"){
//cout << "UP";
myX--;
if(myX < 0){
stateAction[myX+1][myY].up = -10;
}
else{
stateAction[myX+1][myY].up = stateAction[myX][myY].value + getStateReward(myX , myY) - 0.04;
}
}
if(action == "RIGHT"){
//cout << "RIGHT";
myY++;
if(myY > 4){
stateAction[myX][myY-1].right = -10;
}
else{
stateAction[myX][myY-1].right = stateAction[myX][myY].value + getStateReward(myX , myY) - 0.04;
}
}
if(action == "DOWN"){
//cout << "DOWN";
myX++;
if(myX > 4){
stateAction[myX-1][myY].down= -10;
}
else{
stateAction[myX-1][myY].down = stateAction[myX][myY].value + getStateReward(myX , myY) - 0.04;
}
}
if(action == "LEFT"){
//cout << "LEFT";
myY--;
if(myY < 0){
stateAction[myX][myY+1].left = -10;
}
else{
stateAction[myX][myY+1].left = stateAction[myX][myY].value + getStateReward(myX , myY) - 0.04;
}
}
}
double getStateReward(int x , int y){
char data = M_map[x][y];
if(data == '#'){
return -10;
}
if(data == 'G'){
return +10;
}
return 0.0;
}
bool resetGame(){
char data = M_map[myX][myY];
if(data == '#'){
return true;
}
if(data == 'G'){
return true;
}
if(myX < 0){
return true;
}
if(myY < 0){
return true;
}
if(myX > 4){
return true;
}
if(myY > 4){
return true;
}
return false;
}