Example #1
0
        public override Neuron NameCopy()
        {
            WorkingNeuron clone = new WorkingNeuron();

            clone.SetName(GetName());
            return(clone);
        }
Example #2
0
 public void AddOutputNeuronAndMesh(WorkingNeuron neuron)
 {
     OutputNeurons.Add(neuron);
     foreach (WorkingNeuron wn in LastHiddenLayer)
     {
         neuron.AddNeuronConnection(new Connection(wn, 0));
     }
 }
Example #3
0
 public void AddOutputNeuronAndMesh(WorkingNeuron neuron)
 {
     outputNeurons.Add(neuron);
     foreach (WorkingNeuron wn in hiddenNeurons)
     {
         neuron.AddNeuronConnection(new Connection(wn, 0));
     }
 }
Example #4
0
 public void AddHiddenNeuron(WorkingNeuron neuron, int Layer)
 {
     if (Layer + 1 > 0 && Layer + 1 < neurons.Count)
     {
         neurons[Layer + 1].Add(neuron);
     }
     else
     {
         throw new ArgumentException("Layer needs to be within the hidden layer", "Layer");
     }
 }
Example #5
0
        public Connection GetConnection(NetworkIndex index)
        {
            WorkingNeuron neuron = neurons[index.LayerIndex][index.NeuronIndex] as WorkingNeuron;

            if (neuron != null)
            {
                return(neuron.GetConnections()[index.ConnectionIndex]);
            }
            else
            {
                return(null);
            }
        }
Example #6
0
        private float GetStrongestLayerConnection(List <Neuron> layer)
        {
            float strongestConnection = 0;

            foreach (Neuron n in layer)
            {
                WorkingNeuron wn = (WorkingNeuron)n;
                float         strongestNeuronConnection = Math.Abs(wn.GetStrongestConnection());
                if (strongestNeuronConnection > strongestConnection)
                {
                    strongestConnection = strongestNeuronConnection;
                }
            }
            return(strongestConnection);
        }
Example #7
0
        public void Deserialize(BinaryReader reader)
        {
            fullMeshGenerated = reader.ReadBoolean();
            int inputCount = reader.ReadInt32();

            for (int currentIndex = 0; currentIndex < inputCount; currentIndex++)
            {
                InputNeuron newNeuron = new InputNeuron();
                newNeuron.Deserialize(reader);
                inputNeurons.Add(newNeuron);
            }
            int hiddenCount = reader.ReadInt32();

            for (int currentIndex = 0; currentIndex < hiddenCount; currentIndex++)
            {
                WorkingNeuron newNeuron = new WorkingNeuron();
                newNeuron.Deserialize(reader);
                hiddenNeurons.Add(newNeuron);

                int connectionCount = reader.ReadInt32();
                for (int connectionIndex = 0; connectionIndex < connectionCount; connectionIndex++)
                {
                    // Todo get rid of this covariance cast
                    Connection connection = Connection.Deserialize(reader, inputNeurons.Cast <Neuron>());
                    newNeuron.AddNeuronConnection(connection);
                }
            }
            int outputCount = reader.ReadInt32();

            for (int currentIndex = 0; currentIndex < outputCount; currentIndex++)
            {
                WorkingNeuron newNeuron = new WorkingNeuron();
                newNeuron.Deserialize(reader);
                outputNeurons.Add(newNeuron);

                int connectionCount = reader.ReadInt32();
                for (int connectionIndex = 0; connectionIndex < connectionCount; connectionIndex++)
                {
                    // Todo get rid of this covariance cast
                    Connection connection = Connection.Deserialize(reader, hiddenNeurons.Cast <Neuron>());
                    newNeuron.AddNeuronConnection(connection);
                }
            }
            Invalidate();
        }
Example #8
0
        public void CreateHiddenLayer()
        {
            List <Neuron> newLayer   = new List <Neuron>();
            WorkingNeuron newNeuron  = new WorkingNeuron(HiddenLayerCount + 2);
            int           layerIndex = neurons.Count - 1;

            int prevLayerIndex  = layerIndex - 1;
            int connectionCount = 0;

            while (prevLayerIndex >= 0)
            {
                if (layerIndex > 1 && prevLayerIndex == 0)
                {
                    break;
                }
                float weight = 0.0f;
                if (connectionCount < LastHiddenLayer.Count && connectionCount < ((WorkingNeuron)LastHiddenLayer[connectionCount]).GetConnections().Count)
                {
                    weight = ((WorkingNeuron)LastHiddenLayer[0]).GetConnections()[connectionCount].weight;
                }
                for (int prevNeuronIndex = connectionCount; prevNeuronIndex < neurons[prevLayerIndex].Count &&
                     ((!(prevLayerIndex == 0) || connectionCount < FirstHiddenLayer.Count) || layerIndex == 1); ++prevNeuronIndex)
                {
                    newNeuron.AddNeuronConnection(neurons[prevLayerIndex][prevNeuronIndex], weight);
                    connectionCount++;
                }
                --prevLayerIndex;
            }

            for (int outputIndex = 0; outputIndex < OutputNeurons.Count; outputIndex++)
            {
                WorkingNeuron currentOutputNeuron = OutputNeurons[outputIndex] as WorkingNeuron;
                for (int connectionIndex = 0; connectionIndex < currentOutputNeuron.GetConnections().Count; connectionIndex++)
                {
                    if (currentOutputNeuron.GetConnections()[connectionIndex].entryNeuron == LastHiddenLayer[0])
                    {
                        currentOutputNeuron.GetConnections()[connectionIndex].entryNeuron = newNeuron;
                    }
                }
            }
            newLayer.Add(newNeuron);
            AddHiddenLayer(newLayer);
        }
Example #9
0
 public void RemoveHiddenLayer()
 {
     if (HiddenLayerCount > 1)
     {
         neurons.RemoveAt(neurons.Count - 2);
         for (int outNeuronIndex = 0; outNeuronIndex < OutputNeurons.Count; outNeuronIndex++)
         {
             WorkingNeuron outputNeuron = OutputNeurons[outNeuronIndex] as WorkingNeuron;
             if (outputNeuron.GetConnections().Count > LastHiddenLayer.Count)
             {
                 outputNeuron.GetConnections().RemoveRange(LastHiddenLayer.Count, outputNeuron.GetConnections().Count - LastHiddenLayer.Count);
             }
             for (int connectionIndex = 0; connectionIndex < LastHiddenLayer.Count && connectionIndex < outputNeuron.GetConnections().Count; connectionIndex++)
             {
                 outputNeuron.GetConnections()[connectionIndex].entryNeuron = LastHiddenLayer[connectionIndex];
             }
             for (int connectionIndex = outputNeuron.GetConnections().Count; connectionIndex < LastHiddenLayer.Count; connectionIndex++)
             {
                 outputNeuron.AddNeuronConnection(LastHiddenLayer[connectionIndex], (Simulation.RandomFloat() * 2 * -1));
             }
         }
     }
 }
Example #10
0
        public static void Test()
        {
            Console.WriteLine("Begin NN Test!");

            NeuronalNetwork nn  = new NeuronalNetwork();
            InputNeuron     in1 = new InputNeuron();
            InputNeuron     in2 = new InputNeuron();
            InputNeuron     in3 = new InputNeuron();

            WorkingNeuron out1 = new WorkingNeuron(0);
            WorkingNeuron out2 = new WorkingNeuron(0);
            WorkingNeuron out3 = new WorkingNeuron(0);

            nn.AddInputNeuron(in1);
            nn.AddInputNeuron(in2);
            nn.AddInputNeuron(in3);

            nn.GenerateHiddenNeurons(3, 1);

            nn.AddOutputNeuron(out1);
            nn.AddOutputNeuron(out2);
            nn.AddOutputNeuron(out3);

            nn.GenerateFullMesh();

            nn.RandomizeAllWeights();


            NeuronalNetwork nn2 = nn.CloneFullMesh();

            for (int i = 0; i < 3; i++)
            {
                Debug.Assert(nn2.GetOutputNeuronFromIndex(i).GetValue() == nn.GetOutputNeuronFromIndex(i).GetValue());
            }

            Console.WriteLine("NN Test success! <(^.^)>");
        }
Example #11
0
 public void AddOutputNeuron(WorkingNeuron neuron)
 {
     outputNeurons.Add(neuron);
 }
Example #12
0
 public void AddHiddenNeuron(WorkingNeuron neuron)
 {
     hiddenNeurons.Add(neuron);
 }
Example #13
0
        public NeuronalNetwork CloneFullMesh()
        {
            if (!fullMeshGenerated)
            {
                throw new NeuronalNetworkNotFullmeshedException();
            }
            NeuronalNetwork             copy        = new NeuronalNetwork();
            Dictionary <Neuron, Neuron> oldToNewMap = new Dictionary <Neuron, Neuron>();

            foreach (InputNeuron input in InputNeurons)
            {
                Neuron newNeuron = input.NameCopy();
                oldToNewMap.Add(input, newNeuron);

                copy.AddInputNeuron((InputNeuron)newNeuron);
            }
            for (int layerIndex = 0; layerIndex < HiddenLayerCount; layerIndex++)
            {
                List <Neuron> newLayer = new List <Neuron>();
                foreach (WorkingNeuron wn in neurons[layerIndex + 1])
                {
                    WorkingNeuron newNeuron = wn.NameCopy() as WorkingNeuron;
                    oldToNewMap.Add(wn, newNeuron);
                    newLayer.Add(newNeuron);
                    foreach (Connection con in wn.GetConnections())
                    {
                        newNeuron.AddNeuronConnection(oldToNewMap[con.entryNeuron], con.weight);
                    }
                }
                copy.AddHiddenLayer(newLayer);
            }
            foreach (Neuron output in OutputNeurons)
            {
                WorkingNeuron newNeuron = output.NameCopy() as WorkingNeuron;
                copy.AddOutputNeuron(newNeuron);
                foreach (Connection con in (output as WorkingNeuron).GetConnections())
                {
                    newNeuron.AddNeuronConnection(oldToNewMap[con.entryNeuron], con.weight);
                }
            }

            copy.fullMeshGenerated = true;

            //copy.GenerateFullMesh();

            //for (int layerIndex = 1; layerIndex < neurons.Count; layerIndex++)
            //{
            //    for (int neuronIndex = 0; neuronIndex < neurons[layerIndex].Count; neuronIndex++)
            //    {
            //        List<Connection> connectionsOriginal = ((WorkingNeuron)neurons[layerIndex][neuronIndex]).GetConnections();
            //        List<Connection> connectionsCopy = ((WorkingNeuron)copy.neurons[layerIndex][neuronIndex]).GetConnections();
            //        if (connectionsOriginal.Count != connectionsCopy.Count)
            //        {
            //            throw new NotSameAmountOfNeuronsException();
            //        }
            //        for (int k = 0; k < connectionsOriginal.Count; k++)
            //        {
            //            connectionsCopy[k].weight = connectionsOriginal[k].weight;
            //        }
            //    }
            //}

            return(copy);
        }
Example #14
0
 public void RemoveOutputNeuron(WorkingNeuron neuron)
 {
     OutputNeurons.Remove(neuron);
 }