// Update is called once per frame void Update() { if (aliveCount <= 0 && generation < maxGenerations) { population.Sort((x, y) => (y.unitInfo.finalScore.CompareTo(x.unitInfo.finalScore))); Unit parent1 = population[0]; Unit parent2 = population[1]; Debug.Log("generation " + generation + " best \np1: " + parent1.unitInfo.finalScore + "\np2: " + parent2.unitInfo.finalScore); if (bestScore < parent1.unitInfo.finalScore) { bestScore = parent1.unitInfo.finalScore; bestGenom = parent1.genes; } Debug.Log("generation " + generation + " best \np1: " + parent1.unitInfo.finalScore + "\np2: " + parent2.unitInfo.finalScore + "\nglobal best score: " + bestScore); List <Genom> childGenoms = Unit.GetChildsGenoms(parent1.genes, parent2.genes, populationCount); GenerateNextPopulation(childGenoms); generation++; } }
public void GeneticInput(Genom genes, UnitInfo info) { forcePointMat.SetColor("_EmissionColor", Color.cyan); if (!unitInfo.alive) { return; } //Debug.LogError(genes.valuableDegree + " " + info.rotationX); //Debug.LogError(info.rotationX); if (-genes.valuableDegree > info.rotationX && info.rotationX < 0) { rig.AddForceAtPosition(Vector3.forward * genes.forcePower, forcePoint.position); forcePointMat.SetColor("_EmissionColor", Color.magenta); //Debug.LogError("a"); } if (genes.valuableDegree < info.rotationX && info.rotationX > 0) { rig.AddForceAtPosition(Vector3.back * genes.forcePower, forcePoint.position); forcePointMat.SetColor("_EmissionColor", Color.yellow); //Debug.LogError("b"); } }
public static Genom GemerateRandomGenes() { Genom genes = new Genom(); genes.forcePower = Random.Range(0, maxForce); genes.valuableDegree = Random.rotation.eulerAngles.x / 2; return(genes); }
public static List <Genom> GetChildsGenoms(Genom parent1, Genom parent2, float populationCount) { List <Genom> childs = new List <Genom>(); //Debug.LogError("p1 " + parent1.forcePower + " " + parent1.valuableDegree); //Debug.LogError("p2 " + parent2.forcePower + " " + parent2.valuableDegree); //Debug.LogError("c:"); childs.Add(parent1); childs.Add(parent2); for (int i = 0; i < populationCount - 2; i++) { Genom childGenom = GemerateRandomGenes(); if (i % 2 == 0) { if (Random.value > 0.5) { childGenom.forcePower = parent1.forcePower; } else { childGenom.valuableDegree = parent1.valuableDegree; } } if (i % 4 == 0) { if (Random.value < 0.5) { childGenom.forcePower = parent2.forcePower; } else { childGenom.valuableDegree = parent2.valuableDegree; } } //Debug.LogError("c" + i + " " + childGenom.forcePower + " " + childGenom.valuableDegree); childs.Add(childGenom); } return(childs); }
public void InitGenes(Genom genom) { this.genes = genom; }