-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAI.cpp
341 lines (311 loc) · 9.32 KB
/
AI.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
*Author:-Pukar Giri
*Created On:-21Th December 2018 at 20:49
*File Name:-AI.cpp
*Project Name:-dlibprac
*Licence:- MIT
*Email:[email protected]
*/
#include "AI.h"
typedef struct
{
action action1;
float value;
} action_value;
bool operator<(const state state1, const state state2)
{
return (
state1.my_action < state2.my_action &&
state1.opp_action < state2.opp_action &&
state1.distance < state2.distance );
}
bool operator>(const state state1, const state state2)
{
return (
state1.my_action > state2.my_action &&
state1.opp_action > state2.opp_action &&
state1.distance > state2.distance);
}
bool operator==(const state state1, const state state2)
{
return (
state1.my_action == state2.my_action &&
state1.opp_action == state2.opp_action &&
state1.distance == state2.distance);
}
bool operator<(const state_action_pair pair1, const state_action_pair pair2)
{
return (pair1.state1 < pair2.state1 && pair1.action1 < pair2.action1);
}
bool operator>(const state_action_pair pair1, const state_action_pair pair2)
{
return (pair1.state1 > pair2.state1 && pair1.action1 > pair2.action1);
}
bool operator==(const state_action_pair pair1, const state_action_pair pair2)
{
return (pair1.state1 == pair2.state1 && pair1.action1 == pair2.action1);
}
bool comp_action_value(action_value pair1, action_value pair2)
{
return pair1.value < pair2.value;
}
bool comp_key_value(key_value key_value1, key_value key_value2)
{
return key_value1.pair < key_value1.pair;
}
bool comp_reward(float reward1, float reward2)
{
return reward1 < reward2;
}
AI::AI(player & sel,player &opp, float epsilion, float alpha, float gamma) :
self{sel}, opponent{opp}
{
load();
this->epsilion = epsilion;
this->alpha = alpha;
this->gamma = gamma;
}
inline float AI::getq(state_action_pair pair)
{
unsigned int x = 0;
for (auto item : q)
{
if (item.pair == pair)
{
break;
}
x++;
}
if (x >= q.size())
return 0.0;
return q.at(x).value;
}
void AI::setq(state_action_pair pair, float value)
{
unsigned int x = 0;
for (auto item : q)
{
if (item.pair == pair)
{
break;
}
x++;
}
if (x >= q.size())
{
q.push_back({pair, value});
std::sort(q.begin(), q.end(), comp_key_value);
return;
}
q.at(x).value = value;
}
void AI::learnq(state state1, action action1, float reward, float value)
{
state_action_pair state_action{state1, action1};
float oldvalue = getq(state_action);
// if (gencount%1000==0)
// {
// printf("old value : %f\n", oldvalue);
// }
if (oldvalue == 0)
{
// printf("found new state \n");
setq(state_action, reward);
// q.insert(std::make_pair(state_action,reward));
}
else
{
this->setq(state_action, oldvalue + this->alpha * (value - oldvalue));
}
}
action AI::chooseaction(state state1)
{
if ((random() % 100) < this->epsilion)
{
//printf("choosing randomly\n");
switch (random() % 5)
{
case 0:
return action::cwalkb;
case 1:
return action::cwalkf;
case 2:
return action::ckick;
case 3:
return action::cpunch;
case 4:
return action::cjump;
default:
return action::cidle;
}
}
std::vector<action_value> qvals;
qvals.push_back({action::cidle, this->getq({state1, action::cidle})});
qvals.push_back({action::cjump, this->getq({state1, action::cjump})});
qvals.push_back({action::cpunch, this->getq({state1, action::cpunch})});
qvals.push_back({action::ckick, this->getq({state1, action::ckick})});
qvals.push_back({action::cwalkb, this->getq({state1, action::cwalkb})});
qvals.push_back({action::cwalkf, this->getq({state1, action::cwalkf})});
action_value *x, *y;
std::sort(qvals.begin(), qvals.end(), comp_action_value);
std::vector<action_value> maxqs;
// if (gencount % 1010 == 0)
// {
// for (auto item :qvals)
// {
// printf("%f\t", item.value);
// }
// printf("\n");
// }
for (auto qs : qvals)
{
if (qs.value == qvals[0].value)
{
maxqs.push_back(qs);
}
}
unsigned long index = random() % maxqs.size();
// if (gencount % 1000 == 0)
// {
// printf("max q value: %f\nmin q value : %f \n", maxqs[index].value, qvals[qvals.size() - 1].value);
// }
return maxqs[index].action1;
}
void AI::learn(state state1, action action1, float reward, state state2)
{
std::vector<float> qvals;
qvals.push_back(this->getq({state1, action::cidle}));
qvals.push_back(this->getq({state1, action::cwalkb}));
qvals.push_back(this->getq({state1, action::cwalkf}));
qvals.push_back(this->getq({state1, action::cjump}));
qvals.push_back(this->getq({state1, action::ckick}));
qvals.push_back(this->getq({state1, action::cpunch}));
std::sort(qvals.begin(), qvals.end(), comp_reward);
float maxqnew = qvals[0];
this->learnq(state1, action1, reward, reward + this->gamma * maxqnew);
}
inline state_action_pair AI::get_state_action_pair(state &state1, action &action1)
{
return state_action_pair{state1, action1};
}
state AI::calc_state()
{
int distance=abs(opponent.x-self.x);
lastdistance=distance;
return {self.current_action,opponent.current_action,distance};
}
float AI::calc_reward()
{
float reward=0;
if (self.noted) reward-=100;
if (opponent.noted) reward+=200;
else reward -=50;
if (abs(opponent.x-self.x)>130)
{
if (lastdistance <= abs(self.x - opponent.x))reward += 50;
else reward -= 50;
}else reward +=20;
if ((self.left&&self.x+70<opponent.x)||(!self.left&&self.x>opponent.x+70))reward+=50;
else reward-=50;
// cout<<"reward:"<<reward<<endl;
return -reward;
}
void AI::drive()
{
state newstate = calc_state();
action action1 = chooseaction(newstate);
/*
if (gencount % 1000 == 0)
{
system("clear");
printf("turncount:- %lld \nstate:- {%d,%d,%d}\n", this->gencount,newstate.distance,newstate.my_action,newstate.opp_action);;
printf("idle count : %lld\n", no_of_idle_choosen);
printf("jump count : %lld\n", no_of_jump_choosen);
printf("left count : %lld\n", no_of_left_choosen);
printf("right count : %lld\n", no_of_right_choosen);
printf("kick choosen count : %lld\n", no_of_kick_choosen);
printf("punch choosen count : %lld\n", no_of_punch_choosen);
printf("q length : %ld s \n", q.size());
printf("q size : %ld Bytes or %f KB\n", q.size() * sizeof(key_value), q.size() * sizeof(key_value) / 1024.0);
printf("=======================================================\n\n");
// printf("actionchoosen : %d\n\n", action1);
}
// if (gencount%10000==0)dump();*/
switch (action1)
{
case action::cidle:
self.idle();
no_of_idle_choosen++;
break;
case action::cwalkf:
self.walkf();
no_of_right_choosen++;
break;
case action::cwalkb:
self.walkb();
no_of_left_choosen++;
break;
case action::cjump:
self.jump();
no_of_jump_choosen++;
break;
case action::cpunch:
self.punch();
no_of_punch_choosen++;
break;
case action::ckick:
self.kick();
no_of_kick_choosen++;
break;
}
if (gencount)
{
learn(laststate, lastaction, calc_reward(), newstate);
}
lastaction = action1;
laststate = newstate;
gencount++;
}
void AI::dump()
{
system("clear");
cout<<"dumping the file"<<endl;
ofstream outfile("mlvalue.clx");
for( auto item : q)
{
outfile<<item.pair.state1.my_action<<endl;
outfile<<item.pair.state1.opp_action<<endl;
outfile<<item.pair.state1.distance<<endl;
outfile<<item.pair.action1<<endl;
outfile<<item.value<<endl;
outfile<<endl;
}
}
void AI::load()
{
action action1;
state_action_pair pair1;
state state1;
float value;
key_value keyValue1;
string line;
ifstream infile("mlvalue.clx");
while (getline(infile,line))
{
state1.my_action=action(stoi(line));
getline(infile,line);
state1.opp_action=action(stoi(line));
getline(infile,line);
state1.distance=stoi(line);
pair1.state1=state1;
getline(infile,line);
action1=action (stoi(line));
pair1.action1=action1;
getline(infile,line);
value=stof(line);
keyValue1.value=value;
keyValue1.pair=pair1;
q.push_back(keyValue1);
getline(infile,line);
}
}