📄 ann.cc
字号:
} else{ nrOfNeurons++; neurons = (class Neuron**) realloc(neurons,sizeof(class Neuron*)*nrOfNeurons); neurons[nrOfNeurons-1] = new Neuron(iLayerPriority, inType, iFuncT); } if(inType!=BIAS && inType!=INPUT){ neurons[nrOfNeurons-1]->createLink(neurons[0],0,NORMAL); } if(inType==INPUT){ addInNode(neurons[nrOfNeurons-1]); } if(inType==OUTPUT){ addOutNode(neurons[nrOfNeurons-1]); } if(inType==SCN){ addSCNNode(neurons[nrOfNeurons-1]); } sort();}/** * Adds a neuron to the network. * @attention Only use this method if you know what you are doing * @see Ann::addNeuron(int,NODE_T,ACTIVATION_T) * @param iLayerPriority The priority of the new neuron * @param inType The type of new neuron * @param iFuncT The activation function of the new neuron * @param if The unique id of the neuron */void Ann::addNeuron(int iLayerPriority, NODE_T inType, ACTIVATION_T iFuncT, long int id){ if(nrOfNeurons==0){ neurons = (class Neuron**) malloc(sizeof(class Neuron*)); neurons[0]= new Neuron(iLayerPriority, inType, iFuncT, id); nrOfNeurons++; } else{ nrOfNeurons++; neurons = (class Neuron**) realloc(neurons,sizeof(class Neuron*)*nrOfNeurons); neurons[nrOfNeurons-1] = new Neuron(iLayerPriority, inType, iFuncT, id); } if(inType==INPUT){ addInNode(neurons[nrOfNeurons-1]); } if(inType==OUTPUT){ addOutNode(neurons[nrOfNeurons-1]); } if(inType==SCN){ addSCNNode(neurons[nrOfNeurons-1]); } sort();}/** * Compare the priority of two neurons. Used for various quicksorts. * @param neuron1 Neuron A * @param neuron2 Neuron B * @return \f$ (A-B) \f$ */int comparePri(const void *neuron1, const void *neuron2){ return (*(Neuron**)neuron1)->getPriority() -(*(Neuron**)neuron2)->getPriority();}/** * Sort the internal neurons in activation order */void Ann::sort(){ // if(NULL!=actOrder) free(actOrder); actOrder = (Neuron**)malloc(sizeof(Neuron*)*nrOfNeurons); for(int i=0; i < nrOfNeurons; i++) actOrder[i]=neurons[i]; qsort(actOrder,nrOfNeurons,sizeof(Neuron*),comparePri);}/** * Set the activation of a input neuron. * @param index The index of the input neuron [0..nrOfInputs] * @param value The new activation value of the neuron */void Ann::setInput(int index, double value){ if(index < nrOfInputs) inNodes[index]->setActivationValue(value);}/** * Get the activation of a input neuron. * @param index The index of the input neuron [0..nrOfInputs] * @return The activation value of the input neuron or -1 if index is out of range. */double Ann::getInput(int index) const{ if(index < nrOfInputs) return inNodes[index]->getActivationValue(); return(-1);}/** * Get the output of a output neuron. * @param index The index of the output neuron [0..nrOfOutputs] * @return The activation value of the output neuron or -1 if index is out of range. */double Ann::getOutput(int index) const{ if(index < nrOfOutputs) return outNodes[index]->getActivationValue(); return -1;}/** * Get a activation pointer to a output neuron. * @param index The index of the output neuron [0..nrOfOutputs] * @return The activation value pointer or NULL if index is out of range. */double *Ann::getOutputPtr(int index){ if(index < nrOfOutputs) return outNodes[index]->activationPointer(); return NULL;}/** * Add a help pointer to a input neuron * @param The newly created input neuron */void Ann::addInNode(class Neuron *node){ if(nrOfInputs==0){ inNodes = (class Neuron**) malloc(sizeof(class Neuron*)); inNodes[0] = node; nrOfInputs++; }else{ inNodes = (class Neuron**) realloc(inNodes,sizeof(class Neuron*)*++nrOfInputs); inNodes[nrOfInputs-1]=node; }}/** * Add a help pointer to a output neuron * @param The newly created output neuron */void Ann::addOutNode(class Neuron *node){ if(nrOfOutputs==0){ outNodes = (class Neuron**) malloc(sizeof(class Neuron*)); outNodes[0] = node; nrOfOutputs++; }else{ outNodes = (class Neuron**) realloc(outNodes,sizeof(class Neuron*)*++nrOfOutputs); outNodes[nrOfOutputs-1]=node; }}/** * Add a help pointer to a SCN neuron * @param The newly created SCN neuron */void Ann::addSCNNode(class Neuron *node){ if(nrOfSCNs==0){ SCNNodes = (class Neuron**) malloc(sizeof(class Neuron*)); SCNNodes[0] = node; nrOfSCNs++; }else{ SCNNodes = (class Neuron**) realloc(SCNNodes,sizeof(class Neuron*)*++nrOfSCNs); SCNNodes[nrOfSCNs-1]=node; }}/** * Removes a neuron from the ANN. Removes all links to the neuron and from the neuron. * @param The internal index of the neuron to remove. */void Ann::removeNeuron(int index){ int i; if(index==(nrOfNeurons-1)){ for(i=0; i < nrOfNeurons; i++){ neurons[i]->removeLinksTo(neurons[index]); } delete(neurons[index]); neurons = (class Neuron**) realloc(neurons,sizeof(class Neuron*)*--nrOfNeurons); } else{ for(i=0; i < nrOfNeurons; i++){ neurons[i]->removeLinksTo(neurons[index]); } delete(neurons[index]); for(i=index; i < (nrOfNeurons-1); i++){ neurons[i]=neurons[i+1]; } neurons = (class Neuron**) realloc(neurons,sizeof(class Neuron*)*--nrOfNeurons); }}/** * Create a link between two neurons with an inital weight * @param from The neuron where the links comes from * @param to The target of the link. * @param initWeight The inital weight of the link */void Ann::connectNeurons(class Neuron *from, class Neuron *to,int initWeight){ to->createLink(from,initWeight,NORMAL);}/** * Removes a link that goes from a neuron to a neuron. * @param from The neuron where the links comes from * @param to The target of the link. */void Ann::removeConnection(class Neuron *from, class Neuron *to){ to->removeLinksTo(from);}/** * Activate and propagate the network. * */void Ann::activateNet(){ for(int i=1; i < nrOfNeurons; i++){ /* First node is BIAS node */ actOrder[i]->activate(); }}/** * Reset all activation in the network to zero. * @attention The bias node (index 0) remains untouched * */void Ann::resetNet(){ for(int i=1; i < nrOfNeurons; i++){ /* First node is BIAS node */ neurons[i]->setActivationValue(0.0); }}/** * Get a pointer to a input neurons activation value * @param index The internal input index of the neuron. */double *Ann::getInputPointer(int index){ return inNodes[index]->activationPointer();}/** * Connect two neuron layers (nodes with the same priority) using a third layer whichs output should become the weights of the connection. * @attention Some sane tests will take place and exit will be called if they fail. * @param from The priority of the neurons where the links comes from * @param to The priority of the target layer for the links. * @param scPri The priority of the SCN layer which should be used. * */void Ann::connectLayerWithSCN(int fromPri, int toPri, int scnPri){ int SCNsRequired; int freeSCNs; int scnI; SCNsRequired = countNodesWithPri(toPri) * countNodesWithPri(fromPri); if(SCNsRequired==0){ fprintf(stderr," Ann::connectLayerWithSCN there are no nodes with priority specified with fromPri and/or toPri\n"); exit(-1); } freeSCNs = countUnUsedSCNNodesWithPri(scnPri); if(SCNsRequired > freeSCNs){ fprintf(stderr," Ann::connectLayerWithSCN You have not created enough SCN neurons\n"); exit(-1); } // Everything is ok - time to connect SCN nodes with links for(int i=0; i <nrOfNeurons; i++){ if(neurons[i]->getPriority() == fromPri){ for(int q=0; q < nrOfNeurons; q++){ if(neurons[q]->getPriority() == toPri){ scnI = nextUnUsedSCNNodeWithPri(scnPri); if(scnI>= 0){ neurons[q]->createSCNLink(neurons[i],SCNNodes[scnI]); } else{ fprintf(stderr," Ann::connectLayerWithSCN This error shouldnt occur\n"); fprintf(stderr," Ann::connectLayerWithSCN Something is wrong with the SCN code\n"); exit(-1); } } } } }}/** * Connect/link one priority layer with another. * @param fromPri The priority of the from layer. * @param toPri The priority of the layer which the links goes to. */void Ann::connectLayer(int fromPri, int toPri){ for(int i=0; i <nrOfNeurons; i++){ if(neurons[i]->getPriority() == fromPri){ for(int q=0; q < nrOfNeurons; q++){ if(neurons[q]->getPriority() == toPri) connectNeurons(neurons[i],neurons[q],0); } } }}/** * Get the number of neurons of a given priority. * @param cntPri The priority to look for. * @return The number of neurons found with the given priority. */int Ann::countNodesWithPri(int cntPri) const{ int count = 0; for(int i=0; i < nrOfNeurons; i++){ if(neurons[i]->getPriority()==cntPri) count++; } return count;}/** * Get the number of SCN neurons of a given priority. * @param scnPri The priority to look for. * @return The number of SCN neurons found with the given priority. */int Ann::countSCNNodesWithPri(int scnPri) const{ int count = 0; for(int i=0; i < nrOfSCNs; i++){ if(SCNNodes[i]->getPriority()==scnPri) count++; } return count;}/** * Get the number of unused SCN neurons of a given priority. * That is SCN neurons not connected to a weight. * @param scnPri The priority to look for. * @return The number of unused SCN neurons found with the given priority. */int Ann::countUnUsedSCNNodesWithPri(int scnPri) const{ int count = 0; for(int i=0; i < nrOfSCNs; i++){ if(SCNNodes[i]->getPriority()==scnPri && SCNNodes[i]->SCNNotUsed()) count++; } return count;}/** * Get the index of the next unused SCN neuron with a given priority. * @param scnPri The priority to look for. * @return The index of the neuron or -1 if no such neuron was found. */int Ann::nextUnUsedSCNNodeWithPri(int scnPri) const{ int index=0; for(index = 0; index < nrOfSCNs; index++){ if(SCNNodes[index]->getPriority()==scnPri && SCNNodes[index]->SCNNotUsed()) break; } if(nrOfSCNs>0) return index; else return -1;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -