Exemplo n.º 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);
        }
Exemplo n.º 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! <(^.^)>");
        }
Exemplo n.º 3
0
        /// <summary>
        /// Mixes two networks together, meaning taking 50% of the
        /// otherNetwork connections and set the weights of those in this network
        /// </summary>
        /// <param name="otherNetwork">The network to be mixed in</param>
        /// <param name="mixRate">Percentage of the otherNetwork to use</param>
        public void MixNetwork(NeuronalNetwork otherNetwork, float mixRate)
        {
            List <NetworkIndex> mixIndices = new List <NetworkIndex>();
            int totalCount    = 0;
            var maxLayerIndex = Math.Min(neurons.Count, otherNetwork.neurons.Count);

            for (int layerIndex = 1; layerIndex < maxLayerIndex; layerIndex++)
            {
                int maxNeuronIndex = Math.Min(neurons[layerIndex].Count, otherNetwork.neurons[layerIndex].Count);
                for (int neuronIndex = 0; neuronIndex < maxNeuronIndex; neuronIndex++)
                {
                    totalCount += Math.Min(
                        ((WorkingNeuron)neurons[layerIndex][neuronIndex]).GetConnections().Count,
                        ((WorkingNeuron)otherNetwork.neurons[layerIndex][neuronIndex]).GetConnections().Count);
                }
            }
            while (mixIndices.Count < totalCount * mixRate)
            {
                NetworkIndex index = new NetworkIndex();
                do
                {
                    index.LayerIndex      = Simulation.RandomInt(1, maxLayerIndex);
                    index.NeuronIndex     = Simulation.RandomInt(Math.Min(neurons[index.LayerIndex].Count, otherNetwork.neurons[index.LayerIndex].Count));
                    index.ConnectionIndex = Simulation.RandomInt(Math.Min(
                                                                     ((WorkingNeuron)neurons[index.LayerIndex][index.NeuronIndex]).GetConnections().Count,
                                                                     ((WorkingNeuron)otherNetwork.neurons[index.LayerIndex][index.NeuronIndex]).GetConnections().Count));
                } while (mixIndices.Contains(index));
                mixIndices.Add(index);
            }

            foreach (NetworkIndex index in mixIndices)
            {
                Connection toBeChanged = GetConnection(index);
                Connection toBeUsed    = otherNetwork.GetConnection(index);
                toBeChanged.weight = toBeUsed.weight;
            }
        }
Exemplo n.º 4
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);
        }