| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
#include "QNeuralNetwork.h" |
|---|
| 7 |
|
|---|
| 8 |
//#include "fann/fixedfann.h" |
|---|
| 9 |
#include <iostream> |
|---|
| 10 |
|
|---|
| 11 |
using namespace std; |
|---|
| 12 |
|
|---|
| 13 |
QNeuralNetwork::QNeuralNetwork() : NUM_HIDDEN_NEURONS(4) |
|---|
| 14 |
{ |
|---|
| 15 |
_ann = NULL; |
|---|
| 16 |
} |
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
QNeuralNetwork QNeuralNetwork::operator=(const QNeuralNetwork &theNetwork) |
|---|
| 27 |
{ |
|---|
| 28 |
QNeuralNetwork tempNetwork; |
|---|
| 29 |
tempNetwork._ann = theNetwork._ann; |
|---|
| 30 |
tempNetwork._actionLength = theNetwork._actionLength; |
|---|
| 31 |
tempNetwork._stateLength = theNetwork._stateLength; |
|---|
| 32 |
return tempNetwork; |
|---|
| 33 |
} |
|---|
| 34 |
|
|---|
| 35 |
QNeuralNetwork::QNeuralNetwork(int actionLength, int stateLength) : NUM_HIDDEN_NEURONS(4) |
|---|
| 36 |
{ |
|---|
| 37 |
_actionLength = actionLength; |
|---|
| 38 |
_stateLength = stateLength; |
|---|
| 39 |
_ann = fann_create_standard(3, actionLength+stateLength, NUM_HIDDEN_NEURONS, 1); |
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
fann_set_training_algorithm(_ann, FANN_TRAIN_INCREMENTAL); |
|---|
| 46 |
fann_set_learning_rate( _ann, .2); |
|---|
| 47 |
fann_set_learning_momentum(_ann, .3); |
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
fann_set_activation_steepness_hidden(_ann, .7); |
|---|
| 51 |
fann_set_activation_steepness_output(_ann, .5); |
|---|
| 52 |
|
|---|
| 53 |
} |
|---|
| 54 |
|
|---|
| 55 |
QNeuralNetwork::~QNeuralNetwork() |
|---|
| 56 |
{ |
|---|
| 57 |
fann_destroy(_ann); |
|---|
| 58 |
} |
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
void QNeuralNetwork::update(State &state, Action &action, double utility) |
|---|
| 72 |
{ |
|---|
| 73 |
vector<double> input_vec = state.toInputs(); |
|---|
| 74 |
vector<double> action_vec = action.toInputs(); |
|---|
| 75 |
|
|---|
| 76 |
int initSize = input_vec.size(); |
|---|
| 77 |
for(int i = initSize; i < _stateLength; i++) |
|---|
| 78 |
input_vec.push_back(0.0); |
|---|
| 79 |
|
|---|
| 80 |
input_vec.insert(input_vec.end(), action_vec.begin(), action_vec.end()); |
|---|
| 81 |
|
|---|
| 82 |
initSize = action_vec.size(); |
|---|
| 83 |
for(int i = initSize; i < _actionLength; i++) |
|---|
| 84 |
input_vec.push_back(0.0); |
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
fann_train(_ann, (fann_type*) &(input_vec)[0], (fann_type*) &utility); |
|---|
| 90 |
} |
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 |
double QNeuralNetwork::getValue(State &state, Action &action) |
|---|
| 104 |
{ |
|---|
| 105 |
fann_type *output; |
|---|
| 106 |
vector<double> input_vec = state.toInputs(); |
|---|
| 107 |
vector<double> action_vec = action.toInputs(); |
|---|
| 108 |
|
|---|
| 109 |
int initSize = input_vec.size(); |
|---|
| 110 |
for(int i = initSize; i < _stateLength; i++) |
|---|
| 111 |
input_vec.push_back(0.0); |
|---|
| 112 |
|
|---|
| 113 |
input_vec.insert(input_vec.end(), action_vec.begin(), action_vec.end()); |
|---|
| 114 |
|
|---|
| 115 |
initSize = action_vec.size(); |
|---|
| 116 |
for(int i = initSize; i < _actionLength; i++) |
|---|
| 117 |
input_vec.push_back(0.0); |
|---|
| 118 |
|
|---|
| 119 |
|
|---|
| 120 |
|
|---|
| 121 |
input_vec.insert(input_vec.end(), action_vec.begin(), action_vec.end()); |
|---|
| 122 |
output = fann_run(_ann, (fann_type*) &(input_vec)[0]); |
|---|
| 123 |
return output[0]; |
|---|
| 124 |
} |
|---|
| 125 |
|
|---|
| 126 |
|
|---|
| 127 |
|
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 |
|
|---|
| 132 |
|
|---|
| 133 |
|
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 |
void QNeuralNetwork::writeToFile(string filename) |
|---|
| 137 |
{ |
|---|
| 138 |
fann_save(_ann, &(filename[0])); |
|---|
| 139 |
} |
|---|
| 140 |
|
|---|
| 141 |
|
|---|
| 142 |
|
|---|
| 143 |
|
|---|
| 144 |
|
|---|
| 145 |
|
|---|
| 146 |
|
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 |
void QNeuralNetwork::readFromFile(string filename) |
|---|
| 151 |
{ |
|---|
| 152 |
_ann = fann_create_from_file(&(filename[0])); |
|---|
| 153 |
} |
|---|