/// <summary> /// Construct an instance of a Neuron. This should only be called from the /// constructor of the NeuralLayer that will own this Neuron. /// </summary> /// <param name="ParentNeuralLayer">A reference to the parent NeuralLayer that constructed this Neuron.</param> internal Neuron(NeuralLayer ParentNeuralLayer, UInt32 UniqueID) { this.UniqueID = UniqueID; InputConnections = new List <Dendrite>(); OutputConnections = new List <Dendrite>(); this.ParentNeuralLayer = ParentNeuralLayer; }
public bool Equals(NeuralLayer other) { bool Equals = (LayerIndex == other.LayerIndex); for (int iter = 0; (iter < NeuronsInLayer.Count && Equals); iter += 1) { Equals &= NeuronsInLayer[iter].Equals(other.NeuronsInLayer[iter]); } return(Equals); }
internal NeuralLayer GetNextNeuralLayer() { NeuralLayer ReturnValue = default(NeuralLayer); if (ParentNeuralNet.NeuralLayers.ContainsKey((UInt16)(LayerIndex + 1))) { ReturnValue = ParentNeuralNet.NeuralLayers[(UInt16)(LayerIndex + 1)]; } return(ReturnValue); }
/// <summary> /// Connect this Neuron to each of the Neurons in the next layer. /// </summary> internal void Connect() { NeuralLayer NextNeuralLayer = ParentNeuralLayer.GetNextNeuralLayer(); if (NextNeuralLayer != default(NeuralLayer)) { foreach (var NeuronToConnect in NextNeuralLayer.NeuronsInLayer) { OutputConnections.Add(new Dendrite(this, NeuronToConnect)); } } }
internal static NeuralNet Breed(NeuralNet Father, NeuralNet Mother) { NeuralNet Child = new NeuralNet(NeuralLayerInfo); for (UInt16 LayerIter = 0; LayerIter < NeuralLayerInfo.Count(); LayerIter += 1) { NeuralLayer CurrentLayer = Child.NeuralLayers[LayerIter]; for (int NeuronIter = 0; NeuronIter < CurrentLayer.NeuronsInLayer.Count(); NeuronIter += 1) { Neuron CurrentNeuron = CurrentLayer.NeuronsInLayer[NeuronIter]; for (int DendriteIter = 0; DendriteIter < CurrentNeuron.OutputConnections.Count(); DendriteIter += 1) { Dendrite CurrentDendrite = CurrentNeuron.OutputConnections[DendriteIter]; if (GlobalRandom.NextDouble() < Math.Min(MaxMutationRate, (BaseMutationRate + (MutationRateIncreasePerFailedGeneration * GensSinceLastImprv)))) { CurrentDendrite.SetNewRandomConnectionStrength(); } else { // Children are always random switch (BreedingType) { case EnumBreedingType.AlwaysRandom: CurrentDendrite.SetNewRandomConnectionStrength(); break; // Child dendrite connection strength is the average of the mother and father case EnumBreedingType.AverageValue: CurrentDendrite.ConnectionStrength = (Father.NeuralLayers[LayerIter].NeuronsInLayer[NeuronIter].OutputConnections[DendriteIter].ConnectionStrength + Mother.NeuralLayers[LayerIter].NeuronsInLayer[NeuronIter].OutputConnections[DendriteIter].ConnectionStrength) / 2; break; // Child takes fathers or mothers dendrite connection strength default: case EnumBreedingType.Human: CurrentDendrite.ConnectionStrength = (GlobalRandom.NextDouble() < 0.5) ? Father.NeuralLayers[LayerIter].NeuronsInLayer[NeuronIter].OutputConnections[DendriteIter].ConnectionStrength : Mother.NeuralLayers[LayerIter].NeuronsInLayer[NeuronIter].OutputConnections[DendriteIter].ConnectionStrength; break; // Child dendrite connection strength pulled up and down by agreement between father and mother case EnumBreedingType.WeightedPull: if (Father.NeuralLayers[LayerIter].NeuronsInLayer[NeuronIter].OutputConnections[DendriteIter].ConnectionStrength >= 0.5 && Mother.NeuralLayers[LayerIter].NeuronsInLayer[NeuronIter].OutputConnections[DendriteIter].ConnectionStrength >= 0.5) { CurrentDendrite.ConnectionStrength = (float)Math.Min(1.0, Math.Max( Father.NeuralLayers[LayerIter].NeuronsInLayer[NeuronIter].OutputConnections[DendriteIter].ConnectionStrength, Mother.NeuralLayers[LayerIter].NeuronsInLayer[NeuronIter].OutputConnections[DendriteIter].ConnectionStrength ) + 0.01); } else if (Father.NeuralLayers[LayerIter].NeuronsInLayer[NeuronIter].OutputConnections[DendriteIter].ConnectionStrength < 0.5 && Mother.NeuralLayers[LayerIter].NeuronsInLayer[NeuronIter].OutputConnections[DendriteIter].ConnectionStrength < 0.5) { CurrentDendrite.ConnectionStrength = (float)Math.Max(0, Math.Min( Father.NeuralLayers[LayerIter].NeuronsInLayer[NeuronIter].OutputConnections[DendriteIter].ConnectionStrength, Mother.NeuralLayers[LayerIter].NeuronsInLayer[NeuronIter].OutputConnections[DendriteIter].ConnectionStrength ) - 0.01); } else { CurrentDendrite.ConnectionStrength = (GlobalRandom.NextDouble() < 0.5) ? Father.NeuralLayers[LayerIter].NeuronsInLayer[NeuronIter].OutputConnections[DendriteIter].ConnectionStrength : Mother.NeuralLayers[LayerIter].NeuronsInLayer[NeuronIter].OutputConnections[DendriteIter].ConnectionStrength; } break; } } } } } return(Child); }