Exemplo n.º 1
0
        private double weights(List <newGene> genes1, List <newGene> genes2)
        {
            //WEIGHTS
            Dictionary <int, newGene> i2 = new Dictionary <int, newGene>();

            for (int i = 0; i < genes2.Count; i++)
            {
                newGene gene = genes2[i];
                i2[gene.innovation] = gene; //GENE DICTIONARY
            }


            double sum        = 0;
            double coincident = 0;

            for (int i = 0; i < genes1.Count; i++)
            {
                newGene gene = genes1[i];
                if (i2.Keys.Contains(gene.innovation))
                {
                    newGene gene2 = i2[gene.innovation];
                    sum        = sum + Math.Abs(gene.weight - gene2.weight);
                    coincident = coincident + 1;
                }
            }

            return(sum / coincident);
        }
Exemplo n.º 2
0
            public Dictionary <string, bool> evaluateNetwork(List <double> inputs)
            {
                //GET OUTPUT AS BOOLEAN
                Inputs = inputs.ToArray();      //SET INPUTS
                inputs.Add(1);
                if (inputs.Count != InputsCount)
                {
                    Console.WriteLine("Incorrect number of neural network inputs."); return(null);
                }

                for (int i = 0; i < InputsCount; i++)
                {
                    neurons[i].value = inputs[i];
                }

                foreach (int neuron in neurons.Keys.ToList()) //LOOP NEVER INPUTS
                {
                    double sum = 0;
                    for (int i = 0; i < neurons[neuron].incoming.Count; i++)
                    {
                        newGene   incoming = neurons[neuron].incoming[i];
                        newNeuron other    = neurons[incoming.into];
                        sum = sum + incoming.weight * other.value;                              //WEIGHT CALCULATION
                    }
                    if (neurons[neuron].incoming.Count > 0)
                    {
                        neurons[neuron].value = Sigmoid(sum);                                   //SIGMOID FUNCTION
                    }
                }

                Dictionary <string, bool> outputs = new Dictionary <string, bool>();

                for (int i = 0; i < OutputsCount; i++)
                {
                    string button = OutputKeys[i];                                              //GET BUTTON NAMES
                    if (neurons[MaxNodes + i].value > 0)
                    {
                        outputs.Add(button, true);                                              //OUTPUT IS TRUE
                    }
                    else
                    {
                        outputs.Add(button, false);                                             //OUTPUT IS FALSE
                    }
                    Outputs[i] = neurons[MaxNodes + i].value;                                   //SET OUTPUTS
                }

                //REFRESH IO PANEL
                NetMain.IO.setIO(Inputs, Outputs);
                NetMain.IO.SecureRefesh();

                return(outputs);
            }
Exemplo n.º 3
0
 private bool containsLink(List <newGene> genes, newGene link)
 {
     //CONTAINS LINK
     if (genes != null)
     {
         for (int i = 0; i < genes.Count; i++) //LOOP GENES
         {
             newGene gene = genes[i];
             if (gene.into == link.into && gene.output == link.output)
             {
                 return(true);
             }
         }
     }
     return(false);
 }
Exemplo n.º 4
0
        private double disjoint(List <newGene> genes1, List <newGene> genes2)
        {
            //DISJOINT
            Dictionary <int, bool> i1 = new Dictionary <int, bool>();

            //LOOP GENES1
            for (int i = 0; i < genes1.Count; i++)
            {
                newGene gene = genes1[i];
                i1[gene.innovation] = true;
            }

            Dictionary <int, bool> i2 = new Dictionary <int, bool>();

            //LOOP GENES2
            for (int i = 0; i < genes2.Count; i++)
            {
                newGene gene = genes2[i];
                i2[gene.innovation] = true;
            }

            //DISJOINT GENES1
            int disjointGenes = 0;

            for (int i = 0; i < genes1.Count; i++)
            {
                newGene gene = genes1[i];
                if (i2.Keys.Contains(gene.innovation) && !i2[gene.innovation]) //i2.Keys.Contains(gene.innovation) ADD AFTER ERROR (KEY DON'T FIND)
                {
                    disjointGenes = disjointGenes + 1;
                }
            }

            //DISJOINT GENES2
            for (int i = 0; i < genes2.Count; i++)
            {
                newGene gene = genes2[i];
                if (i1.Keys.Contains(gene.innovation) && !i1[gene.innovation]) //i1.Keys.Contains(gene.innovation) ADD AFTER ERROR (KEY DON'T FIND)
                {
                    disjointGenes = disjointGenes + 1;
                }
            }

            int n = Max(genes1.Count, genes2.Count);

            return((double)disjointGenes / n);
        }
Exemplo n.º 5
0
        private newGenome crossover(newGenome g1, newGenome g2)
        {
            //MAKE SURE G1 IS THE HIGHER FITNESS GENOME
            if (g2.fitness > g1.fitness)
            {
                newGenome tempg = g1;
                g1 = g2;
                g2 = tempg;
            }

            newGenome child = new newGenome();

            Dictionary <int, newGene> innovations2 = new Dictionary <int, newGene>();

            for (int i = 0; i < g2.genes.Count; i++)
            {
                newGene gene = g2.genes[i];
                innovations2[gene.innovation] = gene;
            }

            for (int i = 0; i < g1.genes.Count; i++)
            {
                newGene gene1 = g1.genes[i];
                newGene gene2 = innovations2[gene1.innovation];

                //COPY GENE1 OR GENE2
                if (gene2 != null && Random(1, 2) == 1 && gene2.enabled)
                {
                    child.genes.Add(gene2.Copy());
                }
                else
                {
                    child.genes.Add(gene1.Copy());
                }
            }

            //GET MAXIMUM
            child.maxneuron = Max(g1.maxneuron, g2.maxneuron);

            foreach (KeyValuePair <string, double> item in g1.mutationRates)
            {
                child.mutationRates[item.Key] = item.Value;
            }

            return(child);
        }
Exemplo n.º 6
0
        private void linkMutate(newGenome genome, bool forceBias)
        {
            //LINK MUTATE
            int neuron1 = randomNeuron(genome.genes, false);
            int neuron2 = randomNeuron(genome.genes, true);

            newGene newLink = new newGene();

            if (neuron1 <= InputsCount && neuron2 <= InputsCount) //BOTH INPUT NODES
            {
                return;
            }

            if (neuron2 <= InputsCount) //SWAP OUTPUT AND INPUT
            {
                int temp = neuron1;
                neuron1 = neuron2;
                neuron2 = temp;
            }

            newLink.into   = neuron1;
            newLink.output = neuron2;
            if (forceBias)
            {
                newLink.into = InputsCount;
            }

            if (containsLink(genome.genes, newLink))
            {
                return;
            }

            newLink.innovation = newInnovation(Pool);
            newLink.weight     = Random() * 4 - 2;

            //ADD TO GENES LIST
            genome.genes.Add(newLink);
        }
Exemplo n.º 7
0
        private void nodeMutate(newGenome genome)
        {
            //NODE MUTATE
            if (genome.genes.Count == 0)
            {
                return;
            }

            genome.maxneuron = genome.maxneuron + 1;

            //GET RANDOM GENE
            newGene gene = genome.genes[Random(0, genome.genes.Count - 1)];

            if (!gene.enabled) //IF FALSE RETURN
            {
                return;
            }

            gene.enabled = false;

            //ADD GENE1
            newGene gene1 = gene.Copy();

            gene1.output     = genome.maxneuron;
            gene1.weight     = 1.0;
            gene1.innovation = newInnovation(Pool);
            gene1.enabled    = true;
            genome.genes.Add(gene1);

            //ADD GENE2
            newGene gene2 = gene.Copy();

            gene2.into       = genome.maxneuron;
            gene2.innovation = newInnovation(Pool);
            gene2.enabled    = true;
            genome.genes.Add(gene2);
        }
Exemplo n.º 8
0
        private void enableDisableMutate(newGenome genome, bool enable)
        {
            //ENABLE DISABLE MUTATE
            List <newGene> candidates = new List <newGene>();

            //GET ENABLES GENES
            foreach (newGene item in genome.genes)
            {
                if (item.enabled == !enable)
                {
                    candidates.Add(item);
                }
            }

            if (candidates.Count == 0)
            {
                return;
            }

            //GET RANDOM GENE FROM CANIDATES
            newGene gene = candidates[Random(0, candidates.Count - 1)];

            gene.enabled = !gene.enabled;
        }
Exemplo n.º 9
0
            public generateNetwork(newGenome genome)
            {
                //INPUTS
                for (int i = 0; i < InputsCount; i++)
                {
                    neurons[i] = new newNeuron();
                }

                //OUTPUTS
                for (int i = 0; i < OutputsCount; i++)
                {
                    neurons[MaxNodes + i] = new newNeuron();
                }

                genome.genes.Sort(); //table.sort(genome.genes, function(a, b) return (a.output < b.output) end)

                for (int i = 0; i < genome.genes.Count; i++)
                {
                    newGene gene = genome.genes[i];
                    if (gene.enabled)
                    {
                        if (!neurons.Keys.Contains(gene.output))
                        {
                            neurons[gene.output] = new newNeuron();
                        }

                        newNeuron neuron = neurons[gene.output];
                        neuron.incoming.Add(gene);
                        if (!neurons.Keys.Contains(gene.into))
                        {
                            neurons[gene.into] = new newNeuron();
                        }
                    }
                    genome.network = this;
                }
            }
Exemplo n.º 10
0
        public void Load()
        {
            //LOAD
            string[] path = Mod_File.FileOpenDialog(false, FILTER.TXT);

            //ABBRUCH
            if (path == null)
            {
                return;
            }

            //START LOADING
            UniLoad.loadingStart();

            //READ TXT FILE
            string[] file = Mod_TXT.readTXT(path[0]);
            int      x    = 0;

            //GET DURATION
            Duration = TimeSpan.FromMilliseconds(Mod_Convert.StringToDouble(file[x++]));

            //GET OUTPUT KEYS
            OutputKeys = file[x++].Split(' ');

            //INITIALIZE POOL
            Initialize(OutputKeys, true);
            Pool.generation = Mod_Convert.StringToInteger(file[x++]);
            Pool.maxFitness = Mod_Convert.StringToDouble(file[x++]);

            int numSpecies = Mod_Convert.StringToInteger(file[x++]);

            for (int j = 0; j < numSpecies; j++) //SPECIES
            {
                newSpecies species = new newSpecies();
                Pool.species.Add(species);
                species.topFitness = Mod_Convert.StringToDouble(file[x++]);
                species.staleness  = Mod_Convert.StringToInteger(file[x++]);
                int numGenomes = Mod_Convert.StringToInteger(file[x++]);
                for (int i = 0; i < numGenomes; i++) //GENOME
                {
                    newGenome genome = new newGenome();
                    species.genomes.Add(genome);
                    genome.fitness   = Mod_Convert.StringToDouble(file[x++]);
                    genome.maxneuron = Mod_Convert.StringToInteger(file[x++]);

                    string line = file[x++];
                    while (line != "done")
                    {
                        genome.mutationRates[line] = Mod_Convert.StringToDouble(file[x++]);
                        line = file[x++];
                    }

                    int numGenes = Mod_Convert.StringToInteger(file[x++]);
                    for (int k = 0; k < numGenes; k++) //GENE
                    {
                        newGene gene = new newGene();

                        genome.genes.Add(gene);
                        string[] split = file[x++].Split(' ');

                        gene.into       = Mod_Convert.StringToInteger(split[0]);
                        gene.output     = Mod_Convert.StringToInteger(split[1]);
                        gene.weight     = Mod_Convert.StringToDouble(split[2]);
                        gene.innovation = Mod_Convert.StringToInteger(split[3]);
                        gene.enabled    = Mod_Convert.ObjectToBool(split[4]);
                    }
                }
            }
            //FITNESS ALREADY MEASURED
            while (fitnessAlreadyMeasured())
            {
                nextGenome();
            }

            initializeRun();

            //UPDATE LEARN PANEL
            NetMain.RoundFinished(0);

            //END LOADING
            UniLoad.loadingEnd();
        }