Exemplo n.º 1
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);
    }
Exemplo n.º 2
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;
    }