| 107 | } |
| 108 | |
| 109 | double FidoControlSystem::trainOnHistories(std::vector<FidoControlSystem::History> selectedHistories, double allowedError, unsigned int maxIterations) { |
| 110 | |
| 111 | double totalError = DBL_MAX; |
| 112 | net::Adadelta tempTrainer = net::Adadelta(0.95, allowedError, 100); |
| 113 | unsigned int iter = 0; |
| 114 | do { |
| 115 | std::vector< std::vector<double> > input, correctOutput; |
| 116 | for(History history : selectedHistories) { |
| 117 | std::vector<Wire> historyControlWires = getWires(history.initialState); |
| 118 | double newRewardForLastAction = getQValue(history.reward, history.initialState, history.newState, history.action, historyControlWires); |
| 119 | |
| 120 | Wire correctHistoryWire = {history.action, newRewardForLastAction}; |
| 121 | std::vector<Wire> newContolWires = newControlWires(correctHistoryWire, historyControlWires); |
| 122 | |
| 123 | input.push_back(history.initialState); |
| 124 | correctOutput.push_back(getRawOutput(newContolWires)); |
| 125 | |
| 126 | tempTrainer.train(network, {input.back()}, {correctOutput.back()}); |
| 127 | } |
| 128 | |
| 129 | totalError = 0; |
| 130 | for(unsigned int a = 0; a < input.size(); a++) { |
| 131 | std::vector<Wire> wires = getWires(input[a]); |
| 132 | for(unsigned int b = 0; b < input[a].size(); b++) { |
| 133 | totalError += pow(selectedHistories[a].reward - interpolator->getReward(wires, selectedHistories[a].action), 2); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | std::cout << "Error: " << totalError << "\n"; |
| 138 | iter++; |
| 139 | } while (totalError > allowedError*selectedHistories.size() && iter < maxIterations); |
| 140 | |
| 141 | return totalError; |
| 142 | } |
| 143 | |
| 144 | void FidoControlSystem::adjustExploration(double uncertainty) { |
| 145 | explorationLevel = pow(uncertainty, 2) * 10000; |