コード例 #1
0
        public void AddNeuron()
        {
            if (!AllSynapses.Any())
            {
                AddSynapse();
                return;
            }
            //Debug.Log("Poczatek add neuron");
            int     tmp    = RandomGenerator.Next(AllSynapses.Count);
            Synapse oldSyn = AllSynapses.ToList()[tmp].Value;

            AllSynapses.Remove(oldSyn.InnovationNo);
            oldSyn.InputNeuron.OutputSynapses.Remove(oldSyn);
            oldSyn.OutputNeuron.InputSynapses.Remove(oldSyn);
            Neuron neuron = new Neuron(NeuronInnovationNo);


            Synapse newSyn1 = new Synapse(oldSyn.InputNeuron, neuron, SynapseInnovationNo);

            newSyn1.Weight = 1;
            Synapse newSyn2 = new Synapse(neuron, oldSyn.OutputNeuron, SynapseInnovationNo);

            newSyn2.Weight = oldSyn.Weight;

            HiddenLayers.Add(neuron.InnovationNo, neuron);
            AllSynapses.Add(newSyn1.InnovationNo, newSyn1);
            AllSynapses.Add(newSyn2.InnovationNo, newSyn2);
        }
コード例 #2
0
        public float Distance(NEAT partner)
        {
            if (partner == null)
            {
                Debug.Log("NULL partner");
                Debug.Break();
            }
            //var synapseIenumerator = AllSynapses.GetEnumerator();
            //var partnerSynapseIenumerator = partner.AllSynapses.GetEnumerator();
            //synapseIenumerator.MoveNext();
            float result = 0;

            if (AllSynapses.Any() || partner.AllSynapses.Any())
            {
                int   disjointGenes    = 0;
                int   excessGenes      = 0;
                float weightDifference = 0;
                float N = Math.Max(AllSynapses.Count, partner.AllSynapses.Count);
                disjointGenes = 2 * AllSynapses.Keys.Union(partner.AllSynapses.Keys).Count() - AllSynapses.Count - partner.AllSynapses.Count;
                foreach (var synapseInnovationNo in AllSynapses.Keys.Intersect(partner.AllSynapses.Keys))
                {
                    weightDifference += Math.Abs((float)(AllSynapses[synapseInnovationNo].Weight - partner.AllSynapses[synapseInnovationNo].Weight));
                    //if (Math.Abs((float)(AllSynapses[synapseInnovationNo].Weight - partner.AllSynapses[synapseInnovationNo].Weight)) > Constants.Con.synapse_difference_threshold)
                    //    disjointGenes++;
                }

                result += (Constants.Con.c1 * excessGenes + Constants.Con.c2 * disjointGenes + Constants.Con.c3 * weightDifference) / N;
            }
            //if(HiddenLayers.Any() || partner.HiddenLayers.Any())      OutputLayer.Count > 0 always
            {
                int   disjointGenes    = 0;
                int   excessGenes      = 0;
                float weightDifference = 0;
                float N = Math.Max(HiddenLayers.Count, partner.HiddenLayers.Count) + OutputLayer.Count;
                foreach (var neuronPair in OutputLayer.Zip(partner.OutputLayer))
                {
                    weightDifference += Mathf.Abs((float)(neuronPair.Key.Bias - neuronPair.Value.Bias));
                    //if (Mathf.Abs((float)(neuronPair.Key.Bias - neuronPair.Value.Bias)) > Constants.Con.bias_difference_threshold)
                    //    disjointGenes++;
                }

                disjointGenes = 2 * HiddenLayers.Keys.Union(partner.HiddenLayers.Keys).Count() - HiddenLayers.Count - partner.HiddenLayers.Count;
                foreach (var neuronInnovationNo in HiddenLayers.Keys.Intersect(partner.HiddenLayers.Keys))
                {
                    weightDifference += Math.Abs((float)(HiddenLayers[neuronInnovationNo].Bias - partner.HiddenLayers[neuronInnovationNo].Bias));
                    //if (Math.Abs((float)(HiddenLayers[neuronInnovationNo].Bias - partner.HiddenLayers[neuronInnovationNo].Bias)) > Constants.Con.bias_difference_threshold)
                    //    disjointGenes++;
                }
                result += (Constants.Con.c1 * excessGenes + Constants.Con.c2 * disjointGenes + Constants.Con.c3 * weightDifference) / N;
            }


            return(result);
            //if (result < Constants.Con.delta_t)
            //    return true;
            //return false;
        }
コード例 #3
0
        private void DelSynapse()
        {
            if (!AllSynapses.Any())
            {
                return;
            }
            int     tmp    = RandomGenerator.Next(AllSynapses.Count);
            Synapse oldSyn = AllSynapses.Values.ToList()[tmp];

            oldSyn.InputNeuron.OutputSynapses.Remove(oldSyn);
            oldSyn.OutputNeuron.InputSynapses.Remove(oldSyn);
            AllSynapses.Remove(oldSyn.InnovationNo);
        }