Beispiel #1
0
        //returns a list of output controls based off an input list for a network
        List <bool> EvaluateNetwork(NeuralNet network, List <double> inputs)
        {
            inputs.Add(1);
            if (inputs.Count != Inputs)
            {
                Console.WriteLine("Incorrect number of neural network inputs.");
                return(new List <bool>());
            }
            for (int i = 0; i < Inputs; i++)
            {
                network.neurons[i].value = inputs[i];
            }
            foreach (Neuron neuron in network.neurons)
            {
                if (neuron == null)
                {
                    continue;
                }
                double sum = 0;
                for (int j = 0; j < neuron.incoming.Count; j++)
                {
                    Gene   incoming = neuron.incoming[j];
                    Neuron other    = network.neurons[incoming.into];
                    sum += (incoming.weight * other.value);
                }
                if (neuron.incoming.Count > 0)
                {
                    neuron.value = Sigmoid(sum);
                }
            }
            List <bool> outputs = new List <bool>();

            for (int o = 0; o < Outputs; o++)
            {
                if (network.neurons[MaxNodes + o].value > 0)
                {
                    outputs.Add(true);
                }
                else
                {
                    outputs.Add(false);
                }
            }
            return(outputs);
        }
Beispiel #2
0
        //creates a neural network based off a genome's connections and neurons
        void GenerateNetwork(Genome genome)
        {
            NeuralNet network = new NeuralNet();

            network.neurons = new List <Neuron>(new Neuron[MaxNodes + Outputs]);
            for (int i = 0; i < MaxNodes + Outputs; i++)
            {
                network.neurons[i] = null;
            }
            for (int i = 0; i < Inputs; i++)
            {
                network.neurons[i] = NewNeuron();
            }
            for (int o = 0; o < Outputs; o++)
            {
                network.neurons[MaxNodes + o] = NewNeuron();
            }

            genome.genes = genome.genes.OrderBy(x => x.outo).ToList();
            for (int i = 0; i < genome.genes.Count; i++)
            {
                Gene gene = genome.genes[i];
                if (gene.enabled)
                {
                    if (network.neurons[gene.outo] == null)
                    {
                        network.neurons[gene.outo] = NewNeuron();
                    }
                    Neuron neuron = network.neurons[gene.outo];
                    neuron.incoming.Add(gene);
                    if (network.neurons[gene.into] == null)
                    {
                        network.neurons[gene.into] = NewNeuron();
                    }
                }
            }
            genome.network = network;
        }