Пример #1
0
    public Chromosome Crossover(Chromosome parent1, Chromosome parent2)
    {
        int random = r.Next (parent1.genes.Count);
        Chromosome child = new Chromosome (id);
        id++;

        for (int i =0; i< random; i++) {
            child.AddGene (parent1.genes [i]);
        }
        for (int i = random; i<parent1.genes.Count; i++) {
            child.AddGene (parent2.genes [i]);
        }

        return child;
    }
Пример #2
0
        static void Main(string[] args)
        {
            int              numeroGeracoes       = 200;
            int              numeroIndividuos     = 100;
            int              numeroGenesIndividos = 5;
            GerenteGrafo     grafo   = new GerenteGrafo();
            CalculaAvaliacao calcula = new CalculaAvaliacao();

            #region CriaGrafo
            for (int i = 0; i < 5; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    if (i == j)
                    {
                        grafo.AdicionaAresta(i, j, 0);
                    }
                    else
                    {
                        grafo.AdicionaAresta(i, j, 1);
                    }
                }
            }

            Console.WriteLine("Grafo:");
            foreach (var aresta in grafo.Arestas)
            {
                Console.WriteLine($"\t{aresta.Origem.NumeroVertice} -> {aresta.Destino.NumeroVertice} : {aresta.Peso}");
            }
            #endregion CriaGrafo

            calcula.Grafo = grafo;
            GeneticAlgorithm <int> ag = new GeneticAlgorithm <int>(numeroGeracoes, calcula);
            #region PopulacaoInicial
            List <Chromosome <int> > populacaoinicial = new List <Chromosome <int> >();
            Random rand = new Random();
            for (int i = 0; i < numeroIndividuos; i++)
            {
                var novo = new Chromosome <int>(numeroGenesIndividos);
                for (int j = 0; j < numeroGenesIndividos; j++)
                {
                    int gene = rand.Next() % numeroGenesIndividos;
                    if (novo.Genes != null)
                    {
                        while (novo.Genes.Contains(gene))
                        {
                            gene = rand.Next() % numeroGenesIndividos;
                        }
                    }
                    novo.AddGene(gene);
                }
                populacaoinicial.Add(novo);
            }
            #endregion PopulacaoInicial
            ag.DefineInitialPopulation(numeroIndividuos, numeroGenesIndividos, populacaoinicial);
            ag.Run();
            var melhor = ag.GetBestChromosome();
            Console.WriteLine(melhor.GetJson());
        }
        public void ctor_ShouldThrowWhenAddingNullGene()
        {
            var    sut = new Chromosome();
            Action act = () =>
            {
                sut.AddGene(null);
            };

            act.ShouldThrow <NullReferenceException>();
        }
        public void ctor_ShouldNotThrowWhenAddingDuplicateGene()
        {
            var sameName  = "TEST1";
            var sut       = new Chromosome();
            var fakeGene1 = new Gene {
                Allele = { Name = sameName }
            };
            var fakeGene2 = new Gene {
                Allele = { Name = sameName }
            };

            Action act = () =>
            {
                sut.AddGene(fakeGene1);
                sut.AddGene(fakeGene2);
            };

            act.ShouldNotThrow();
        }
Пример #5
0
        // Sinh một tổ hợp ngẫu nhiên từ dữ liệu các thành phố
        public static void khoitaoNST(Chromosome chromose)
        {
            var input = Enumerable.Range(0, MainTSP.points.Count).ToList();
            int index = 0;

            for (int i = 0; i < MainTSP.points.Count; i++)
            {
                index = MainTSP.rand.Next(input.Count);
                chromose.AddGene(input[index].ToString());
                input.RemoveAt(index);
            }
        }
Пример #6
0
        private List <Chromosome <string> > CriaPopulacaoInicial(int numeroIndividuos)
        {
            int ValorMinimo           = 30;
            int valorMaximo           = 120;
            int numeroGenesCromossomo = 14;// cada tempo tem o valor mázimo de 127 (7 bits)
            List <Chromosome <string> > populacaoinicial = new List <Chromosome <string> >();
            Random rand = new Random();

            for (int i = 0; i < numeroIndividuos; i++)
            {
                var           novo          = new Chromosome <string>(numeroGenesCromossomo * Semaforos.Count);
                StringBuilder strcromossomo = new StringBuilder();
                for (int j = 0; j < Semaforos.Count(); j++)
                {
                    int tempoaberto  = 0;
                    int tempofechado = 0;
                    while (tempoaberto < ValorMinimo)
                    {
                        tempoaberto = rand.Next() % valorMaximo;
                    }
                    while (tempofechado < ValorMinimo)
                    {
                        tempofechado = rand.Next() % valorMaximo;
                    }
                    string cromossomo = $"{Convert.ToString(tempoaberto, 2)}{Convert.ToString(tempofechado, 2)}";
                    while (cromossomo.Count() < numeroGenesCromossomo)
                    {
                        if (rand.Next() % 2 == 0)
                        {
                            cromossomo = cromossomo.Insert(0, "0");
                        }
                        else
                        {
                            cromossomo = cromossomo.Insert(1, "1");
                        }
                    }
                    strcromossomo.Append(cromossomo);
                }
                var str = strcromossomo.ToString();
                for (int j = 0; j < str.Length; j++)
                {
                    novo.AddGene(str[j].ToString());
                }
                populacaoinicial.Add(novo);
            }
            return(populacaoinicial);
        }
Пример #7
0
    /// <summary>
    /// Each input has a duration time in the genes of the agent's chromosome. This function runs every input for its allotted
    /// duration time.
    /// </summary>
    void CheckInputTimes()
    {
        if (!GenerationManager.instance.timeLimitReached && !GenerationManager.instance.goalReached)
        {
            if (Time.time - timeLimit <= chromosome.genes[chromosomeIndex].pressTime)
            {
                RunInputs();
            }
            else
            {
                if (!lastGeneAdded)
                {
                    chromosome.AddGene(ref lastGeneAdded);
                }

                timeLimit = Time.time;
                if (chromosomeIndex + 1 != chromosome.genes.Count)
                {
                    chromosomeIndex++;
                    hasJumped = false;
                }
            }
        }
    }
Пример #8
0
    public Chromosome Mutation(Chromosome original)
    {
        Chromosome mChild = new Chromosome (original.id);
        for (int i =0; i<original.genes.Count; i++) {
            if (r.NextDouble () < mutationRate) {
                mChild.AddGene (r.NextDouble ());
            } else {
                mChild.AddGene (original.genes [i]);
            }

        }
        return mChild;
    }
Пример #9
0
    public void GeneratePopulation(int size)
    {
        for (int i = 0; i<size; i++) {
            Chromosome c = new Chromosome (id);
            for (int j=0; j<connections.Count; j++) {
                c.AddGene (r.Next(-10,10));

            }
            population.Add (c);
            id++;
        }
    }