float[][] mate(MissileControlRandomSpaceJunk parent1, MissileControlRandomSpaceJunk parent2) { float[] childThrusterLeft = new float[numGenes]; float[] childThrusterRight = new float[numGenes]; float parent1Percentage = (float)parent1.fitness / (float)(parent1.fitness + parent2.fitness); float parent2Percentage = (float)parent2.fitness / (float)(parent1.fitness + parent2.fitness); for (int i = 0; i < numGenes; i++) { childThrusterLeft[i] = (parent1.thrusterLeftForces[i] * parent1Percentage) + (parent2.thrusterLeftForces[i] * parent2Percentage); childThrusterRight[i] = (parent1.thrusterRightForces[i] * parent1Percentage) + (parent2.thrusterRightForces[i] * parent2Percentage); } return(new float[][] { mutate(childThrusterLeft), mutate(childThrusterRight) }); }
void destroyAndCreate(List <MissileControlRandomSpaceJunk> matingpool, MissileControlRandomSpaceJunk mostFit) { Quaternion rotation = new Quaternion(0, 0, 0, 1); //assign random mass? Destroy(rockets[0]); rockets[0] = Instantiate(rocketPrefab, startPos.position, rotation) as GameObject; rocketsControl[0] = rockets[0].GetComponentInChildren <MissileControlRandomSpaceJunk>(); rocketsControl[0].thrusterLeftForces = mostFit.thrusterLeftForces; rocketsControl[0].thrusterRightForces = mostFit.thrusterRightForces; rocketsControl[0].goalTransform = goalTransform; rocketsControl[0].speed = speed; rocketsControl[0].mileStones = mileStones; rocketsControl[0].numGenes = numGenes; rocketsControl[0].slerpRate = slerpRate; Destroy(rockets[1]); rockets[1] = Instantiate(rocketPrefab, startPos.position, rotation) as GameObject; rocketsControl[1] = rockets[1].GetComponentInChildren <MissileControlRandomSpaceJunk>(); rocketsControl[1].thrusterLeftForces = mostFit.thrusterLeftForces; rocketsControl[1].thrusterRightForces = mostFit.thrusterRightForces; rocketsControl[1].goalTransform = goalTransform; rocketsControl[1].speed = speed; rocketsControl[1].mileStones = mileStones; rocketsControl[1].numGenes = numGenes; rocketsControl[1].slerpRate = slerpRate; for (int i = 2; i < numRockets; i++) { int parent1 = UnityEngine.Random.Range(0, matingpool.Count); int parent2 = UnityEngine.Random.Range(0, matingpool.Count); float[][] forces = mate(matingpool[parent1], matingpool[parent2]); Destroy(rockets[i]); rockets[i] = Instantiate(rocketPrefab, startPos.position, rotation) as GameObject; rocketsControl[i] = rockets[i].GetComponentInChildren <MissileControlRandomSpaceJunk>(); rocketsControl[i].thrusterLeftForces = forces[0]; rocketsControl[i].thrusterRightForces = forces[1]; rocketsControl[i].goalTransform = goalTransform; rocketsControl[i].speed = speed; rocketsControl[i].mileStones = mileStones; rocketsControl[i].numGenes = numGenes; rocketsControl[i].slerpRate = slerpRate; } for (int i = 0; i < numRockets; i++) { rocketsControl[i].isReady = true; } }