Ejemplo n.º 1
0
        public void TestToggleConnectionEnabilityMutation()
        {
            Genome gen5 = gen2.Copy();

            Assert.IsTrue(gen5.ConnectionGenes[7].IsEnabled == true);
            gen5.ToggleEnabledMutation();
            Assert.IsTrue(gen5.ConnectionGenes[7].IsEnabled == false);
        }
Ejemplo n.º 2
0
        public Genome GenerateGenesisGenome()
        {
            var genome = _grandFather.Copy();

            foreach (var Connection in genome.Connections.Values)
            {
                Connection.Weight = _random.NextDouble();
            }
            return(_grandFather.Copy());
        }
Ejemplo n.º 3
0
        public void Load(int inputCount, int outputCount, IBitInput logic)
        {
            levels.Clear();
            if (genome == null)
            {
                brainStructure = new BrainStructure()
                {
                    InputCount = inputCount,
                    LevelSizes = new int[] { outputCount }
                };

                genome = new Genome(brainStructure);
                genome.Load(logic);
            }

            // add memory input and output
            inputCount += brainStructure.MemoryBitCount;

            int genomeOffset = 0;

            for (int levelIndex = 0; levelIndex < brainStructure.LevelSizes.Length; levelIndex++)
            {
                DigitalLevel level = new DigitalLevel();

                outputCount = brainStructure.LevelSizes[levelIndex];
                int logicLength = BrainStructure.GetLevelLogicLength(inputCount, outputCount);

                logic         = genome.Copy(genomeOffset, logicLength);
                genomeOffset += logicLength;

                level.Load(inputCount, outputCount, logic);
                levels.Add(level);
                inputCount = outputCount;
            }

            // Load initial memory status
            if (brainStructure.MemoryBitCount > 0)
            {
                Memory = new BitStorage();
                Memory.Load(genome.Copy(genomeOffset, brainStructure.MemoryBitCount));
            }

            InputCount  = brainStructure.InputCount;
            OutputCount = brainStructure.OutputCount;

            if (autoCompile)
            {
                new Thread(() =>
                {
                    Compile();
                }).Start();
            }
        }
Ejemplo n.º 4
0
    public List <Genome> CrossoverMutation(List <Genome> selected)
    {
        Crossover     crossover  = new Crossover();
        List <Genome> newGenomes = new List <Genome>();

        for (int i = 0; i < agents.Count - survivors; i++)//crossover and mutation
        {
            Genome parent1 = selected[Random.Range(0, selected.Count)];

            //Genome child = parent1.Copy();//crossover.CrossoverGenes(parent1, parent2, gt);
            Genome child = null;
            if (Random.Range(0.0f, 1.0f) <= crossoverProbability)
            {
                Genome parent2 = selected[Random.Range(0, selected.Count)];
                child = crossover.CrossoverGenes(parent1, parent2, gt);
            }
            else
            {
                child = parent1.Copy();
            }

            //Debug.Log(child);
            child.Mutate();
            newGenomes.Add(child);
            //Debug.Log(newGenomes[i]);
            //Debug.Log(child);
        }

        return(newGenomes);
    }
Ejemplo n.º 5
0
        public override void CalcFitnessDefault(Genome genome)
        {
            genome.Fitness = 0;
            Genome tempGenome = genome.Copy();
            tempGenome.Add(1);

            int[,] matrix = new int[,]
                             {
            //								{ 0,  5,  8, 11,  4,  7 },
            //								{ 5,  0, 10,  4,  9, 12 },
            //								{ 8, 10,  0,  6, 17,  8 },
            //								{11,  4,  6,  0,  6,  5 },
            //								{ 4,  9, 17,  6,  0, 11 },
            //								{ 7, 12,  8,  5, 11,  0 }
                                { 0,  5,  8, 11,  4,  7,  3, 16,  4,  7 },
                                { 5,  0, 10,  4,  9, 12,  9,  7,  6, 12 },
                                { 8, 10,  0,  6, 17,  8,  4,  8,  9,  6 },
                                {11,  4,  6,  0,  6,  5, 10,  5,  7,  4 },
                                { 4,  9, 17,  6,  0, 11,  5,  3,  9, 13 },
                                { 7, 12,  8,  5, 11,  0,  6,  4,  3,  5 },
                                { 3,  9,  4, 10,  5,  6,  0, 10, 11,  5 },
                                {16,  7,  8,  5,  3,  4, 10,  0, 12,  8 },
                                { 4,  6,  9,  7,  9,  3, 11, 12,  0,  9 },
                                { 7, 12,  6,  4, 13,  5,  5,  8,  9,  0 }
                             };

            foreach (double gene in genome) {
                genome.Fitness += matrix[(int)gene - 1,(int)tempGenome[tempGenome.IndexOf(gene)+1]-1];
            }
        }
Ejemplo n.º 6
0
            public Genome GenerateGenesisGenome()
            {
                var genome = _GenomeGrandfather.Copy();

                foreach (var connection in genome.Connections)
                {
                    connection.Value.Weight = _random.NextDouble();
                }
                return(genome);
            }
Ejemplo n.º 7
0
        public void TestGenomeConstruction_ByCopying()
        {
            Genome genCopied = gen1.Copy();


            Assert.IsTrue(genCopied.Nodes.Count == gen1.Nodes.Count);
            Assert.IsTrue(genCopied.ConnectionGenes.Count == gen1.ConnectionGenes.Count);
            for (int i = 0; i < genCopied.Nodes.Count; ++i)
            {
                Assert.IsTrue(genCopied.Nodes[i].Equals(gen1.Nodes[i]));
                Assert.AreNotSame(genCopied.Nodes[i], gen1.Nodes[i]);
            }

            for (int i = 0; i < genCopied.ConnectionGenes.Count; ++i)
            {
                Assert.IsTrue(genCopied.ConnectionGenes[i].Equals(gen1.ConnectionGenes[i]));
                Assert.AreNotSame(genCopied.ConnectionGenes[i], gen1.ConnectionGenes[i]);
            }
        }
Ejemplo n.º 8
0
        private void DeterminePopulationFitness()
        {
            object syncObject = new object();

            Parallel.ForEach(Population, () => new Genome(Structure), (genome, loopState, localState) =>
            {
                DetermineGenomeFitness(ref genome);
                return(genome.Fitness < localState.Fitness ? genome : localState);
            },
                             localState =>
            {
                lock (syncObject)
                {
                    if (localState.Fitness < BestGenome.Fitness)
                    {
                        BestGenome.Copy(localState);
                    }
                }
            });
        }
Ejemplo n.º 9
0
    public void CrossoverMutationSpecies(List <Genome> selected)
    {
        for (int i = 0; i < selected.Count; i++)//group selected into species
        {
            int id = selected[i].GetSpeciesId();
            //Genome genome = selected[Random.Range(0, selected.Count)];
            //Debug.Log(selected[i].GetSpeciesId());
            //Debug.Log("225");
            for (int j = 0; j < species.Count; j++)
            {
                if (id == species[j].GetId())
                {
                    //Debug.Log("id: "+id+"   species: " + species[j].GetId()+"size: "+species[j].GetGenomes().Count);
                    species[j].GetSelectedGenomes().Add(selected[i]);
                    // Debug.Log("225");
                    break;
                }
            }
        }


        Crossover crossover = new Crossover();
        int       sum2      = 0;

        for (int i = 0; i < species.Count; i++)
        {
            if (!species[i].CheckExtinct())
            {
                for (int j = 0; j < species[i].GetSelectedGenomes().Count; j++)
                {
                    Genome parent1 = species[i].GetSelectedGenomes()[Random.Range(0, species[i].GetSelectedGenomes().Count)];
                    Genome child   = null;
                    if (Random.Range(0.0f, 1.0f) <= crossoverProbability)
                    {
                        Genome parent2 = species[i].GetSelectedGenomes()[Random.Range(0, species[i].GetSelectedGenomes().Count)];
                        child = crossover.CrossoverGenes(parent1, parent2, gt);
                    }
                    else
                    {
                        child = parent1.Copy();
                    }

                    //Debug.Log(child);
                    child.Mutate();
                    child.NextGen();
                    InsertGenomeIntoSpeciesChild(child, species);
                    //newGenomes.Add(child);
                    sum2++;
                }
            }
        }
        // Debug.Log("new" + sum2);
        // Debug.Log("count s" + species.Count);
        // Debug.Log("count s" + species[species.Count-1].GetChildren().Count);
        for (int i = 0; i < species.Count; i++)
        {
            //species[i].RemoveGen(generation-1);


            species[i].SetChildrenToSpecies();
            species[i].GetSelectedGenomes().Clear();
        }
        //Debug.Log("count s2" + species.Count);
        // Debug.Log("count s" + species[species.Count-1].GetGenomes().Count);
        for (int i = 0; i < species.Count; i++)
        {
            // Debug.Log("ss count" + species[i].GetGenomes().Count);
            //species[i].RemoveGen(generation-1);
            if (species[i].CheckExtinct())
            {
                species.RemoveAt(i);
                i--;
            }
        }
        // Debug.Log(newGenomes.Count);
        //species = newSpecies;
        //Debug.Log("s count"+species.Count);
        // Debug.Log("s1 count" + species[0].GetGenomes().Count);

        // Debug.Log(newGenomes.Count);
        //return newGenomes;
    }
Ejemplo n.º 10
0
        public Generation MakeFirstGeneration(InnovationGenerator generator, InnovationGenerator genomeGenerator,
                                              int initialPopulationSize, int selectionAlgorithm, int reproductionsPerGenome, int nBest)
        {
            // check if there is a Lua implementation of this
            LuaFunction func = LuaState["MakeFirstGeneration"] as LuaFunction;

            if (func != null)
            {
                try
                {
                    return((Generation)func.Call(generator, initialPopulationSize)?.First());
                }
                catch (Exception e)
                {
                    logger.Error(e, "Error running lua code for first generation.");
                }
            }

            Generation generation = new Generation(0, selectionAlgorithm, reproductionsPerGenome, nBest);
            Genome     genome     = new Genome(0);

            int inputs  = Data.Constants.NetworkInputs;
            int outputs = Data.Constants.NetworkOutputs;

            List <int> inputIds = new List <int>(inputs);

            // input nodes
            for (int i = 0; i < inputs; i++)
            {
                int currentId = generator.Innovate();
                inputIds.Add(currentId);
                genome.AddNode(new Node(currentId, NodeType.Input, int.MinValue));
            }

            // output nodes
            for (int i = 0; i < outputs; i++)
            {
                int currentId = generator.Innovate();
                genome.AddNode(new Node(currentId, NodeType.Output, int.MaxValue));

                foreach (int hiddenId in inputIds)
                {
                    genome.AddConnection(new Connection(hiddenId, currentId, Double() * 4f - 2f, true, generator.Innovate()));
                }
            }

            Species original = new Species(genome);

            generation.Species.Add(original);

            for (int i = 0; i < initialPopulationSize; i++)
            {
                Genome g = genome.Copy();
                g.Mutate(generator);
                g.Id = genomeGenerator.Innovate();

                original.AddGenome(g);
            }

            return(generation);
        }