Created
July 4, 2016 19:40
-
-
Save zoq/181c036cb4b903f8e7cb2977639ee6b8 to your computer and use it in GitHub Desktop.
testing_Activate.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void printGenes(std::vector<NeuronGene>& genes) | |
{ | |
for (size_t i = 0; i < genes.size(); ++i) | |
{ | |
std::cout << "Id: " << genes[i].Id() << std::endl; | |
std::cout << "Type: " << genes[i].Type() << std::endl; | |
std::cout << "Input: " << genes[i].Input() << std::endl; | |
std::cout << "Activation: " << genes[i].Activation() << std::endl; | |
std::cout << "Depth: " << genes[i].Depth() << std::endl; | |
std::cout << "..................................\n"; | |
} | |
} | |
void printLinks(std::vector<LinkGene>& links) | |
{ | |
for (size_t i = 0; i < links.size(); ++i) | |
{ | |
std::cout << "Id: " << links[i].FromNeuronId() << std::endl; | |
std::cout << "FromNeuronId: " << links[i].FromNeuronId() << std::endl; | |
std::cout << "ToNeuronId: " << links[i].ToNeuronId() << std::endl; | |
std::cout << "Weight: " << links[i].Weight() << std::endl; | |
std::cout << "Enabled: " << links[i].Enabled() << std::endl; | |
std::cout << "..................................\n"; | |
} | |
} | |
// Activate genome. The last dimension of input is always 1 for bias. 0 means no bias. | |
void Activate(std::vector<double>& input) { | |
assert(input.size() == aNumInput); | |
SortLinkGenes(); | |
// Set all neurons' input to be 0. | |
for (ssize_t i=0; i<NumNeuron(); ++i) { | |
aNeuronGenes[i].Input(0); | |
} | |
std::cout << "===============================================\n"; | |
printGenes(aNeuronGenes); | |
std::cout << "-----------------------------------------------\n"; | |
printLinks(aLinkGenes); | |
std::cout << "===============================================\n"; | |
// Set input neurons. | |
for (ssize_t i=0; i<aNumInput; ++i) { | |
aNeuronGenes[i].Activation(input[i]); // assume INPUT, BIAS, OUTPUT, HIDDEN sequence | |
} | |
std::cout << "===============================================\n"; | |
printGenes(aNeuronGenes); | |
std::cout << "-----------------------------------------------\n"; | |
printLinks(aLinkGenes); | |
std::cout << "===============================================\n"; | |
// Activate hidden and output neurons. | |
for (ssize_t i = 0; i < NumLink(); ++i) { | |
if (aLinkGenes[i].Enabled()) { | |
ssize_t toNeuronIdx = GetNeuronIndex(aLinkGenes[i].ToNeuronId()); | |
ssize_t fromNeuronIdx = GetNeuronIndex(aLinkGenes[i].FromNeuronId()); | |
double input = aNeuronGenes[toNeuronIdx].Input() + | |
aNeuronGenes[fromNeuronIdx].Activation() * aLinkGenes[i].Weight(); | |
std::cout << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"; | |
std::cout << "from: " << aNeuronGenes[fromNeuronIdx].Id() << std::endl; | |
std::cout << "to: " << aNeuronGenes[toNeuronIdx].Id() << std::endl; | |
std::cout << "aNeuronGenes[toNeuronIdx].Input(): " << aNeuronGenes[toNeuronIdx].Input() << std::endl; | |
std::cout << "aNeuronGenes[fromNeuronIdx].Activation(): " << aNeuronGenes[fromNeuronIdx].Activation() << std::endl; | |
std::cout << "aLinkGenes[i].Weight(): " << aLinkGenes[i].Weight() << std::endl; | |
std::cout << "input: " << input << std::endl; | |
std::cout << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"; | |
aNeuronGenes[toNeuronIdx].Input(input); | |
if (i == NumLink() - 1) { | |
std::cout << "output id: " << aNeuronGenes[toNeuronIdx].Id() << std::endl; | |
aNeuronGenes[toNeuronIdx].CalcActivation(); | |
std::cout << "activation: " << aNeuronGenes[toNeuronIdx].Activation() << std::endl; | |
} else if (GetNeuronIndex(aLinkGenes[i + 1].ToNeuronId()) != toNeuronIdx) { | |
aNeuronGenes[toNeuronIdx].CalcActivation(); | |
std::cout << "activation: " << aNeuronGenes[toNeuronIdx].Activation() << std::endl; | |
} | |
} | |
} | |
std::cout << "===============================================\n"; | |
printGenes(aNeuronGenes); | |
std::cout << "-----------------------------------------------\n"; | |
printLinks(aLinkGenes); | |
std::cout << "===============================================\n"; | |
exit(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment