📄 simplenet.cpp
字号:
{ LOGGER(3, "Destroying the cube"); for(int x=-oR; x<=oR; x++){ for(int y=-oR; y<=oR; y++){ for(int z=-oR; z<=oR; z++) { GrowNeuron *gn = cube[x+oR][y+oR][z+oR]; if(gn) delete gn; } delete [] cube[x+oR][y+oR]; } delete [] cube[x+oR]; } delete [] cube;}void SimpleNet::AddInputNeuron(float phi, float psi, AmIdInt id){ if(id > 255) throw runtime_error("IDs of input neurons must be <= 255"); std::string gene; gene.push_back((signed char)(phi/FACTOR)); gene.push_back((signed char)(psi/FACTOR)); gene.push_back((unsigned char)id); inputChromosome.push_back(gene);}void SimpleNet::AddOutputNeuron(float phi, float psi, AmIdInt id){ if(id > 255) throw runtime_error("IDs of output neurons must be <= 255"); std::string gene; gene.push_back((signed char)(phi/FACTOR)); gene.push_back((signed char)(psi/FACTOR)); gene.push_back((unsigned char)id); outputChromosome.push_back(gene);}void SimpleNet::Upload(string uri){ Genome g(uri); MakeIOChromosome(inputChromosome, g); MakeIOChromosome(outputChromosome, g); g.MakeChromosome(20, 5, 'v'); // Sensitivities g.MakeChromosome(20, 5, 'v'); // Attractors try { g.Upload(); } catch (runtime_error e) { std::cout << e.what() << std::endl; throw e; }}void SimpleNet::FinishParsing(){ LOGGER(3, "Finished parsing the genome"); MakeCube();}bool SimpleNet::Step(){ LOGGER(2, "Doing one step of axon growth"); bool lastStep = true; for (cube_iterator it = begin(); it != end(); it++){ GrowNeuron *growNeuron = *it; if(growNeuron->GrowAxon(attractors)){ lastStep = false; } } for(neuron_iterator it = input_begin(); it != input_end(); it++){ GrowNeuron *growNeuron = *it; if(growNeuron->GrowAxon(attractors)){ lastStep = false; } } // Output neurons don't grow axons anyway so they're left out if (lastStep) { MakeInhibitories(); OutputManager::EnableOutput(true, 1); LOGGER(3, "morphogenesis done"); return false; } return true;}void SimpleNet::MakeEmitter(Emitter & emitter, const string & gene){ const float factor = outerRadius / 127.; emitter.type = ((unsigned char)gene[0]) & 0x7; // only the last 3 bit emitter.strength = ((float)(unsigned char)gene[1]) / 256.; emitter.x = factor * (float)(signed char)gene[2]; emitter.y = factor * (float)(signed char)gene[3]; emitter.z = factor * (float)(signed char)gene[4]; LOGGER(2, "Made an emitter of type " << (int)emitter.type << " strength " << emitter.strength);}void SimpleNet::MakeIONeuron(const std::string & gene, int type){ float phi = FACTOR * (float)(signed char)gene[0]; float psi = FACTOR * (float)(signed char)gene[1]; float x = oR * sin(phi) * cos(psi); float y = oR * sin(phi) * sin(psi); float z = oR * cos(phi); AmIdInt id = (AmIdInt)(unsigned char)gene[2]; if(id>maxId) maxId = id; GrowNeuron *sNNeuron = new GrowNeuron(this, oR, net); if(type == INPUTNEURON){ string nType = "InputNeuron"; LOGGER(3, "Making InputNeuron " << id << " at location " << x << " " << y << " " << z); InputNeuronProperties iProps(1); iProps.GetPhysicalProps()->SetPosition(x, y, z); PhysicalProperties::RGB color = {0, 88, 188}; iProps.GetPhysicalProps()->SetBodyColor(color); NFactory * iFactory = dynamic_cast<NFactory*> (FactoryBase::GetRegistry().GetFactory(nType)); if(iFactory == NULL) throw runtime_error("Cannot get a NeuronFactory for input"); Neuron * n = iFactory->MakeNeuron(id, &iProps, top); if(n == NULL) throw runtime_error(string("Creating InputNeuron id ") + Utilities::itostr(id) + "failed"); sNNeuron->InitAxon(sensitivities, n); inputNeurons.push_back(sNNeuron); OutputManager::AddNeuronToGroup(n, 2); } else { string nType = "BasicNeuron"; LOGGER(3, "Making OutputNeuron " << id << " at location " << x << " " << y << " " << z); BasicNeuronProperties bProps(1); bProps.GetPhysicalProps()->SetPosition(x, y, z); PhysicalProperties::RGB color = {120, 0, 20}; bProps.GetPhysicalProps()->SetBodyColor(color); NFactory * iFactory = dynamic_cast<NFactory*> (FactoryBase::GetRegistry().GetFactory(nType)); if(iFactory == NULL) throw runtime_error("Cannot get a NeuronFactory for output"); Neuron * n = iFactory->MakeNeuron(id, &bProps, top); if(n == NULL) throw runtime_error(string("Creating OutputNeuron id ") + Utilities::itostr(id) + "failed"); sNNeuron->InitAxon(sensitivities, n); sNNeuron->Growing(false); outputNeurons.push_back(sNNeuron); OutputManager::AddNeuronToGroup(n, 1); }}void SimpleNet::Gene(const string & gene, AmIdInt chromosomeId){ Emitter emitter; switch (chromosomeId) { case 0: // fixed chromosome for input neurons MakeIONeuron(gene, INPUTNEURON); break; case 1: // fixed chromosome for output neurons MakeIONeuron(gene, OUTPUTNEURON); break; case 2: // sensitivities MakeEmitter(emitter, gene); sensitivities.push_back(emitter); break; case 3: // attractors MakeEmitter(emitter, gene); attractors.push_back(emitter); break; default: throw runtime_error("Unexpected chromosome"); break; }}void SimpleNet::MakeCube(){ BasicNeuronProperties bProps(1); maxId += 10; // make a gap which can be seen in the spikeplot LOGGER(1, "Making neurons, radius of sphere is " << oR << " within " << outerRadius << " hollow = " << innerRadius); cube = new GrowNeuron ***[2*oR+1]; // iterate through a qube of length 2*outerRadius for(int x=-oR; x<=oR; x++){ cube[x+oR] = new GrowNeuron**[2*oR+1]; for(int y=-oR; y<=oR; y++){ cube[x+oR][y+oR] = new GrowNeuron*[2*oR+1]; for(int z=-oR; z<=oR; z++){ float r = CDIAG(x, y, z); if(r<=outerRadius && r>=innerRadius){ bProps.GetPhysicalProps()->SetPosition(float(x), float(y), float(z)); GrowNeuron *sNNeuron = new GrowNeuron(this, oR, net); Neuron * n = dynamic_cast<Neuron*> (neuronFactory->MakeNeuron(maxId++, &bProps, top)); sNNeuron->InitAxon(sensitivities, n); cube[x+oR][y+oR][z+oR] = sNNeuron; OutputManager::AddNeuronToGroup(n, 3); } else { cube[x+oR][y+oR][z+oR] = NULL; } } } }}void SimpleNet::MakeIOChromosome (std::vector<std::string> & neurons, Amygdala::Genome & g) { g.StartChromosome('c'); for(std::vector<std::string>::const_iterator it=neurons.begin(); it != neurons.end(); it++){ g.AddGene(*it); }}std::vector < AmIdInt > SimpleNet::GetInputIDs(){ std::vector < AmIdInt > input; for (std::vector <GrowNeuron *>::const_iterator it = inputNeurons.begin(); it != inputNeurons.end(); it++){ input.push_back((*it)->GetNeuron()->GetId()); } return input;}std::vector < AmIdInt > SimpleNet::GetOutputIDs(){ std::vector < AmIdInt > output; for (std::vector <GrowNeuron *>::const_iterator it = outputNeurons.begin(); it != outputNeurons.end(); it++){ output.push_back((*it)->GetNeuron()->GetId()); } return output;}void SimpleNet::MakeInhibitories(){ ConnectorRegistry & cr = ConnectorRegistry::GetRegistry(); NConnector * connector = cr.GetConnector("StaticSynapse"); StaticSynapseProperties * sProps = dynamic_cast <StaticSynapseProperties *> (connector->GetDefaultProperties()); sProps->SetDelay(1000); sProps->SetWeight(-0.5); for(int x=-oR; x<=oR; x++){ for(int y=-oR; y<=oR; y++){ for(int z=-oR; z<=oR; z++){ GrowNeuron *gPre = cube[x+oR][y+oR][z+oR]; if(gPre == NULL)continue; for(int xp = x-1; xp<=x+1; xp++){ if(xp < -oR || xp > oR) continue; for(int yp = y-1; yp<=y+1; yp++){ if(yp < -oR || yp > oR) continue; for(int zp = z-1; zp<=z+1; zp++){ if(zp < -oR || zp > oR) continue; GrowNeuron * gPost = cube[xp+oR][yp+oR][zp+oR]; if(gPost == NULL || gPre == gPost)continue; connector->Connect(gPre->GetNeuron(), static_cast <SpikingNeuron*>(gPost->GetNeuron()), *sProps); } } } } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -