Example #1
0
        public void Mutate(NeuronMutationProbabilities p, double mutationRate)
        {
            var cluster = new Cluster();

            cluster.GenerateFromStructure(Genes);
            Genes        = cluster.Mutate(p, mutationRate);
            NeuronCount  = cluster.NeuronCount;
            SynapseCount = cluster.SynapseCount;

            // TODO: these shouldn't change ever, check if do
            InputSize  = cluster.InputSize;
            OutputSize = cluster.OutputSize;
        }
Example #2
0
        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();
            }
        }