private void InitPop() { for (int i = 0; i < gardenSize; i++) { GameObject newBlamb = (GameObject)Instantiate(BlambPrefab, new Vector3(0, 10, 0), Quaternion.identity); Base_BHV newBase = newBlamb.GetComponent <Base_BHV>(); newBase.Randomize(); population.Add(newBase); } }
private List <Base_BHV> Crossover(List <Base_BHV> selection) { List <Base_BHV> children = new List <Base_BHV>(); for (int i = 0; i < selection.Count; i += 2) { GameObject newChild = (GameObject)Instantiate(BlambPrefab, new Vector3(0, 10, 0), Quaternion.identity); Base_BHV newBase = newChild.GetComponent <Base_BHV>(); newBase.DefinePropertiesFrom(selection[i], selection[i + 1]); children.Add(newBase); } return(children); }
private void NewGeneration() { generationNumber++; List <Base_BHV> selection = Select(); List <Base_BHV> children = Crossover(selection); Mutation(children); List <Base_BHV> entrees = new List <Base_BHV>(); entrees.AddRange(population); List <Base_BHV> oldPopulation = new List <Base_BHV>(); oldPopulation.AddRange(population); population.Clear(); population.AddRange(children); Tournment(entrees); List <GameObject> objPop = new List <GameObject>(); population.ForEach(b => objPop.Add(b.gameObject)); foreach (Base_BHV oldBlamb in oldPopulation) { if (objPop.Contains(oldBlamb.gameObject)) { /* * oldBlamb.transform.position = new Vector3(0, 5, 0); * oldBlamb.transform.rotation = Quaternion.identity; * oldBlamb.Reset(); */ GameObject newObj = (GameObject)Instantiate(BlambPrefab, new Vector3(0, 10, 0), Quaternion.identity); Base_BHV newBlamb = newObj.GetComponent <Base_BHV>(); newBlamb.CopyProperties(oldBlamb); newBlamb.ColorMeshes(oldBlamb.color); population.Remove(oldBlamb); population.Add(newBlamb); Destroy(oldBlamb.gameObject); } else { Destroy(oldBlamb.gameObject); } } if (alltimeBestFit < generationBestFit) { alltimeBestFit = generationBestFit; } print("Generation " + generationNumber + ", AllTimeBest=" + alltimeBestFit); generationBestFit = 0f; }
private void Tournment(List <Base_BHV> entrees) { List <List <Base_BHV> > loosers = new List <List <Base_BHV> >(); while (entrees.Count > 1) { List <Base_BHV> roundLoosers = new List <Base_BHV>(); List <Base_BHV> roundWinners = new List <Base_BHV>(); while (entrees.Count > 1) { int index = Random.Range(0, entrees.Count); Base_BHV first = entrees[index]; entrees.RemoveAt(index); index = Random.Range(0, entrees.Count); Base_BHV second = entrees[index]; entrees.RemoveAt(index); if (Base_BHV.CompareDist(first, second) > 0) { roundWinners.Add(first); roundLoosers.Add(second); } else { roundWinners.Add(second); roundLoosers.Add(first); } } roundLoosers.AddRange(entrees); loosers.Add(roundLoosers); entrees.Clear(); entrees.AddRange(roundWinners); } population.AddRange(entrees); for (int i = loosers.Count - 1; population.Count < gardenSize; i--) { if (loosers[i].Count > gardenSize - population.Count) { population.AddRange(loosers[i].GetRange(0, gardenSize - population.Count)); } else { population.AddRange(loosers[i]); } } }
public void CopyProperties(Base_BHV other) { for (int i = 0; i < other.childrenProperties.Length; i++) { SectionProperties otherProp = other.childrenProperties[i]; childrenProperties[i].parentSection = otherProp.parentSection; childrenProperties[i].rotation = otherProp.rotation; childrenProperties[i].scale = otherProp.scale; SectionProperties.AnimationPatternProperties otherAnim = otherProp.animationProperties; childrenProperties[i].animationProperties.animationAxis = otherAnim.animationAxis; childrenProperties[i].animationProperties.animationStartOffset = otherAnim.animationStartOffset; childrenProperties[i].animationProperties.cycleTime = otherAnim.cycleTime; childrenProperties[i].animationProperties.animationCurve = otherAnim.animationCurve; childrenProperties[i].animationProperties.animationAttribute = PatternTransformAnimation_BHV.AnimAttrib.Rotation; childrenProperties[i].animationProperties.animationAmplitude = otherAnim.animationAmplitude; } }
public void DefinePropertiesFrom(Base_BHV first, Base_BHV second) { for (int i = 0; i < childrenProperties.Length; i++) { SectionProperties firstProp = first.childrenProperties[i]; SectionProperties secondProp = second.childrenProperties[i]; int fp = firstProp.parentSection; int sp = secondProp.parentSection; childrenProperties[i].parentSection = Random.Range(((fp > sp)? sp:fp), ((fp < sp)? sp:fp) + 1); childrenProperties[i].rotation = (firstProp.rotation + secondProp.rotation) / 2f; childrenProperties[i].scale = (firstProp.scale + secondProp.scale) / 2f; SectionProperties.AnimationPatternProperties firstAnim = firstProp.animationProperties; SectionProperties.AnimationPatternProperties secondAnim = secondProp.animationProperties; childrenProperties[i].animationProperties.animationAxis = (Random.value > 0.5f)? firstAnim.animationAxis : secondAnim.animationAxis; childrenProperties[i].animationProperties.animationStartOffset = (firstAnim.animationStartOffset + secondAnim.animationStartOffset) / 2f; childrenProperties[i].animationProperties.cycleTime = (firstAnim.cycleTime + secondAnim.cycleTime) / 2f; childrenProperties[i].animationProperties.animationCurve = (Random.value > 0.5f) ? firstAnim.animationCurve : secondAnim.animationCurve; childrenProperties[i].animationProperties.animationAttribute = PatternTransformAnimation_BHV.AnimAttrib.Rotation; childrenProperties[i].animationProperties.animationAmplitude = (firstAnim.animationAmplitude + secondAnim.animationAmplitude) / 2f; } }
public static int CompareDist(Base_BHV first, Base_BHV second) { return(((int)first.maxDistAchieved) - ((int)second.maxDistAchieved)); }