// returns true if activation was successful public bool Fire(long incomingSignalDepth, double?forceValue = null) { // if its -1 then the neuron already fired from being called at that depth // if its 0 then the neuron fired at the same time as the incoming signal (absolute refactory period) if (incomingSignalDepth - Depth <= 0) { return(false); } // send this to gpu double signal = Bias; if (forceValue != null) { signal += forceValue.Value; } else { foreach (var dendrite in Dendrites) { if (dendrite.Depth == incomingSignalDepth) { signal += dendrite.Signal * DendriteStrength[dendrite]; } } if (DendriteStrength.ContainsKey(this)) { signal += Signal * DendriteStrength[this]; } } return(Activate(signal, incomingSignalDepth)); }
public void RemoveDendrite(Neuron origin) { if (origin == null || IsRoot() || origin.IsRoot()) { return; } DendriteStrength.Remove(origin); Dendrites.Remove(origin); origin.Axons.Remove(this); }
public void ForceCreateDendrite(Neuron source, double strength) { if (DendriteStrength.ContainsKey(source)) { DendriteStrength[source] = strength; } else { DendriteStrength.Add(source, strength); Dendrites.Add(source); source.Axons.Add(this); } }
public void Mutate(NeuronMutationProbabilities p, double mutationRate) { double chance = R.NG.NextDouble(); if (chance > mutationRate) { return; } if (IsRoot()) { return; } double percent = R.NG.NextDouble(); if (percent < p.NeuronCreation) { percent = R.NG.NextDouble(); if (percent < 0.4 && !IsInputNeuron() && !IsOutputNeuron()) // creates neuron on the same depth as this one { SpawnNeuronFromNeuron(); } else if (percent < 0.7 && !IsInputNeuron()) { SpawnNeuronFromDendrite(RandomDendrite()); } else if (!IsOutputNeuron()) { SpawnNeuronFromAxon(RandomAxon()); } } percent = R.NG.NextDouble(); if (percent < p.SynapseDeletion / 2) { if (Dendrites.Count > 0) { var dendrite = RandomDendrite(); while (dendrite.IsRoot() && Dendrites.Count > 1) { dendrite = RandomDendrite(); } RemoveDendrite(dendrite); } } percent = R.NG.NextDouble(); if (percent < p.SynapseDeletion / 2) { if (Axons.Count > 0) { var axon = RandomAxon(); while (axon.IsRoot() && Axons.Count > 1) { axon = RandomAxon(); } RemoveAxon(axon); } } percent = R.NG.NextDouble(); if (percent < p.SynapseAlteration / 2) // alters an existing dendrite { var neuron = RandomDendrite(); if (neuron != null) { if (IsInputNeuron() && Dendrites.Count > 1) { while (neuron.IsRoot()) { neuron = RandomDendrite(); } } if (!neuron.IsRoot()) { if (neuron.Identifier == Identifier || R.NG.Next(Dendrites.Count + 1) == Dendrites.Count) { Bias = RandomSynapseStrength(); } //Bias = (Bias + RandomSynapseStrength()) / 2; else { DendriteStrength[neuron] = RandomSynapseStrength(); } //DendriteStrength[neuron] = (DendriteStrength[neuron] + RandomSynapseStrength()) / 2; } } } percent = R.NG.NextDouble(); if (percent < p.SynapseCreation / 2) // dendrite creation { int i = 0; Neuron neuron = parentCluster.RandomNeuron(); //TODO: Remove hardcoded values if (Dendrites.Count != 0) { percent = R.NG.NextDouble(); if (percent < 0.51879) // creates dendrite on an existing dendrite depth { for (i = 0, neuron = RandomStepUp(this, 1); i < 3 && DendriteStrength.ContainsKey(neuron); ++i, neuron = RandomStepUp(this, 1)) { ; } } if (percent < 0.78793 || DendriteStrength.ContainsKey(neuron)) // creates dendrite on a level above { for (i = 0, neuron = RandomStepUp(this, 2); i < 3 && DendriteStrength.ContainsKey(neuron); ++i, neuron = RandomStepUp(this, 2)) { ; } } if (percent < 0.92756 || DendriteStrength.ContainsKey(neuron)) // creates dendrite on same level as this neuron { for (i = 0, neuron = RandomStepUp(this, 0); i < 3 && DendriteStrength.ContainsKey(neuron); ++i, neuron = RandomStepUp(this, 0)) { ; } } else if (DendriteStrength.ContainsKey(neuron))// creates dendrite to a random neuron { for (i = 0, neuron = parentCluster.RandomNeuron(); i < 3 && DendriteStrength.ContainsKey(neuron); ++i, neuron = parentCluster.RandomNeuron()) { ; } } } if (neuron.IsRoot()) { neuron = parentCluster.RandomNeuron(); } CreateDendrite(neuron, RandomSynapseStrength()); } percent = R.NG.NextDouble(); if (percent < p.SynapseAlteration / 2) // alters an existing axon { var neuron = RandomAxon(); if (neuron != null) { if (IsOutputNeuron() && Axons.Count > 1) { while (neuron.IsRoot()) { neuron = RandomAxon(); } } if (!neuron.IsRoot()) { neuron.DendriteStrength[this] = RandomSynapseStrength(); //neuron.DendriteStrength[this] = (RandomSynapseStrength() + neuron.DendriteStrength[this]) / 2; } } } percent = R.NG.NextDouble(); if (percent < p.SynapseCreation / 2) // axon creation { Neuron neuron = parentCluster.RandomNeuron(); int i = 0; if (Axons.Count != 0) { percent = R.NG.NextDouble(); if (percent < 0.51879) // creates axon on an existing axon depth { for (i = 0, neuron = RandomStepDown(this, 1); i < 3 && neuron.DendriteStrength.ContainsKey(this); ++i, neuron = RandomStepDown(this, 1)) { ; } } if (percent < 0.78793 || neuron.DendriteStrength.ContainsKey(this)) // creates axon on a level bellow { for (i = 0, neuron = RandomStepDown(this, 2); i < 3 && neuron.DendriteStrength.ContainsKey(this); ++i, neuron = RandomStepDown(this, 2)) { ; } } if (percent < 0.92756 || neuron.DendriteStrength.ContainsKey(this)) // creates axon on same level as this neuron { for (i = 0, neuron = RandomStepDown(this, 0); i < 3 && neuron.DendriteStrength.ContainsKey(this); ++i, neuron = RandomStepDown(this, 0)) { ; } } if (neuron.DendriteStrength.ContainsKey(this)) // creates axon to a random neuron { for (i = 0, neuron = parentCluster.RandomNeuron(); i < 3 && neuron.DendriteStrength.ContainsKey(this); ++i, neuron = parentCluster.RandomNeuron()) { ; } } } if (neuron.IsRoot()) { neuron = parentCluster.RandomNeuron(); } CreateAxon(neuron, RandomSynapseStrength()); } percent = R.NG.NextDouble(); if (percent < p.NeuronDeletion) { // remove neuron RemoveNeuron(); } }
public bool IsInputNeuron() { return(DendriteStrength.ContainsKey(parentCluster.GetNeuron(Cluster.InputGuid))); }