Beispiel #1
0
        void ShowGenomeConnections(NeatGenome genome)
        {
            int savedIndex = listBoxConnections.SelectedIndex;

            listBoxConnections.Items.Clear();

            /* show information about each connection in the listbox */
            foreach (ConnectionGene connection in genome.ConnectionGeneList)
            {
                NeuronGene sourceNeuron      = genome.NeuronGeneList.GetNeuronById(connection.SourceNeuronId);
                NeuronGene destinationNeuron = genome.NeuronGeneList.GetNeuronById(connection.TargetNeuronId);
                string     info = string.Format("{0}({1}) --> {2}({3}); weight:{4:F3})", connection.SourceNeuronId, sourceNeuron.NeuronType, connection.TargetNeuronId, destinationNeuron.NeuronType, connection.Weight);
                /* add the info text and the conection object itself in the listitem */
                ListItem item = new ListItem("", info, connection);
                listBoxConnections.Items.Add(item);
            }

            /* try to update selected savedIndex and refresh the drawed network */
            listBoxConnections.SelectedIndex = listBoxConnections.Items.Count > savedIndex ? savedIndex : -1;
            // ShowNetworkFromGenome(genome);
        }
Beispiel #2
0
        private int getOffset(int leg, int cppnIterations, NeuronGene neuron)
        {
            int offset      = 0;
            var coordinates = new double[3];

            coordinates[0] = neuron.XValue / compression;
            cppn.ClearSignals();
            cppn.SetInputSignals(coordinates);
            cppn.MultipleSteps(cppnIterations);
            float activation = cppn.GetOutputSignal(1);

            offset = (int)Math.Ceiling((activation + 1) * wavelength / 2);
            if (offset <= 0)
            {
                offset = 1;
            }
            if (offset >= wavelength)
            {
                offset = wavelength - 1;
            }

            return(offset);
        }
        internal NeuronGene GetRandomHiddenNeuronGene(NeuralNetworkGene networkGenes, int hiddenLayerIndex)
        {
            var neuronGene = new NeuronGene
            {
                Axon = new AxonGene
                {
                    Weights            = new List <double>(),
                    ActivationFunction = GetRandomActivationFunction().GetType()
                },
                Soma = new SomaGene
                {
                    Bias = _weightInitializer.InitializeWeight(),
                    SummationFunction = GetRandomSummationFunction().GetType()
                }
            };
            //update terminals for current neuron
            LayerGene nextlayer = GetNextLayerGene(networkGenes, hiddenLayerIndex);

            for (int i = 0; i < nextlayer.Neurons.Count; i++)
            {
                neuronGene.Axon.Weights.Add(_weightInitializer.InitializeWeight());
            }
            return(neuronGene);
        }
        private static void WriteNeuron(XmlElement xmlNeurons, NeuronGene neuronGene)
        {
            XmlElement xmlNeuron = XmlUtilities.AddElement(xmlNeurons, "neuron");

            XmlUtilities.AddAttribute(xmlNeuron, "id", neuronGene.InnovationId.ToString());
            XmlUtilities.AddAttribute(xmlNeuron, "type", XmlUtilities.GetNeuronTypeString(neuronGene.NeuronType));
            XmlUtilities.AddAttribute(xmlNeuron, "activationFunction", neuronGene.ActivationFunction.FunctionId);
            XmlUtilities.AddAttribute(xmlNeuron, "bias", neuronGene.Bias.ToString());
            XmlUtilities.AddAttribute(xmlNeuron, "layer", neuronGene.Layer.ToString());
            //    newNode.attributes["layer"] = neuronGene.Layer;
        }
Beispiel #5
0
        //public static

        public static NeatGenome Read(XmlElement xmlGenome)
        {
            int inputNeuronCount  = 0;
            int outputNeuronCount = 0;

            uint id = uint.Parse(XmlUtilities.GetAttributeValue(xmlGenome, "id"));

            //--- Read neuron genes into a list.
            NeuronGeneList neuronGeneList  = new NeuronGeneList();
            XmlNodeList    listNeuronGenes = xmlGenome.SelectNodes("neurons/neuron");

            foreach (XmlElement xmlNeuronGene in listNeuronGenes)
            {
                NeuronGene neuronGene = ReadNeuronGene(xmlNeuronGene);

                // Count the input and output neurons as we go.
                switch (neuronGene.NeuronType)
                {
                case NeuronType.Input:
                    inputNeuronCount++;
                    break;

                case NeuronType.Output:
                    outputNeuronCount++;
                    break;
                }

                neuronGeneList.Add(neuronGene);
            }

            //--- Read module genes into a list.
            List <ModuleGene> moduleGeneList  = new List <ModuleGene>();
            XmlNodeList       listModuleGenes = xmlGenome.SelectNodes("modules/module");

            foreach (XmlElement xmlModuleGene in listModuleGenes)
            {
                moduleGeneList.Add(ReadModuleGene(xmlModuleGene));
            }

            //--- Read connection genes into a list.
            ConnectionGeneList connectionGeneList  = new ConnectionGeneList();
            XmlNodeList        listConnectionGenes = xmlGenome.SelectNodes("connections/connection");

            foreach (XmlElement xmlConnectionGene in listConnectionGenes)
            {
                connectionGeneList.Add(ReadConnectionGene(xmlConnectionGene));
            }


            // Read behavior list
            NeatGenome g = new NeatGenome(id, neuronGeneList, moduleGeneList, connectionGeneList, inputNeuronCount, outputNeuronCount);

            XmlNode behaviorNode = xmlGenome.SelectSingleNode("behavior");

            if (behaviorNode != null)
            {
                g.Behavior            = ReadBehavior(behaviorNode); //TODO bug is here
                g.Behavior.objectives = new double[6];
                g.objectives          = new double[6];
            }



            /*
             * XmlNode behaviorNode = xmlGenome.SelectSingleNode("behavior");
             * if (behaviorNode != null)
             * {
             *  g.Behavior = ReadBehavior(behaviorNode.SelectSingleNode("list")); //TODO bug is here
             *  g.Behavior.objectives = new double[6];
             *  g.objectives = new double[6];
             * }
             */



            return(g);
        }
 private string getState(NeuronGene neuronGene)
 {
     return(((IMarkovActivationFunction)(_fnLib.GetFunction(neuronGene.ActivationFnId))).State);
 }
        internal INeuron CreateNeuronFromGene(NeuronGene neuronGene, Dictionary<int, Dictionary<int, IList<Synapse>>> mapping, int layerIndex, int neuronIndex)
        {
            var dendrites = (layerIndex > 0) ? getDendritesForSoma(layerIndex, neuronIndex, mapping) : mapping[layerIndex][neuronIndex];

            var soma = _somaFactory.Create(dendrites, neuronGene.Soma.Bias, neuronGene.Soma.SummationFunction);

            var terminals = mapping[layerIndex + 1][neuronIndex];
            var axon = _axonFactory.Create(terminals, neuronGene.Axon.ActivationFunction);

            return Neuron.GetInstance(soma, axon);
        }
Beispiel #8
0
 internal ConnectionNeurons(NeuronGene sourceNeuronGene, NeuronGene targetNeuronGene)
 {
     Source = sourceNeuronGene;
     Target = targetNeuronGene;
 }
        internal NeuronGene TryMutateNeuron(NeuronGene gene, double mutateChance, out bool didMutate)
        {
            didMutate = false;
            NeuronGene toReturn = new NeuronGene
            {
                Axon = new AxonGene
                {
                    Weights            = new List <double>(),
                    ActivationFunction = gene.Axon.ActivationFunction
                },
                Soma = new SomaGene
                {
                    SummationFunction = gene.Soma.SummationFunction
                }
            };

            //weights
            for (int j = 0; j < gene.Axon.Weights.Count; j++)
            {
                if (_config.MutateSynapseWeights && _random.NextDouble() <= mutateChance)
                {
                    didMutate = true;
                    double val = _random.NextDouble();
                    if (_random.NextDouble() < 0.5)
                    {
                        // 50% chance of being negative, being between -1 and 1
                        val = 0 - val;
                    }
                    toReturn.Axon.Weights.Add(val);
                }
                else
                {
                    toReturn.Axon.Weights.Add(gene.Axon.Weights[j]);
                }
            }


            //bias
            if (_config.MutateSomaBiasFunction && _random.NextDouble() <= mutateChance)
            {
                didMutate = true;
                double val = _random.NextDouble();
                if (_random.NextDouble() < 0.5)
                {
                    // 50% chance of being negative, being between -1 and 1
                    val = 0 - val;
                }
                toReturn.Soma.Bias = val;
            }
            else
            {
                toReturn.Soma.Bias = gene.Soma.Bias;
            }

            //activation
            if (_config.MutateAxonActivationFunction && _random.NextDouble() <= mutateChance)
            {
                didMutate = true;
                toReturn.Axon.ActivationFunction = GetRandomActivationFunction().GetType();
            }
            else
            {
                toReturn.Axon.ActivationFunction = gene.Axon.ActivationFunction;
            }

            //summation
            if (_config.MutateSomaSummationFunction && _random.NextDouble() <= mutateChance)
            {
                didMutate = true;
                toReturn.Soma.SummationFunction = GetRandomSummationFunction().GetType();
            }
            else
            {
                toReturn.Soma.SummationFunction = gene.Soma.SummationFunction;
            }
            return(gene);
        }
Beispiel #10
0
        public static NeatGenome Read(XmlElement xmlGenome)
        {
            int inputNeuronCount  = 0;
            int outputNeuronCount = 0;

            uint id = uint.Parse(XmlUtilities.GetAttributeValue(xmlGenome, "id"));

            //--- Read neuron genes into a list.
            NeuronGeneList neuronGeneList  = new NeuronGeneList();
            XmlNodeList    listNeuronGenes = xmlGenome.SelectNodes("neurons/neuron");

            foreach (XmlElement xmlNeuronGene in listNeuronGenes)
            {
                NeuronGene neuronGene = ReadNeuronGene(xmlNeuronGene);

                // Count the input and output neurons as we go.
                switch (neuronGene.NeuronType)
                {
                case NeuronType.Input:
                    inputNeuronCount++;
                    break;

                case NeuronType.Output:
                    outputNeuronCount++;
                    break;
                }

                neuronGeneList.Add(neuronGene);
            }

            //--- Read module genes into a list.
            List <ModuleGene> moduleGeneList  = new List <ModuleGene>();
            XmlNodeList       listModuleGenes = xmlGenome.SelectNodes("modules/module");

            foreach (XmlElement xmlModuleGene in listModuleGenes)
            {
                moduleGeneList.Add(ReadModuleGene(xmlModuleGene));
            }

            //--- Read connection genes into a list.
            ConnectionGeneList connectionGeneList  = new ConnectionGeneList();
            XmlNodeList        listConnectionGenes = xmlGenome.SelectNodes("connections/connection");

            foreach (XmlElement xmlConnectionGene in listConnectionGenes)
            {
                connectionGeneList.Add(ReadConnectionGene(xmlConnectionGene));
            }

            //return new NeatGenome(id, neuronGeneList, connectionGeneList, inputNeuronCount, outputNeuronCount);
            NeatGenome g = new NeatGenome(id, neuronGeneList, moduleGeneList, connectionGeneList, inputNeuronCount, outputNeuronCount);

            g.Behavior            = ReadBehavior(xmlGenome.SelectSingleNode("behavior"));
            g.Behavior.objectives = new double[6];
            g.objectives          = new double[6];


            // JUSTIN: Read grid/trajectory info
            g.GridCoords          = ReadGrid(xmlGenome.SelectSingleNode("grid"));
            g.Behavior.trajectory = ReadTrajectory(xmlGenome.SelectSingleNode("trajectory"));

            return(g);
        }
Beispiel #11
0
        internal NeuronGene TryMutateNeuron(NeuronGene gene, double mutateChance, out bool didMutate)
        {
            didMutate = false;
            NeuronGene toReturn = new NeuronGene
            {
                Axon = new AxonGene
                {
                    Weights = new List<double>(),
                    ActivationFunction = gene.Axon.ActivationFunction
                },
                Soma = new SomaGene
                {
                    SummationFunction = gene.Soma.SummationFunction
                }
            };
            //weights
            for (int j = 0; j < gene.Axon.Weights.Count; j++)
            {
                if (_config.MutateSynapseWeights && _random.NextDouble() <= mutateChance)
                {
                    didMutate = true;
                    double val = _random.NextDouble();
                    if (_random.NextDouble() < 0.5)
                    {
                        // 50% chance of being negative, being between -1 and 1
                        val = 0 - val;
                    }
                    toReturn.Axon.Weights.Add(val);
                }
                else
                {
                    toReturn.Axon.Weights.Add(gene.Axon.Weights[j]);
                }
            }

            //bias
            if (_config.MutateSomaBiasFunction && _random.NextDouble() <= mutateChance)
            {
                didMutate = true;
                double val = _random.NextDouble();
                if (_random.NextDouble() < 0.5)
                {
                    // 50% chance of being negative, being between -1 and 1
                    val = 0 - val;
                }
                toReturn.Soma.Bias = val;
            }
            else
            {
                toReturn.Soma.Bias = gene.Soma.Bias;
            }

            //activation
            if (_config.MutateAxonActivationFunction && _random.NextDouble() <= mutateChance)
            {
                didMutate = true;
                toReturn.Axon.ActivationFunction = GetRandomActivationFunction().GetType();
            }
            else
            {
                toReturn.Axon.ActivationFunction = gene.Axon.ActivationFunction;
            }

            //summation
            if (_config.MutateSomaSummationFunction && _random.NextDouble() <= mutateChance)
            {
                didMutate = true;
                toReturn.Soma.SummationFunction = GetRandomSummationFunction().GetType();
            }
            else
            {
                toReturn.Soma.SummationFunction = gene.Soma.SummationFunction;
            }
            return gene;
        }
Beispiel #12
0
 internal NeuronGene GetRandomHiddenNeuronGene(NeuralNetworkGene networkGenes, int hiddenLayerIndex)
 {
     var neuronGene = new NeuronGene
     {
         Axon = new AxonGene
         {
             Weights = new List<double>(),
             ActivationFunction = GetRandomActivationFunction().GetType()
         },
         Soma = new SomaGene
         {
             Bias = _weightInitializer.InitializeWeight(),
             SummationFunction = GetRandomSummationFunction().GetType()
         }
     };
     //update terminals for current neuron
     LayerGene nextlayer = GetNextLayerGene(networkGenes, hiddenLayerIndex);
     for (int i = 0; i < nextlayer.Neurons.Count; i++)
     {
         neuronGene.Axon.Weights.Add(_weightInitializer.InitializeWeight());
     }
     return neuronGene;
 }
Beispiel #13
0
    void generateRandom(int numberOfInputs, int numberOfOutputs)
    {
        #region genome generation
        // Generate random Genome
        genome = new Genome();
        //-- Generate Neuron part (include input and output)
        IDictionary <int, NeuronGene> neuronGenes = new Dictionary <int, NeuronGene>();
        //---- Outputs
        for (int i = 0; i < numberOfOutputs; i++)
        {
            NeuronGene ng = new NeuronGene(outputLayer, (Neuron.Type)Random.Range(0, (int)Neuron.Type.amountOfTypes), i, false, true);
            neuronGenes.Add(ng.innovation, ng);
        }
        //---- Inputs
        for (int i = 0; i < numberOfInputs; i++)
        {
            NeuronGene ng = new NeuronGene(inputLayer, Neuron.Type.input, i + numberOfOutputs, true, false);
            neuronGenes.Add(ng.innovation, ng);
        }
        //---- Generate hidden 0
        int nRandoms = Random.Range(2, 5);
        for (int i = 0; i < nRandoms; i++)
        {
            NeuronGene ng = new NeuronGene(0, (Neuron.Type)Random.Range(0, (int)Neuron.Type.amountOfTypes), neuronIA.getInnovation(), false, false);
            neuronGenes.Add(ng.innovation, ng);
        }
        genome.neuronGenes = neuronGenes;

        //-- Generate Connection part
        IDictionary <int, ConnectionGene> connectionGenes = new Dictionary <int, ConnectionGene>();
        List <int> hasInput  = new List <int>();
        List <int> hasOutput = new List <int>();
        foreach (var n in genome.neuronGenes)
        {
            if (n.Value.layer != inputLayer)
            {
                if (!hasInput.Contains(n.Value.innovation))
                {
                    int[] candidates = getInputCandidates(n.Value.layer);
                    //Debug.Log(candidates.Length);
                    int            chosenCandidate = candidates[Random.Range(0, candidates.Length)];
                    ConnectionGene cg = new ConnectionGene(connectionIA.getInnovation(), chosenCandidate, n.Value.innovation, Random.Range(-1f, 1f));
                    connectionGenes.Add(cg.innovation, cg);
                    hasInput.Add(n.Value.innovation);
                    hasOutput.Add(chosenCandidate);
                    //Debug.Log("Added input");
                }
            }

            if (n.Value.layer != outputLayer)
            {
                if (!hasOutput.Contains(n.Value.innovation))
                {
                    int[]          candidates      = getOutputCandidates(n.Value.layer);
                    int            chosenCandidate = candidates[Random.Range(0, candidates.Length)];
                    ConnectionGene cg = new ConnectionGene(connectionIA.getInnovation(), n.Value.innovation, chosenCandidate, Random.Range(-1f, 1f));
                    connectionGenes.Add(cg.innovation, cg);
                    hasOutput.Add(n.Value.innovation);
                    hasInput.Add(chosenCandidate);
                    //Debug.Log("Added output");
                }
            }
        }
        genome.connectionGenes = connectionGenes;
        //printGenome();
        #endregion

        #region creation from genome
        // Create neurons from Genome
        inputs  = new int[numberOfInputs];
        outputs = new int[numberOfOutputs];
        int ii = 0, io = 0;
        foreach (var gn in genome.neuronGenes.Values)
        {
            Neuron neuron = new Neuron(gn.type, gn.layer, gn.innovation);
            neurons[gn.innovation] = neuron;
            if (neuron.layer == inputLayer)
            {
                inputs[ii] = neuron.innovation;
            }
            else if (neuron.layer == outputLayer)
            {
                outputs[io] = neuron.innovation;
            }
        }
        //Debug.Log("inputs: " + inputs.Length);
        // Create connections from genome
        foreach (var gc in genome.connectionGenes.Values)
        {
            neurons[gc.toNeuronInnovation].inputs.Add(new NeuronInput(neurons[gc.fromNeuronInnovation], gc.weight));
        }
        #endregion
    }