| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
#include "QLearner.h" |
|---|
| 9 |
|
|---|
| 10 |
#include <string> |
|---|
| 11 |
#include <iostream> |
|---|
| 12 |
#include "GameWorld.h" |
|---|
| 13 |
#include "QFunction.h" |
|---|
| 14 |
#include "QNeuralNetwork.h" |
|---|
| 15 |
#include "State.h" |
|---|
| 16 |
#include "Action.h" |
|---|
| 17 |
|
|---|
| 18 |
#define GAMMA 0.3 |
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
QLearner::QLearner(GameWorld* world) { |
|---|
| 25 |
gameWorld = world; |
|---|
| 26 |
qFunction = new QNeuralNetwork(gameWorld->getState()->getSize(), 5); |
|---|
| 27 |
} |
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
void QLearner::learn(double learnTime) { |
|---|
| 38 |
if (learnTime == DEFAULT_LEARN_TIME) { |
|---|
| 39 |
learnTime = gameWorld->getStartClock(); |
|---|
| 40 |
} |
|---|
| 41 |
|
|---|
| 42 |
gameWorld->startGame(); |
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
for (int count = 0; count < learnTime; count++) { |
|---|
| 46 |
|
|---|
| 47 |
if (gameWorld->isDone()) { |
|---|
| 48 |
gameWorld->startGame(); |
|---|
| 49 |
} |
|---|
| 50 |
|
|---|
| 51 |
State* state = gameWorld->getState(); |
|---|
| 52 |
|
|---|
| 53 |
vector<Action*> legalMoves = gameWorld->getLegalMoves(); |
|---|
| 54 |
Action* action = chooseMove(legalMoves); |
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
performAction(action); |
|---|
| 58 |
|
|---|
| 59 |
State* newState = gameWorld->getState(); |
|---|
| 60 |
vector<Action*> newActions = gameWorld->getLegalMoves(); |
|---|
| 61 |
|
|---|
| 62 |
int reward = gameWorld->getReward(gameWorld->getRole()); |
|---|
| 63 |
double nextUtility = getMaxUtility(newState, newActions); |
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
updateUtility(state, action, reward, nextUtility); |
|---|
| 67 |
} |
|---|
| 68 |
|
|---|
| 69 |
} |
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
Action* QLearner::getAction(double decisionTime) { |
|---|
| 80 |
|
|---|
| 81 |
State* state = gameWorld->getState(); |
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
vector<Action*> actions = gameWorld->getLegalMoves(); |
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 |
double maxUtility = 0; |
|---|
| 88 |
Action* bestAction = NULL; |
|---|
| 89 |
for (unsigned int i = 0; i < actions.size(); i++) { |
|---|
| 90 |
double utility = qFunction->getValue(*state, *(actions[i])); |
|---|
| 91 |
if (bestAction == NULL || utility > maxUtility) { |
|---|
| 92 |
maxUtility = utility; |
|---|
| 93 |
bestAction = actions[i]; |
|---|
| 94 |
} |
|---|
| 95 |
} |
|---|
| 96 |
|
|---|
| 97 |
return bestAction; |
|---|
| 98 |
} |
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 |
void QLearner::performAction(Action* action) { |
|---|
| 107 |
vector<string> roles = gameWorld->getRoles(); |
|---|
| 108 |
vector<Action*> actions(roles.size()); |
|---|
| 109 |
|
|---|
| 110 |
for (unsigned int roleNum = 0; roleNum < roles.size(); roleNum++) { |
|---|
| 111 |
if (roles[roleNum] == gameWorld->getRole()) { |
|---|
| 112 |
actions[roleNum] = action; |
|---|
| 113 |
} else { |
|---|
| 114 |
vector<Action*> possibleActions |
|---|
| 115 |
= gameWorld->getLegalMoves(roles[roleNum]); |
|---|
| 116 |
actions[roleNum] = getRandomMove(possibleActions); |
|---|
| 117 |
} |
|---|
| 118 |
} |
|---|
| 119 |
|
|---|
| 120 |
|
|---|
| 121 |
gameWorld->update(actions); |
|---|
| 122 |
} |
|---|
| 123 |
|
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 |
|
|---|
| 127 |
|
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 |
double QLearner::getMaxUtility(State* state, vector<Action*> actions) { |
|---|
| 132 |
double max = 0; |
|---|
| 133 |
for (unsigned int actionIndex = 0; actionIndex < actions.size(); |
|---|
| 134 |
actionIndex++) { |
|---|
| 135 |
Action* action = actions[actionIndex]; |
|---|
| 136 |
double utility = qFunction->getValue(*state, *action); |
|---|
| 137 |
if (utility > max) { |
|---|
| 138 |
max = utility; |
|---|
| 139 |
} |
|---|
| 140 |
} |
|---|
| 141 |
return max; |
|---|
| 142 |
} |
|---|
| 143 |
|
|---|
| 144 |
|
|---|
| 145 |
|
|---|
| 146 |
|
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 |
|
|---|
| 151 |
|
|---|
| 152 |
|
|---|
| 153 |
|
|---|
| 154 |
void QLearner::updateUtility(State* state, Action* action, int reward, |
|---|
| 155 |
double nextUtility) { |
|---|
| 156 |
double utility = reward + GAMMA * nextUtility; |
|---|
| 157 |
qFunction->update(*state, *action, utility); |
|---|
| 158 |
} |
|---|
| 159 |
|
|---|
| 160 |
|
|---|
| 161 |
|
|---|
| 162 |
|
|---|
| 163 |
|
|---|
| 164 |
|
|---|
| 165 |
|
|---|
| 166 |
|
|---|
| 167 |
|
|---|
| 168 |
|
|---|
| 169 |
|
|---|
| 170 |
Action* QLearner::chooseMove(vector<Action*> possibleActions) const { |
|---|
| 171 |
|
|---|
| 172 |
vector<double> weights(possibleActions.size()); |
|---|
| 173 |
for (unsigned int i = 0; i < possibleActions.size(); i++) { |
|---|
| 174 |
weights[i] = qFunction->getValue(*gameWorld->getState(), |
|---|
| 175 |
*possibleActions[i]); |
|---|
| 176 |
} |
|---|
| 177 |
|
|---|
| 178 |
|
|---|
| 179 |
return getRandomMove(possibleActions, weights); |
|---|
| 180 |
} |
|---|
| 181 |
|
|---|
| 182 |
|
|---|
| 183 |
|
|---|
| 184 |
|
|---|
| 185 |
|
|---|
| 186 |
|
|---|
| 187 |
|
|---|
| 188 |
|
|---|
| 189 |
Action* QLearner::getRandomMove(vector<Action*> possibleActions) const { |
|---|
| 190 |
|
|---|
| 191 |
vector<double> weights(possibleActions.size()); |
|---|
| 192 |
for (unsigned int num = 0; num < possibleActions.size(); num++) { |
|---|
| 193 |
weights[num] = 1.0; |
|---|
| 194 |
} |
|---|
| 195 |
|
|---|
| 196 |
|
|---|
| 197 |
return getRandomMove(possibleActions, weights); |
|---|
| 198 |
} |
|---|
| 199 |
|
|---|
| 200 |
|
|---|
| 201 |
|
|---|
| 202 |
|
|---|
| 203 |
|
|---|
| 204 |
|
|---|
| 205 |
|
|---|
| 206 |
|
|---|
| 207 |
|
|---|
| 208 |
|
|---|
| 209 |
Action* QLearner::getRandomMove(vector<Action*> possibleActions, |
|---|
| 210 |
vector<double> weights) const { |
|---|
| 211 |
|
|---|
| 212 |
unsigned int maxIndex = possibleActions.size(); |
|---|
| 213 |
if (weights.size() < possibleActions.size()) { |
|---|
| 214 |
maxIndex = weights.size(); |
|---|
| 215 |
} |
|---|
| 216 |
|
|---|
| 217 |
if (maxIndex == 0) { |
|---|
| 218 |
cerr << "Empty set of actions" << endl; |
|---|
| 219 |
exit(1); |
|---|
| 220 |
} |
|---|
| 221 |
|
|---|
| 222 |
|
|---|
| 223 |
double sum = 0; |
|---|
| 224 |
for (unsigned int i = 0; i < maxIndex; i++) { |
|---|
| 225 |
sum += weights[i]; |
|---|
| 226 |
} |
|---|
| 227 |
|
|---|
| 228 |
|
|---|
| 229 |
double randomNumber = sum * (rand() / (RAND_MAX + 1.0)); |
|---|
| 230 |
|
|---|
| 231 |
|
|---|
| 232 |
double curMax = 0; |
|---|
| 233 |
for (unsigned int i = 0; i < maxIndex; i++) { |
|---|
| 234 |
curMax += weights[i]; |
|---|
| 235 |
if (randomNumber <= curMax) { |
|---|
| 236 |
return possibleActions[i]; |
|---|
| 237 |
} |
|---|
| 238 |
} |
|---|
| 239 |
|
|---|
| 240 |
|
|---|
| 241 |
cerr << "No action was selected! curMax = " << curMax << " rand = " |
|---|
| 242 |
<< randomNumber << endl; |
|---|
| 243 |
exit(1); |
|---|
| 244 |
|
|---|
| 245 |
} |
|---|