private void BuildFromGenes() { _neurons = new List <NeatNeuron>(Chromosome.NeuronCount); for (var i = 0; i < Chromosome.NeuronCount; i++) { var neuron = new NeatNeuron { Activation = _neat.Neural.ActivationFunction }; _neurons.Add(neuron); } for (var i = 0; i < Chromosome.GeneCount; i++) { var gene = Chromosome.GetGeneAt(i); if (gene.Enabled) { var inConnection = new NeatNeuron.Connection(); inConnection.Weight = gene.Weight; inConnection.Recursive = gene.Recursive; inConnection.Neuron = _neurons[gene.From]; _neurons[gene.To].Connections.Add(inConnection); var outConnection = new NeatNeuron.Connection(); outConnection.Weight = gene.Weight; outConnection.Recursive = gene.Recursive; outConnection.Neuron = _neurons[gene.To]; outConnection.OutGoing = true; _neurons[gene.From].Connections.Add(outConnection); } } for (var i = 0; i < _neat.Structure.BiasNeuronCount; i++) { _neurons[i].Value = 1.0; } CategorizeNeuronsIntoLayers(); }
private void AddRandomConnection(InnovationCacher currentGeneration) { var neuronPair = GetTwoUnconnectedNeurons(); var fromNeuron = neuronPair[0]; var toNeuron = neuronPair[1]; var newConnectionGene = new NeatGene(_neat, _neurons.IndexOf(fromNeuron), _neurons.IndexOf(toNeuron)); if (fromNeuron.Layer > toNeuron.Layer) { if (!_neat.Structure.AllowRecurrentConnections) { throw new Exception("Illegal recurrent connection"); } newConnectionGene.Recursive = true; } currentGeneration.AssignHistory(newConnectionGene); var inConnection = new NeatNeuron.Connection(); inConnection.Recursive = newConnectionGene.Recursive; inConnection.Neuron = fromNeuron; inConnection.Weight = newConnectionGene.Weight; toNeuron.Connections.Add(inConnection); var outConnection = new NeatNeuron.Connection(); outConnection.Recursive = newConnectionGene.Recursive; outConnection.Neuron = toNeuron; outConnection.Weight = newConnectionGene.Weight; outConnection.OutGoing = true; fromNeuron.Connections.Add(outConnection); Chromosome.AppendGene(newConnectionGene); CategorizeNeuronsIntoLayers(); }