-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRLSolver.cpp
More file actions
53 lines (47 loc) · 1.6 KB
/
Copy pathRLSolver.cpp
File metadata and controls
53 lines (47 loc) · 1.6 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
#include <iostream>
#include "RLSolver.h"
RLSolver::RLSolver(Model& m, MultiGraph_QL& g, TrainingPara tp):
model(m),graph(g), trainingPara(tp){
}
void RLSolver::train(){
int iter;
int action = 0;
char tag[10];
for( int i = 0; i < trainingPara.numEpisodes; i++) {
std::cout << "training Episodes " << i << std::endl;
iter = 0;
model.createInitialState();
graph.update_Q(model.getCurrState());
while(!targetReached(model.getCurrState()) && iter < trainingPara.maxIter) {
State oldState = model.getCurrState();
action = graph.selectNextAction(oldState);
model.run(action);
State newState = model.getCurrState();
double reward = this->getRewards(oldState, newState);
graph.update_Q(Experience(oldState, newState, action, reward), trainingPara.learningRate, trainingPara.discount);
iter++;
std::cout << "numV " << graph.getNumV() << " numE " << graph.getNumE() << std::endl;
}
if ( (i+1)%50 == 0){
sprintf(tag,"%d",i);
graph.outputQ("Q_"+(std::string)tag);
}
}
// this->getPolicy();
}
double RLSolver::getRewards(State oldState, State currState){
if (oldState.equals(currState) ){
return -trainingPara.defaultBigValue;
} else if (currState.equals(trainingPara.targetState)){
return 1;
} else {
return 0;
}
}
bool RLSolver::targetReached(State s){
if( s.equals(trainingPara.targetState)){
std::cout << "hit target!!!!!!!!!!!!!!!!!!!!" << std::endl;
return true;
}
return false;
}