Exemple #1
0
        public NeuronalNetwork CloneFullMesh()
        {
            //TODO make this mess pretty
            if (!this.fullMeshGenerated)
            {
                throw new NeuronalNetworkNotFullmeshedException();
            }
            NeuronalNetwork copy = new NeuronalNetwork();

            foreach (InputNeuron input in inputNeurons)
            {
                copy.AddInputNeuron((InputNeuron)input.NameCopy());
            }
            foreach (WorkingNeuron wn in hiddenNeurons)
            {
                copy.AddHiddenNeuron((WorkingNeuron)wn.NameCopy());
            }
            foreach (WorkingNeuron wn in outputNeurons)
            {
                copy.AddOutputNeuron((WorkingNeuron)wn.NameCopy());
            }

            copy.GenerateFullMesh();

            for (int i = 0; i < hiddenNeurons.Count; i++)
            {
                List <Connection> connectionsOrginal = hiddenNeurons[i].GetConnections();
                List <Connection> connectionsCopy    = copy.hiddenNeurons[i].GetConnections();
                if (connectionsOrginal.Count != connectionsCopy.Count)
                {
                    throw new NotSameAmountOfNeuronsException();
                }
                for (int k = 0; k < connectionsOrginal.Count; k++)
                {
                    connectionsCopy[k].weight = connectionsOrginal[k].weight;
                }
            }
            for (int i = 0; i < outputNeurons.Count; i++)
            {
                List <Connection> connectionsOrginal = outputNeurons[i].GetConnections();
                List <Connection> connectionsCopy    = copy.outputNeurons[i].GetConnections();
                if (connectionsOrginal.Count != connectionsCopy.Count)
                {
                    throw new NotSameAmountOfNeuronsException();
                }
                for (int k = 0; k < connectionsOrginal.Count; k++)
                {
                    connectionsCopy[k].weight = connectionsOrginal[k].weight;
                }
            }

            return(copy);
        }
Exemple #2
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! <(^.^)>");
        }
Exemple #3
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);
        }