Example #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);
        }
        public void ConnectAxon(INeuron input, INeuron output, int neuronsCount)
        {
            var axon = new Synapse(input, output, XavierWeight(neuronsCount));

            (output.Inputs as SynapseCollection <ISynapse>).Add(axon);
            (input.Outputs as SynapseCollection <ISynapse>).Add(axon);
        }
Example #3
0
        public void AddInputNeuron(INeuron inputNeuron)
        {
            var synapse = new Synapse(inputNeuron, this);

            Inputs.Add(synapse);
            inputNeuron.Outputs.Add(synapse);
        }
Example #4
0
        public void AddOutputNeuron(INeuron outputNeuron)
        {
            var synapse = new Synapse(this, outputNeuron);

            Outputs.Add(synapse);
            outputNeuron.Inputs.Add(synapse);
        }
Example #5
0
        public void AddOutputNeuron(Neuron neuron)
        {
            Synapse synapse = new Synapse(this, neuron);

            Outputs.Add(synapse);
            neuron.Inputs.Add(synapse);
        }
Example #6
0
 public Neuron(IEnumerable <Neuron> inputNeurons) : this()
 {
     foreach (var inputNeuron in inputNeurons)
     {
         var synapse = new Synapse(inputNeuron, this);
         inputNeuron.OutputSynapses.Add(synapse);
         InputSynapses.Add(synapse);
     }
 }
Example #7
0
 public void RenderSynapses(Vector3 startPos, Vector3 endPos, float neuronDist, float width)
 {
     for (int i = 0; i < OutputSynapses.Count; i++)
     {
         Synapse synapse    = OutputSynapses[i];
         float   unitWeight = synapse.Weight / 10f;
         Color   neuronCol  = GLHelper.ColorFromUnitFloat(unitWeight);
         GLHelper.DrawLine(startPos, endPos, neuronCol, width);
         endPos.y -= neuronDist;
     }
 }
Example #8
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);
        }
Example #9
0
        public void AddSynapse()
        {
            //Debug.Log("Poczatek add synapse");
            //get 2 random neurons and connect them
            int    ineur1 = RandomGenerator.Next(InputLayer.Count + HiddenLayers.Count);
            Neuron neuron1;

            if (ineur1 < InputLayer.Count)
            {
                neuron1 = InputLayer[ineur1];
            }
            else
            {
                neuron1 = HiddenLayers.Values.ToArray()[ineur1 - InputLayer.Count];
            }
            int    ineur2 = RandomGenerator.Next(OutputLayer.Count + HiddenLayers.Count);
            Neuron neuron2;

            if (ineur2 < OutputLayer.Count)
            {
                neuron2 = OutputLayer[ineur2];
            }
            else
            {
                neuron2 = HiddenLayers.Values.ToArray()[ineur2 - OutputLayer.Count];
                if (!recurrent && neuron1.ConnectionWouldMakeCycle(neuron2)) //lets not make cycles
                {
                    Neuron tmp = neuron1;
                    neuron1 = neuron2;
                    neuron2 = tmp;
                }
            }

            //check if it already exist
            if (neuron1 == neuron2)
            {
                return;
            }
            foreach (var synapse in AllSynapses)
            {
                if (synapse.Value.InputNeuron == neuron1 && synapse.Value.OutputNeuron == neuron2)
                {
                    return;
                }
            }


            Synapse syn = new Synapse(neuron1, neuron2, SynapseInnovationNo);

            AllSynapses.Add(syn.InnovationNo, syn);
        }
Example #10
0
        public void MutateAddSynapse()
        {
            Neuron source = GetRandomNeuron(iNodes);
            Neuron dest   = source;

            while (dest.Equals(source))
            {
                dest = GetRandomNeuron(oNodes);
            }

            Synapse syn = new Synapse(source, dest, randomGen.NextDouble());

            source.AddOutput(syn);
            dest.AddInput(syn);
        }
Example #11
0
        void AddSynapse(Synapse template)
        {
            if (AllSynapses.ContainsKey(template.InnovationNo))
            {
                Debug.Log("wtf??");
                Debug.Break();
            }
            Neuron neuron1 = null, neuron2 = null;

            if (template.InputNeuron.InnovationNo <= InputLayer.Count)
            {
                neuron1 = InputLayer[template.InputNeuron.InnovationNo - 1];
            }
            else
            {
                if (HiddenLayers.ContainsKey(template.InputNeuron.InnovationNo))
                {
                    neuron1 = HiddenLayers[template.InputNeuron.InnovationNo];
                }
                else
                {
                    neuron1 = new Neuron(template.InputNeuron);
                    HiddenLayers.Add(neuron1.InnovationNo, neuron1);
                }
            }
            if (template.OutputNeuron.InnovationNo <= InputLayer.Count + OutputLayer.Count)
            {
                neuron2 = OutputLayer[template.OutputNeuron.InnovationNo - InputLayer.Count - 1];
            }
            else
            {
                if (HiddenLayers.ContainsKey(template.OutputNeuron.InnovationNo))
                {
                    neuron2 = HiddenLayers[template.OutputNeuron.InnovationNo];
                }
                else
                {
                    neuron2 = new Neuron(template.OutputNeuron);
                    HiddenLayers.Add(neuron2.InnovationNo, neuron2);
                }
            }

            Synapse newSyn = new Synapse(neuron1, neuron2, template.InnovationNo);

            newSyn.Weight = template.Weight;
            AllSynapses.Add(newSyn.InnovationNo, newSyn);
            //Debug.Log("Koniec add synapse template");
        }
        public void Init(Layer prev)
        {
            //No input synapses
            if (prev == null)
            {
                return;
            }

            foreach (var pn in prev.Neurons)
            {
                //int inputIndex = 1;
                foreach (var n in Neurons)
                {
                    var synapse = new Synapse(pn, n, n.InputSynapses.Count + 1, Util.r.NextDouble());
                    pn.OutputSynapses.Add(synapse);
                    n.InputSynapses.Add(synapse);
                }
            }
        }
Example #13
0
        public void MutateAddNeuron()
        {
            Neuron temp = new Neuron();

            Neuron  source = GetRandomNeuron(iNodes);
            Synapse synIn  = new Synapse(source, temp, randomGen.NextDouble());

            source.AddOutput(synIn);
            temp.AddInput(synIn);

            Neuron  destination = GetRandomNeuron(oNodes);
            Synapse synOut      = new Synapse(temp, destination, randomGen.NextDouble());

            destination.AddInput(synOut);
            temp.AddOutput(synOut);

            iNodes.Add(temp);
            oNodes.Add(temp);
            ioNodes.Add(temp);
        }
Example #14
0
        public void MutateSplice()
        {
            Neuron  source        = GetRandomNeuron(iNodes);
            Synapse sourceSynapse = GetRandomSynapse(source.GetOutputs());
            Neuron  destination   = sourceSynapse.GetDestination();

            Neuron temp = new Neuron();

            sourceSynapse.SetDestinationNeuron(temp);

            destination.GetInputs().Remove(sourceSynapse);

            Synapse tempSynapse = new Synapse(temp, destination, randomGen.NextDouble());

            temp.GetOutputs().Add(tempSynapse);
            destination.GetInputs().Add(tempSynapse);

            iNodes.Add(temp);
            oNodes.Add(temp);
            ioNodes.Add(temp);
        }
Example #15
0
        public void AddInputSynapse(double input)
        {
            Synapse synapse = new Synapse(this, input);

            Inputs.Add(synapse);
        }
Example #16
0
        public Network Copy()
        {
            /* Needed for breadth first graph traversal */
            Queue <Neuron> queue            = new Queue <Neuron>();
            Dictionary <Neuron, Neuron> map = new Dictionary <Neuron, Neuron> ();

            /* Cloned network */
            Network copyNetwork = new Network();

            /* We will start traversal from the actuator */
            Neuron originalActuator = this.actuator;

            Neuron copyActuator = new Neuron();

            copyNetwork.SetActuator(copyActuator);

            /* Add the first in line for processing, the original actuator */
            queue.Enqueue(originalActuator);
            map.Add(originalActuator, copyActuator);


            List <Neuron> copyioNodes = new List <Neuron>();
            List <Neuron> copyiNodes  = new List <Neuron>();
            List <Neuron> copyoNodes  = new List <Neuron>();
            List <Neuron> copySensors = new List <Neuron>();

            foreach (Neuron s in sensors)
            {
                Neuron temp = new Neuron();
                map.Add(s, temp);
                queue.Enqueue(s);
                copySensors.Add(temp);
                copyiNodes.Add(temp);
            }

            while (queue.Count > 0)
            {
                /* Node we are currently processing */
                Neuron originalNode = queue.Dequeue();

                List <Synapse> inputSynapses  = originalNode.GetInputs();
                List <Synapse> outputSynapses = originalNode.GetOutputs();

                /* Clone of the original node that is being processed */
                Neuron copyNode = map[originalNode];

                foreach (Synapse inputSynapse in inputSynapses)
                {
                    /* Get the real input node */
                    Neuron inputOriginal = inputSynapse.GetSource();

                    /* If the copy of the inputOriginal doesn't exist, create it. Otherwise just connect */
                    if (!map.ContainsKey(inputOriginal))
                    {
                        Neuron inputCopy = new Neuron();
                        copyioNodes.Add(inputCopy);
                        Synapse synapseCopy = new Synapse(inputCopy, copyNode, inputSynapse.getWeight());
                        inputCopy.AddOutput(synapseCopy);
                        copyNode.AddInput(synapseCopy);
                        map.Add(inputOriginal, inputCopy);
                        queue.Enqueue(inputOriginal);
                    }
                    else
                    {
                        /* Get the copy of the real node since it exists */
                        Neuron inputCopy = map[inputOriginal];

                        bool alreadyExists = false;
                        foreach (Synapse n in inputCopy.GetOutputs())
                        {
                            if (n.GetSource().Equals(inputCopy) && n.GetDestination().Equals(copyNode))
                            {
                                alreadyExists = true;
                                break;
                            }
                        }
                        if (!alreadyExists)
                        {
                            /* Create input synapse between cloned nodes */
                            Synapse copySynapse = new Synapse(inputCopy, copyNode, inputSynapse.getWeight());
                            copyNode.AddInput(copySynapse);
                            inputCopy.AddOutput(copySynapse);
                        }
                    }
                }
                foreach (Synapse outputSynapse in outputSynapses)
                {
                    /* Get the real output node */
                    Neuron outputOriginal = outputSynapse.GetDestination();

                    /* If the copy of the outputOriginal doesn't exist, create it. Otherwise just connect */
                    if (!map.ContainsKey(outputOriginal))
                    {
                        Neuron outputCopy = new Neuron();
                        copyioNodes.Add(outputCopy);
                        Synapse syn = new Synapse(copyNode, outputCopy, outputSynapse.getWeight());
                        copyNode.AddOutput(syn);
                        outputCopy.AddInput(syn);
                        map.Add(outputOriginal, outputCopy);
                        queue.Enqueue(outputOriginal);
                    }
                    else
                    {
                        /* Get the copy of the real node since it exists */
                        Neuron outputCopy = map[outputOriginal];

                        bool alreadyExists = false;
                        foreach (Synapse n in outputCopy.GetInputs())
                        {
                            if (n.GetSource().Equals(copyNode) && n.GetDestination().Equals(outputCopy))
                            {
                                alreadyExists = true;
                                break;
                            }
                        }
                        if (!alreadyExists)
                        {
                            /* Create output synapse between cloned nodes */
                            Synapse copySynapse = new Synapse(copyNode, outputCopy, outputSynapse.getWeight());
                            copyNode.AddOutput(copySynapse);
                            outputCopy.AddInput(copySynapse);
                        }
                    }
                }
            }

            /* Set newly created sensors */
            copyNetwork.SetSensors(copySensors);

            /* Within cloned network, remove sensors from ioNodes, add to iNodes*/
            //for (Neuron copySensor : copySensors) {
            //   copyioNodes.remove(copySensor);
            //   copyiNodes.add(copySensor);
            // }

            /* Add all nodes (except sensors, and actuator) to iNodes and oNodes */
            foreach (Neuron ioNode in copyioNodes)
            {
                copyiNodes.Add(ioNode);
                copyoNodes.Add(ioNode);
            }

            /* Add actuator to oNodes */
            copyoNodes.Add(copyActuator);

            copyNetwork.SetInputNodes(copyiNodes);
            copyNetwork.SetOutputNodes(copyoNodes);
            copyNetwork.SetInputOutputNodes(copyioNodes);

            // Crap code ahead ================================ WOO HOO WATCH ME ==========================================
            // TODO: Erase this souts once we create a few working networks
            //        System.out.println("Length of ioNodes " + copyioNodes.size());
            //        System.out.println("Length of iNodes  " + copyiNodes.size() + " it should be ioNodes + 3");
            //        System.out.println("Length of oNodes  " + copyoNodes.size() + " it should be ioNodes + 1");
            //        System.out.println(copyNetwork.actuator);
            //        System.out.println(copyActuator);
            //        System.out.println(copyActuator.weightedSum);
            //        for (Neuron n : copyNetwork.iNodes)
            //        {
            //            System.out.println(n.weightedSum);
            //            System.out.println(n.getOutputs());
            //            System.out.println(n.getInputs());
            //        }
            // Crap code finished ============================= WOO HOO WATCH ME ==========================================

            return(copyNetwork);
        }
Example #17
0
 public void AddInput(Synapse input)
 {
     InputSynapses.Add(input);
 }
Example #18
0
 public void AddInput(Synapse input)
 {
     inputs.Add(input);
 }
Example #19
0
 public void AddOutput(Synapse output)
 {
     outputs.Add(output);
 }