/// <summary> /// Calculates the current activity, and then sets the result acording to /// the ActivationFunction. /// </summary> public virtual void Propogate() { //calculate the current input activity this.Activity = 0; IEnumerator SynapseEnum = this.SourceSynapses.GetEnumerator(); while (SynapseEnum.MoveNext()) { if (SynapseEnum.Current is Synapse) { Synapse CurrentSynapse = SynapseEnum.Current as Synapse; this.Activity += CurrentSynapse.CalculateOutput(); } else { throw new Exception("SourceSynapses should only contain synapses"); } } //Add the bias to the activity this.Activity += this.BiasWeight; //calculate the activity function and set the result as the output this.Output = this.ActivationFunction(); //check if it needs to be restrivted to byte resolution if (this.ByteResolution == true) { this.Output = (Math.Floor((this.Output + 1) * (((double)256) / ((double)2))) / (((double)256) / ((double)2))) - 1; } }
/// <summary> /// Called from the ConnectToNeuron of the source neuron to add the synapse as a source. /// </summary> /// <param name="ToConnectFrom">The new synapse to add as a source.</param> protected virtual void ConnectFromSynapse(Synapse ToConnectFrom) { //make sure you arent already connected fromt his neuron //add the synapse to the source list this.SourceSynapses.Add(ToConnectFrom); }
/// <summary> /// Call this method to cause this neuron to start the process of connecting /// to another neuron. /// </summary> /// <param name="ToConnectTo">The neuron to connect to.</param> public virtual void ConnectToNeuron(Neuron ToConnectTo) { //make sure you arent already connected to the neuron if (ToConnectTo == null) { throw new Exception("Cant connect to a null pointer"); } //connect to the neuron //double NewWeight = (this.OwnedDNA.RandomGenerator.NextDouble() * 2 - 1) * ToConnectTo.GetLeastSignificantSourceWeight() * 0.1; //double NewWeight = (this.OwnedDNA.RandomGenerator.NextDouble() * 2 - 1) * this.OwnedDNA.MinimumWeight; //ToConnectTo.CalculateSourceWeightTotal(); double NewWeight = 0; if ((ToConnectTo.SourceWeightTotal != 0) && (ToConnectTo.SourceSynapses.Count > 0)) { NewWeight = ToConnectTo.SourceWeightTotal * (((double)1) / ((double)ToConnectTo.SourceSynapses.Count)) * this.OwnedDNA.ConnectPercentageAverage * (this.OwnedDNA.RandomGenerator.NextDouble() * 2 - 1); } else { NewWeight = this.OwnedDNA.ConnectPercentageAverage * (this.OwnedDNA.RandomGenerator.NextDouble() * 2 - 1); } if (NewWeight == 0) { NewWeight = Double.Epsilon; } Synapse NewSynapse = new Synapse(this, ToConnectTo, NewWeight); this.DestinationSynapses.Add(NewSynapse); ToConnectTo.ConnectFromSynapse(NewSynapse); }
public double[] GetSourceWeightValues() { double[] RetVal = new double[this.SourceSynapses.Count + 1]; //first add the bias weight int RetValIndex = 0; RetVal[0] = this.BiasWeight; RetValIndex++; //update SourceWeightTotal this.CalculateSourceWeightTotal(); //iterate thru and add source synapse weights IEnumerator SynapseEnum = this.SourceSynapses.GetEnumerator(); while (SynapseEnum.MoveNext()) { if (SynapseEnum.Current is Synapse) { Synapse CurrentSynapse = SynapseEnum.Current as Synapse; //RetVal[RetValIndex] = CurrentSynapse.Weight; RetVal[RetValIndex] = CurrentSynapse.GetSignificanceDiviation(); RetValIndex++; } else { throw new Exception("Only synapses should be int he sourceSynapses array"); } } return(RetVal); }
/// <summary> /// removes the synapse from the list. called from the DisconnectDestinationSynapse /// of the neuron sharing the synapse /// </summary> /// <param name="ToDisconnect">Synapse to remove from source list.</param> private void RemoveSourceSynapse(Synapse ToDisconnect) { if (this.SourceSynapses.Contains(ToDisconnect) == false) { throw new Exception("Synapse is not a source synapse"); } this.SourceSynapses.Remove(ToDisconnect); }
/// <summary> /// removes the synapse from the list. called from the DisconnectSourceSynapse /// of the neuron sharing the synapse /// </summary> /// <param name="ToDisconnect">Synapse to remove from destination list.</param> private void RemoveDestinationSynapse(Synapse ToDisconnect) { if (this.DestinationSynapses.Contains(ToDisconnect) == false) { throw new Exception("The specified synapse is not a destination synapse"); } this.DestinationSynapses.Remove(ToDisconnect); }
/// <summary> /// Call this method to cause this neuron to start the process of connecting /// to another neuron. /// </summary> /// <param name="ToConnectTo">The neuron to connect to.</param> public virtual void ConnectToNeuron(Neuron ToConnectTo, double InitialWeight) { //make sure you arent already connected to the neuron if (ToConnectTo == null) { throw new Exception("Cant connect to a null pointer"); } //connect to the neuron Synapse NewSynapse = new Synapse(this, ToConnectTo, InitialWeight); this.DestinationSynapses.Add(NewSynapse); ToConnectTo.ConnectFromSynapse(NewSynapse); }
/// <summary> /// Disconnects a outgoing synapse /// </summary> /// <param name="ToDisconnect">Synapse to disconnect</param> public void DisconnectDestinationSynapse(Synapse ToDisconnect) { //make sure we are currently connected to the synapse if (this.DestinationSynapses.Contains(ToDisconnect) == false) { throw new Exception("Not currently connected to that synapse"); } this.DestinationSynapses.Remove(ToDisconnect); if (ToDisconnect.DestinationNeuron != null) { ToDisconnect.DestinationNeuron.RemoveSourceSynapse(ToDisconnect); } }
/// <summary> /// Disconnects a Source synapse. /// </summary> /// <param name="ToDisconnect">Synapse to disconnect</param> public void DisconnectSourceSynapse(Synapse ToDisconnect) { //checks to make sure it is a source synapse if (this.SourceSynapses.Contains(ToDisconnect) == false) { throw new Exception("Not a source synapse"); } this.SourceSynapses.Remove(ToDisconnect); if (ToDisconnect.SourceNeuron != null) { ToDisconnect.SourceNeuron.RemoveDestinationSynapse(ToDisconnect); } }
/// <summary> /// Disconnects all incomming synapses /// </summary> public void DisconnectAllSourceSynpases() { IEnumerator MyEnum = this.SourceSynapses.GetEnumerator(); while (MyEnum.MoveNext()) { if (MyEnum.Current is Synapse) { Synapse CurrentSynapse = MyEnum.Current as Synapse; this.DisconnectSourceSynapse(CurrentSynapse); } else { throw new Exception("This collection should only contain synapses"); } } }
/// <summary> /// Calculates the current DeltaTrain from the destination neurons. /// </summary> public virtual void CalculateDeltaTrain() { this.DeltaTrain = 0; IEnumerator DestSynapseEnum = this.DestinationSynapses.GetEnumerator(); while (DestSynapseEnum.MoveNext()) { if (DestSynapseEnum.Current is Synapse) { Synapse CurrentSynapse = DestSynapseEnum.Current as Synapse; this.DeltaTrain += CurrentSynapse.CalculateDifferential(); } else { throw new Exception("DestinationSynapses should only contain synapses"); } } this.DeltaTrain *= this.ActivationFunctionDerivitive(); }
public void CalculateSourceWeightTotal() { this.SourceWeightTotal = 0; IEnumerator SynapseEnum = this.SourceSynapses.GetEnumerator(); while (SynapseEnum.MoveNext()) { if (SynapseEnum.Current is Synapse) { Synapse CurrentSynapse = SynapseEnum.Current as Synapse; this.SourceWeightTotal += Math.Abs(CurrentSynapse.Weight); } else { throw new Exception("The array list contains an item it shouldnt"); } } }
private Synapse FindClosestSynapseWithWeight(double WeightToCheck) { Synapse CurrentClosestSynapse = null; double CurrentClosestWeightDelta = 0; IEnumerator SynapseEnum = this.SourceSynapses.GetEnumerator(); if (SynapseEnum.MoveNext()) { if (SynapseEnum.Current is Synapse) { CurrentClosestSynapse = SynapseEnum.Current as Synapse; CurrentClosestWeightDelta = WeightToCheck - CurrentClosestSynapse.Weight; } else { throw new Exception("Source synapses can only contain synapses"); } } while (SynapseEnum.MoveNext()) { if (SynapseEnum.Current is Synapse) { Synapse CurrentSynapse = SynapseEnum.Current as Synapse; double CurrentWeightDelta = WeightToCheck - CurrentSynapse.Weight; if (Math.Abs(CurrentWeightDelta) < Math.Abs(CurrentClosestWeightDelta)) { CurrentClosestSynapse = CurrentSynapse; CurrentClosestWeightDelta = CurrentWeightDelta; } } else { throw new Exception("Source synapses can only contain synapses"); } } return(CurrentClosestSynapse); }
private bool IsConnectedTo(Neuron NeuronToCheck) { IEnumerator SynapseEnum = this.DestinationSynapses.GetEnumerator(); while (SynapseEnum.MoveNext()) { if (SynapseEnum.Current is Synapse) { Synapse CurrentSynapse = SynapseEnum.Current as Synapse; if (CurrentSynapse.DestinationNeuron == NeuronToCheck) { return(true); } } else { throw new Exception("Destination synapses should only contain synapses"); } } return(false); }
private double CalculateHighestWeight() { double CurrentHighestSourceWeight = 0; IEnumerator SynapseEnum = this.SourceSynapses.GetEnumerator(); while (SynapseEnum.MoveNext()) { if (SynapseEnum.Current is Synapse) { Synapse CurrentSynapse = SynapseEnum.Current as Synapse; if ((Math.Abs(CurrentHighestSourceWeight)) < (Math.Abs(CurrentSynapse.Weight))) { CurrentHighestSourceWeight = CurrentSynapse.Weight; } } else { throw new Exception("Source Synapses should only contain synapses"); } } return(CurrentHighestSourceWeight); }
/// <summary> /// Calculates the DeltaTrain for the neuron, and then modifies the weights /// of all source synapses and bias acording to the new delta. /// </summary> public virtual void BackPropogateWeight() { this.CalculateDeltaTrain(); //step thru source synapses and make them learn their new weight. IEnumerator SynapseEnum = this.SourceSynapses.GetEnumerator(); while (SynapseEnum.MoveNext()) { if (SynapseEnum.Current is Synapse) { Synapse CurrentSynapse = SynapseEnum.Current as Synapse; CurrentSynapse.LearnWeight(); } else { throw new Exception("Source Synapses should only contain synapses"); } } //learn the biases new weight this.BiasWeight += this.OwnedDNA.LearningRate * this.DeltaTrain; }
protected double GetLeastSignificantSourceWeight() { double RetVal = this.BiasWeight; IEnumerator SynapseEnum = this.SourceSynapses.GetEnumerator(); while (SynapseEnum.MoveNext()) { if (SynapseEnum.Current is Synapse) { Synapse SynapseCurrent = SynapseEnum.Current as Synapse; if (Math.Abs(SynapseCurrent.Weight) < Math.Abs(RetVal)) { if (SynapseCurrent.Weight != 0) { RetVal = SynapseCurrent.Weight; } else { this.DisconnectSourceSynapse(SynapseCurrent); } } } else { throw new Exception("Source Synapses should only contain synapses"); } } if (RetVal != 0) { return(RetVal); } else { throw new Exception("None of the synapses should have a weight of 0"); } }
protected bool DropSourceSynapse() { if (this.OwnedDNA.UseMinimumWeight == true) { IEnumerator SynapseEnum = this.SourceSynapses.GetEnumerator(); while (SynapseEnum.MoveNext()) { if (SynapseEnum.Current is Synapse) { Synapse CurrentSynapse = SynapseEnum.Current as Synapse; if (CurrentSynapse.Weight < this.OwnedDNA.MinimumWeight) { this.DisconnectSourceSynapse(CurrentSynapse); return(true); } } else { throw new Exception("SourceSynapses should only contain synapses"); } } return(false); } else if ((this.OwnedDNA.UseCountDrop == true) && (this.SourceSynapses.Count > this.OwnedDNA.MaximumIncomming)) { //0 most common, 1 least likely double PercentageOfTop = 1 - Math.Tanh(this.OwnedDNA.RandomGenerator.NextDouble() * this.OwnedDNA.IncommingDropFactor); double IdealDropWeight = PercentageOfTop * this.CalculateHighestWeight(); Synapse SynapseToDrop = this.FindClosestSynapseWithWeight(IdealDropWeight); this.DisconnectSourceSynapse(SynapseToDrop); return(false); } else if (this.OwnedDNA.UseSignificanceDrop == true) { this.CalculateSourceWeightTotal(); //iterate thru all the source synapses for (int SynapseLcv = 0; SynapseLcv < this.SourceSynapses.Count; SynapseLcv++) { if (this.SourceSynapses[SynapseLcv] is Synapse) { Synapse CurrentSynapse = this.SourceSynapses[SynapseLcv] as Synapse; if (CurrentSynapse.GetSignificanceDiviation() <= this.OwnedDNA.DropBelowSignificanceDiviation) { this.DisconnectSourceSynapse(CurrentSynapse); SynapseLcv--; } } else { throw new Exception("SourceSynapses can only contain synapses"); } } return(false); } else if (this.OwnedDNA.DropBelowWeight == true) { //iterate thru all the source synapses for (int SynapseLcv = 0; SynapseLcv < this.SourceSynapses.Count; SynapseLcv++) { if (this.SourceSynapses[SynapseLcv] is Synapse) { Synapse CurrentSynapse = this.SourceSynapses[SynapseLcv] as Synapse; if (CurrentSynapse.Weight <= this.OwnedDNA.DropBelow) { this.DisconnectSourceSynapse(CurrentSynapse); SynapseLcv--; } } else { throw new Exception("SourceSynapses can only contain synapses"); } } return(false); } else { throw new Exception("One of the structure learning methods must be used"); } }
/// <summary> /// This makes sure that this neuron is never connected to. Calling this /// function throws an exception. /// </summary> /// <param name="ToConnectFrom">Not used</param> protected override void ConnectFromSynapse(Synapse ToConnectFrom) { throw new Exception("InputNeurons shouldnt be connected to"); }
public void GetObjectData(SerializationInfo info, StreamingContext context) { //Add brains attributes info.AddValue("LayerCount", this.LayerCount); info.AddValue("InputLayerUid", this.InLayer.Uid); info.AddValue("OutputLayerUid", this.OutLayer.Uid); //Add each layer and the neurons it contains Layer CurrentLayer = this.InLayer; int LayerId = 0; while (CurrentLayer != null) { string LayerIdName = "Layer" + LayerId; //Add layers attributes info.AddValue(LayerIdName + "Uid", CurrentLayer.Uid); info.AddValue(LayerIdName + "UseByteRes", CurrentLayer.ByteResolution); info.AddValue(LayerIdName + "NeuronCount", CurrentLayer.NeuronsOwned.Count); if (CurrentLayer is InputLayer) { info.AddValue(LayerIdName + "IsInput", true); } else { info.AddValue(LayerIdName + "IsInput", false); info.AddValue(LayerIdName + "SourceLayerUid", CurrentLayer.SourceLayer.Uid); } if (CurrentLayer is OutputLayer) { info.AddValue(LayerIdName + "IsOutput", true); } else { info.AddValue(LayerIdName + "IsOutput", false); info.AddValue(LayerIdName + "DestLayerUid", CurrentLayer.DestinationLayer.Uid); } //Add all the layers neurons IEnumerator NeuronEnum = CurrentLayer.NeuronsOwned.GetEnumerator(); int NeuronId = 0; while (NeuronEnum.MoveNext()) { if (NeuronEnum.Current is Neuron) { Neuron CurrentNeuron = NeuronEnum.Current as Neuron; string NeuronIdName = LayerIdName + "Neuron" + NeuronId; //add neurons attributes info.AddValue(NeuronIdName + "BiasWeight", CurrentNeuron.BiasWeight); info.AddValue(NeuronIdName + "OwningLayerUid", CurrentNeuron.OwningLayer.Uid); info.AddValue(NeuronIdName + "UseByteResolution", CurrentNeuron.ByteResolution); info.AddValue(NeuronIdName + "OutConCount", CurrentNeuron.DestinationSynapses.Count); info.AddValue(NeuronIdName + "Uid", CurrentNeuron.Uid); //Add all the neurons outgoing connection IEnumerator SynapseEnum = CurrentNeuron.DestinationSynapses.GetEnumerator(); int SynapseId = 0; while (SynapseEnum.MoveNext()) { if (SynapseEnum.Current is Synapse) { Synapse CurrentSynapse = SynapseEnum.Current as Synapse; string SynapseIdName = NeuronIdName + "Synapse" + SynapseId; info.AddValue(SynapseIdName + "ToUid", CurrentSynapse.DestinationNeuron.Uid); info.AddValue(SynapseIdName + "Weight", CurrentSynapse.Weight); SynapseId++; } else { throw new Exception("DestinationSynapses should only contain synapses"); } } NeuronId++; } else { throw new Exception("NeuronsOwned should only contain neurons"); } } LayerId++; CurrentLayer = CurrentLayer.DestinationLayer; } }
/// <summary> /// This makes sure that this neuron is never connected to. Calling this /// function throws an exception. /// </summary> /// <param name="ToConnectFrom">Not used</param> protected override void ConnectFromSynapse(Synapse ToConnectFrom) { throw new Exception("InputNeurons shouldnt be connected to"); }