Ejemplo n.º 1
0
        public void Crossover(ref Genome genome2, out Genome child1, out Genome child2)
        {
            int pos = (int)(m_random.NextDouble() * (double)m_length);
            child1 = new Genome(m_length, false);
            child2 = new Genome(m_length, false);

            // Array.Copy is the same speed (fraction slower) than iterating over the array.
            /*// Copy first half for child1 with random genes, second half with parent genes.
            Array.Copy(m_genes, 0, child1.m_genes, 0, pos);
            Array.Copy(genome2.m_genes, pos, child1.m_genes, pos, m_length - pos);

            // Copy first half for child2 with parent genes, second half with random genes.
            Array.Copy(genome2.m_genes, 0, child2.m_genes, 0, pos);
            Array.Copy(m_genes, pos, child2.m_genes, pos, m_length - pos);*/

            for (int i = 0; i < m_length; i++)
            {
                if (i < pos)
                {
                    child1.m_genes[i] = m_genes[i];
                    child2.m_genes[i] = genome2.m_genes[i];
                }
                else
                {
                    child1.m_genes[i] = genome2.m_genes[i];
                    child2.m_genes[i] = m_genes[i];
                }
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Create the *initial* genomes by repeated calling the supplied fitness function
 /// </summary>
 private void CreateGenomes()
 {
     for (int i = 0; i < GAParams.PopulationSize; i++)
     {
         Genome g = new Genome(GAParams.GenomeSize);
         GAParams.ThisGeneration.Add(g);
     }
 }
Ejemplo n.º 3
0
 public Genome DeepCopy()
 {
     Genome g = new Genome(m_length, false);
     Array.Copy(m_genes, g.m_genes, m_length);
     return g;
 }