void Start() { Bounds boundaries = environment.GetComponent <Renderer> ().bounds; for (int i = 0; i < populationSize; i++) { bunny rabbit = CreateBunny(boundaries); population.Add(rabbit); } StartCoroutine(EvaluationLoop()); }
public bunny CreateBunny(Bounds bounds) { Vector3 randomPosition = new Vector3(Random.Range(-0.5f, 0.5f) * bounds.size.x, Random.Range(-0.5f, 0.5f) * bounds.size.y, Random.Range(-0.5f, 0.5f) * bounds.size.z); Vector3 worldPosition = environment.transform.position + randomPosition; GameObject temp = Instantiate(Bunny, worldPosition, Quaternion.identity); bunny rabbit = temp.GetComponent <bunny> (); AssignRandomColor(temp); AssignRandomSize(temp); AssignRandomTemperature(temp); return(rabbit); }
void Breed() { //new list List <bunny> tempList = new List <bunny> (); for (int i = 1; i < population.Count; i += 2) { int index1 = i - 1; int index2 = i; float howGenesAreSplit = Random.Range(0f, 1f); Bounds bounds = environment.GetComponent <Renderer> ().bounds; bunny child1 = CreateBunny(bounds); bunny child2 = CreateBunny(bounds); tempList.Add(child1); tempList.Add(child2); if (howGenesAreSplit <= 0.16f) { Color tempColor = new Color(population [index1].color.r, population [index1].color.g, population [index2].color.b); tempColor = mutatedColor(tempColor); child1.SetColor(tempColor); tempColor = new Color(population [index2].color.r, population [index2].color.g, population [index1].color.b); tempColor = mutatedColor(tempColor); child2.SetColor(tempColor); Vector3 tempSize = new Vector3(population [index1].transform.localScale.x, population [index1].transform.localScale.y, population [index2].transform.localScale.z); tempSize = mutatedSize(tempSize); child1.SetSize(tempSize); tempSize = new Vector3(population [index2].transform.localScale.x, population [index2].transform.localScale.y, population [index1].transform.localScale.z); tempSize = mutatedSize(tempSize); child2.SetSize(tempSize); } else if (howGenesAreSplit <= 0.32f) { Color tempColor = new Color(population [index1].color.r, population [index1].color.g, population [index1].color.b); tempColor = mutatedColor(tempColor); child1.SetColor(tempColor); tempColor = new Color(population [index2].color.r, population [index2].color.g, population [index2].color.b); tempColor = mutatedColor(tempColor); child1.SetColor(tempColor); Vector3 tempSize = new Vector3(population [index1].transform.localScale.x, population [index1].transform.localScale.y, population [index1].transform.localScale.z); tempSize = mutatedSize(tempSize); child1.SetSize(tempSize); tempSize = new Vector3(population [index2].transform.localScale.x, population [index2].transform.localScale.y, population [index2].transform.localScale.z); tempSize = mutatedSize(tempSize); child2.SetSize(tempSize); } else if (howGenesAreSplit <= 0.48f) { Color tempColor = new Color(population [index2].color.r, population [index2].color.g, population [index2].color.b); tempColor = mutatedColor(tempColor); child1.SetColor(tempColor); tempColor = new Color(population [index1].color.r, population [index1].color.g, population [index1].color.b); tempColor = mutatedColor(tempColor); child2.SetColor(tempColor); Vector3 tempSize = new Vector3(population [index2].transform.localScale.x, population [index2].transform.localScale.y, population [index2].transform.localScale.z); tempSize = mutatedSize(tempSize); child1.SetSize(tempSize); tempSize = new Vector3(population [index1].transform.localScale.x, population [index1].transform.localScale.y, population [index1].transform.localScale.z); tempSize = mutatedSize(tempSize); child2.SetSize(tempSize); } else if (howGenesAreSplit <= 0.64f) { Color tempColor = new Color(population [index1].color.r, population [index2].color.g, population [index2].color.b); tempColor = mutatedColor(tempColor); child1.SetColor(tempColor); tempColor = new Color(population [index2].color.r, population [index1].color.g, population [index1].color.b); tempColor = mutatedColor(tempColor); child2.SetColor(tempColor); Vector3 tempSize = new Vector3(population [index1].transform.localScale.x, population [index1].transform.localScale.y, population [index2].transform.localScale.z); tempSize = mutatedSize(tempSize); child1.SetSize(tempSize); tempSize = new Vector3(population [index2].transform.localScale.x, population [index1].transform.localScale.y, population [index1].transform.localScale.z); tempSize = mutatedSize(tempSize); child2.SetSize(tempSize); } else if (howGenesAreSplit <= 0.8f) { Color tempColor = new Color(population [index1].color.r, population [index2].color.g, population [index1].color.b); tempColor = mutatedColor(tempColor); child1.SetColor(tempColor); tempColor = new Color(population [index2].color.r, population [index1].color.g, population [index2].color.b); tempColor = mutatedColor(tempColor); child2.SetColor(tempColor); Vector3 tempSize = new Vector3(population [index1].transform.localScale.x, population [index2].transform.localScale.y, population [index1].transform.localScale.z); tempSize = mutatedSize(tempSize); child1.SetSize(tempSize); tempSize = new Vector3(population [index2].transform.localScale.x, population [index1].transform.localScale.y, population [index2].transform.localScale.z); tempSize = mutatedSize(tempSize); child2.SetSize(tempSize); } else { Color tempColor = new Color(population [index2].color.r, population [index1].color.g, population [index2].color.b); tempColor = mutatedColor(tempColor); child1.SetColor(tempColor); tempColor = new Color(population [index1].color.r, population [index2].color.g, population [index1].color.b); tempColor = mutatedColor(tempColor); child2.SetColor(tempColor); Vector3 tempSize = new Vector3(population [index2].transform.localScale.x, population [index1].transform.localScale.y, population [index2].transform.localScale.z); tempSize = mutatedSize(tempSize); child1.SetSize(tempSize); tempSize = new Vector3(population [index1].transform.localScale.x, population [index2].transform.localScale.y, population [index1].transform.localScale.z); tempSize = mutatedSize(tempSize); child2.SetSize(tempSize); } float tempTemp = Random.Range(population [index1].temperature, population [index2].temperature); tempTemp = mutatedTemperature(tempTemp); child1.SetTemp(tempTemp); tempTemp = Random.Range(population [index1].temperature, population [index2].temperature); tempTemp = mutatedTemperature(tempTemp); child2.SetTemp(tempTemp); } population.AddRange(tempList); }
void Start() { Bunny = new bunny(); }