-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAgent.cpp
More file actions
335 lines (242 loc) · 6.24 KB
/
Copy pathAgent.cpp
File metadata and controls
335 lines (242 loc) · 6.24 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
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
#include "Agent.h"
// Constructor -- create a new ActionList to store the actions passed to this.
Agent::Agent()
{
actionList = new ActionList();
discount = 0.0;
}
// Destructur -- delete the ActionList
Agent::~Agent()
{
delete actionList;
}
// addAction -- add an action to the ActionList.
void Agent::addAction(Action *action)
{
actionList->addAction(action);
}
// addTrainingState -- add a new state to the list to train this Agent.
void Agent::addTrainingState(state* trainingState)
{
trainingStates.push_back(trainingState);
}
// transition - perform an action on the state and determine the new state by
// recalculating the comparisons.
bool Agent::transition(int actionID, state &s)
{
bool results = actionList->getAction(actionID+1)->act(s);
s.setLastActionID(actionID);
s.buildcomps();
return results;
}
// learn - update Q values for the state by performing actions.
int Agent::learn(state &curstate, qvaltree *tree)
{
dataStructure stateData = curstate.getData();
int reward;
qvalweb *qvw = tree->getqvw(curstate);
int itercount = 0;
bool terminate = false;
while (!terminate && itercount < maxIterations)
{
itercount++;
int act = qvw->bestact;
if (act == NOOP || qvw->qval < 0 || abs(drand48()) > PROB)
{
act = actionList->getRandomAction(curstate);
}
terminate = transition(act, curstate);
qvalweb *nqvw = tree->getqvw(curstate);
reward = actionList->getAction(act+1)->reward(curstate);
qvw->updateqval(act, nqvw, reward, discount);
qvw = nqvw;
}
if (tree->qvw->visits < maxVisits / 2)
return itercount;
itercount = 0;
curstate = state();
curstate.setData(stateData);
curstate.buildcomps();
qvw = tree->getqvw(curstate);
terminate = false;
while (!terminate && (qvw->bestact != NOOP) && itercount < 2*maxIterations)
{
itercount++;
terminate = transition(qvw->bestact, curstate);
qvalweb *nqvw = tree->getqvw(curstate);
int act = qvw->bestact;
reward = actionList->getAction(act+1)->reward(curstate);
qvw->updateqval(qvw->bestact, nqvw, reward, discount);
qvw = nqvw;
}
return itercount;
}
// treesort - resorts the qvaltree.
int Agent::treesort(state &curstate, qvaltree *tree, bool show)
{
int itercount = actionList->getNumberActions() * actionList->getNumberActions() * curstate.getData().list.size() * curstate.getData().list.size() + 1;
int score = 0;
if (show)
{
cout << curstate.showData() << endl;
cout << curstate.tostring() << endl;
}
bool terminate = false;
while (!terminate)
{
itercount--;
if (itercount < 0)
return -1;
qvalweb *qvw = tree->getqvw(curstate);
qvw->visited = true;
if (qvw->bestact == NOOP)
break;
terminate = transition(qvw->bestact, curstate);
if (show)
{
cout << curstate.tostring() << "\t";
cout << actionList->getAction(qvw->bestact+1)->getDescription();
cout << endl;
}
if (qvw->bestact == SWAP)
score++;
qvalweb *nqvw = tree->getqvw(curstate);
}
if (!curstate.isFinished())
return -1;
return score;
}
// set Discount, MaxIterations, MaxIterationCount, MaxVisits, setReportCount
// sets RP specific values for calculating Q-Values.
void Agent::setDiscount(double discount)
{
this->discount = discount;
}
void Agent::setMaxIterations(int maxIter)
{
maxIterations = maxIter;
}
void Agent::setMaxIterationCount(int maxCount)
{
maxIterationCount = maxCount;
}
void Agent::setMaxVisits(int maxVisits)
{
this->maxVisits = maxVisits;
}
void Agent::setReportCount(int repCount)
{
reportCount = repCount;
}
// run - calculates a policy based on the training states provided.
bool Agent::run()
{
long srseed = time(NULL);
srand(srseed);
vector< state* > trainStates;
trainStates.reserve(trainingStates.size());
trainStates.push_back(trainingStates[trainingStates.size() - 1]);
tree = NULL;
bool failure = true;
int itercount = 0;
while (failure && itercount < maxIterationCount)
{
failure = false;
for (int i = 0; i < trainStates.size(); i++)
{
state s = *trainStates[i];
if (tree == NULL)
{
tree = new qvaltree(s, actionList);
}
else
tree->getqvw(s);
}
bool notmaxed = true;
int reportscore = 0;
while (notmaxed)
{
itercount++;
notmaxed = false;
for (int i = 0; i < trainStates.size(); i++)
{
state s = *trainStates[i];
qvalweb *qvw = tree->getqvw(s);
reportscore += learn(s, tree);
if (qvw->visits < maxVisits)
notmaxed = true;
if (qvw->visits < maxVisits && itercount % reportCount == 0)
{
cout << itercount << "\t" << (i+1) << " / ";
cout << trainStates.size() << "\t";
cout << s.showData() << "\t";
cout << qvw->visits << "\t" << qvw->qval << "\t";
cout << actionList->getAction(qvw->bestact+1)->getDescription();
cout << "\t" << tree->number << endl;
}
}
if (itercount % reportCount == 0)
{
reportscore = reportscore / (reportCount * trainStates.size());
cout << "avg iters:\t" << reportscore << endl << endl;
reportscore = 0;
}
}
for (int i = 0; i < trainStates.size(); i++)
{
state temp = *trainStates[i];
if (treesort(temp, tree, true) < 0)
{
failure = true;
tree->clearvisits();
break;
}
}
if (!failure)
{
tree->clearvisits();
trainStates.clear();
for (int i = 0; i < trainingStates.size(); i++)
{
state temp = *trainingStates[i];
if (treesort(temp, tree, true) < 0)
{
trainStates.push_back(trainingStates[i]);
failure = true;
cout << "failure on:\t" << i << endl;
i = trainingStates.size();
}
}
}
else
continue;
if (failure)
{
tree->clearvisits();
tree->clearvisited();
}
}
if (itercount >= maxIterationCount)
{
cerr << "FAILURE TO CONVERGE" << endl;
cerr << discount << endl;
delete tree;
return false;
}
return true;
}
// printReport -- Prints the policy report generated by the agent.
void Agent::printReport()
{
vector<qvalweb*> qvws = tree->getqvws();
int depth, leaf;
cout << "TREE" << endl;
tree->showtree(true);
cout << "BUILDING EDT" << endl;
edt *etree = new edt(qvws, depth, leaf, 0, actionList);
etree->showedt();
cout << "depth = " << depth << endl;
cout << "leaves = " << leaf << endl;
delete etree;
delete tree;
}