MCPcopy Create free account
hub / github.com/FidoProject/Fido / applyReinforcementToLastAction

Method applyReinforcementToLastAction

src/FidoControlSystem.cpp:24–89  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

22}
23
24void FidoControlSystem::applyReinforcementToLastAction(double reward, State newState) {
25 // SGD on current scenario
26 std::vector<Wire> newContolWires = newControlWiresForHistory(History(lastState, newState, lastAction, reward));
27
28 // Calculate uncertainty and adjust exploration
29 double uncertainty = getError(lastState, getRawOutput(newContolWires));
30 adjustExploration(uncertainty);
31 lastUncertainty = uncertainty;
32
33 // History sample and resize nn
34 histories.push_back(History(lastState, newState, lastAction, reward));
35 std::vector<History> selectedHistories = selectHistories();
36
37 std::cout << "----------RESIZING-----------\n";
38 if(selectedHistories.size() > 3) {
39 bool didChange = false;
40 net::Pruner pruner;
41 net::NeuralNet originalNet;
42 int iter = 0;
43 while(iter < 1) {
44 iter++;
45 originalNet = net::NeuralNet(network);
46
47 // Randomize
48 for(net::Layer &l : network->net) {
49 for(net::Neuron &n : l.neurons) {
50 n.randomizeWeights();
51 }
52 }
53
54 double totalCurrentError = trainOnHistories(selectedHistories, 0.001, 1);
55
56 if(network->numberOfHiddenNeurons() > 4) {
57 pruner.pruneRandomnly(network);
58 double totalPrunedError = trainOnHistories(selectedHistories, 0.001, 1);
59 if(totalPrunedError*1.05 < totalCurrentError) {
60 didChange = true;
61 continue;
62 }
63 }
64
65 *network = originalNet;
66 network->net[0].neurons.push_back(net::Neuron(network->numberOfInputs()));
67 for(net::Neuron &n : network->net[1].neurons) {
68 n.weights.push_back(1);
69 n.randomizeWeights();
70 }
71 double totalAddedError = trainOnHistories(selectedHistories, 0.001, 1);
72 if(totalAddedError*1.05 < totalCurrentError) {
73 didChange = true;
74 continue;
75 }
76
77 *network = originalNet;
78 break;
79 }
80
81 if(didChange == false) {

Callers

nothing calls this directly

Calls 8

HistoryClass · 0.85
NeuronClass · 0.85
numberOfHiddenNeuronsMethod · 0.80
pruneRandomnlyMethod · 0.80
numberOfInputsMethod · 0.80
NeuralNetClass · 0.50
sizeMethod · 0.45
randomizeWeightsMethod · 0.45

Tested by

no test coverage detected