AddNewNeuronLinks(int linkIndex, CInnovation innovations) { float initialWeight = m_vecLinks[linkIndex].Weight; int from = m_vecLinks[linkIndex].FromNeuron; int to = m_vecLinks[linkIndex].ToNeuron; SNeuronGene fromNeuron = m_vecNeurons[GetNeuronIndexById(from)]; SNeuronGene toNeuron = m_vecNeurons[GetNeuronIndexById(from)]; // The depth of the neuron is used to determine if the new link // feeds backwards (i.e it is a recurrent link) or forwards. float newDepth = (fromNeuron.SplitY + toNeuron.SplitY) / 2; float newWidth = (fromNeuron.SplitX + toNeuron.SplitX) / 2; int innovationID = innovations.CheckInnovation(from, to, InnovationType.NewNeuron); bool alreadyUsed = innovationID >= 0 && UsingNeuronID(innovations.GetNeuronID(innovationID)); if (innovationID < 0 || alreadyUsed) { AddNeuronNewInnovation(innovations, from, to, newWidth, newDepth, initialWeight); } else { int newNeuronID = innovations.GetNeuronID(innovationID); AddNeuronExistingInnovation(innovations, newNeuronID, from, to, newWidth, newDepth, initialWeight); } }
AddLoopedRecurrentLink(out int neuron1_ID, out int neuron2_ID, int numTriesToFindLoop) { // Attempt numTriesToFindLoop times to find a neuron that has no // loopback connection and is either a hidden or output neuron. while (numTriesToFindLoop-- > 0) { int index = Random.Range(0, m_vecNeurons.Count); SNeuronGene randNeuron = m_vecNeurons[index]; if (!randNeuron.IsRecurrent && (randNeuron.Type == NeuronType.Hidden || randNeuron.Type == NeuronType.Output)) { neuron1_ID = neuron2_ID = randNeuron.ID; randNeuron.IsRecurrent = true; return(true); } } neuron1_ID = neuron2_ID = -1; return(false); }
SNeuronGene(SNeuronGene original) { ID = original.ID; Type = original.Type; IsRecurrent = original.IsRecurrent; SplitX = original.SplitX; SplitY = original.SplitY; ActivationResponse = original.ActivationResponse; }
MutateActivationResponse(float mutationRate, float maxPertubation) { for (int i = 0; i < m_vecNeurons.Count; i++) { SNeuronGene neuron = m_vecNeurons[i]; if (Random.value < mutationRate) { neuron.ActivationResponse += Random.Range(-1.0f, 1.0f) * maxPertubation; } } }
GetNeuronIndexById(int id) { for (int i = 0; i < m_vecNeurons.Count; i++) { SNeuronGene neuron = m_vecNeurons[i]; if (neuron.ID == id) { return(i); } } return(-1); }
CreatePhenotype(int depth) { // Delete previous phenotype assigned to this genome. DeletePhenotype(); List <SNeuron> neurons = new List <SNeuron> (); // Add the neuron genes to the phenotype neurons. for (int i = 0; i < m_vecNeurons.Count; i++) { SNeuronGene neuron = m_vecNeurons[i]; neurons.Add(new SNeuron(neuron.Type, neuron.ID, neuron.SplitY, neuron.SplitX, neuron.ActivationResponse)); } // Create the links for the phenotype. for (int i = 0; i < m_vecLinks.Count; i++) { SLinkGene link = m_vecLinks[i]; // Ignore the disabled links if (link.Enabled) { SNeuron fromNeuron = neurons[GetNeuronIndexById(link.FromNeuron)]; SNeuron toNeuron = neurons[GetNeuronIndexById(link.ToNeuron)]; SLink newLink = new SLink(link.Weight, fromNeuron, toNeuron, link.IsRecurrent); fromNeuron.linksFrom.Add(newLink); toNeuron.linksTo.Add(newLink); } } m_Phenotype = new CNeuralNet(neurons, depth); return(m_Phenotype); }