void crossover(string indiv)
    {
        newObj = population.genIndividual(indiv, "random");     //create new individual
        newObj.GetComponent <GA_Traits>().isChild = true;       //set is to be a child

        //set male and female parents
        GameObject selectedMale   = ((indiv == "bunny") ? gameObject.GetComponent <GA_Selection>().selectedMaleBunny : gameObject.GetComponent <GA_Selection>().selectedMaleFox);
        GameObject selectedFemale = ((indiv == "bunny") ? gameObject.GetComponent <GA_Selection>().selectedFemaleBunny : gameObject.GetComponent <GA_Selection>().selectedFemaleFox);


        int random;

        for (int i = 0; i < newObj.GetComponent <GA_Traits>().traitsArray.Length; i++)
        {
            random = Random.Range(0, (100 + mutationRate));     //generate a random number
            if (random < 50)
            {
                newObj.GetComponent <GA_Traits>().traitsArray[i] = selectedMale.GetComponent <GA_Traits>().traitsArray[i];
            }                                                                                                                                   //take trait from the male parent
            else if (random >= 50 && random < 100)
            {
                newObj.GetComponent <GA_Traits>().traitsArray[i] = selectedFemale.GetComponent <GA_Traits>().traitsArray[i];
            }                                                                                                                                                           //take trait from the female parent
            else
            {
                newObj.GetComponent <GA_Traits>().genRandomTrait(i);
            }                                                                  //mutate the trait
        }
    }
 void breed()
 {
     if (transform.position == breedTarget.transform.position)
     {
         traits.foodLevel -= 10;
         population.genIndividual(gameObject.tag, "random"); //create new random individual from the same species
         breedTarget = null;
     }
 }
Beispiel #3
0
 public void genBunny()
 {
     population.genIndividual("bunny", "random");
 }